list.js 4.06 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

/**
 * @deprecated since version 2.2.0
 */
define([
    'jquery',
    'mage/template',
    'jquery/ui'
], function ($, mageTemplate) {
    'use strict';

    $.widget('mage.list', {
        options: {
            template: '[data-role=item]', //template for item,
            templateWrapper: null, //template wrapper
            templateClass: null, //template wrapper class
            destinationSelector: '[data-role=container]', //destination selector of list
            itemIndex: 0, //setting an item index
            itemCount: 0, //get count of items
            addButton: '[data-button=add]', //button for adding list item
            removeButton: '[data-button=remove]', //button for removing list item
            maxItems: null,
            maxItemsAlert: null
        },

        /**
         * @private
         */
        _create: function () {
            var options, destination, addButton;

            this.options.itemCount = this.options.itemIndex = 0;

            options = this.options;
            destination = $(options.destinationSelector);
            addButton = this.element.find($(options.addButton));

            this.element
                .addClass('list-widget');

            addButton.bind('click', $.proxy(this.handleAdd, this));

            //handle remove
            destination.on('click', this.options.removeButton, $.proxy(this.removeItem, this));
        },

        /**
         * @return {Boolean}
         */
        handleAdd: function () {
            this.addItem(++this.options.itemIndex);

            return false;
        },

        /**
         * @param {*} index
         * @param {*} parent
         * @return {*|jQuery|HTMLElement}
         */
        addItem: function (index, parent) {
            var options = this.options,
                template = $(options.template),
                destination = $(options.destinationSelector),
                item = $(options.templateWrapper),
                source, preTemplate, context, compiledTemplate;

            item.addClass(this.options.templateClass)
                .attr('id', 'list-item-' + index)
                .attr('data-role', 'addedItem')
                .attr('data-parent', parent);

            source = template.html();
            preTemplate = mageTemplate(source);
            context = this.handleContext(index);
            compiledTemplate = preTemplate({
                data: context
            });

            item.append(compiledTemplate);
            destination.append(item);

            this.checkLimit(++this.options.itemCount);

            return item;
        },

        /**
         * @param {*} index
         * @return {Object}
         */
        handleContext: function (index) {
            var context = {
                _index_: index
            };

            return context;
        },

        /**
         * @param {jQuery.Event} e
         * @return {Boolean}
         */
        removeItem: function (e) {
            $(e.currentTarget).closest('[data-role="addedItem"]').remove();

            this.checkLimit(--this.options.itemCount);

            return false;
        },

        /**
         * @param {*} index
         */
        checkLimit: function (index) {
            var addButton = $(this.options.addButton),
                maxItems = this.options.maxItems,
                maxItemsAlert = $(this.options.maxItemsAlert);

            if (maxItems !== null && index >= maxItems) {
                addButton.hide();
                maxItemsAlert.show();
            } else if (addButton.is(':hidden')) {
                addButton.show();
                maxItemsAlert.hide();
            }
        },

        /**
         * @private
         */
        _destroy: function () {
            var destination = $(this.options.destinationSelector);

            this.element
                .removeClass('list-widget');
            destination
                .find('[data-role="addedItem"]')
                .remove();
        }
    });

    return $.mage.list;
});