sortmenu.js 2.14 KB
'use strict';
var $ = require('jquery');

var init=function(callback){

    var $navItem = $('.list-group-item');
    var $navIcon = $navItem.find('span.icon');
    $navItem.click(function (event) {
        var $this = $(this),
            $thisNext = $this.nextAll(),
            thisVal = $(this).attr('data-val'),
            $target = $(event.target);
        if (!$this.attr('data-active')) {
            if ($target.hasClass('list-group-item')) {
                $navItem.removeClass('attr-item-active');
                $(this).addClass('attr-item-active').attr('data-active', 'data-active');
                $('.model-attr-btn').removeAttr('disabled');
                $('input#categoryId').val($this.attr('data-val'));
            }
        } else {
            if ($target.hasClass('list-group-item')) {
                $(this).removeClass('attr-item-active').removeAttr('data-active');
                $('.model-attr-btn').attr('disabled', 'disabled');
            }
        }

        if ($this.attr('data-status') === 'close') {
            if ($target.hasClass('icon')) {
                $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 {
            if ($target.hasClass('icon')) {
                $thisNext.filter(function (index) {
                    var thisR = $(this).attr('data-parent') === thisVal;
                    if (thisR && $(this).attr('data-status') === 'open') {
                        $(this).find('span.icon').trigger('click');
                    }
                    return thisR;
                }).addClass('hidden');
                $this.attr('data-status', 'close').find('span')
                    .removeClass('glyphicon-minus').addClass('glyphicon-plus');
            }
        }
        window.sortid=thisVal;
        window.sortname=$.trim($(".attr-item-active[data-val="+thisVal+"]").text());//c.join('/');
        callback&&callback();
    });

}

module.exports={init:init};