Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

401 wiersze
12 KiB

  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. /* global Variables, updateElementAtCursor, MagentovariablePlugin, Base64 */
  6. define([
  7. 'jquery',
  8. 'mage/backend/notification',
  9. 'mage/translate',
  10. 'wysiwygAdapter',
  11. 'uiRegistry',
  12. 'mage/apply/main',
  13. 'mageUtils',
  14. 'Magento_Variable/js/config-directive-generator',
  15. 'Magento_Variable/js/custom-directive-generator',
  16. 'Magento_Ui/js/lib/spinner',
  17. 'jquery/ui',
  18. 'prototype',
  19. 'mage/adminhtml/tools'
  20. ], function (jQuery, notification, $t, wysiwyg, registry, mageApply, utils, configGenerator, customGenerator, loader) {
  21. 'use strict';
  22. window.Variables = {
  23. textareaElementId: null,
  24. variablesContent: null,
  25. dialogWindow: null,
  26. dialogWindowId: 'variables-chooser',
  27. overlayShowEffectOptions: null,
  28. overlayHideEffectOptions: null,
  29. insertFunction: 'Variables.insertVariable',
  30. selectedPlaceholder: null,
  31. isEditMode: null,
  32. editor: null,
  33. /**
  34. * Initialize Variables handler.
  35. *
  36. * @param {*} textareaElementId
  37. * @param {Function} insertFunction
  38. * @param {Object} editor
  39. * @param {Object} selectedPlaceholder
  40. */
  41. init: function (textareaElementId, insertFunction, editor, selectedPlaceholder) {
  42. if ($(textareaElementId)) {
  43. this.textareaElementId = textareaElementId;
  44. }
  45. if (insertFunction) {
  46. this.insertFunction = insertFunction;
  47. }
  48. if (selectedPlaceholder) {
  49. this.selectedPlaceholder = selectedPlaceholder;
  50. }
  51. if (editor) {
  52. this.editor = editor;
  53. }
  54. },
  55. /**
  56. * Reset data.
  57. */
  58. resetData: function () {
  59. this.variablesContent = null;
  60. this.dialogWindow = null;
  61. },
  62. /**
  63. * Open variables chooser slideout.
  64. *
  65. * @param {Object} variables
  66. */
  67. openVariableChooser: function (variables) {
  68. if (variables) {
  69. this.openDialogWindow(variables);
  70. }
  71. },
  72. /**
  73. * Close variables chooser slideout dialog window.
  74. */
  75. closeDialogWindow: function () {
  76. jQuery('#' + this.dialogWindowId).modal('closeModal');
  77. },
  78. /**
  79. * Init ui component grid on the form
  80. *
  81. * @return void
  82. */
  83. initUiGrid: function () {
  84. mageApply.apply(document.getElementById(this.dialogWindow));
  85. jQuery('#' + this.dialogWindowId).applyBindings();
  86. loader.get('variables_modal.variables_modal.variables').hide();
  87. },
  88. /**
  89. * Open slideout dialog window.
  90. *
  91. * @param {*} variablesContent
  92. * @param {Object} selectedElement
  93. */
  94. openDialogWindow: function (variablesContent, selectedElement) {
  95. var html = utils.copy(variablesContent),
  96. self = this;
  97. jQuery('<div id="' + this.dialogWindowId + '">' + html + '</div>').modal({
  98. title: self.isEditMode ? $t('Edit Variable') : $t('Insert Variable'),
  99. type: 'slide',
  100. buttons: self.getButtonsConfig(self.isEditMode),
  101. /**
  102. * @param {jQuery.Event} e
  103. * @param {Object} modal
  104. */
  105. closed: function (e, modal) {
  106. modal.modal.remove();
  107. registry.get(
  108. 'variables_modal.variables_modal.variables.variable_selector',
  109. function (radioSelect) {
  110. radioSelect.selectedVariableCode('');
  111. }
  112. );
  113. }
  114. });
  115. this.selectedPlaceholder = selectedElement;
  116. this.addNotAvailableMessage(selectedElement);
  117. jQuery('#' + this.dialogWindowId).modal('openModal');
  118. if (typeof selectedElement !== 'undefined') {
  119. registry.get(
  120. 'variables_modal.variables_modal.variables.variable_selector',
  121. function (radioSelect) {
  122. radioSelect.selectedVariableCode(MagentovariablePlugin.getElementVariablePath(selectedElement));
  123. }
  124. );
  125. }
  126. },
  127. /**
  128. * Add message to slide out that variable is no longer available
  129. *
  130. * @param {Object} selectedElement
  131. */
  132. addNotAvailableMessage: function (selectedElement) {
  133. var name,
  134. msg,
  135. variablePath,
  136. $wrapper,
  137. lostVariableClass = 'magento-placeholder-error';
  138. if (
  139. this.isEditMode &&
  140. typeof selectedElement !== 'undefined' &&
  141. jQuery(selectedElement).hasClass(lostVariableClass)
  142. ) {
  143. variablePath = MagentovariablePlugin.getElementVariablePath(selectedElement);
  144. name = variablePath.split(':');
  145. msg = $t('The variable %1 is no longer available. Select a different variable.')
  146. .replace('%1', name[1]);
  147. jQuery('body').notification('clear')
  148. .notification('add', {
  149. error: true,
  150. message: msg,
  151. /**
  152. * @param {String} message
  153. */
  154. insertMethod: function (message) {
  155. $wrapper = jQuery('<div></div>').html(message);
  156. jQuery('.modal-header .page-main-actions').after($wrapper);
  157. }
  158. });
  159. }
  160. },
  161. /**
  162. * Get selected variable directive.
  163. *
  164. * @returns {*}
  165. */
  166. getVariableCode: function () {
  167. var code = registry.get('variables_modal.variables_modal.variables.variable_selector')
  168. .selectedVariableCode(),
  169. directive = code;
  170. // processing switch here as content must contain only path/code without type
  171. if (typeof code !== 'undefined') {
  172. if (code.match('^default:')) {
  173. directive = configGenerator.processConfig(code.replace('default:', ''));
  174. } else if (code.match('^custom:')) {
  175. directive = customGenerator.processConfig(code.replace('custom:', ''));
  176. }
  177. return directive;
  178. }
  179. },
  180. /**
  181. * Get buttons configuration for slideout dialog.
  182. *
  183. * @param {Boolean} isEditMode
  184. *
  185. * @returns {Array}
  186. */
  187. getButtonsConfig: function (isEditMode) {
  188. var self = this,
  189. buttonsData;
  190. buttonsData = [
  191. {
  192. text: $t('Cancel'),
  193. 'class': 'action-scalable cancel',
  194. /**
  195. * @param {jQuery.Event} event
  196. */
  197. click: function (event) {
  198. this.closeModal(event);
  199. }
  200. },
  201. {
  202. text: isEditMode ? $t('Save') : $t('Insert Variable'),
  203. class: 'action-primary ' + (isEditMode ? '' : 'disabled'),
  204. attr: {
  205. 'id': 'insert_variable'
  206. },
  207. /**
  208. * Insert Variable
  209. */
  210. click: function () {
  211. self.insertVariable(self.getVariableCode());
  212. }
  213. }
  214. ];
  215. return buttonsData;
  216. },
  217. /**
  218. * Prepare variables row.
  219. *
  220. * @param {String} varValue
  221. * @param {*} varLabel
  222. * @return {String}
  223. * @deprecated This method isn't relevant after ui changes
  224. */
  225. prepareVariableRow: function (varValue, varLabel) {
  226. var value = varValue.replace(/"/g, '&quot;').replace(/'/g, '\\&#39;');
  227. return '<a href="#" onclick="' +
  228. this.insertFunction +
  229. '(\'' +
  230. value +
  231. '\');return false;">' +
  232. varLabel +
  233. '</a>';
  234. },
  235. /**
  236. * Insert variable into WYSIWYG editor.
  237. *
  238. * @param {*} value
  239. * @return {Object}
  240. */
  241. insertVariable: function (value) {
  242. var windowId = this.dialogWindowId,
  243. textareaElm, scrollPos, wysiwygEditorFocused;
  244. jQuery('#' + windowId).modal('closeModal');
  245. textareaElm = $(this.textareaElementId);
  246. //to support switching between wysiwyg editors
  247. wysiwygEditorFocused = wysiwyg && wysiwyg.activeEditor();
  248. if (wysiwygEditorFocused && wysiwyg.get(this.textareaElementId)) {
  249. if (jQuery(this.selectedPlaceholder).hasClass('magento-placeholder')) {
  250. wysiwyg.setCaretOnElement(this.selectedPlaceholder, 1);
  251. }
  252. wysiwyg.insertContent(value, false);
  253. if (this.selectedPlaceholder && jQuery(this.selectedPlaceholder).hasClass('magento-placeholder')) {
  254. this.selectedPlaceholder.remove();
  255. }
  256. } else if (textareaElm) {
  257. scrollPos = textareaElm.scrollTop;
  258. updateElementAtCursor(textareaElm, value);
  259. textareaElm.focus();
  260. textareaElm.scrollTop = scrollPos;
  261. jQuery(textareaElm).trigger('change');
  262. textareaElm = null;
  263. }
  264. return this;
  265. }
  266. };
  267. window.MagentovariablePlugin = {
  268. editor: null,
  269. variables: null,
  270. textareaId: null,
  271. /**
  272. * Bind editor.
  273. *
  274. * @param {*} editor
  275. */
  276. setEditor: function (editor) {
  277. this.editor = editor;
  278. },
  279. /**
  280. * Load variables chooser.
  281. *
  282. * @param {String} url
  283. * @param {*} textareaId
  284. * @param {Object} selectedElement
  285. *
  286. * @return {Object}
  287. */
  288. loadChooser: function (url, textareaId, selectedElement) {
  289. this.textareaId = textareaId;
  290. new Ajax.Request(url, {
  291. parameters: {},
  292. onComplete: function (transport) {
  293. Variables.init(this.textareaId, 'MagentovariablePlugin.insertVariable', this.editor);
  294. Variables.isEditMode = !!this.getElementVariablePath(selectedElement);
  295. this.variablesContent = transport.responseText;
  296. Variables.openDialogWindow(this.variablesContent, selectedElement);
  297. Variables.initUiGrid();
  298. }.bind(this)
  299. });
  300. return this;
  301. },
  302. /**
  303. * Open variables chooser window.
  304. *
  305. * @param {*} variables
  306. * @deprecated This method isn't relevant after ui changes
  307. */
  308. openChooser: function (variables) {
  309. Variables.openVariableChooser(variables);
  310. },
  311. /**
  312. * Insert variable.
  313. *
  314. * @param {*} value
  315. *
  316. * @return {Object}
  317. */
  318. insertVariable: function (value) {
  319. if (this.textareaId) {
  320. Variables.init(this.textareaId);
  321. Variables.insertVariable(value);
  322. } else {
  323. Variables.closeDialogWindow();
  324. Variables.insertVariable(value);
  325. }
  326. return this;
  327. },
  328. /**
  329. * Get element variable path.
  330. *
  331. * @param {Object} element
  332. * @returns {String}
  333. */
  334. getElementVariablePath: function (element) {
  335. var type, code;
  336. if (!element || !jQuery(element).hasClass('magento-variable')) {
  337. return '';
  338. }
  339. type = jQuery(element).hasClass('magento-custom-var') ? 'custom' : 'default';
  340. code = Base64.idDecode(element.getAttribute('id'));
  341. return type + ':' + code;
  342. }
  343. };
  344. });