(function ($) { $.fm = { 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()); } }; Object.init = function(options) { if (prototyping) { return; } if (options) { var name = null, value; for (name in options) { value = options[name]; 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(options) { if (prototyping) { return; } Object.init.call(this, options); // 创建画布 this.createCanvas(options); }, createCanvas: function(options) { if (!this.element) { this.canvas = $("body"); return; } this.getEl(); this.canvas = this.element; }, 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; }, getEl: function() { var element = this.element; if (typeof element == "String") { if ("#" != element.subString(0, 1)) { element = "#" + element; } this.element = $(element); return; } }, }); $.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 (param) { if ($.isFunction(param)) { callback = param; param = null; } else { url = url + "?" + param; } } url = serverAddress + url; 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); } }, 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(); })(jQuery);