fieldset-controls.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. /**
  6. * @deprecated since version 2.2.0
  7. */
  8. define([
  9. 'jquery',
  10. 'jquery/ui'
  11. ], function ($) {
  12. 'use strict';
  13. /**
  14. * This widget will allow a control with the fieldsetResetControl widget attached to reset a set of input fields.
  15. * The input fields to reset are defined by the inputSelector selector. The widget will store a clone of the fields
  16. * on create, and on trigger of fieldsetReset event it resets the defined fields. The event is triggered by the
  17. * reset control widget.
  18. *
  19. * For inputs of type file, the whole dom element is replaced as changing the value is a security violation
  20. * For inputs of type checkbox or radio, the checked attribute is added or removed as appropriate
  21. * For all others the jquery .val method is used to update to value to the original.
  22. */
  23. $.widget('mage.fieldsetControls', {
  24. original: undefined,
  25. options: {
  26. inputSelector: '[data-reset="true"]'
  27. },
  28. /**
  29. * @private
  30. */
  31. _create: function () {
  32. this.original = this.element.find(this.options.inputSelector).clone(true);
  33. this._bind();
  34. },
  35. /**
  36. * @private
  37. */
  38. _bind: function () {
  39. this._on({
  40. 'fieldsetReset': '_onReset'
  41. });
  42. },
  43. /**
  44. * @param {jQuery.Event} e
  45. * @private
  46. */
  47. _onReset: function (e) {
  48. var items;
  49. e.stopPropagation();
  50. // find all the ones we have to remove
  51. items = this.element.find(this.options.inputSelector);
  52. // loop over replacing each one.
  53. items.each($.proxy(function (index, item) {
  54. if ($(item).attr('type') == 'file') { //eslint-disable-line eqeqeq
  55. // Replace the current one we found with a clone of the original saved earlier
  56. $(item).replaceWith($(this.original[index]).clone(true));
  57. } else if ($(item).attr('type') == 'checkbox' || $(item).attr('type') == 'radio') { //eslint-disable-line
  58. // Return to original state.
  59. if ($(this.original[index]).attr('checked') === undefined) {
  60. $(item).removeAttr('checked');
  61. } else {
  62. $(item).attr('checked', $(this.original[index]).attr('checked'));
  63. }
  64. } else {
  65. // Replace the value with the original
  66. $(item).val($(this.original[index]).val());
  67. }
  68. }, this));
  69. }
  70. });
  71. $.widget('mage.fieldsetResetControl', {
  72. /**
  73. * @private
  74. */
  75. _create: function () {
  76. this._bind();
  77. },
  78. /**
  79. * @private
  80. */
  81. _bind: function () {
  82. this._on({
  83. click: '_onClick'
  84. });
  85. },
  86. /**
  87. * @param {jQuery.Event} e
  88. * @private
  89. */
  90. _onClick: function (e) {
  91. e.stopPropagation();
  92. $(this.element).trigger('fieldsetReset');
  93. }
  94. });
  95. return {
  96. fieldsetControls: $.mage.fieldsetControls,
  97. fieldsetResetControl: $.mage.fieldsetResetControl
  98. };
  99. });