builder_page.js 12 KB

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