index.js 4.52 KB
/*
 *@time: 2016/1/26
 *@author: chenglong
 */


var $ = require('jquery');
var edit = require('../common/edit');
var dropDown = require('../common/dropDown');

require('../util/select2');

exports.init = function () {

    //列表展示效果

    var $toggleTd = $('.toggle-td');

    $toggleTd.click(function () {

        var $this = $(this),
            $thisNext = $this.closest('tr').nextAll(),
            thisVal = $(this).closest('tr').attr('data-val');

        if ($this.closest('tr').attr('data-status') === 'close') {

            $thisNext.filter(function (index) {

                return $(this).attr('data-parent') === thisVal;

            }).removeClass('hidden');

            $this.closest('tr').attr('data-status', 'open').find('.toggle-td').find('span')
                .removeClass('glyphicon-folder-close').addClass('glyphicon-folder-open');

        } else {

            $thisNext.filter(function (index) {

                var thisR = $(this).attr('data-parent') === thisVal;

                if (thisR && $(this).attr('data-status') === 'open') {
                    $(this).find('.toggle-td').trigger('click');
                }

                return thisR;

            }).addClass('hidden');

            $this.closest('tr').attr('data-status', 'close').find('.toggle-td').find('span')
                .removeClass('glyphicon-folder-open').addClass('glyphicon-folder-close');
        }

    });

    //列表编辑

    var $table = $('.toggle-table');

    $table.click(function (event) {

        var $target = $(event.target),
            id;

        //编辑
        if ($target.hasClass('edit-class-btn')) {

        } else if ($target.hasClass('open-close-btn')) {

            var status;

            if ($target.attr('data-status') === '1') {
                status = 0;
            } else if ($target.attr('data-status') === '0') {
                status = 1;
            }

            console.log(status);

            $.ajax({
                type: 'POST',
                dataType: 'json',
                url: '/product/class/update',
                data: {
                    id: $target.closest('td').attr('data-id'),
                    status: status
                }
            }).then(function (data) {

                if (data.code === 200) {

                    $target.attr('data-status', status);
                    if ($target.hasClass('btn-danger')) {
                        $target.text('开启');
                        $target.removeClass('btn-danger')
                            .addClass('btn-warning').closest('td').prev('td').text('关闭');
                    } else {
                        $target.text('关闭');
                        $target.removeClass('btn-warning')
                            .addClass('btn-danger').closest('td').prev('td').text('开启');
                    }

                }
            });

            event.preventDefault();
        }
    });

    new dropDown({
        el:'.level-select'
    });

    //产品属性列表三级导航

    var $navItem = $('.list-group-item');

    $navItem.click(function () {

        var $this = $(this),
            $thisNext = $this.nextAll(),
            thisVal = $(this).attr('data-val');

        if ($this.attr('data-status') === 'close') {

            $thisNext.filter(function (index) {

                return $(this).attr('data-parent') === thisVal;

            }).removeClass('hidden');

            $this.attr('data-status', 'open').find('span')
                .removeClass('glyphicon-plus').addClass('glyphicon-minus');

        } else {

            $thisNext.filter(function (index) {

                var thisR = $(this).attr('data-parent') === thisVal;

                if (thisR && $(this).attr('data-status') === 'open') {
                    $(this).trigger('click');
                }

                return thisR;

            }).addClass('hidden');

            $this.attr('data-status', 'close').find('span')
                .removeClass('glyphicon-minus').addClass('glyphicon-plus');
        }

    });

    //添加属性
    //表单验证
    var addAttrVerification = new edit("#add-attr-form");

    $('.add-attr-btn').click(function(){

        addAttrVerification.submit($("#add-attr-form").attr("action"), function(option,that) {
            option.success=function(res){
                //todo
            };
            option.error=function(res){
                //todo
            }
        });
        return false;
    });

    //添加产品属性下拉框实例化
    new dropDown({
        el:'.attr-select'
    });

};