products.js 7.34 KB
$(function() {
    /**
     * Product table scripts
     */
    $(document)
        .on('click', 'td.arrow-adm>a, td.title-adm>a', function(e) {
            e.preventDefault();
            var $tr = $(this)
                .parent()
                .parent();

            var id = $tr.data('key');

            var $oldTable = $('#variants-table');

            if ($oldTable.length > 0) {
                $oldTable.remove();
                var $activeTr = $('tr.active');
                $activeTr.removeClass('active');
                if ($activeTr.data('key') !== id) {
                    addTable($tr, id);
                }
            } else {
                addTable($tr, id);
            }

        });

    $(document)
        .on('click', '.variant-sort-save', function(e) {
            e.preventDefault();
            var id = $(this)
                .data('key');
            var value = $('#variant-sort-input-' + id)
                .val();

            $.ajax({
                type: "GET",
                url: '/admin/catalog/product/sort-variant?id=' + id + '&sort=' + value,
                success: function(data) {
                    $('#variants-table')
                        .replaceWith(data);
                    initSwitchers();
                    initCheckboxes();
                    new PNotify({
                        title: 'Info',
                        text: 'Sort updated',
                        type: 'info',
                        styling: "bootstrap3"
                    });
                },
                error: function() {
                    new PNotify({
                        title: 'Info',
                        text: 'Server error',
                        type: 'error',
                        styling: "bootstrap3"
                    });
                }
            });

        });

    $(document)
        .on('change', '.variant-status input', function() {
            var id = $(this)
                .data('key');
            var status;
            if (this.checked) {
                status = 1;
            } else {
                status = 0;
            }

            $.ajax({
                type: "GET",
                url: '/admin/catalog/product/status-variant?id=' + id + '&status=' + status,
                success: function(data) {
                    $('#variants-table')
                        .replaceWith(data);
                    initSwitchers();
                    initCheckboxes();
                    new PNotify({
                        title: 'Info',
                        text: 'Sort updated',
                        type: 'info',
                        styling: "bootstrap3"
                    });
                },
                error: function() {
                    new PNotify({
                        title: 'Info',
                        text: 'Server error',
                        type: 'error',
                        styling: "bootstrap3"
                    });
                }
            });
        });

    $(document)
        .on('blur', '.variant-active-input', function() {
            var value = $(this)
                .val(), field = $(this)
                .data('field'), id = $(this)
                .data('id');

            if (value !== activeValue) {
                $.ajax({
                    type: "GET",
                    url: '/admin/catalog/product/update-attribute?id=' + id + '&field=' + field + '&value=' + value,
                    success: function(data) {
                        $('#variants-table')
                            .replaceWith(data);
                        initSwitchers();
                        initCheckboxes();
                        activeValue = null;
                        new PNotify({
                            title: 'Info',
                            text: 'Attribute updated',
                            type: 'info',
                            styling: "bootstrap3"
                        });
                    },
                    error: function() {
                        activeValue = null;
                        new PNotify({
                            title: 'Info',
                            text: 'Server error',
                            type: 'error',
                            styling: "bootstrap3"
                        });
                    }
                });
            }
        });

    var activeValue = null;

    $(document)
        .on('focus', '.variant-active-input', function() {
            activeValue = $(this)
                .val();
        });

    function addTable(row, id) {
        $.ajax({
            type: "GET",
            url: '/admin/catalog/product/variants?id=' + id,
            success: function(data) {
                row.after(data);
                row.addClass('active');
                initCheckboxes();
                initSwitchers();
            }
        });
    }

    function initCheckboxes() {
        $('.variant-checkbox')
            .iCheck({
                checkboxClass: 'icheckbox_flat-green',
                radioClass: 'iradio_flat-green'
            });
    }

    function initSwitchers() {
        var elems = Array.prototype.slice.call(document.querySelectorAll('.varint-switcher'));

        elems.forEach(function(html) {
            new Switchery(html);
        });

    }

    /**
     * Product save page scripts
     */
    $(document)
        .on('click', '.pos-minus-adm', function(e) {
            e.preventDefault();
            var input = $(this)
                .parent()
                .find('input'), newVal;

            if (input.val().length > 0) {
                newVal = parseInt(input.val()) - 1;
            } else {
                newVal = 1;
            }

            if (newVal > 0) {
                input.val(newVal);
            }
        });
    $(document)
        .on('click', '.pos-pluse-adm', function(e) {
            e.preventDefault();
            var input = $(this)
                .parent()
                .find('input'), newVal;

            if (input.val().length > 0) {
                newVal = parseInt(input.val()) + 1;
            } else {
                newVal = 1;
            }

            input.val(newVal);
        });

    function initVariantSwitchers() {
        var elems = Array.prototype.slice.call(document.querySelectorAll('.variant-switchery'));
        elems.forEach(function(html) {
            if (!html.hasAttribute('data-switchery')) {
                new Switchery(html);
            }
        });
    }

    $(document)
        .on('click', '.delete-variant', function(e) {
            e.preventDefault();
            if ($('.variant-wr').length > 1) {
                $(this)
                    .parent()
                    .parent()
                    .parent()
                    .remove();
            }
        });

    $(document)
        .on('click', '#add-variant', function(e) {
            e.preventDefault();
            var $button = $(this);
            var key = $button
                .data('key');
            $.ajax({
                type: "GET",
                url: '/admin/catalog/product/add-variant?key=' + key,
                success: function(data) {
                    $('#add-variant-wr')
                        .prepend(data);
                    $button.data('key', key + 1);
                    initVariantSwitchers();
                }
            });
        });

    initVariantSwitchers();
});