builder_page.js 9.9 KB

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