visual.js 14 KB

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