NavPanel_wev8.js 9.87 KB
define(['mUtil', "Component"],function(mUtil, Component) {
	var NavPanel = function(options) {
		var _navPanel = {};
		
		Component.super(this, options);
		
		this.type = "NavPanel";
		this.tpl = this.type + "_html";
		this.css = ["swipe_css", "grid_css", "mService/portal_css"];

		var vm = this.viewModel = {
			lite: true,//精简模式
			row: 2,//行数
        	col: 4,//列数
        	radius: false,//图片圆角
			categories:[],
			badge: null,//提醒数字
			callback: {
				click: function () { }
			}
		};
		
		this.beforeMount = function(){
			var items = [],
				categories = vm.categories;
			var screenSize = vm.row * vm.col, radius = vm.radius, screenItem;
			
			var showItems = 0;
			var totalItems = 0;
	
			var isPreversion = false;//兼容没有分类的数据
			if(vm.categories.length == 0){
				isPreversion = true;
				var nav_category = {};
				nav_category["items"] = vm.items;
				vm.categories.push(nav_category);
			}
			var customItems;
			if(vm.lite && !isPreversion){//获取用户自定义显示
				var actionUrl = mUtil.getActionUrl("service.MobileCommon", {action: "getUserMecConfig", mecid: this.id});
				mUtil.ajax(actionUrl, function (result) {
					var data = result.data || {};
					if(data.status && data.status == 1){
						customItems = data.result;
					}
				}, {async: false, dataType: "json"});
			}
			
			var isUserCustom = customItems && mUtil.isArray(customItems);//用户自定义
			if(vm.badge === true){
        		vm.badge = mUtil.getActionUrl(this.type, {action:"getBadge", "mec_id": this.id, "lite": vm.lite, "custom": isUserCustom ? customItems.toString() : ""}, this.pageid);
    		}
			vm.remindApis = [];
			var permItems = [];
			categories.forEach(function(category){
			    permItems.push.apply(permItems, category["items"].filter(function(item){
			        return item.permission;
                }));
            });
			var done = $.Deferred().resolve();
			if(permItems.length){
                var perms = permItems.map(function(item){
                    return item.permission;
                });
                done = this.judgePermission(perms).done(function(res){
                    if(res.status == '1'){
                        var permRes = res.data;
                        permRes.forEach(function(hasPerm, i){
                            if(!hasPerm){
                                categories.every(function(category){
                                    return category["items"].every(function(item, index){
                                        if(item.id == permItems[i].id){
                                            category["items"].splice(index, 1);
                                            return false;
                                        }
                                        return true;
                                    });
                                });
                            }
                        });
                    } else {
                        vm.categories = [];
                    }
                }).fail(function(){
                    vm.categories = [];
                });
            }

            return done.then(function() {
                categories.forEach(function (category) {
                    category["items"].forEach(function (item) {
                        totalItems++;
                        item.iconClass = radius && "wev-icon-radius" || "";
                        if (item.remind && item.remindapi && item.remindapi.api) {
                            item.uuid = item.remindapi.uuid = mUtil.UUID();
                            item.apiid = item.remindapi.api.id;
                            vm.remindApis.push(item.remindapi);
                            delete item.remindapi;
                        }
                        if (isUserCustom) {
                            var arrIndex = customItems.indexOf(item.id);
                            if (arrIndex > -1) {
                                customItems[arrIndex] = item;
                            }
                        } else {
                            if (isPreversion) item.show = true;

                            if (item.show) {
                                item.del = true;
                                if (showItems % screenSize == 0) {
                                    screenItem = [];
                                    items.push(screenItem);
                                }
                                showItems++;
                                var icon = item.icon;
                                if (mUtil.isObject(icon)) {
                                    item.icon = icon.path;
                                }
                                screenItem.push(item);
                            }
                        }
                    });

                    while (category["items"].length % vm.col != 0) {//不够一列数量用空li补位
                        category["items"].push({fillIn: true});
                    }
                });

                if (isUserCustom) {
                    customItems.forEach(function (item) {
                        if (mUtil.isObject(item)) {
                            item.iconClass = radius && "wev-icon-radius" || "";
                            if (showItems % screenSize == 0) {
                                screenItem = [];
                                items.push(screenItem);
                            }
                            showItems++;
                            item.show = true;
                            item.del = true;
                            var icon = item.icon;

                            if (mUtil.isObject(icon)) {
                                item.icon = icon.path;
                            }
                            screenItem.push(item);
                        }
                    });
                }

                if (vm.lite) {
                    var hasMore = totalItems > showItems;
                    if (hasMore || isUserCustom) {
                        var moreItem = {
                            icon: "/mobilemode/piclibrary/01-E9_default/more.png",
                            iconClass: radius && "wev-icon-radius wev-portal-more" || "wev-portal-more",
                            show: true,//显示
                            text: mUtil.getLabel(131654, "更多"),
                            fillIn: true//补位
                        };
                        if (showItems % screenSize == 0) {
                            items.push([]);
                        }
                        items[items.length - 1].push(moreItem);
                    }

                    var last = items.length - 1;
                    if (last >= 0) {
                        while (items[last].length % vm.col != 0) {//li补位
                            items[last].push({fillIn: true});
                        }
                    }

                    vm.isUserCustom = isUserCustom;
                    vm.hasMore = hasMore;
                    vm.items = items;
                } else {
                    vm.perCol = 100 - vm.col;//css计算li宽度
                }

                vm.categories = categories.filter(function(category){
                    return (category["items"].length > 0);
                });

            });
		};

        this.mounted = function() {
        	var that = this;
        	var $el = that.$el,
				$comp = $el.children(".wev-comp-" + that.type);
        	        	
        	$comp.on("click", "li.wev-table-view-cell > a", function(event){
				if (!mUtil.isFunction(vm.callback.click)) return;
				var itemId = that.id, currItem;
				vm.items.every(function (item) {
					if (vm.id != itemId) return true;
					currItem = item;
				});
				vm.callback.click.call(that, currItem);
        	});
        	
			_navPanel.loadBadge();
			
			if(vm.lite){
				if(vm.hasMore || vm.isUserCustom){
					$(".wev-portal-more", $comp).parent().on("click",function(){
						require(["mService"], function (mService) {
							mService.show("portal", {
								id: that.id,
								pageid: that.pageid,
								title: mUtil.getLabel(383193,"全部应用"),
								categories: vm.categories,
								items: vm.items,
								remindApis: vm.remindApis,
								col: vm.col,
								customItems : vm.customItems
							});
						});
					});
				}
				if (vm.items.length <= 1) return;
				
				var params = {
					continuous: false,
					disableScroll: true,
					stopPropagation: false,
					callback: function (index, element) {
						$(".wev-comp-swipe-point b", $comp).removeClass("curr-point").eq(index).addClass("curr-point");
					}
				};
				
				var pageid = this.pageid;
				var curpageid = $("#"+pageid).hasClass("tabpanel") ? pageid.substr(0, pageid.lastIndexOf("_")) : pageid;
				
				if($("#"+curpageid).hasClass("out")){//不是当前页面刷新
					params.width = $(window).width();
				}
				require(["swipe"], function () {
					Swipe($comp[0], params, function () {
						$el.find(".wev-panel-hide").removeClass("wev-panel-hide");
					});
				});
			}
        };
        
        this.refreshBadge = function(){
        	_navPanel.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'
            });
        }
        
        _navPanel.loadBadge = function(){
        	if(vm.badge == null){
        		return;
        	}
        	var $comp = this.$el.children(".wev-comp-"+this.type),
	    		$badges = $(".wev-badge", $comp);
        	
        	require(["remindHelper"], function(helper){
				helper.renderBadge(vm.badge, $badges, vm.remindApis);
        	});
		};
		
		_navPanel.loadBadge = _navPanel.loadBadge.bind(this);
    };

    return Component.init(NavPanel);
});