| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519 |
- /** ***** BEGIN LICENSE BLOCK *****
- * |------------------------------------------------------------------------------|
- * | O2OA 活力办公 创意无限 o2.core.js |
- * |------------------------------------------------------------------------------|
- * | Distributed under the AGPL license: |
- * |------------------------------------------------------------------------------|
- * | Copyright © 2018, o2oa.net, o2server.io O2 Team |
- * | All rights reserved. |
- * |------------------------------------------------------------------------------|
- *
- * This file is part of O2OA.
- *
- * O2OA is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * O2OA is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Foobar. If not, see <https://www.gnu.org/licenses/>.
- *
- * ***** END LICENSE BLOCK ******/
- (function (){
- var _Class = {
- create: function(options) {
- var newClass = function() {
- this.initialize.apply(this, arguments);
- };
- _copyPrototype(newClass, options);
- return newClass;
- }
- };
- var _copyPrototype = function (currentNS, props){
- if (!props){return currentNS;}
- if (!currentNS){return currentNS;}
- if ((typeof currentNS).toLowerCase()==="object"){
- for (var prop in props){
- currentNS[prop] = props[prop];
- }
- }
- if ((typeof currentNS).toLowerCase()==="function"){
- for (var propfun in props){
- currentNS.prototype[propfun] = props[propfun];
- }
- }
- return currentNS;
- };
- var _loaded = {};
- var _requireJs = function(url, callback, async, compression, module){
- var key = encodeURIComponent(url);
- if (_loaded[key]){o2.runCallback(callback, "success", [module]); return "";}
- var jsPath = (compression || !this.o2.session.isDebugger) ? url.replace(/\.js/, ".min.js") : url;
- jsPath = (jsPath.indexOf("?")!==-1) ? jsPath+"&v="+this.o2.version.v : jsPath+"?v="+this.o2.version.v;
- var xhr = new Request({
- url: o2.filterUrl(jsPath), async: async, method: "get",
- onSuccess: function(){
- //try{
- _loaded[key] = true;
- o2.runCallback(callback, "success", [module]);
- //}catch (e){
- // o2.runCallback(callback, "failure", [e]);
- //}
- },
- onFailure: function(r){
- o2.runCallback(callback, "failure", [r]);
- }
- });
- xhr.send();
- };
- var _requireSingle = function(module, callback, async, compression){
- if (o2.typeOf(module)==="array"){
- _requireAppSingle(module, callback, async, compression);
- }else{
- module = module.replace("MWF.", "o2.");
- var levels = module.split(".");
- if (levels[levels.length-1]==="*") levels[levels.length-1] = "package";
- levels.shift();
- var o = o2;
- var i = 0;
- while (o && i<levels.length){
- o = o[levels[i]];
- i++
- }
- if (!o){
- var jsPath = this.o2.session.path;
- jsPath +="/"+levels.join("/")+".js";
- var loadAsync = (async!==false);
- _requireJs(jsPath, callback, loadAsync, compression, module);
- }else{
- o2.runCallback(callback, "success", [module]);
- }
- }
- };
- var _requireSequence = function(fun, module, thisLoaded, thisErrorLoaded, callback, async, compression){
- var m = module.shift();
- fun(m, {
- "onSuccess": function(m){
- thisLoaded.push(m);
- o2.runCallback(callback, "every", [m]);
- if (module.length){
- _requireSequence(fun, module, thisLoaded, thisErrorLoaded, callback, async, compression);
- }else{
- if (thisErrorLoaded.length){
- o2.runCallback(callback, "failure", [thisLoaded, thisErrorLoaded]);
- }else{
- o2.runCallback(callback, "success", [thisLoaded, thisErrorLoaded]);
- }
- }
- },
- "onFailure": function(){
- thisErrorLoaded.push(module[i]);
- o2.runCallback(callback, "failure", [thisLoaded, thisErrorLoaded]);
- }
- }, async, compression);
- };
- var _requireDisarray = function(fun, module, thisLoaded, thisErrorLoaded, callback, async, compression){
- for (var i=0; i<module.length; i++){
- fun(module[i], {
- "onSuccess": function(m){
- thisLoaded.push(m);
- o2.runCallback(callback, "every", [m]);
- if ((thisLoaded.length+thisErrorLoaded.length)===module.length){
- if (thisErrorLoaded.length){
- o2.runCallback(callback, "failure", [thisLoaded, thisErrorLoaded]);
- }else{
- o2.runCallback(callback, "success", [thisLoaded, thisErrorLoaded]);
- }
- }
- },
- "onFailure": function(){
- thisErrorLoaded.push(module[i]);
- o2.runCallback(callback, "failure", [thisLoaded, thisErrorLoaded]);
- }
- }, async, compression);
- }
- };
- var _require = function(module, callback, async, sequence, compression){
- var type = typeOf(module);
- if (type==="array"){
- var sql = !!sequence;
- var thisLoaded = [];
- var thisErrorLoaded = [];
- if (sql){
- _requireSequence(_requireSingle, module, thisLoaded, thisErrorLoaded, callback, async, compression);
- }else{
- _requireDisarray(_requireSingle, module, thisLoaded, thisErrorLoaded, callback, async, compression);
- }
- }
- if (type==="string"){
- _requireSingle(module, callback, async, compression);
- }
- };
- var _requireAppSingle = function(modules, callback, async, compression){
- var module = modules[0];
- var clazz = modules[1];
- var levels = module.split(".");
- var o = o2.xApplication;
- var i = 0;
- while (o && i<levels.length){
- o = o[levels[i]];
- i++
- }
- if (o) o = o[clazz || "Main"];
- if (!o){
- //levels.shift();
- var root = "x_component_"+levels.join("_");
- var clazzName = clazz || "Main";
- var path = "../"+root+"/"+clazzName.replace(/\./g, "/")+".js";
- var loadAsync = (async!==false);
- _requireJs(path, callback, loadAsync, compression);
- }else{
- o2.runCallback(callback, "success");
- }
- };
- var _requireApp = function(module, clazz, callback, async, sequence, compression){
- var type = typeOf(module);
- if (type==="array"){
- var sql = !!sequence;
- var thisLoaded = [];
- var thisErrorLoaded = [];
- if (sql){
- _requireSequence(_requireAppSingle, module, thisLoaded, thisErrorLoaded, callback, async, compression);
- }else{
- _requireDisarray(_requireAppSingle, module, thisLoaded, thisErrorLoaded, callback, async, compression);
- }
- }
- if (type==="string"){
- var modules = [module, clazz];
- _requireAppSingle(modules, callback, async, compression);
- }
- };
- JSON = window.JSON || {};
- var _json = JSON;
- _json.get = function(url, callback, async, nocache){
- var loadAsync = (async !== false);
- var noJsonCache = (nocache === true);
- url = (url.indexOf("?")!==-1) ? url+"&v="+o2.version.v : url+"?v="+o2.version.v;
- var json = null;
- var res = new Request.JSON({
- url: o2.filterUrl(url),
- secure: false,
- method: "get",
- noCache: noJsonCache,
- async: loadAsync,
- withCredentials: true,
- onSuccess: function(responseJSON, responseText){
- json = responseJSON;
- if (typeOf(callback).toLowerCase() === 'function'){
- callback(responseJSON, responseText);
- }else{
- o2.runCallback(callback, "success", [responseJSON, responseText]);
- }
- }.bind(this),
- onFailure: function(xhr){
- o2.runCallback(callback, "requestFailure", [xhr]);
- }.bind(this),
- onError: function(text, error){
- o2.runCallback(callback, "error", [text, error]);
- }.bind(this)
- });
- res.send();
- return json;
- };
- _json.getJsonp = function(url, callback, async, callbackKey){
- var loadAsync = (async !== false);
- var callbackKeyWord = callbackKey || "callback";
- url = (url.indexOf("?")!==-1) ? url+"&v="+o2.version.v : url+"?v="+o2.version.v;
- var res = new Request.JSONP({
- url: o2.filterUrl(url),
- secure: false,
- method: "get",
- noCache: true,
- async: loadAsync,
- callbackKey: callbackKeyWord,
- onSuccess: function(responseJSON, responseText){
- o2.runCallback(callback, "success",[responseJSON, responseText]);
- }.bind(this),
- onFailure: function(xhr){
- o2.runCallback(callback, "requestFailure",[xhr]);
- }.bind(this),
- onError: function(text, error){
- o2.runCallback(callback, "error",[text, error]);
- }.bind(this)
- });
- res.send();
- };
- var _loadLP = function(name){
- var jsPath = o2.session.path;
- jsPath = jsPath+"/lp/"+name+".js";
- var r = new Request({
- url: o2.filterUrl(jsPath),
- async: false,
- method: "get",
- onSuccess: function(responseText){
- try{
- Browser.exec(responseText);
- }catch (e){}
- },
- onFailure: function(xhr){
- throw "loadLP Error: "+xhr.responseText;
- }
- });
- r.send();
- };
- var _cacheUrls = [
- /jaxrs\/form\/workorworkcompleted\/.+/ig,
- // /jaxrs\/script/ig,
- /jaxrs\/script\/.+\/app\/.+\/imported/ig,
- /jaxrs\/script\/portal\/.+\/name\/.+\/imported/ig,
- /jaxrs\/script\/[\S\s]+\/application\/[\S\s]+\/imported/ig,
- /jaxrs\/page\/.+\/portal\/.+/ig
- // /jaxrs\/authentication/ig
- // /jaxrs\/statement\/.*\/execute\/page\/.*\/size\/.*/ig
- ];
- _restful = function(method, address, data, callback, async, withCredentials, cache){
- var loadAsync = (async !== false);
- var credentials = (withCredentials !== false);
- address = (address.indexOf("?")!==-1) ? address+"&v="+o2.version.v : address+"?v="+o2.version.v;
- //var noCache = cache===false;
- var noCache = !cache;
- //if (Browser.name == "ie")
- if (_cacheUrls.length){
- for (var i=0; i<_cacheUrls.length; i++){
- _cacheUrls[i].lastIndex = 0;
- if (_cacheUrls[i].test(address)){
- noCache = false;
- break;
- }
- }
- }
- //var noCache = false;
- var res = new Request.JSON({
- url: o2.filterUrl(address),
- secure: false,
- method: method,
- emulation: false,
- noCache: noCache,
- async: loadAsync,
- withCredentials: credentials,
- onSuccess: function(responseJSON, responseText){
- // var xToken = this.getHeader("authorization");
- // if (!xToken) xToken = this.getHeader("x-token");
- var xToken = this.getHeader("x-token");
- if (xToken){
- if (window.layout){
- if (!layout.session) layout.session = {};
- layout.session.token = xToken;
- }
- }
- o2.runCallback(callback, "success", [responseJSON]);
- },
- onFailure: function(xhr){
- o2.runCallback(callback, "requestFailure", [xhr]);
- }.bind(this),
- onError: function(text, error){
- o2.runCallback(callback, "error", [text, error]);
- }.bind(this)
- });
- res.setHeader("Content-Type", "application/json; charset=utf-8");
- res.setHeader("Accept", "text/html,application/json,*/*");
- if (window.layout) {
- if (layout["debugger"]){
- res.setHeader("x-debugger", "true");
- }
- if (layout.session && layout.session.user){
- if (layout.session.user.token) {
- res.setHeader("x-token", layout.session.user.token);
- res.setHeader("authorization", layout.session.user.token);
- }
- }
- }
- //Content-Type application/x-www-form-urlencoded; charset=utf-8
- res.send(data);
- return res;
- };
- var _release = function(o){
- var type = typeOf(o);
- switch (type){
- case "object":
- for (var k in o){
- //if (o[k] && o[k].destroy) o[k].destroy();
- o[k] = null;
- }
- break;
- case "array":
- for (var i=0; i< o.length; i++){
- _release(o[i]);
- if (o[i]) o[i] = null;
- }
- break;
- }
- };
- var _defineProperties = Object.defineProperties || function (obj, properties) {
- function convertToDescriptor(desc) {
- function hasProperty(obj, prop) {
- return Object.prototype.hasOwnProperty.call(obj, prop);
- }
- function isCallable(v) {
- // NB: modify as necessary if other values than functions are callable.
- return typeof v === "function";
- }
- if (typeof desc !== "object" || desc === null)
- throw new TypeError("bad desc");
- var d = {};
- if (hasProperty(desc, "enumerable"))
- d.enumerable = !!desc.enumerable;
- if (hasProperty(desc, "configurable"))
- d.configurable = !!desc.configurable;
- if (hasProperty(desc, "value"))
- d.value = desc.value;
- if (hasProperty(desc, "writable"))
- d.writable = !!desc.writable;
- if (hasProperty(desc, "get")) {
- var g = desc.get;
- if (!isCallable(g) && typeof g !== "undefined")
- throw new TypeError("bad get");
- d.get = g;
- }
- if (hasProperty(desc, "set")) {
- var s = desc.set;
- if (!isCallable(s) && typeof s !== "undefined")
- throw new TypeError("bad set");
- d.set = s;
- }
- if (("get" in d || "set" in d) && ("value" in d || "writable" in d))
- throw new TypeError("identity-confused descriptor");
- return d;
- }
- if (typeof obj !== "object" || obj === null)
- throw new TypeError("bad obj");
- properties = Object(properties);
- var keys = Object.keys(properties);
- var descs = [];
- for (var j = 0; j < keys.length; j++)
- descs.push([keys[j], convertToDescriptor(properties[keys[j]])]);
- for (var i = 0; i < descs.length; i++){
- if (Object.defineProperty && (Browser.name=="ie" && Browser.version!=8)){
- Object.defineProperty(obj, descs[i][0], descs[i][1]);
- }else{
- if (descs[i][1].value) obj[descs[i][0]] = descs[i][1].value;
- if (descs[i][1].get) obj["get"+descs[i][0].capitalize()] = descs[i][1].get;
- if (descs[i][1].set) obj["set"+descs[i][0].capitalize()] = descs[i][1].set;
- }
- }
- return obj;
- };
- if (!Array.prototype.findIndex) {
- Object.defineProperty(Array.prototype, 'findIndex', {
- value: function(predicate) {
- if (this == null) {
- throw new TypeError('"this" is null or not defined');
- }
- var o = Object(this);
- var len = o.length >>> 0;
- if (typeof predicate !== 'function') {
- throw new TypeError('predicate must be a function');
- }
- var thisArg = arguments[1];
- var k = 0;
- while (k < len) {
- var kValue = o[k];
- if (predicate.call(thisArg, kValue, k, o)) {
- return k;
- }
- k++;
- }
- return -1;
- }
- });
- }
- if (!Array.prototype.find) {
- Object.defineProperty(Array.prototype, 'find', {
- value: function(predicate) {
- if (this == null) {
- throw new TypeError('"this" is null or not defined');
- }
- var o = Object(this);
- var len = o.length >>> 0;
- if (typeof predicate !== 'function') {
- throw new TypeError('predicate must be a function');
- }
- var thisArg = arguments[1];
- var k = 0;
- while (k < len) {
- var kValue = o[k];
- if (predicate.call(thisArg, kValue, k, o)) {
- return kValue;
- }
- k++;
- }
- return undefined;
- }
- });
- }
- var _txt = function(v){
- var t = v.replace(/\</g, "<");
- t = t.replace(/\</g, ">");
- return t;
- };
- this.o2.Class = _Class;
- this.o2.require = _require;
- this.o2.requireApp = _requireApp;
- this.o2.JSON = _json;
- this.o2.loadLP = _loadLP;
- this.o2.restful = _restful;
- this.o2.release = _release;
- this.o2.defineProperties = _defineProperties;
- this.o2.txt = _txt;
- Object.repeatArray = function(o, count){
- var arr = [];
- for (var i=0; i<count; i++){
- arr.push(o)
- }
- return arr;
- }
- Date.implement({
- "getFromServer": function(){
- var d;
- o2.Actions.get("x_program_center").echo(function(json){
- d = Date.parse(json.data.serverTime);
- }, null, false);
- return d;
- }
- });
- })();
- o2.core = true;
|