Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

416 linhas
14 KiB

  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. /* global FORM_KEY */
  6. /**
  7. * @api
  8. */
  9. define([
  10. 'jquery',
  11. 'mage/template',
  12. 'uiRegistry',
  13. 'jquery/colorpicker/js/colorpicker',
  14. 'prototype',
  15. 'jquery/ui',
  16. 'validation'
  17. ], function (jQuery, mageTemplate, rg) {
  18. 'use strict';
  19. return function (config) {
  20. var swatchOptionVisualDefaultInputType = 'radio',
  21. swatchVisualOption = {
  22. table: $('swatch-visual-options-table'),
  23. itemCount: 0,
  24. totalItems: 0,
  25. rendered: 0,
  26. isReadOnly: config.isReadOnly,
  27. template: mageTemplate('#swatch-visual-row-template'),
  28. /**
  29. * Add new option using template
  30. *
  31. * @param {Object} data
  32. * @param {Object} render
  33. */
  34. add: function (data, render) {
  35. var isNewOption = false,
  36. element;
  37. if (typeof data.id == 'undefined') {
  38. data = {
  39. 'id': 'option_' + this.itemCount,
  40. 'sort_order': this.itemCount + 1,
  41. 'empty_class': 'unavailable'
  42. };
  43. isNewOption = true;
  44. } else if (data.defaultswatch0 === '') {
  45. data['empty_class'] = 'unavailable';
  46. }
  47. if (!data.intype) {
  48. data.intype = swatchOptionVisualDefaultInputType;
  49. }
  50. element = this.template({
  51. data: data
  52. });
  53. if (isNewOption && !this.isReadOnly) {
  54. this.enableNewOptionDeleteButton(data.id);
  55. }
  56. this.itemCount++;
  57. this.totalItems++;
  58. this.elements += element;
  59. if (render) {
  60. this.render();
  61. }
  62. },
  63. /**
  64. * ColorPicker initialization process
  65. */
  66. initColorPicker: function () {
  67. var element = this,
  68. hiddenColorPicker = !jQuery(element).data('colorpickerId');
  69. jQuery(this).ColorPicker({
  70. /**
  71. * ColorPicker onShow action
  72. */
  73. onShow: function () {
  74. var color = jQuery(element).parent().parent().prev().prev('input').val(),
  75. menu = jQuery(this).parents('.swatch_sub-menu_container');
  76. menu.hide();
  77. jQuery(element).ColorPickerSetColor(color);
  78. },
  79. /**
  80. * ColorPicker onSubmit action
  81. *
  82. * @param {String} hsb
  83. * @param {String} hex
  84. * @param {String} rgb
  85. * @param {String} el
  86. */
  87. onSubmit: function (hsb, hex, rgb, el) {
  88. var container = jQuery(el).parent().parent().prev();
  89. jQuery(el).ColorPickerHide();
  90. container.parent().removeClass('unavailable');
  91. container.prev('input').val('#' + hex);
  92. container.css('background', '#' + hex);
  93. }
  94. });
  95. if (hiddenColorPicker) {
  96. jQuery(this).ColorPickerShow();
  97. }
  98. },
  99. /**
  100. * Remove action
  101. *
  102. * @param {Object} event
  103. */
  104. remove: function (event) {
  105. var element = $(Event.findElement(event, 'tr')),
  106. elementFlags; // !!! Button already have table parent in safari
  107. // Safari workaround
  108. element.ancestors().each(function (parentItem) {
  109. if (parentItem.hasClassName('option-row')) {
  110. element = parentItem;
  111. throw $break;
  112. } else if (parentItem.hasClassName('box')) {
  113. throw $break;
  114. }
  115. });
  116. if (element) {
  117. elementFlags = element.getElementsByClassName('delete-flag');
  118. if (elementFlags[0]) {
  119. elementFlags[0].value = 1;
  120. }
  121. element.addClassName('no-display');
  122. element.addClassName('template');
  123. element.hide();
  124. this.totalItems--;
  125. this.updateItemsCountField();
  126. }
  127. },
  128. /**
  129. * Update items count field
  130. */
  131. updateItemsCountField: function () {
  132. $('swatch-visual-option-count-check').value = this.totalItems > 0 ? '1' : '';
  133. },
  134. /**
  135. * Enable delete button for new option
  136. *
  137. * @param {String} id
  138. */
  139. enableNewOptionDeleteButton: function (id) {
  140. $$('#delete_button_swatch_container_' + id + ' button').each(function (button) {
  141. button.enable();
  142. button.removeClassName('disabled');
  143. });
  144. },
  145. /**
  146. * Bind remove button
  147. */
  148. bindRemoveButtons: function () {
  149. jQuery('#swatch-visual-options-panel').on('click', '.delete-option', this.remove.bind(this));
  150. },
  151. /**
  152. * Render options
  153. */
  154. render: function () {
  155. Element.insert($$('[data-role=swatch-visual-options-container]')[0], this.elements);
  156. this.elements = '';
  157. },
  158. /**
  159. * Render elements with delay (performance fix)
  160. *
  161. * @param {Object} data
  162. * @param {Number} from
  163. * @param {Number} step
  164. * @param {Number} delay
  165. * @returns {Boolean}
  166. */
  167. renderWithDelay: function (data, from, step, delay) {
  168. var arrayLength = data.length,
  169. len;
  170. for (len = from + step; from < len && from < arrayLength; from++) {
  171. this.add(data[from]);
  172. }
  173. this.render();
  174. if (from === arrayLength) {
  175. this.updateItemsCountField();
  176. this.rendered = 1;
  177. jQuery('body').trigger('processStop');
  178. return true;
  179. }
  180. setTimeout(this.renderWithDelay.bind(this, data, from, step, delay), delay);
  181. },
  182. /**
  183. * Ignore validate action
  184. */
  185. ignoreValidate: function () {
  186. var ignore = '.ignore-validate input, ' +
  187. '.ignore-validate select, ' +
  188. '.ignore-validate textarea';
  189. jQuery('#edit_form').data('validator').settings.forceIgnore = ignore;
  190. }
  191. };
  192. if ($('add_new_swatch_visual_option_button')) {
  193. Event.observe(
  194. 'add_new_swatch_visual_option_button',
  195. 'click',
  196. swatchVisualOption.add.bind(swatchVisualOption, {}, true)
  197. );
  198. }
  199. jQuery('#swatch-visual-options-panel').on('render', function () {
  200. swatchVisualOption.ignoreValidate();
  201. if (swatchVisualOption.rendered) {
  202. return false;
  203. }
  204. jQuery('body').trigger('processStart');
  205. swatchVisualOption.renderWithDelay(config.attributesData, 0, 100, 300);
  206. swatchVisualOption.bindRemoveButtons();
  207. jQuery('#swatch-visual-options-panel').on(
  208. 'click',
  209. '.colorpicker_handler',
  210. swatchVisualOption.initColorPicker
  211. );
  212. });
  213. jQuery('body').on('click', function (event) {
  214. var element = jQuery(event.target);
  215. if (
  216. element.parents('.swatch_sub-menu_container').length === 1 ||
  217. element.next('div.swatch_sub-menu_container').length === 1
  218. ) {
  219. return true;
  220. }
  221. jQuery('.swatch_sub-menu_container').hide();
  222. });
  223. if (config.isSortable) {
  224. jQuery(function ($) {
  225. $('[data-role=swatch-visual-options-container]').sortable({
  226. distance: 8,
  227. tolerance: 'pointer',
  228. cancel: 'input, button',
  229. axis: 'y',
  230. /**
  231. * Update component
  232. */
  233. update: function () {
  234. $('[data-role=swatch-visual-options-container] [data-role=order]').each(
  235. function (index, element) {
  236. $(element).val(index + 1);
  237. }
  238. );
  239. }
  240. });
  241. });
  242. }
  243. window.swatchVisualOption = swatchVisualOption;
  244. window.swatchOptionVisualDefaultInputType = swatchOptionVisualDefaultInputType;
  245. rg.set('swatch-visual-options-panel', swatchVisualOption);
  246. jQuery(function ($) {
  247. var swatchComponents = {
  248. /**
  249. * div wrapper for to hide all evement
  250. */
  251. wrapper: null,
  252. /**
  253. * iframe component to perform file upload without page reload
  254. */
  255. iframe: null,
  256. /**
  257. * form component for upload image
  258. */
  259. form: null,
  260. /**
  261. * Input file component for upload image
  262. */
  263. inputFile: null,
  264. /**
  265. * Create swatch component for upload files
  266. *
  267. * @this {swatchComponents}
  268. * @public
  269. */
  270. create: function () {
  271. this.wrapper = $('<div>').css({
  272. display: 'none'
  273. }).appendTo($('body'));
  274. this.iframe = $('<iframe></iframe>', {
  275. id: 'upload_iframe',
  276. name: 'upload_iframe'
  277. }).appendTo(this.wrapper);
  278. this.form = $('<form></form>', {
  279. id: 'swatch_form_image_upload',
  280. name: 'swatch_form_image_upload',
  281. target: 'upload_iframe',
  282. method: 'post',
  283. enctype: 'multipart/form-data',
  284. class: 'ignore-validate',
  285. action: config.uploadActionUrl
  286. }).appendTo(this.wrapper);
  287. this.inputFile = $('<input />', {
  288. type: 'file',
  289. name: 'datafile',
  290. class: 'swatch_option_file'
  291. }).appendTo(this.form);
  292. $('<input />', {
  293. type: 'hidden',
  294. name: 'form_key',
  295. value: FORM_KEY
  296. }).appendTo(this.form);
  297. }
  298. };
  299. /**
  300. * Create swatch components
  301. */
  302. swatchComponents.create();
  303. /**
  304. * Register event for swatch input[type=file] change
  305. */
  306. swatchComponents.inputFile.change(function () {
  307. var container = $('#' + $(this).attr('data-called-by')).parents().eq(2).children('.swatch_window'),
  308. /**
  309. * @this {iframe}
  310. */
  311. iframeHandler = function () {
  312. var imageParams = $.parseJSON($(this).contents().find('body').html()),
  313. fullMediaUrl = imageParams['swatch_path'] + imageParams['file_path'];
  314. container.prev('input').val(imageParams['file_path']);
  315. container.css({
  316. 'background-image': 'url(' + fullMediaUrl + ')',
  317. 'background-size': 'cover'
  318. });
  319. container.parent().removeClass('unavailable');
  320. };
  321. swatchComponents.iframe.off('load');
  322. swatchComponents.iframe.load(iframeHandler);
  323. swatchComponents.form.submit();
  324. $(this).val('');
  325. });
  326. /**
  327. * Register event for choose "upload image" option
  328. */
  329. $(document).on('click', '.btn_choose_file_upload', function () {
  330. swatchComponents.inputFile.attr('data-called-by', $(this).attr('id'));
  331. swatchComponents.inputFile.trigger('click');
  332. });
  333. /**
  334. * Register event for remove option
  335. */
  336. $(document).on('click', '.btn_remove_swatch', function () {
  337. var optionPanel = $(this).parents().eq(2);
  338. optionPanel.children('input').val('');
  339. optionPanel.children('.swatch_window').css('background', '');
  340. optionPanel.addClass('unavailable');
  341. jQuery('.swatch_sub-menu_container').hide();
  342. });
  343. /**
  344. * Toggle color upload chooser
  345. */
  346. $(document).on('click', '.swatches-visual-col', function () {
  347. var currentElement = $(this).find('.swatch_sub-menu_container');
  348. jQuery('.swatch_sub-menu_container').not(currentElement).hide();
  349. currentElement.toggle();
  350. });
  351. });
  352. };
  353. });