Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

205 строки
5.7 KiB

  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define([
  6. 'jquery',
  7. 'jquery/ui'
  8. ], function ($) {
  9. 'use strict';
  10. $.widget('mage.form', {
  11. options: {
  12. handlersData: {
  13. save: {},
  14. saveAndContinueEdit: {
  15. action: {
  16. args: {
  17. back: 'edit'
  18. }
  19. }
  20. },
  21. preview: {
  22. target: '_blank'
  23. }
  24. }
  25. },
  26. /**
  27. * Form creation
  28. * @protected
  29. */
  30. _create: function () {
  31. this._bind();
  32. },
  33. /**
  34. * Set form attributes to initial state
  35. * @protected
  36. */
  37. _rollback: function () {
  38. if (this.oldAttributes) {
  39. this.element.prop(this.oldAttributes);
  40. }
  41. },
  42. /**
  43. * Check if field value is changed
  44. * @protected
  45. * @param {Object} e - event object
  46. */
  47. _changesObserver: function (e) {
  48. var target = $(e.target),
  49. changed;
  50. if (e.type === 'focus' || e.type === 'focusin') {
  51. this.currentField = {
  52. statuses: {
  53. checked: target.is(':checked'),
  54. selected: target.is(':selected')
  55. },
  56. val: target.val()
  57. };
  58. } else {
  59. if (this.currentField) { //eslint-disable-line no-lonely-if
  60. changed = target.val() !== this.currentField.val ||
  61. target.is(':checked') !== this.currentField.statuses.checked ||
  62. target.is(':selected') !== this.currentField.statuses.selected;
  63. if (changed) { //eslint-disable-line max-depth
  64. target.trigger('changed');
  65. }
  66. }
  67. }
  68. },
  69. /**
  70. * Get array with handler names
  71. * @protected
  72. * @return {Array} Array of handler names
  73. */
  74. _getHandlers: function () {
  75. var handlers = [];
  76. $.each(this.options.handlersData, function (key) {
  77. handlers.push(key);
  78. });
  79. return handlers;
  80. },
  81. /**
  82. * Store initial value of form attribute
  83. * @param {String} attrName - name of attribute
  84. * @protected
  85. */
  86. _storeAttribute: function (attrName) {
  87. var prop;
  88. this.oldAttributes = this.oldAttributes || {};
  89. if (!this.oldAttributes[attrName]) {
  90. prop = this.element.attr(attrName);
  91. this.oldAttributes[attrName] = prop ? prop : '';
  92. }
  93. },
  94. /**
  95. * Bind handlers
  96. * @protected
  97. */
  98. _bind: function () {
  99. this.element
  100. .on(this._getHandlers().join(' '), $.proxy(this._submit, this))
  101. .on('focus blur focusin focusout', $.proxy(this._changesObserver, this));
  102. },
  103. /**
  104. * Get action url for form
  105. * @param {Object|String} data - object with parameters for action url or url string
  106. * @return {String} action url
  107. */
  108. _getActionUrl: function (data) {
  109. if (typeof data === 'object') {
  110. return this._buildURL(this.oldAttributes.action, data.args);
  111. }
  112. return typeof data === 'string' ? data : this.oldAttributes.action;
  113. },
  114. /**
  115. * Add additional parameters into URL
  116. * @param {String} url - original url
  117. * @param {Object} params - object with parameters for action url
  118. * @return {String} action url
  119. * @private
  120. */
  121. _buildURL: function (url, params) {
  122. var concat = /\?/.test(url) ? ['&', '='] : ['/', '/'];
  123. url = url.replace(/[\/&]+$/, '');
  124. $.each(params, function (key, value) {
  125. url += concat[0] + key + concat[1] + window.encodeURIComponent(value);
  126. });
  127. return url + (concat[0] === '/' ? '/' : '');
  128. },
  129. /**
  130. * Prepare data for form attributes
  131. * @protected
  132. * @param {Object} data
  133. * @return {Object}
  134. */
  135. _processData: function (data) {
  136. $.each(data, $.proxy(function (attrName, attrValue) {
  137. this._storeAttribute(attrName);
  138. if (attrName === 'action') {
  139. data[attrName] = this._getActionUrl(attrValue);
  140. }
  141. }, this));
  142. return data;
  143. },
  144. /**
  145. * Get additional data before form submit
  146. * @protected
  147. * @param {String} handlerName
  148. * @param {Object} data
  149. */
  150. _beforeSubmit: function (handlerName, data) {
  151. var submitData = {},
  152. event = new $.Event('beforeSubmit');
  153. this.element.trigger(event, [submitData, handlerName]);
  154. data = $.extend(
  155. true, {},
  156. this.options.handlersData[handlerName] || {},
  157. submitData,
  158. data
  159. );
  160. this.element.prop(this._processData(data));
  161. return !event.isDefaultPrevented();
  162. },
  163. /**
  164. * Submit the form
  165. * @param {Object} e - event object
  166. * @param {Object} data - event data object
  167. */
  168. _submit: function (e, data) {
  169. this._rollback();
  170. if (this._beforeSubmit(e.type, data) !== false) {
  171. this.element.trigger('submit', e);
  172. }
  173. }
  174. });
  175. return $.mage.form;
  176. });