service.portal_wev8.js 8.19 KB
define("mService/portal", ["mUtil", "mService"], function(mUtil, mService) {
	return {
		mounted: function($page, cfg){
			var loadBadge = function(){
	    		var actionUrl = mUtil.getActionUrl("NavPanel", {action:"getBadge", "mec_id": defCfg.id, "lite": false}, defCfg.pageid);
	        	var $badges = $(".wev-badge", $(".wev-page-portal", $page));
	        	
	        	require(["remindHelper"], function(helper){
					helper.renderBadge(actionUrl, $badges, defCfg.remindApis);
	        	});
			};
			var modifyCfg = function(){
				var showItems = [];
				$(".sort-handle", $(".portal-items-big ul")).each(function(){
					var itemid = $(this).find("a").attr("id").substring(2);
					showItems.push(itemid);
				});
				
	    		var actionUrl =  mUtil.getActionUrl("service.MobileCommon", {action: "setUserMecConfig"});
	    		var paramData = {"config":JSON.stringify(showItems), "mecid": defCfg.id};
	    		mUtil.ajax(actionUrl, paramData, function(result){
	    			if(result.status == 1){
	    				Mobile_NS.triggerRefresh(defCfg.id);
	    				Mobile_NS.msg(mUtil.getLabel(384445,"保存设置成功"));
	    			}
	    		}, {type: "POST", dataType: "json"});
			};
			var defCfg = {
				id: "",
				pageid: "",
				categories : [],
				items : [],
				col : "4",
				title: ""
		    };
			$.extend(defCfg, cfg);
			
			$(".wev-header", $page).html(defCfg.title);
			
	  		var tmpl_items = [
    			'<div class="portal-items">',
    			'<div>',
    				//应用编辑小图标
	    			'<div class="portal-items-small"><span>#390289#</span>',
	    			  '<span>',
					  '{@each datas as d}',
					  '{@each d as item}',
						  '{@if !item.fillIn}',
						        '<img id="s_${item.id}" class="${item.iconClass}" src="${item.icon}"/>',
				          '{@/if}',  
				      '{@/each}',
					  '{@/each}',
					  '</span>',
					  '<span class="portal-edit-btn"></span>',
	    			'</div>',
	    			
	    			//应用编辑拖拽内容
	    			'<div  class="portal-items-big">',
                    '<div class="portal-category">',
                	'<div>',
                	'<div class="portal-title"><span>#390289#<span class="portal-item-dragtips">#390461#</span></span><span class="portal-done-btn"></span></div>',
                    '<div class="portal-square">',
                    	  '<ul class="portal-square-inner portal-flex">',
    					  '{@each datas as d}',
    					  '{@each d as item}',
							  '{@if item.fillIn}',
							  	  '<li class="sort-hidden" style="width: calc(${perCol}% / ${col});"></li>',
							  '{@else}',
							  	  '<li class="sort-handle" style="width: calc(${perCol}% / ${col});">',
								  '<a id="b_${item.id}">',
							        '<img class="wev-big-icon ${item.iconClass}" src="${item.icon}"/>',
							        '<div class="portal-text">',
							        	'$${item.text}',
							        '</div>',
							        '<div class="portal-item-edit portal-item-del"></div>',
							       '</a>',
							       '</li>',
					          '{@/if}',   
						      '{@/each}',
							  '{@/each}',
						  '</ul>',
						'</div>',
						'</div>',
						'</div>',
	    			'</div>',
    			'</div>',
            ].join('');
			
    		var tmpl_categories = [
    			//全部应用
                '{@each datas as d}',
                    '<div class="portal-category portal-items-select">',
                	'<div>',
                	'<div class="portal-title"><span>${d.category}</span></div>',
                    '<div class="portal-square">',
                    	  '<ul class="portal-square-inner portal-flex">',
						  '{@each d.items as item}',
							  '{@if item.fillIn}',
							  	  '<li style="width: calc(${perCol}% / ${col});"></li>',
							  '{@else}',
							  	  '<li style="width: calc(${perCol}% / ${col});">',
								  '<a id="c_${item.id}" href="${item.url}" data-ajax="${item.dataAjax}" data-reload="true" data-formdata="${item.queryString}">',
							        '<img class="wev-big-icon ${item.iconClass}" src="${item.icon}"/>',
							        '<div class="portal-text">',
							        	'$${item.text}',
							        '</div>',
							        '{@if item.remind}',
							         '<span class="wev-badge" data-index="${index}" {@if item.apiid}data-api="${item.uuid}_${item.apiid}"{@/if}></span>',
							        '{@/if}',
							        '<div class="portal-item-edit {@if item.del}portal-item-del{@else}portal-item-add{@/if}"></div>',
							       '</a>',
							       '</li>',
					          '{@/if}',   
						  '{@/each}',
						  '</ul>',
						'</div>',
						'</div>',
						'</div>',
                '{@/each}'    
            ].join('');
			
			var labelCahe = {
				'131654':'更多',
				'384116':'编辑',
				'386812':'完成',
				'390289':'我的应用',
				'383193':'全部应用',
				'390461':'(按住拖动调整排序)'
			};
			var replaceTempi18nLabel = function(tmpl,labels){
				for(var labelIndex in labels){
					tmpl = tmpl.replace(eval("/#"+labelIndex+"#/g"),labels[labelIndex]);
				}
				return tmpl;
			};
			
			var hrefMap = {};
			mUtil.getMultiLabel(labelCahe,function(resultlabels){
				!defCfg.title && $(".wev-header", $page).html(resultlabels['383193']);
				$(".wev-loading", $page).hide();
				
				var calWidth = 100-defCfg.col;
				var html = mUtil.parseTemplate(replaceTempi18nLabel(tmpl_items,resultlabels),  {"datas" : defCfg.items, "perCol" : calWidth, "col": defCfg.col});
				html += mUtil.parseTemplate(replaceTempi18nLabel(tmpl_categories,resultlabels),  {"datas" : defCfg.categories, "perCol" : calWidth, "col": defCfg.col});
				$(".wev-page-portal", $page).html(html);
				
				loadBadge();
				
				$(".portal-edit-btn", $page).html(resultlabels['384116']).click(function(){
					$(".portal-items-small").hide();
					$(".portal-items-big").show();
					$(".wev-page-portal", $page).addClass("portal-editing");
					$(".portal-items-select").find("a").each(function(){
						var a = $(this);
						hrefMap[a.attr("id")] = a.attr("href");
						a.attr("href", "javascript:void(0);");
					});
				});
				
				$(".portal-done-btn", $page).html(resultlabels['386812']).click(function(){
					$(".portal-items-big").hide();
					$(".portal-items-small").show();
					$(".wev-page-portal", $page).removeClass("portal-editing");
					$(".portal-items-select").find("a").each(function(){
						var a = $(this);
						a.attr("href", hrefMap[a.attr("id")]);
					});
					
					modifyCfg();
				});
				
				var fillinNum = $(".sort-hidden", $(".portal-items-big")).size();
				while(fillinNum < defCfg.col){
					$(".portal-square-inner", $(".portal-items-big")).append("<li class=\"sort-hidden\" style=\"width: calc("+calWidth+"% / "+defCfg.col+");\"></li>");
					fillinNum++;
				}
				
				//拖拽排序、删除
				require(["sortable"], function (sortable) {
					sortable.create($(".portal-square-inner", $(".portal-items-big", $page))[0], {
						//handle: ".sort-handle",
						draggable: ".sort-handle",
						animation: 50,
						filter: ".portal-item-del",
						onFilter: function (evt) {
							var item = evt.item,
								ctrl = evt.target;
							if (sortable.utils.is(ctrl, ".portal-item-del")) { 
								var itemid = $(item).find("a").attr("id").substring(2);
								$(".portal-item-edit", $("#c_"+itemid)).removeClass("portal-item-del").addClass("portal-item-add");

								item.parentNode.removeChild(item);
								$("#s_"+itemid).remove();
							}
						}
					});
					
				});
				
				$(".portal-item-edit", $(".portal-items-select")).on("click",function(){
					var that = $(this);
					if(that.hasClass("portal-item-del")) return;
					
					that.removeClass("portal-item-add").addClass("portal-item-del");
					var $item = that.parent(); 
					var itemid = $item.attr("id").substring(2);
					$item.attr("id", "b_"+itemid);
					
					var $lastLi = $(".sort-handle", $(".portal-items-big")).last();
					if($lastLi.length > 0){
						$lastLi.after($item.parent().clone(true).addClass("sort-handle"));
					}else{
						$(".portal-square-inner", $(".portal-items-big")).prepend($item.parent().clone(true).addClass("sort-handle"));
					}
					$item.attr("id", "c_"+itemid);

					var imgsrc = $item.find("img").attr("src");
					var imgClass = $item.find("img").attr("class");
					
					$(".portal-items-small>span:nth-child(2)").append("<img id=\"s_"+itemid+"\" class=\""+imgClass+"\" src=\""+imgsrc+"\">");
				});
				
			});
		}
	};
});