builder_page.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. var $this = this;
  75. bx_confirm(aDolLang['_adm_bp_wrn_page_delete'], function() {
  76. $this.performAction('page_delete');
  77. });
  78. };
  79. BxDolStudioBuilderPage.prototype.reorder = function(oDraggable) {
  80. var $this = this;
  81. var oDate = new Date();
  82. var aParams = new Array();
  83. $(this.aSortingConf.parent).each(function(iIndex, oElement){
  84. var sId = $(oElement).attr('id');
  85. aParams.push($('#' + sId).sortable('serialize', {key: 'bp_items_' + sId.replace($this.aSortingConf.parent_prefix, '') + '[]'}));
  86. });
  87. $.post(
  88. this.sActionUrl + (this.sActionUrl.indexOf('?') == -1 ? '?' : '&') + aParams.join('&'),
  89. {
  90. bp_action: 'reorder',
  91. bp_page: $this.sPage,
  92. _t:oDate.getTime()
  93. },
  94. function(oData) {
  95. if(oData.code != 0) {
  96. bx_alert(oData.message);
  97. return;
  98. }
  99. },
  100. 'json'
  101. );
  102. return true;
  103. };
  104. /**
  105. * "Add Block" popup methods.
  106. */
  107. BxDolStudioBuilderPage.prototype.onChangeModule = function(sName, oLink) {
  108. var $this = this;
  109. var oDate = new Date();
  110. var sClass = 'bx-menu-tab-active';
  111. $(oLink).parents('.bx-std-pmen-item:first').addClass(sClass).siblings('.bx-std-pmen-item').removeClass(sClass);
  112. var sId = '#' + $this.oHtmlIds['block_list_id'] + sName;
  113. if($(sId).length != 0) {
  114. $('#' + this.oHtmlIds['block_lists_id'] + ' > div:visible').bx_anim('hide', this.sAnimationEffect, 0, function() {
  115. $(sId).show();
  116. });
  117. return;
  118. }
  119. bx_loading(this.oHtmlIds['create_block_popup_id'], true);
  120. $.post(
  121. this.sActionUrl,
  122. {
  123. bp_action: 'block_list',
  124. bp_module: sName,
  125. _t:oDate.getTime()
  126. },
  127. function(oData) {
  128. bx_loading($this.oHtmlIds['create_block_popup_id'], false);
  129. $('#' + $this.oHtmlIds['block_lists_id'] + ' > div:visible').bx_anim('hide', $this.sAnimationEffect, 0, function() {
  130. $(this).parent().append(oData.content);
  131. });
  132. },
  133. 'json'
  134. );
  135. };
  136. BxDolStudioBuilderPage.prototype.onSelectBlock = function(oCheckbox) {
  137. var iCounter = parseInt($('#adm-bp-cbf-counter').html());
  138. iCounter += $(oCheckbox).attr('checked') == 'checked' ? 1 : -1;
  139. $('#adm-bp-cbf-counter').html(iCounter);
  140. };
  141. BxDolStudioBuilderPage.prototype.onCreateBlock = function(oData) {
  142. window.location.href = this.parsePageUrl({page: this.sPage});
  143. };
  144. BxDolStudioBuilderPage.prototype.deleteBlockImage = function(iId) {
  145. bx_loading(this.oHtmlIds['edit_block_popup_id'], true);
  146. this.performAction('image_delete', {id:iId});
  147. };
  148. BxDolStudioBuilderPage.prototype.onChangeVisibleFor = function(oSelect) {
  149. $(oSelect).parents('form:first').find('#bx-form-element-visible_for_levels').bx_anim($(oSelect).val() == 'all' ? 'hide' : 'show', this.sAnimationEffect, this.iAnimationSpeed);
  150. };
  151. BxDolStudioBuilderPage.prototype.onEditBlockBeforeShow = function(oPopup) {
  152. var oTextarea = $(oPopup).find("textarea.bx-form-input-textarea-codemirror[name = 'content']");
  153. if(oTextarea.length > 0)
  154. CodeMirror.fromTextArea(oTextarea.get(0), {
  155. lineNumbers: true,
  156. mode: "htmlmixed",
  157. htmlMode: true,
  158. matchBrackets: true
  159. }).on('blur', function(oEditor) {
  160. oEditor.save();
  161. });
  162. };
  163. BxDolStudioBuilderPage.prototype.onEditBlock = function(oData) {
  164. window.location.href = this.parsePageUrl({page: this.sPage});
  165. };
  166. BxDolStudioBuilderPage.prototype.onEditBlockCancel = function(oButton) {
  167. if ('undefined' !== typeof(bx_editor_remove_all))
  168. bx_editor_remove_all($(oButton).parents('form:first'));
  169. $('.bx-popup-applied:visible').dolPopupHide();
  170. };
  171. BxDolStudioBuilderPage.prototype.deleteBlock = function(iId) {
  172. var $this = this;
  173. bx_confirm(aDolLang['_adm_bp_wrn_page_block_delete'], function() {
  174. bx_loading($this.oHtmlIds['edit_block_popup_id'], true);
  175. $this.performAction('block_delete', {id:iId});
  176. });
  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.onSaveSettingsLayout = function() {
  202. window.location.href = this.parsePageUrl({page: this.sPage});
  203. };
  204. BxDolStudioBuilderPage.prototype.onCloseSettings = function() {
  205. $('.bx-popup-applied:visible').dolPopupHide({
  206. onHide: function(oPopup) {
  207. $(oPopup).remove();
  208. }
  209. });
  210. };
  211. /**
  212. * General methods.
  213. */
  214. BxDolStudioBuilderPage.prototype.performAction = function(sAction, aParams) {
  215. var $this = this;
  216. var oDate = new Date();
  217. if(aParams == undefined)
  218. aParams = {};
  219. aParams.bp_action = sAction;
  220. aParams.bp_type = $this.sType;
  221. aParams.bp_page = $this.sPage;
  222. aParams._t = oDate.getTime();
  223. if($('.bx-loading-ajax:visible').length == 0)
  224. bx_loading('bx-std-page-columns', true);
  225. $.post(
  226. this.sActionUrl,
  227. aParams,
  228. function(oData) {
  229. oBxDolStudioPage.processJson(oData);
  230. },
  231. 'json'
  232. );
  233. };
  234. BxDolStudioBuilderPage.prototype.parsePageUrl = function(aParams) {
  235. var sType = aParams.type != undefined ? aParams.type : this.sType;
  236. var sPage = aParams.page != undefined ? aParams.page : '';
  237. return this.sPageUrl.replace('{0}', sType).replace('{1}', sPage);
  238. };
  239. BxDolStudioBuilderPage.prototype.changeDimension = function(oText) {
  240. var oText = $(oText);
  241. var sKey = oText.attr('name');
  242. var iValue = parseInt(oText.val());
  243. if(!iValue || iValue <= 0)
  244. return;
  245. var oUrl = oText.parents('.bx-uploader-ghost:first').find('.bx-ug-url');
  246. var aUrl = oUrl.val().split('?');
  247. var aPairs = aUrl[1].split('&');
  248. var aParams = {};
  249. var bUpdated = false;
  250. for(var i in aPairs) {
  251. var aPair = aPairs[i].split('=');
  252. aParams[aPair[0]] = aPair[1];
  253. if(aPair[0] == sKey) {
  254. aParams[aPair[0]] = iValue;
  255. bUpdated = true;
  256. }
  257. }
  258. if(!bUpdated)
  259. aParams[sKey] = iValue;
  260. oUrl.val(bx_append_url_params(aUrl[0], aParams));
  261. };
  262. BxDolStudioBuilderPage.prototype.deleteGhost = function(iFileId, sFileUrl, sFileIcon, aEditors, oUploaderInstance) {
  263. bx_editor_remove_img(aEditors, ['img[src="' + sFileIcon + '"]', 'img[src="' + sFileUrl + '"]', '#bx-base-text-img-' + iFileId, '#bx-base-text-icon-' + iFileId]);
  264. oUploaderInstance.deleteGhost(iFileId);
  265. };
  266. BxDolStudioBuilderPage.prototype.insertIntoPost = function(oButton, iFileId, sEditorId) {
  267. var sFileUrl = $(oButton).parents('.bx-uploader-ghost:first').find('.bx-ug-url').val();
  268. bx_editor_insert_img (sEditorId, 'bx-base-text-img-' + iFileId, sFileUrl, 'bx-base-text-img');
  269. };
  270. /** @} */