item-table.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. 'mage/template',
  11. 'jquery/ui'
  12. ], function ($, mageTemplate) {
  13. 'use strict';
  14. $.widget('mage.itemTable', {
  15. options: {
  16. addBlock: '[data-template="add-block"]',
  17. addBlockData: {},
  18. addEvent: 'click',
  19. addSelector: '[data-role="add"]',
  20. itemsSelector: '[data-container="items"]',
  21. keepLastRow: true
  22. },
  23. /**
  24. * This method adds a new instance of the block to the items.
  25. * @private
  26. */
  27. _add: function () {
  28. var hideShowDelete,
  29. deletableItems,
  30. addedBlock;
  31. // adding a new row, so increment the count to give each row a unique index
  32. this.rowIndex++;
  33. // make sure the block data has the rowIndex
  34. this.options.addBlockData.rowIndex = this.rowIndex;
  35. // render the form
  36. addedBlock = $(this.addBlockTmpl({
  37. data: this.options.addBlockData
  38. }));
  39. // add the row to the item block
  40. this.element.find(this.options.itemsSelector).append(addedBlock);
  41. // initialize all mage content
  42. addedBlock.trigger('contentUpdated');
  43. // determine all existing items in the collection
  44. deletableItems = this._getDeletableItems();
  45. // for the most part, show the delete mechanism, except in the case where there is only one it should not
  46. // be deleted
  47. hideShowDelete = 'showDelete';
  48. if (this.options.keepLastRow && deletableItems.length === 1) {
  49. hideShowDelete = 'hideDelete';
  50. }
  51. // loop through each control and perform that action on the deletable item
  52. $.each(deletableItems, function (index) {
  53. $(deletableItems[index]).trigger(hideShowDelete);
  54. });
  55. },
  56. /**
  57. * This method binds elements found in this widget.
  58. * @private
  59. */
  60. _bind: function () {
  61. var handlers = {};
  62. // since the first handler is dynamic, generate the object using array notation
  63. handlers[this.options.addEvent + ' ' + this.options.addSelector] = '_add';
  64. handlers.deleteItem = '_onDeleteItem';
  65. this._on(handlers);
  66. },
  67. /**
  68. * This method constructs a new widget.
  69. * @private
  70. */
  71. _create: function () {
  72. this._bind();
  73. this.addBlockTmpl = mageTemplate(this.options.addBlock);
  74. // nothing in the table, so indicate that
  75. this.rowIndex = -1;
  76. // make sure the block data is an object
  77. if (this.options.addBlockData == null || typeof this.options.addBlockData !== 'object') {
  78. // reset the block data to an empty object
  79. this.options.addBlockData = {};
  80. }
  81. // add the first row to the table
  82. this._add();
  83. },
  84. /**
  85. * This method returns the list of widgets associated with deletable items from the container (direct children
  86. * only).
  87. * @private
  88. */
  89. _getDeletableItems: function () {
  90. return this.element.find(this.options.itemsSelector + '> .deletableItem');
  91. },
  92. /**
  93. * This method removes the item associated with the message.
  94. * @private
  95. */
  96. _onDeleteItem: function (e) {
  97. var deletableItems;
  98. // parent elements don't need to see this event
  99. e.stopPropagation();
  100. // remove the deletable item
  101. $(e.target).remove();
  102. if (this.options.keepLastRow) {
  103. // determine if there is only one element remaining, in which case, disable the delete mechanism on it
  104. deletableItems = this._getDeletableItems();
  105. if (deletableItems.length === 1) {
  106. $(deletableItems[0]).trigger('hideDelete');
  107. }
  108. }
  109. }
  110. });
  111. return $.mage.itemTable;
  112. });