debug = function(message) { if ($.fm.isDebug) { console.log(message); } }; Root = { popup:{ zindex: 1800, zno: 0, }, popupParames: {}, }; RootRole = { isshow_jl: false,//商务经理 isshow_db: false,//商务代表 isshow_cw: false,//财务 isshow_fh: false,//发货人员 }; pageTo = function(frame, url) { if (!url) { url = frame; frame = null; } if (!url) { return; } //1. root var exists = url && url.lastIndexOf("root/"); if (exists >= 0) { var current = window.top.location.pathname; var pos = current.indexOf("root/"); if (pos >= 0) { url = current.substring(0, pos) + url; } } //2. random if (url.indexOf("?") > 0) { url = url + "&" + new Date().getTime(); } else { url = url + "?" + new Date().getTime(); } //3. set if (frame) { frame.attr("src", url); } else { window.top.location.href = url; } }; getEl = function(element) { var type = typeof element; if (type == "string") { if ("#" != element.substring(0, 1)) { element = "#" + element; } var el = $(element); return el; } return element; }; function prefixedEventListener(element, type, callback) { var pfx = ["webkit", "moz", "MS", "o", ""]; for (var p = 0; p < pfx.length; p++) { if (!pfx[p]) type = type.toLowerCase(); element.addEventListener(pfx[p]+type, callback, false); } }; function getSessionData(name) { var data = sessionStorage.getItem(name); return data ? JSON.parse(data) : null; }; function setSessionData(name, value) { sessionStorage.setItem(name, JSON.stringify(value)); }; function getLocalData(name) { var data = localStorage.getItem(name); return data ? JSON.parse(data) : null; }; function setLocalData(name, value) { localStorage.setItem(name, JSON.stringify(value)); }; function getURLParams(url) { var result = {}; if (!url) { return result; }; var begin = url.indexOf("?") + 1; var end = url.length; var value = url.substring(begin, end); var params = value.split("&"); for (var i = 0; i < params.length; i++) { var param = params[i]; var idx = param.indexOf("="); if (idx >= 0) { result[param.substring(0, idx)] = param.substring(idx + 1, param.length); } else { result[param] = null; } } result = $.fm.decode(result); return result; }; function toMoney(num) { if (num) { if (isNaN(num)) { alert('金额中含有不能识别的字符'); return; } num = typeof num == 'string' ? parseFloat(num) : num // 判断是否是字符串如果是字符串转成数字 num = num.toFixed(2); // 保留两位 console.log(num) num = parseFloat(num); // 转成数字 num = num.toLocaleString(); // 转成金额显示模式 // 判断是否有小数 if (num.indexOf('.') === -1) { num = '¥' + num + '.00'; } else { console.log(num.split('.')[1].length) // num = num.split('.')[1].length < 2 ? '¥' + num + '0' : '¥' + num; } return num; // 返回的是字符串23,245.12保留2位小数 } else { return num = null; } }; function clone(obj) { var result, oClass = isClass(obj); //确定result的类型 if (oClass === "Object") { result = {}; } else if (oClass === "Array") { result = []; } else { return obj; } for (var key in obj) { var copy = obj[key]; if (isClass(copy) == "Object") { result[key] = arguments.callee(copy); //递归调用 } else if (isClass(copy) == "Array") { result[key] = arguments.callee(copy); } else { result[key] = obj[key]; } } return result; }; function dateFormat(date, fmt) { if (null == date || undefined == date) return ''; var o = { "M+": date.getMonth() + 1, //月份 "d+": date.getDate(), //日 "h+": date.getHours(), //小时 "m+": date.getMinutes(), //分 "s+": date.getSeconds(), //秒 "S": date.getMilliseconds() //毫秒 }; if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); return fmt; }; (function ($) { $.fm = { isDebug: false, isClientMode: true, decode: function (obj) { if (!obj) { return null; } if ($.isArray(obj)) { return this.decodeArray(obj); } else if (typeof obj == "object") { return this.decodeObject(obj); } else if (typeof obj == "string") { try { return decodeURIComponent(obj); } catch (e) { return obj; } } else { return obj; } }, decodeArray: function(data) { for (var i = 0; i < data.length; i++) { if (typeof data[i] == 'string') { data[i] = this.decode(data[i]); } else { data[i] = this.decodeObject(data[i]); } } return data; }, decodeObject: function(data) { for (var prop in data) { if (data[prop]) { data[prop] = this.decode(data[prop]); } } return data; }, encode: function(obj) { if ($.isArray(obj)) { return this.encodeArray(obj); } else if (typeof obj == "object") { return this.encodeObject(obj); } else if (typeof obj == "string") { return encodeURI(obj); } else if (typeof obj == "boolean") { return String(obj); } else if (typeof obj == "number") { return String(obj); } else if (typeof obj === "function") { return ""; } }, encodeArray: function(o) { var a = ["[", ""], len = o.length, i; for (i = 0; i < len; i += 1) { a.push(this.encode(o[i]), ','); } a[a.length - 1] = ']'; return a.join(""); }, encodeObject: function(o) { if (!o) { return "null"; } if (o.length) { return "el"; } var a = ["{", ""]; for (var i in o) { if (i == 'parent') { continue; } a.push(this.encode(i), ":", this.encode(o[i]), ','); } a[a.length - 1] = '}'; return a.join(""); }, objectToURI: function(object) { if (!object) { return null; } if (typeof object == "String") { return encodeURI(object); } var param = null; for (var prop in object) { if (object[prop]) { if (param) { param = param + "&" + prop + "=" + encodeURI(object[prop]); } else { param = prop + "=" + encodeURI(object[prop]); } } } return param; }, formatBarcode: function(value) { if (!value) { return value; } var pos = value.indexOf(","); if (pos > 0) { value = value.subString(pos + 1); } return value; }, S4 : function(){// 验证码 return (((1 + Math.random()) * 0x10000) | 0).toString(16).subString(1); }, guid : function(){ return (this.S4() + this.S4() + this.S4() + this.S4() + this.S4() + this.S4() + this.S4() + this.S4()); }, setNotValueNull: function(target, name, value) { if (value === undefined) { return; } target[name] = value; }, setNotTargetNull: function(target, name, value) { if (value === undefined) { return; } if (target[name] === undefined) { target[name] = value; } }, combine: function(obj1, obj2, override) { if (!obj1) { return obj2; } if (!obj2) { return obj1; } for (var name in obj2) { if (override || obj1[name] === undefined) { obj1[name] = obj2[name]; } } return obj1; }, setVisible: function(el, visible) { if (visible) { el.show(); } else { el.hide(); } } }; Array.select = function(array, selecFun) { var result = []; if (!array || !array.length) { return result; } for (var i = 0; i < array.length; i++) { var line = array[i]; if (selecFun(line)) { result.push(line); } } return result; }; Array.getString = function(array, propName, spliter, defaultValue) { var result = null; if (!array || !array.length) { return defaultValue; } for (var i = 0; i < array.length; i++) { var line = array[i]; result = result ? result + spliter + line[propName] : line[propName]; } return result; }; Object.init = function(config) { if (prototyping) { return; } this.config = config; if (config) { var name = null, value; for (name in config) { value = config[name]; if ($.isFunction(value)) { this[name] = value; } } } }; Object.subClass = function(properties) { if (!properties) { return; } var clazz = properties.init; delete properties.init; if (!clazz) { clazz = new Function(); } prototyping = true; try { var prototype = new this(); } finally { prototyping = false; } $.extend(prototype, properties); clazz.prototype = prototype; clazz.subClass = arguments.callee; return clazz; }; Control = $.fm.Control = Object.subClass({ init: function(config) { if (prototyping) { return; } //0. default settings this.style = config.style = config.style || "A"; this.visible = config.visible; this.sequence = 0; //1. call object create Object.init.call(this, config); //2. create container this.visible = (this.visible === undefined) || this.visible; //3. try create content if (this.create) { this.create(config); return; } //4. create container this.container = this.createContainer(this.visible); //5. append to parent if (config.element == "body") { this.parent = $("body"); } else { this.parent = config.element ? getEl(config.element) : $("body"); } this.parent.append(this.container); //6. create items if (this.createContent) { if (this.beforeCreateContent) { this.beforeCreateContent(config); } this.createContent(config); } else if (this.items) { this.createItems(this, this.container, config.items, 1); } }, createContainer: function(visible) { var config = this.config; var template = this.getContainerTemplate(config); var el = $(template); if (config.css) { this.addClass(el, config.css.container); } var container = $("#body", el); if (!container.length) { container = el; } if (this.onCreateContainer) { this.onCreateContainer(container, config); } if (!visible) { container.hide(); } return container; }, getContainerTemplate: function(config) { return config.element; }, setText: function(el, text, css) { if (!el || !el.length) { return; } el.html(text); this.addClass(el, css); }, setImage: function(el, imageURL, imageCss) { if (!el || !el.length) { return; } if (imageURL) { var element = el.get(0); var node = element.nodeName; if (node == "IMG") { el.attr("src", imageURL); } else { el.css({ "background-image": "url(" + imageURL + ")" }); } return; } this.addClass(el, imageCss); }, setIcon: function(el, iconClass, iconCss) { if (!el || !el.length) { return; } this.addClass(el, iconClass); this.addClass(el, iconCss); }, addClass: function(el, css) { if (css) { el.addClass(css); } }, removeClass: function(el, css) { if (css) { el.removeClass(css); } }, setVisible: function(visible) { if (visible) { this.container.show(); } else { this.container.hide(); } }, replaceParameter: function(obj) { for (var name in obj) { var value = obj[name]; if (typeof value == "string") { obj[name] = value.replace("@{style}", this.style); } } }, getTemplate: function(ctrlName, segmentName) { var name = ctrlName + "_Template" + this.config.style; var template = window[name]; if (!segmentName) { return template.join(""); } return template[segmentName].join(""); }, createSequenceValue: function(ctrlName) { this.sequence = this.sequence + 1; return ctrlName + sequence; }, averageTo: function(total, size) { if (size <= 0) { return null; } var result = new Array(size); var max = size - 1; var value = Math.floor(total / size); var sum = 0; for (var i = 0; i < max; i++) { result[i] = value; sum = sum + value; } result[max] = total - sum; return result; } }); Control.subClass = function(properties) { if (!properties.items) { return Object.subClass.call(this, properties); } if (!properties.createItem) { properties.createItem = function(container, option, level) { }; } if (!properties.attachItem) { properties.attachItem = function(container, item) { container.append(item); }; } if (!properties.doAddItem) { properties.doAddItem = function(parentObject, container, option, level) { var item = this.createItem(container, option, level); item.option = option; this.attachItem(container, item); parentObject.items.push(item); return item; }; } if (!properties.deleteItem) { properties.deleteItem = function(item) { item.header.remove(); item.body.remove(); }; } if (!properties.createItems) { properties.createItems = function(parentObject, container, itemOptions, level) { parentObject.items = []; if (!itemOptions) { return; } if (this.beforeCreateItems) { this.beforeCreateItems(this.config); } for (var i = 0; i < itemOptions.length; i++) { var option = itemOptions[i]; this.doAddItem(parentObject, container, option, level); }; }; } properties.getItemBy = function(name, value) { for (var i = 0; i < this.items.length; i++) { var item = this.items[i]; if (item[name] == value) { return item; } } return null; }; return Object.subClass.call(this, properties); }; $.fm.RootParentClass = Object.subClass({ init: function() { this.controls = []; this.win = $(window); this.document = $(document); this._width = this.win.width(); this._height = this.win.height(); }, width: function() { return this._width; }, height: function() { return this._height; }, append: function(element) { if (!this.body) { this.body = $('body'); var me = this; this.document.click(function() { me.notifyAll(); }); } this.body.append(element); }, register: function(control) { this.controls.push(control); }, notifyAll: function() { for (var i = 0 ; i < this.controls.length; i++) { this.notifyOne(this.controls[i]); } }, notifyOne: function(control) { if (control && control.onNotify) { control.onNotify.call(control, "rootClick"); } } }); RootParent = new $.fm.RootParentClass(); var ServerClass = Object.subClass({ dataNames: ["line", "dataset", "entityset", "rows", "tree", "user"], excludeNames: {"success": true, "error": true}, sysParams: { timeoutPage: "root/page/system/index.html" }, ajaxRequest: function(url, param, callback, onSuccess, onFail) { var me = this; if (!url) { return; } //1. handle url var to = url.lastIndexOf("root/"); if (to) { var from = -1, temp = url.length; while (temp >= 0) { temp = url.lastIndexOf("root/", temp - 1); if (temp >= 0) { from = temp; } } if (from >= 0) { url = url.substring(0, from) + url.substring(to); } } //2. handle param if (param) { if ($.isFunction(param)) { callback = param; param = null; } else { if (typeof param == "object") { param = $.fm.objectToURI(param); } if (url.indexOf("?") > 0) { url = url + "&" + param; } else { url = url + "?" + param; } } } if ($.fm.isClientMode) { var result = $.fm.loadClientData(url); onSuccess(callback, result); return; } //3. request try { $.ajax(url, { dataType: 'json', success: function(result) { if (result) { if ("timeout" == result.errorcode) { window.top.location.href = me.sysParams.timeoutPage; } else if (!result.success) { if (onFail) { onFail(callback, result); } else { var error = result.errorcode + ": " + $.fm.decode(result.errormessage); try { if (console.log) { console.log(error); } } catch (e) { } alert(error); } } } onSuccess(callback, result); }, error: function(d1, error, message) { alert(error); try { if (console.log) { console.log(error); } } catch (e) { } } }); } catch(e) { } }, getResultData: function(result) { if (!result) return; // 1. for (var i = 0; i < this.dataNames.length; i++) { var name = this.dataNames[i]; if (result[name]) { var data = result[name]; var page = result['page']; return [$.fm.decode(data), $.fm.decode(page), "byPage"]; } } // 2. var names = []; for (var prop in result) { if (!this.excludeNames[prop]) { names.push(prop); } } if (names.length == 1) { var data = result[names[0]]; return $.fm.decode(data); } }, getData: function(url, param, callback) { if (!url) {return;} var me = this; if ($.isFunction(param)) { callback = param; param = null; } param = $.fm.objectToURI(param); me.ajaxRequest(url, param, callback, function(doCallback, result) { if (doCallback) { var data = me.getResultData(result) || result; if($.isArray(data) && data[2] == "byPage") { doCallback(data[0], data[1]); return; } doCallback(data); } }); }, call: function(url, param, callback) { if (!url) {return;} var me = this; var afterRequest = function(doCallback, result) { if (doCallback) { try { result = $.fm.decode(result); } finally { doCallback(result); } } }; me.ajaxRequest(url, param, callback, afterRequest, afterRequest); }, upload: function(url, file, onProgress, onSuccess, onError) { var formdata = new FormData(); formdata.append("fileList", file); try { $.ajax({ cache: true, type: "POST", url: serverAddress + url, data: formdata, dataType: "json", processData: false, contentType: false, xhr: function(){ var xhr = $.ajaxSettings.xhr(); if (onProgress && xhr.upload) { xhr.upload.addEventListener("progress" , onProgress, false); return xhr; } }, error: function(request) { if (onError) { onError(); } }, success: function(data) { data = $.fm.decode(data); if (onSuccess) { onSuccess(data); } } }); } catch(e) { if (onError) { onError(); } } } }); Server = $.fm.Server = new ServerClass(); $.fm.loadClientData = function(url) { var result = DataCache[url]; if (!result) { result = { success: true, data: null } var name, prop; for (name in DataMap) { if (url.indexOf(name) >= 0) { prop = DataMap[name]; result.data = prop; break; } } } return result; }; DataMap = $.fm.DataMap = {}; DataCache = $.fm.DataCache = {}; function initDataMap() { var name, prop, subname, subprob; for (name in dataRoot) { prop = dataRoot[name]; if (!prop) { continue; } for (subname in prop) { subprob = prop[subname]; DataMap[subname] = subprob; } } }; $.fm.initDataMap = initDataMap; initDataMap(); function toClientURL(url) { if (!url) { return; } var pos = url.indexOf("root/"); if (pos >= 0) { url = "../../" + url.substr(pos + 5); } return url; } $.fm.toClientURL = toClientURL; DataObject = $.fm.DataObject = Object.subClass({ init: function(data) { this.record = data || {}; this.old = {}; this.changed = false; this.valueFunc = {}; Object.call(this, {}); }, initValueFunc: function(field, setFunc, getFunc) { this.valueFunc[field] = {"set": setFunc, "get": getFunc}; }, setValue: function(field, rawValue) { var valueFunc = this.valueFunc[field]; var value = rawValue; if (valueFunc && valueFunc.set) { value = valueFunc.set(rawValue); } this.record[field] = value; if (this.old[field] != this.record[field]) { this.changed = true; } if (this.onDataChange) { this.onDataChange(); } }, getValue: function(field) { var valueFunc = this.setValueFunc[field]; var value = this.record[field]; if (valueFunc && valueFunc.get) { value = valueFunc.get(value); } return value; }, isEmptyValue: function(field) { return !this.record[field]; }, clearValue: function() { this.record = {}; this.old = {}; this.changed = false; }, isValueChanged: function(field) { return this.old[field] != this.record[field]; }, isChanged: function() { for (var field in record) { if (old[field] != record[field]) { return true; } } return false; }, commit: function() { this.old = $.extend({}, this.record); this.changed = false; }, rollback: function() { if (!this.changed) { return; } this.record = $.extend(this.record, this.old); this.changed = false; } }); Dialog = {}; $.fm.DialogClass = Control.subClass({ template: [ '