mobilemode.js 11.4 KB
define(['mUtil', 'pageStack', 'pageEvent', 'zepto','i18n'], function () {
	var mUtil = require("mUtil");
	var pageEvent = require("pageEvent");
	var i18n = require("i18n");
	var _page = (function () {
		var $el = null;
		var mods = null;
		var queues = {
			first: $.Deferred(),
			second: $.Deferred()
		};

		return {
			setParams: function (pageid, formdata) {
				var pageIntId = mUtil.getPageIntId(pageid);
				var params = { "pageKey": mUtil.UUID() + "_" + pageIntId };
				var pArr = (formdata || "").split("&");

				pArr.forEach(function (pStr) {
					var pIndex = pStr.indexOf("=");

					if (pIndex != -1) {
						var pName = pStr.substring(0, pIndex);
						var pValue = pStr.substring(pIndex + 1);

						params[pName] = pValue;
					}
				});

				mUtil.setPageParam(pageid, params);
			},
			setPaths: function () {
				if (mods) return;

				mods = ['mApi/popup'];

				$(document.body).find(".page").each(function (i, page) {
					var pageIntid = mUtil.getPageIntId(page.id);
					var path = mUtil.getPageScriptPath(pageIntid);

					mods.push(path);
				});
			},
			setElement: function ($page) {
				$el = $page;
			},
			hideLoading: function () {
				$el.children(".page-loading").remove();
			},
			prevcache: {
				start: function () {
					if (mods.length === 0) return;

					var _start = function (i) {
						var modName = mods.shift();

						if (!modName) return;

						mUtil.require(modName).then(function () {
							queues[i].resolve().then(function () {
								queues[i] = $.Deferred();
								_start(i);
							});
						});
					};

					queues.first = $.Deferred();
					queues.second = $.Deferred();

					_start("first");
					_start("second");
				},
				stop: function () {
					queues.first.reject();
					queues.second.reject();
				}
			},
			blurPageEle: function(pageOut, back) {
				var $page = $(pageOut).children(".page-content")[0];
				if(!$page) return;
				
				try{
					if(back){
						$("input,textarea,select", $page).not("input[type='hidden']").each(function(i, ele){
							ele.blur();
						});
						
						//明细表
						var $dtpage = $(pageOut).children(".wev-detailtable-page.in")[0];
						if($dtpage){
							$("input,textarea,select", $dtpage).not("input[type='hidden']").each(function(i, ele){
								ele.blur();
							});
						}
					}

                    require(['audioHelper'], function(audioHelper){//语音暂停播放
                        audioHelper.pauseAllAudios();
					});

					$(".wev-comp-Video", $page).each(function(i, ele){//视频控件暂停播放
						var instance =  mUtil.getInstance($(ele).parent().attr("id"), pageOut.id);
						instance && instance.pauseVideo();
					});
					
				}catch(e){
					console.log(e);
				}
			},
			clearPage: function($page){
			    var that = this;
			    var $pageHeader = $page.children(".page-header"),
                    $pageContent = $page.children(".page-content"),
                    $pageFooter = $page.children(".page-footer");
                
                $page.children().not(".page-header, .page-content, .page-footer, .page-tip").remove();
                $pageHeader.children().not("abbr[data-type]").remove();
                $pageContent.children(".page-scroller").children().not("abbr[data-type]").remove();
                $pageFooter.children().not("abbr[data-type]").remove();
                
                $pageContent.children(".tabpanel").each(function () {
                    var $tabpanel = $(this);
                    var clsName = "page-scroller";
                    if (!$tabpanel.hasClass("page-scroller")) {
                        clsName = "tabpanel show_hide out";
                        that.clearPage($tabpanel);
                    }
                    $tabpanel.attr("class", clsName).removeAttr("style");
                });

                $pageHeader.children("abbr[data-tabpanel]").remove();
                $pageFooter.children("abbr[data-tabpanel]").remove();
                
                $(
                    $pageHeader.children("abbr[data-type]").concat(
                        $pageContent.children(".page-scroller").children("abbr[data-type]"),
                        $pageFooter.children("abbr[data-type]")
                    )
                ).each(function () {
                    $(this).data("loaded", false).html("");
                });
			}
		};
	})();
	
	(function handleIosKeyboard() {
		if(mUtil.isIOS()){
			$('body').on('blur', 'input, select, textarea', function () {
				if(mUtil.isFunction(this.scrollIntoViewIfNeeded)){
					this.scrollIntoViewIfNeeded();
				}
			});
		}
	}())

	return {
		pageInit: function (page, pageOut, options) {
			//添加水印
			var $watermarkWrap = $('#wea_watermark_wrap');
			if(!$watermarkWrap.length){
			    require(['watermark'], function(WM){
                    WM.getSystemSetting();
				});
			}

			var $page = $(page);

			if ($page.hasClass("page-scroller")) return;

			var pageid = page.id;
			var pageIntId = mUtil.getPageIntId(pageid);
			var Component = require("Component");
			var _evalPageScript = function(type) {
				mUtil.getPageScript(pageIntId).then(function(o) {
					var code = o[type].toString();
					code = mUtil.replaceVariables(code);
					code = code.replace(/^function([\s\S]*)/i, '(function fun$1)();');
                    mUtil.eval(code, pageid);
				});
			}

			if (!options.isrefresh) { // 页面刷新
				mUtil.setCurrentPageId(pageid);
			}

			_page.setElement($page);
			_page.setParams(pageid, options.formdata);
			_page.setPaths();

			var components = [];
			$(
		        $page.children(".page-header").children("abbr[data-type]").concat(
	                $page.children(".page-content").children(".page-scroller").children("abbr[data-type]"),
	                $page.children(".page-footer").children("abbr[data-type]")
				)
			).each(function () {
				var $abbr = $(this);
				var priority = $abbr.data("priority");
				
				$abbr.data("loaded", false).html("");
				
				if ($abbr.data("lazyload") == true) return;

				components.push({
					el: this.id,
					type: $abbr.data("type"),
					container: $page,
					priority: priority === undefined ? Math.pow(2, 53) - 1 : priority,
					pageid: pageid
				});
			});

            _page.prevcache.stop();
            // 防止在A页面调用refresh函数刷新B页面后,再次打开B页面,B页面事件无法及时清空,造成多次绑定的问题
            mUtil.unbind(pageid);
			_evalPageScript("sourcecode");

			if (components.length > 0) {
				mUtil.getPageScript(pageIntId).then(function (o) {
					var props = o.props;

					components.forEach(function (component) {
						component.option = props[component.el];
					});

					Component.load(components, function () {
						mUtil.trigger('load', pageid); 
						_page.prevcache.start();
					}, function (component, index) {
						if (index === 0) {
							_page.hideLoading();
						}
						
						mUtil.trigger('load', pageid, component.id);
					});
				});

				Component.preload(components);
			}else{
			    _page.hideLoading();
			    mUtil.trigger('load', pageid); 
			}

			mUtil.onload(pageid, function () {
				_evalPageScript("onload");

				components.forEach(function (component) {
					var Com = Component.getInstance(component.el, component.pageid);
					Com.$emit("moveComponent");
				});

				$page.off("click.viewimg");
				$page.on("click.viewimg", "img[data-groupid]", function (e) {
                    var currSrc = $(this).attr("src");

                    if($(this).closest("a[data-ignoreviewimg]").length) return;

					if (currSrc) {
                        var imgs = [];
                        var groupid = $(this).attr("data-groupid");
                        
						$("img[data-groupid='" + groupid + "']", $page).each(function () {
                            if (!this.src) return true;
                            
                            imgs.push(this);
						});
                        var actIndex = imgs.indexOf(this);

                        require(["imgViewerHelper"], function (Viewer) {
                            Viewer(imgs, actIndex);
						});
					}
					e.stopPropagation();
					e.preventDefault();
				});

                $page.off("click.viewfile");
                $page.on("click.viewfile", ".wev-file[data-id]", function(e) {
					var $this = $(this);
                    mUtil.previewFile($this.data("id"), decodeURIComponent($this.data("name")), $this.data("authorize")||"");
					e.stopPropagation();
					e.preventDefault();
				});

                $page.off("click.textlink").on("click.textlink", ".text-haslink", function(e){
                    var $this = $(this);
                    require(["browserHelper"], function (browserHelper) {
                        browserHelper.openBrowserLink($this);
                    });
                    e.stopPropagation();
                    e.preventDefault();
                });

				options.onPageLoad && options.onPageLoad();
			});
		},
		pageChange: function (pageInto, pageOut, options, back) {

		    //触发水印hashChange事件
			require(['watermark'], function(WM){
                WM.handleHashChange();
			});

			//页面切换时input、select焦点blur处理
			pageOut && _page.blurPageEle(pageOut, back);
			
			var tabpanelPageId;
			var pageTitle = pageInto.getAttribute("data-title") || "";
			if (back) {
				//返回时包含tab页获取当前页
				var tabpanel = $(pageInto).children(".page-content").children(".tabpanel.in")[0];
				tabpanel && (tabpanelPageId = pageInto.id, pageInto = tabpanel);
			}

			var pageid = pageInto.id,
				pageOutId = pageOut && pageOut.id;

			var stack = require("pageStack");

			if (pageInto.classList.contains("page-scroller")) {
				pageid = pageid.replace("_scroller", "");
			}
			if (pageOut && pageOut.classList.contains("page-scroller")) {
				pageOutId = pageOutId.replace("_scroller", "");
			}

			var isTabChange = $(pageInto).hasClass("tabpanel") && $(pageOut).hasClass("tabpanel");
			if (!isTabChange) {//非标签栏tab之间切换
			    //切换页面时改变标题
                mUtil.checkEmpJsApi("changeTitle") && mUtil.invokeEmApi("changeTitle", {title: pageTitle});
				if (!options.replaceState) {
					!back && stack.push(pageInto) || stack.pop(tabpanelPageId || pageid);
				} else {
					stack.pop();
					stack.push(pageInto);
				}
				//返回或者replace页面时清空pageOut
				if((back || options.replaceState) && pageOut){
				    var $pageOut = $(pageOut);
				    if(!~$pageOut.attr("id").indexOf("page_service_")){
				        _page.clearPage($pageOut);
				    }
				}
			}

            //触发插件内定义的pageChange方法
            var component = require('Component'),
                intoComs = component.getPageComs(pageid),
                outComs = component.getPageComs(pageOutId);
            intoComs.forEach(function(com){
                com.$emit('pageChange', true);
            });
            outComs.forEach(function(com){
                com.$emit('pageChange', false);
            });

			pageOutId && mUtil.unload(pageOutId);
			mUtil.setCurrentPageId(pageid);
			pageEvent.trigger("change");
		},
		none: function (pageInto, pageOut) {

		},
		serviceChange: function (pageInto, pageOut, options, back) {
			var stack = require("pageStack");
			if (!options.replaceState) {
				!back && stack.push(pageInto) || stack.pop();
                //页面切换时input、select焦点blur处理
                pageOut && _page.blurPageEle(pageOut, back);
			} else {
				stack.pop();
				stack.push(pageInto);
			}
			
            var pageOutId = pageOut && pageOut.id;
            pageOutId && mUtil.unload(pageOutId);
		}
	};

});