Navigation_wev8.js 3.24 KB
define(['mUtil', "Component"],function(mUtil, Component) {
    var Navigation = function(options) {
        var _navigation = {};

        Component.super(this, options);
        
        this.type = "Navigation";
        this.tpl = this.type + "_html";
        this.css = this.type + "_css";

        var vm = this.viewModel = {
        	items : [],
        	badge : null
        };
        
        this.beforeMount = function(){
            var remindApis = [];
            vm.items.map(function(item){
                if(item.remind && item.remindapi && item.remindapi.api){
                    item.uuid = item.remindapi.uuid = mUtil.UUID();
                    item.apiid = item.remindapi.api.id;
                    remindApis.push(item.remindapi);
                    delete item.remindapi;
                }
            });
            vm.remindApis = remindApis;
        	if(vm.badge === true){
        		vm.badge = mUtil.getActionUrl(this.type, {action:"getBadge", "mec_id": this.id}, this.pageid);
    		}

            var permItems = vm.items.filter(function(item){
                return item.permission;
            });

            if(permItems.length){
                var perms = permItems.map(function(item){
                        return item.permission;
                    });
                return this.judgePermission(perms).done(function(res){
                    if(res.status == '1'){
                        var permRes = res.data, orgIndex, orgItem;
                        permRes.forEach(function(hasPerm, index){
                            if(!hasPerm){
                                orgItem = permItems[index];
                                orgIndex = vm.items.indexOf(permItems[index]);
                                if(orgItem.group && orgIndex != 0){
                                    vm.items[orgIndex - 1].group = true;
                                }
                                vm.items.splice(orgIndex, 1);
                            }
                        });
                    } else {
                        vm.items = [];
                    }
                }).fail(function(){
                    vm.items = [];
                });
            }
        };
        
        this.mounted = function() {
        	_navigation.loadBadge();
        };
        
        this.refreshBadge = function(){
        	_navigation.loadBadge();
		};

        this.judgePermission = function(perms) {
            var actionUrl = mUtil.getActionUrl(this.type, {action: "judgePermission"}, this.pageid);
            return $.ajax({
                url: mUtil.completeUrl(actionUrl),
                data: {
                    perms: JSON.stringify(perms)
                },
                type: 'POST',
                dataType: 'json'
            });
        }
        
        _navigation.loadBadge = function(){
        	if(vm.badge == null){
        		return;
        	}
        	var $abbr = this.$el,
        		$badges = $(".wev-badge", $abbr).not(".wev-badge-text");//对于提示文本跳过 
        	require(["remindHelper"], function(helper){
                helper.renderBadge(vm.badge, $badges, vm.remindApis);
        	});
        };

        _navigation.loadBadge = _navigation.loadBadge.bind(this);
    };

    return Component.init(Navigation);
});