form.js 5.9 KB

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