service.viewimg_wev8.js 5.32 KB
define("mService/viewimg", ["mUtil", "photoswipe", "photoswipe_ui", "i18n"], function(mUtil, PhotoSwipe, PhotoSwipeUI_Default, i18n) {
	
	return {
		mounted: function($page, cfg){
			var defCfg = {
		    	items : [],
		    	active : 0
		    };
			$.extend(defCfg, cfg);
			
			var base64Items = [];
			var webSrcs = [];
			var defItems = defCfg.items;
			defItems.forEach(function(theSrc, index){
				if(theSrc.indexOf("data:image") == 0){
					var img = new Image();
					img.src = theSrc;
					var itmCfg = {
						"src": theSrc,
						"w": img.naturalWidth,
						"h": img.naturalHeight
					};
					base64Items.push(itmCfg);
				}else{
					webSrcs.push({src: theSrc, index: index});
				}
			});
			
			if(webSrcs.length > 0){
				var url = mUtil.jionActionUrl("com.api.mobilemode.web.mobile.service.MobileCommonAction", "action=getImgMeta&items="+encodeURIComponent(JSON.stringify(webSrcs)));
	        	mUtil.getJSON(url, function(result){
	        	    var webItems = result.datas;
	        	    var allItems = webItems.concat(base64Items);
	        	    var pswpItems = [];
	        	    defItems.forEach(function(theSrc, defIndex){
                        allItems.every(function(theItem, index){
                            if(theItem.src == theSrc || theItem.index == defIndex){
                                pswpItems.push(theItem);
                                allItems.splice(index, 1);
                                return false;
                            }
                            return true;
                        });
	        	    });
	        	    initPswp(pswpItems);
	        	});
			}else{
				initPswp(base64Items);
			}
        	
        	function initPswp(pswpItems){
        		if(pswpItems.length == 0 ){
        			Mobile_NS.alert(i18n.IMAGE_LOADING_ERROER);
        		}
        		$(".wev-loading", $page).hide(); 
        		var options = {
        	        history : false,
        	        focus : false,
        	        showAnimationDuration : 0,
        	        hideAnimationDuration : 0,
        	        captionAndToolbarOpacity : 0.1,
        			maxSpreadZoom: 4,
        			errorMsg:"<div class='pswp__error-msg'>"+ i18n.IMAGE_LOADING_ERROER +"</div>",
        			escKey: false,
        	        index : defCfg.active, //显示图片位置
        	        loop : false,
					closeOnVerticalDrag: false,
					clickToCloseNonZoomable: false,
					closeOnScroll:false,
					closeElClasses: [],
					pinchToClose:false,
					onClose: function(){
        	        	mUtil.back();
        	        }
        	    };
        		var pswpElement = $(".pswp", $page)[0];
        	    var pswp = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, pswpItems, options);
        	    pswp.init();

                var pressImgForSave = function(){
                    if(!mUtil.runtime.isEmobile() && !mUtil.runtime.isEmobile7()){
                        return;
                    }
                    require(["hammer"], function(Hammer){
                    	$(".pswp__img", $page).each(function(){

                    		var $img = $(this);
                    		var mc = new Hammer($img[0], {
                    			recognizers: [
                    	      		[Hammer.Press,{ time : 500 }]
                    	      	]
                    		});
                    		mc.on('press', function(ev) {
                    			var imgSrc = $(ev.target).attr("src");
                    			if(imgSrc == null || imgSrc == ""){
                    				return;
                    			}
                    			Mobile_NS.footerMenu([
                    	      		{
                    	      			text : "<div style='text-align: center;margin-left: -18px;font-size: 20px;color: #017afd;'>"+i18n.SAVE_IMAGE+"</div>",
                    					callback : function(){
                    						if(mUtil.checkEmpJsApi("downloadImage")){
                    							mUtil.invokeEmApi("downloadImage", {
                    								url: imgSrc,
                    								isShowProgressTips: 1,
                    								success: function(){
                    									Mobile_NS.msg(i18n.SAVE_SUCCESS);//保存成功
                    								}
                    							});
                    						}else{
                    							if(imgSrc.indexOf("data:image") == 0){
                    								try {
                    									var baseArr=imgSrc.split(",");
                    									var basepic64=baseArr[1];
                    									if(mUtil.checkEmpJsApi("saveBase64Image")) {
	                    									mUtil.invokeEmApi("saveBase64Image", {
	                    										base64Img: basepic64,
	                    										success: function(){
	                    											Mobile_NS.msg(i18n.SAVE_SUCCESS);//保存成功
	                    										}
	                    									});
                    									}
                    								} catch (e){
                    									console.log(e.message);
                    								}
                    							} else {
                    								location = "emobile:saveImage:"+imgSrc+":111";
                    							}
                    						}
                    	      			}
                    	      		}
                    	      	]);
                    		});
                    	});
                    });
                }
        	    pswp.listen('imageLoadComplete', pressImgForSave);
        	}
		}
	};
});