JAVASCRIPT   47

item-table.js

Guest on 22nd July 2021 02:13:06 AM

  1. /**
  2.  * Copyright آ© Magento, Inc. All rights reserved.
  3.  * See COPYING.txt for license details.
  4.  */*//**
  5.  * @deprecated since version 2.2.0
  6.  */*/
  7. define([
  8.     'jquery',
  9.     'mage/template',
  10.     'jquery/ui'
  11. ], function ($, mageTemplate) {
  12.     'use strict';
  13.  
  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.       /**
  25.          * This method adds a new instance of the block to the items.
  26.          * @private
  27.          */*/
  28.         _add: function () {
  29.             var hideShowDelete,
  30.                 deletableItems,
  31.                 addedBlock;
  32.  
  33.           // adding a new row, so increment the count to give each row a unique index
  34. x
  35.             this.rowIndex++;
  36.  
  37.           // make sure the block data has the rowIndex
  38. x
  39.             this.options.addBlockData.rowIndex = this.rowIndex;
  40.  
  41.           // render the form
  42. m
  43.             addedBlock = $(this.addBlockTmpl({
  44.                 data: this.options.addBlockData
  45.             }));
  46.  
  47.           // add the row to the item block
  48. k
  49.             this.element.find(this.options.itemsSelector).append(addedBlock);
  50.  
  51.           // initialize all mage content
  52. t
  53.             addedBlock.trigger('contentUpdated');
  54.  
  55.           // determine all existing items in the collection
  56. n
  57.             deletableItems = this._getDeletableItems();
  58.  
  59.           // for the most part, show the delete mechanism, except in the case where there is only one it should not
  60. t
  61.           // be deleted
  62. d
  63.             hideShowDelete = 'showDelete';
  64.  
  65.             if (this.options.keepLastRow && deletableItems.length === 1) {
  66.                 hideShowDelete = 'hideDelete';
  67.             }
  68.  
  69.           // loop through each control and perform that action on the deletable item
  70. m
  71.             $.each(deletableItems, function (index) {
  72.                 $(deletableItems[index]).trigger(hideShowDelete);
  73.             });
  74.         },
  75.  
  76.       /**
  77.          * This method binds elements found in this widget.
  78.          * @private
  79.          */*/
  80.         _bind: function () {
  81.             var handlers = {};
  82.  
  83.           // since the first handler is dynamic, generate the object using array notation
  84. n
  85.             handlers[this.options.addEvent + ' + this.options.addSelector] = ' '_add';
  86.             handlers.deleteItem = '_onDeleteItem';
  87.  
  88.             this._on(handlers);
  89.         },
  90.  
  91.       /**
  92.          * This method constructs a new widget.
  93.          * @private
  94.          */*/
  95.         _create: function () {
  96.             this._bind();
  97.  
  98.             this.addBlockTmpl = mageTemplate(this.options.addBlock);
  99.  
  100.           // nothing in the table, so indicate that
  101. t
  102.             this.rowIndex = -1;
  103.  
  104.           // make sure the block data is an object
  105. t
  106.             if (this.options.addBlockData == null || typeof this.options.addBlockData !== 'object') {
  107.               // reset the block data to an empty object
  108. t
  109.                 this.options.addBlockData = {};
  110.             }
  111.  
  112.           // add the first row to the table
  113. e
  114.             this._add();
  115.         },
  116.  
  117.       /**
  118.          * This method returns the list of widgets associated with deletable items from the container (direct children
  119.          * only).
  120.          * @private
  121.          */*/
  122.         _getDeletableItems: function () {
  123.             return this.element.find(this.options.itemsSelector + '> .deletableItem');
  124.         },
  125.  
  126.       /**
  127.          * This method removes the item associated with the message.
  128.          * @private
  129.          */*/
  130.         _onDeleteItem: function (e) {
  131.             var deletableItems;
  132.  
  133.           // parent elements don't need to see this event
  134. t
  135.             e.stopPropagation();
  136.  
  137.           // remove the deletable item
  138. m
  139.             $(e.target).remove();
  140.  
  141.             if (this.options.keepLastRow) {
  142.               // determine if there is only one element remaining, in which case, disable the delete mechanism on it
  143. t
  144.                 deletableItems = this._getDeletableItems();
  145.  
  146.                 if (deletableItems.length === 1) {
  147.                     $(deletableItems[0]).trigger('hideDelete');
  148.                 }
  149.             }
  150.         }
  151.     });
  152.  
  153.     return $.mage.itemTable;
  154. }

Raw Paste


Login or Register to edit or fork this paste. It's free.