o2.core.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /** ***** BEGIN LICENSE BLOCK *****
  2. * |------------------------------------------------------------------------------|
  3. * | O2OA 活力办公 创意无限 o2.core.js |
  4. * |------------------------------------------------------------------------------|
  5. * | Distributed under the AGPL license: |
  6. * |------------------------------------------------------------------------------|
  7. * | Copyright © 2018, o2oa.net, o2server.io O2 Team |
  8. * | All rights reserved. |
  9. * |------------------------------------------------------------------------------|
  10. *
  11. * This file is part of O2OA.
  12. *
  13. * O2OA is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as published by
  15. * the Free Software Foundation, either version 3 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * O2OA is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with Foobar. If not, see <https://www.gnu.org/licenses/>.
  25. *
  26. * ***** END LICENSE BLOCK ******/
  27. (function (){
  28. var _Class = {
  29. create: function(options) {
  30. var newClass = function() {
  31. this.initialize.apply(this, arguments);
  32. };
  33. _copyPrototype(newClass, options);
  34. return newClass;
  35. }
  36. };
  37. var _copyPrototype = function (currentNS, props){
  38. if (!props){return currentNS;}
  39. if (!currentNS){return currentNS;}
  40. if ((typeof currentNS).toLowerCase()==="object"){
  41. for (var prop in props){
  42. currentNS[prop] = props[prop];
  43. }
  44. }
  45. if ((typeof currentNS).toLowerCase()==="function"){
  46. for (var propfun in props){
  47. currentNS.prototype[propfun] = props[propfun];
  48. }
  49. }
  50. return currentNS;
  51. };
  52. var _loaded = {};
  53. var _requireJs = function(url, callback, async, compression, module){
  54. var key = encodeURIComponent(url);
  55. if (_loaded[key]){o2.runCallback(callback, "success", [module]); return "";}
  56. var jsPath = (compression || !this.o2.session.isDebugger) ? url.replace(/\.js/, ".min.js") : url;
  57. jsPath = (jsPath.indexOf("?")!==-1) ? jsPath+"&v="+this.o2.version.v : jsPath+"?v="+this.o2.version.v;
  58. var xhr = new Request({
  59. url: o2.filterUrl(jsPath), async: async, method: "get",
  60. onSuccess: function(){
  61. //try{
  62. _loaded[key] = true;
  63. o2.runCallback(callback, "success", [module]);
  64. //}catch (e){
  65. // o2.runCallback(callback, "failure", [e]);
  66. //}
  67. },
  68. onFailure: function(r){
  69. o2.runCallback(callback, "failure", [r]);
  70. }
  71. });
  72. xhr.send();
  73. };
  74. var _requireSingle = function(module, callback, async, compression){
  75. if (o2.typeOf(module)==="array"){
  76. _requireAppSingle(module, callback, async, compression);
  77. }else{
  78. module = module.replace("MWF.", "o2.");
  79. var levels = module.split(".");
  80. if (levels[levels.length-1]==="*") levels[levels.length-1] = "package";
  81. levels.shift();
  82. var jsPath = this.o2.session.path;
  83. jsPath +="/"+levels.join("/")+".js";
  84. var loadAsync = (async!==false);
  85. _requireJs(jsPath, callback, loadAsync, compression, module);
  86. }
  87. };
  88. var _requireSequence = function(fun, module, thisLoaded, thisErrorLoaded, callback, async, compression){
  89. var m = module.shift();
  90. fun(m, {
  91. "onSuccess": function(m){
  92. thisLoaded.push(m);
  93. o2.runCallback(callback, "every", [m]);
  94. if (module.length){
  95. _requireSequence(module, thisLoaded, thisErrorLoaded, callback);
  96. }else{
  97. if (thisErrorLoaded.length){
  98. o2.runCallback(callback, "failure", [thisLoaded, thisErrorLoaded]);
  99. }else{
  100. o2.runCallback(callback, "success", [thisLoaded, thisErrorLoaded]);
  101. }
  102. }
  103. },
  104. "onFailure": function(){
  105. thisErrorLoaded.push(module[i]);
  106. o2.runCallback(callback, "failure", [thisLoaded, thisErrorLoaded]);
  107. }
  108. }, async, compression);
  109. };
  110. var _requireDisarray = function(fun, module, thisLoaded, thisErrorLoaded, callback, async, compression){
  111. for (var i=0; i<module.length; i++){
  112. fun(module[i], {
  113. "onSuccess": function(m){
  114. thisLoaded.push(m);
  115. o2.runCallback(callback, "every", [m]);
  116. if ((thisLoaded.length+thisErrorLoaded.length)===module.length){
  117. if (thisErrorLoaded.length){
  118. o2.runCallback(callback, "failure", [thisLoaded, thisErrorLoaded]);
  119. }else{
  120. o2.runCallback(callback, "success", [thisLoaded, thisErrorLoaded]);
  121. }
  122. }
  123. },
  124. "onFailure": function(){
  125. thisErrorLoaded.push(module[i]);
  126. o2.runCallback(callback, "failure", [thisLoaded, thisErrorLoaded]);
  127. }
  128. }, async, compression);
  129. }
  130. };
  131. var _require = function(module, callback, async, sequence, compression){
  132. var type = typeOf(module);
  133. if (type==="array"){
  134. var sql = !!sequence;
  135. var thisLoaded = [];
  136. var thisErrorLoaded = [];
  137. if (sql){
  138. _requireSequence(_requireSingle, module, thisLoaded, thisErrorLoaded, callback, async, compression);
  139. }else{
  140. _requireDisarray(_requireSingle, module, thisLoaded, thisErrorLoaded, callback, async, compression);
  141. }
  142. }
  143. if (type==="string"){
  144. _requireSingle(module, callback, async, compression);
  145. }
  146. };
  147. var _requireAppSingle = function(modules, callback, async, compression){
  148. var module = modules[0];
  149. var clazz = modules[1];
  150. var levels = module.split(".");
  151. //levels.shift();
  152. var root = "x_component_"+levels.join("_");
  153. var clazzName = clazz || "Main";
  154. var path = "/"+root+"/"+clazzName.replace(/\./g, "/")+".js";
  155. var loadAsync = (async!==false);
  156. _requireJs(path, callback, loadAsync, compression);
  157. };
  158. var _requireApp = function(module, clazz, callback, async, sequence, compression){
  159. var type = typeOf(module);
  160. if (type==="array"){
  161. var sql = !!sequence;
  162. var thisLoaded = [];
  163. var thisErrorLoaded = [];
  164. if (sql){
  165. _requireSequence(_requireAppSingle, module, thisLoaded, thisErrorLoaded, callback, async, compression);
  166. }else{
  167. _requireDisarray(_requireAppSingle, module, thisLoaded, thisErrorLoaded, callback, async, compression);
  168. }
  169. }
  170. if (type==="string"){
  171. var modules = [module, clazz];
  172. _requireAppSingle(modules, callback, async, compression);
  173. }
  174. };
  175. JSON = window.JSON || {};
  176. var _json = JSON;
  177. _json.get = function(url, callback, async, nocache){
  178. var loadAsync = (async !== false);
  179. var noJsonCache = (nocache === true);
  180. url = (url.indexOf("?")!==-1) ? url+"&v="+o2.version.v : url+"?v="+o2.version.v;
  181. var json = null;
  182. var res = new Request.JSON({
  183. url: o2.filterUrl(url),
  184. secure: false,
  185. method: "get",
  186. noCache: noJsonCache,
  187. async: loadAsync,
  188. withCredentials: true,
  189. onSuccess: function(responseJSON, responseText){
  190. json = responseJSON;
  191. if (typeOf(callback).toLowerCase() === 'function'){
  192. callback(responseJSON, responseText);
  193. }else{
  194. o2.runCallback(callback, "success", [responseJSON, responseText]);
  195. }
  196. }.bind(this),
  197. onFailure: function(xhr){
  198. o2.runCallback(callback, "requestFailure", [xhr]);
  199. }.bind(this),
  200. onError: function(text, error){
  201. o2.runCallback(callback, "error", [text, error]);
  202. }.bind(this)
  203. });
  204. res.send();
  205. return json;
  206. };
  207. _json.getJsonp = function(url, callback, async, callbackKey){
  208. var loadAsync = (async !== false);
  209. var callbackKeyWord = callbackKey || "callback";
  210. url = (url.indexOf("?")!==-1) ? url+"&v="+o2.version.v : url+"?v="+o2.version.v;
  211. var res = new Request.JSONP({
  212. url: o2.filterUrl(url),
  213. secure: false,
  214. method: "get",
  215. noCache: true,
  216. async: loadAsync,
  217. callbackKey: callbackKeyWord,
  218. onSuccess: function(responseJSON, responseText){
  219. o2.runCallback(callback, "success",[responseJSON, responseText]);
  220. }.bind(this),
  221. onFailure: function(xhr){
  222. o2.runCallback(callback, "requestFailure",[xhr]);
  223. }.bind(this),
  224. onError: function(text, error){
  225. o2.runCallback(callback, "error",[text, error]);
  226. }.bind(this)
  227. });
  228. res.send();
  229. };
  230. var _loadLP = function(name){
  231. var jsPath = o2.session.path;
  232. jsPath = jsPath+"/lp/"+name+".js";
  233. var r = new Request({
  234. url: o2.filterUrl(jsPath),
  235. async: false,
  236. method: "get",
  237. onSuccess: function(responseText){
  238. try{
  239. Browser.exec(responseText);
  240. }catch (e){}
  241. },
  242. onFailure: function(xhr){
  243. throw "loadLP Error: "+xhr.responseText;
  244. }
  245. });
  246. r.send();
  247. };
  248. var _cacheUrls = [
  249. /jaxrs\/form\/workorworkcompleted\/.+/ig,
  250. // /jaxrs\/script/ig,
  251. /jaxrs\/script\/.+\/app\/.+\/imported/ig,
  252. /jaxrs\/script\/portal\/.+\/name\/.+\/imported/ig,
  253. /jaxrs\/script\/.+\/application\/.+\/imported/ig,
  254. /jaxrs\/page\/.+\/portal\/.+/ig
  255. // /jaxrs\/authentication/ig
  256. // /jaxrs\/statement\/.*\/execute\/page\/.*\/size\/.*/ig
  257. ];
  258. _restful = function(method, address, data, callback, async, withCredentials, cache){
  259. var loadAsync = (async !== false);
  260. var credentials = (withCredentials !== false);
  261. address = (address.indexOf("?")!==-1) ? address+"&v="+o2.version.v : address+"?v="+o2.version.v;
  262. //var noCache = cache===false;
  263. var noCache = !cache;
  264. //if (Browser.name == "ie")
  265. if (_cacheUrls.length){
  266. for (var i=0; i<_cacheUrls.length; i++){
  267. if (_cacheUrls[i].test(address)){
  268. noCache = false;
  269. break;
  270. }
  271. }
  272. }
  273. //var noCache = false;
  274. var res = new Request.JSON({
  275. url: o2.filterUrl(address),
  276. secure: false,
  277. method: method,
  278. emulation: false,
  279. noCache: noCache,
  280. async: loadAsync,
  281. withCredentials: credentials,
  282. onSuccess: function(responseJSON, responseText){
  283. // var xToken = this.getHeader("authorization");
  284. // if (!xToken) xToken = this.getHeader("x-token");
  285. // if (xToken){
  286. // if (layout){
  287. // if (!layout.session) layout.session = {};
  288. // layout.session.token = xToken;
  289. // }
  290. // }
  291. o2.runCallback(callback, "success", [responseJSON]);
  292. },
  293. onFailure: function(xhr){
  294. o2.runCallback(callback, "requestFailure", [xhr]);
  295. }.bind(this),
  296. onError: function(text, error){
  297. o2.runCallback(callback, "error", [text, error]);
  298. }.bind(this)
  299. });
  300. res.setHeader("Content-Type", "application/json; charset=utf-8");
  301. res.setHeader("Accept", "text/html,application/json,*/*");
  302. if (window.layout) {
  303. if (layout["debugger"]){
  304. res.setHeader("x-debugger", "true");
  305. }
  306. if (layout.session && layout.session.user){
  307. if (layout.session.user.token) {
  308. res.setHeader("x-token", layout.session.user.token);
  309. res.setHeader("authorization", layout.session.user.token);
  310. }
  311. }
  312. }
  313. //Content-Type application/x-www-form-urlencoded; charset=utf-8
  314. res.send(data);
  315. return res;
  316. };
  317. var _release = function(o){
  318. var type = typeOf(o);
  319. switch (type){
  320. case "object":
  321. for (var k in o){
  322. //if (o[k] && o[k].destroy) o[k].destroy();
  323. o[k] = null;
  324. }
  325. break;
  326. case "array":
  327. for (var i=0; i< o.length; i++){
  328. _release(o[i]);
  329. if (o[i]) o[i] = null;
  330. }
  331. break;
  332. }
  333. };
  334. var _defineProperties = Object.defineProperties || function (obj, properties) {
  335. function convertToDescriptor(desc) {
  336. function hasProperty(obj, prop) {
  337. return Object.prototype.hasOwnProperty.call(obj, prop);
  338. }
  339. function isCallable(v) {
  340. // NB: modify as necessary if other values than functions are callable.
  341. return typeof v === "function";
  342. }
  343. if (typeof desc !== "object" || desc === null)
  344. throw new TypeError("bad desc");
  345. var d = {};
  346. if (hasProperty(desc, "enumerable"))
  347. d.enumerable = !!desc.enumerable;
  348. if (hasProperty(desc, "configurable"))
  349. d.configurable = !!desc.configurable;
  350. if (hasProperty(desc, "value"))
  351. d.value = desc.value;
  352. if (hasProperty(desc, "writable"))
  353. d.writable = !!desc.writable;
  354. if (hasProperty(desc, "get")) {
  355. var g = desc.get;
  356. if (!isCallable(g) && typeof g !== "undefined")
  357. throw new TypeError("bad get");
  358. d.get = g;
  359. }
  360. if (hasProperty(desc, "set")) {
  361. var s = desc.set;
  362. if (!isCallable(s) && typeof s !== "undefined")
  363. throw new TypeError("bad set");
  364. d.set = s;
  365. }
  366. if (("get" in d || "set" in d) && ("value" in d || "writable" in d))
  367. throw new TypeError("identity-confused descriptor");
  368. return d;
  369. }
  370. if (typeof obj !== "object" || obj === null)
  371. throw new TypeError("bad obj");
  372. properties = Object(properties);
  373. var keys = Object.keys(properties);
  374. var descs = [];
  375. for (var j = 0; j < keys.length; j++)
  376. descs.push([keys[j], convertToDescriptor(properties[keys[j]])]);
  377. for (var i = 0; i < descs.length; i++){
  378. if (Object.defineProperty && (Browser.name=="ie" && Browser.version!=8)){
  379. Object.defineProperty(obj, descs[i][0], descs[i][1]);
  380. }else{
  381. if (descs[i][1].value) obj[descs[i][0]] = descs[i][1].value;
  382. if (descs[i][1].get) obj["get"+descs[i][0].capitalize()] = descs[i][1].get;
  383. if (descs[i][1].set) obj["set"+descs[i][0].capitalize()] = descs[i][1].set;
  384. }
  385. }
  386. return obj;
  387. };
  388. var _txt = function(v){
  389. var t = v.replace(/\</g, "&lt;");
  390. t = t.replace(/\</g, "&gt;");
  391. return t;
  392. };
  393. this.o2.Class = _Class;
  394. this.o2.require = _require;
  395. this.o2.requireApp = _requireApp;
  396. this.o2.JSON = _json;
  397. this.o2.loadLP = _loadLP;
  398. this.o2.restful = _restful;
  399. this.o2.release = _release;
  400. this.o2.defineProperties = _defineProperties;
  401. this.o2.txt = _txt;
  402. Object.repeatArray = function(o, count){
  403. var arr = [];
  404. for (var i=0; i<count; i++){
  405. arr.push(o)
  406. }
  407. return arr;
  408. }
  409. })();
  410. o2.core = true;