builder_page.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /**
  2. * Copyright (c) UNA, Inc - https://una.io
  3. * MIT License - https://opensource.org/licenses/MIT
  4. *
  5. * @defgroup UnaStudio UNA Studio
  6. * @{
  7. */
  8. function BxDolStudioBuilderPage(oOptions) {
  9. this.sActionUrl = oOptions.sActionUrl;
  10. this.sPageUrl = oOptions.sPageUrl;
  11. this.sObjName = oOptions.sObjName == undefined ? 'oBxDolStudioBuilderPage' : oOptions.sObjName;
  12. this.sType = oOptions.sType == undefined ? '' : oOptions.sType;
  13. this.sPage = oOptions.sPage == undefined ? '' : oOptions.sPage;
  14. this.sAnimationEffect = oOptions.sAnimationEffect == undefined ? 'fade' : oOptions.sAnimationEffect;
  15. this.iAnimationSpeed = oOptions.iAnimationSpeed == undefined ? 'slow' : oOptions.iAnimationSpeed;
  16. this.oHtmlIds = oOptions.oHtmlIds == undefined ? {} : oOptions.oHtmlIds;
  17. this.oLanguages = oOptions.oLanguages == undefined ? {} : oOptions.oLanguages;
  18. this.aSortingConf = {
  19. parent: '.adm-bp-cell-cnt',
  20. parent_prefix: 'adm-bpc-',
  21. item: '.adm-bp-block',
  22. placeholder: 'adm-bp-block adm-bp-block-empty'
  23. };
  24. $('div.bx-std-pt-bp-types select').select2({
  25. templateSelection: d => d.text
  26. });
  27. $('div.bx-std-pt-bp-pages select').select2({
  28. templateResult: this.formatItemPage,
  29. templateSelection: d => d.text
  30. });
  31. var $this = this;
  32. $(document).ready(function() {
  33. $($this.aSortingConf.parent).sortable({
  34. handle: $($this.aSortingConf.item + ' .adm-bpb-drag-handle'),
  35. items: $this.aSortingConf.item,
  36. placeholder: $this.aSortingConf.placeholder,
  37. connectWith: $this.aSortingConf.parent,
  38. start: function(oEvent, oUi) {
  39. oUi.item.addClass('adm-bp-block-dragging').width(300);
  40. bx_sound('select'); // picked up
  41. },
  42. stop: function(oEvent, oUi) {
  43. oUi.item.removeClass('adm-bp-block-dragging');
  44. bx_sound('tap', {emphasis: 'strong'}); // dropped
  45. $this.reorder(oUi.item);
  46. }
  47. });
  48. });
  49. }
  50. BxDolStudioBuilderPage.prototype.formatItemPage = function(oItem) {
  51. if(!oItem.id)
  52. return oItem.text;
  53. const sUrl = $(oItem.element).data('url');
  54. const iBlocks = $(oItem.element).data('blocks');
  55. const sBlocks = parseInt(iBlocks) >= 0 ? iBlocks : _t('_sys_not_available');
  56. return $(`
  57. <div class="adm-bp-page">
  58. <div>
  59. <div class="adm-bpp-ttl">${oItem.text}</div>
  60. <div class="adm-bpp-inf bx-def-font-meta">${sUrl || ''}</div>
  61. </div>
  62. <div class="adm-bpp-cnt">${sBlocks}</div>
  63. </div>
  64. `);
  65. };
  66. /**
  67. * Main page methods.
  68. */
  69. BxDolStudioBuilderPage.prototype.onChangeType = function(oSelect) {
  70. document.location.href = this.parsePageUrl({type: $(oSelect).val()});
  71. };
  72. BxDolStudioBuilderPage.prototype.onChangePage = function(oSelect) {
  73. document.location.href = this.parsePageUrl({page: $(oSelect).val()});
  74. };
  75. BxDolStudioBuilderPage.prototype.getUri = function(oElement) {
  76. var mixedParams = {};
  77. var sUri = $('#' + this.oHtmlIds['uri_field_id']).val();
  78. if(sUri.length > 0)
  79. mixedParams['uri'] = sUri;
  80. else {
  81. var bGot = false;
  82. $.each(this.oLanguages, function(sKey, sValue) {
  83. mixedParams[sKey] = $('#bx-form-input-title-' + sKey).val();
  84. if(mixedParams[sKey].length > 0)
  85. bGot = true;
  86. });
  87. if(!bGot)
  88. return;
  89. }
  90. bx_loading(this.oHtmlIds['add_popup_id'], true);
  91. this.performAction('uri_get', mixedParams);
  92. };
  93. BxDolStudioBuilderPage.prototype.onGetUri = function(oData) {
  94. bx_loading(this.oHtmlIds['add_popup_id'], false);
  95. $('#' + this.oHtmlIds['uri_field_id']).val(oData.uri);
  96. $('#' + this.oHtmlIds['url_field_id']).val(oData.url);
  97. };
  98. BxDolStudioBuilderPage.prototype.onCreatePage = function(sType, sPage) {
  99. window.location.href = this.parsePageUrl({type: sType, page: sPage});
  100. };
  101. BxDolStudioBuilderPage.prototype.deletePage = function() {
  102. var $this = this;
  103. bx_confirm(aDolLang['_adm_bp_wrn_page_delete'], function() {
  104. $this.performAction('page_delete');
  105. });
  106. };
  107. BxDolStudioBuilderPage.prototype.reorder = function(oDraggable) {
  108. var $this = this;
  109. var oDate = new Date();
  110. var aParams = new Array();
  111. $(this.aSortingConf.parent).each(function(iIndex, oElement){
  112. var sId = $(oElement).attr('id');
  113. aParams.push($('#' + sId).sortable('serialize', {key: 'bp_items_' + sId.replace($this.aSortingConf.parent_prefix, '') + '[]'}));
  114. });
  115. $.post(
  116. this.sActionUrl + (this.sActionUrl.indexOf('?') == -1 ? '?' : '&') + aParams.join('&'),
  117. {
  118. bp_action: 'reorder',
  119. bp_page: $this.sPage,
  120. _t:oDate.getTime()
  121. },
  122. function(oData) {
  123. if(oData.code != 0) {
  124. bx_sound('error');
  125. bx_alert(oData.message);
  126. return;
  127. }
  128. },
  129. 'json'
  130. );
  131. return true;
  132. };
  133. /**
  134. * "Add Block" popup methods.
  135. */
  136. BxDolStudioBuilderPage.prototype.onChangeModule = function(sName, oLink) {
  137. var $this = this;
  138. var oDate = new Date();
  139. var sClass = 'bx-menu-tab-active';
  140. $(oLink).parents('.bx-std-pmen-item:first').addClass(sClass).siblings('.bx-std-pmen-item').removeClass(sClass);
  141. var sId = '#' + $this.oHtmlIds['block_list_id'] + sName;
  142. if($(sId).length != 0) {
  143. $('#' + this.oHtmlIds['block_lists_id'] + ' > div:visible').bx_anim('hide', this.sAnimationEffect, 0, function() {
  144. $(sId).show();
  145. });
  146. return;
  147. }
  148. bx_loading(this.oHtmlIds['create_block_popup_id'], true);
  149. $.post(
  150. this.sActionUrl,
  151. {
  152. bp_action: 'block_list',
  153. bp_module: sName,
  154. _t:oDate.getTime()
  155. },
  156. function(oData) {
  157. bx_loading($this.oHtmlIds['create_block_popup_id'], false);
  158. $('#' + $this.oHtmlIds['block_lists_id'] + ' > div:visible').bx_anim('hide', $this.sAnimationEffect, 0, function() {
  159. $(this).parent().append(oData.content).bxProcessHtml();
  160. });
  161. },
  162. 'json'
  163. );
  164. };
  165. BxDolStudioBuilderPage.prototype.onSelectBlock = function(oCheckbox) {
  166. var iCounter = parseInt($('#adm-bp-cbf-counter').html());
  167. iCounter += $(oCheckbox).prop('checked') ? 1 : -1;
  168. $('#adm-bp-cbf-counter').html(iCounter);
  169. };
  170. BxDolStudioBuilderPage.prototype.onCreateBlock = function(oData) {
  171. window.location.href = this.parsePageUrl({page: this.sPage});
  172. };
  173. BxDolStudioBuilderPage.prototype.onChangeVisibleFor = function(oSelect) {
  174. $(oSelect).parents('form:first').find('#bx-form-element-visible_for_levels').bx_anim($(oSelect).val() == 'all' ? 'hide' : 'show', this.sAnimationEffect, this.iAnimationSpeed);
  175. };
  176. BxDolStudioBuilderPage.prototype.onEditBlockBeforeShow = function(oPopup) {
  177. var oTextarea = $(oPopup).find("textarea.bx-form-input-textarea-codemirror[name = 'content']");
  178. if(oTextarea.length > 0)
  179. CodeMirror.fromTextArea(oTextarea.get(0), {
  180. lineNumbers: true,
  181. mode: "htmlmixed",
  182. htmlMode: true,
  183. matchBrackets: true
  184. }).on('blur', function(oEditor) {
  185. oEditor.save();
  186. });
  187. };
  188. BxDolStudioBuilderPage.prototype.onEditBlock = function(oData) {
  189. window.location.href = this.parsePageUrl({page: this.sPage});
  190. };
  191. BxDolStudioBuilderPage.prototype.onEditBlockCancel = function(oButton) {
  192. if ('undefined' !== typeof(bx_editor_remove_all))
  193. bx_editor_remove_all($(oButton).parents('form:first'));
  194. $('.bx-popup-applied:visible').dolPopupHide();
  195. };
  196. BxDolStudioBuilderPage.prototype.deleteBlock = function(iId) {
  197. var $this = this;
  198. bx_confirm(aDolLang['_adm_bp_wrn_page_block_delete'], function() {
  199. bx_loading($this.oHtmlIds['edit_block_popup_id'], true);
  200. $this.performAction('block_delete', {id:iId});
  201. });
  202. };
  203. BxDolStudioBuilderPage.prototype.onDeleteBlock = function(iId, oData) {
  204. bx_sound('close', {emphasis: 'strong'}); // a confirmed delete
  205. bx_loading(this.oHtmlIds['edit_block_popup_id'], false);
  206. $('.bx-popup-applied:visible').dolPopupHide();
  207. $('#' + this.oHtmlIds['block_id'] + iId).bx_anim('hide', this.sAnimationEffect, this.iAnimationSpeed, function() {
  208. $(this).remove();
  209. });
  210. };
  211. /**
  212. * "Settings" popup methods.
  213. */
  214. BxDolStudioBuilderPage.prototype.onChangeSettingGroup = function(sName, oLink) {
  215. var $this = this;
  216. bx_sound('select');
  217. var sClass = 'bx-menu-tab-active';
  218. $(oLink).parents('.bx-std-pmen-item:first').addClass(sClass).siblings('.bx-std-pmen-item').removeClass(sClass);
  219. $('#' + this.oHtmlIds['settings_groups_id'] + ' > div:visible').bx_anim('hide', this.sAnimationEffect, 0, function() {
  220. $('#' + $this.oHtmlIds['settings_group_id'] + sName).show(function() {
  221. $(this).find('.CodeMirror').each(function() {
  222. this.CodeMirror.refresh();
  223. });
  224. });
  225. });
  226. };
  227. BxDolStudioBuilderPage.prototype.onChangeLayout = function(iId, oLink) {
  228. bx_sound('select');
  229. $('#' + this.oHtmlIds['settings_group_id'] + 'layout > .adm-bp-layout-active').removeClass('adm-bp-layout-active');
  230. $('#' + this.oHtmlIds['layout_id'] + iId).addClass('adm-bp-layout-active');
  231. $("[name = 'layout_id']").val(iId);
  232. };
  233. BxDolStudioBuilderPage.prototype.onSaveSettingsLayout = function() {
  234. window.location.href = this.parsePageUrl({page: this.sPage});
  235. };
  236. BxDolStudioBuilderPage.prototype.onCloseSettings = function() {
  237. $('.bx-popup-applied:visible').dolPopupHide({
  238. onHide: function(oPopup) {
  239. $(oPopup).remove();
  240. }
  241. });
  242. };
  243. /**
  244. * General methods.
  245. */
  246. BxDolStudioBuilderPage.prototype.performAction = function(sAction, aParams) {
  247. var $this = this;
  248. var oDate = new Date();
  249. if(aParams == undefined)
  250. aParams = {};
  251. aParams.bp_action = sAction;
  252. aParams.bp_type = $this.sType;
  253. aParams.bp_page = $this.sPage;
  254. aParams._t = oDate.getTime();
  255. if($('.bx-loading-ajax:visible').length == 0)
  256. bx_loading('bx-std-page-columns', true);
  257. $.post(
  258. this.sActionUrl,
  259. aParams,
  260. function(oData) {
  261. oBxDolStudioPage.processJson(oData);
  262. },
  263. 'json'
  264. );
  265. };
  266. BxDolStudioBuilderPage.prototype.parsePageUrl = function(aParams) {
  267. var sType = aParams.type != undefined ? aParams.type : this.sType;
  268. var sPage = aParams.page != undefined ? aParams.page : '';
  269. return this.sPageUrl.replace('{0}', sType).replace('{1}', sPage);
  270. };
  271. BxDolStudioBuilderPage.prototype.changeDimension = function(oText) {
  272. var oText = $(oText);
  273. var sKey = oText.attr('name');
  274. var iValue = parseInt(oText.val());
  275. if(!iValue || iValue <= 0)
  276. return;
  277. var oUrl = oText.parents('.bx-uploader-ghost:first').find('.bx-ug-url');
  278. var aUrl = oUrl.val().split('?');
  279. var aPairs = aUrl[1].split('&');
  280. var aParams = {};
  281. var bUpdated = false;
  282. for(var i in aPairs) {
  283. var aPair = aPairs[i].split('=');
  284. aParams[aPair[0]] = aPair[1];
  285. if(aPair[0] == sKey) {
  286. aParams[aPair[0]] = iValue;
  287. bUpdated = true;
  288. }
  289. }
  290. if(!bUpdated)
  291. aParams[sKey] = iValue;
  292. oUrl.val(bx_append_url_params(aUrl[0], aParams));
  293. };
  294. BxDolStudioBuilderPage.prototype.deleteGhost = function(iFileId, sFileUrl, sFileIcon, aEditors, oUploaderInstance) {
  295. bx_editor_remove_img(aEditors, ['img[src="' + sFileIcon + '"]', 'img[src="' + sFileUrl + '"]', '#bx-base-text-img-' + iFileId, '#bx-base-text-icon-' + iFileId]);
  296. oUploaderInstance.deleteGhost(iFileId);
  297. };
  298. BxDolStudioBuilderPage.prototype.insertIntoPost = function(oButton, iFileId, sEditorId) {
  299. var sFileUrl = $(oButton).parents('.bx-uploader-ghost:first').find('.bx-ug-url').val();
  300. bx_editor_insert_img (sEditorId, 'bx-base-text-img-' + iFileId, sFileUrl, 'bx-base-text-img');
  301. };
  302. /** @} */