/lib/web/mage/backend/form.js

https://gitlab.com/axeltizon/magento-demopoweraccess · JavaScript · 194 lines · 121 code · 13 blank · 60 comment · 16 complexity · a9941749129498905194ff75cc903e37 MD5 · raw file

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