o2.core.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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 o = o2;
  83. var i = 0;
  84. while (o && i<levels.length){
  85. o = o[levels[i]];
  86. i++
  87. }
  88. if (!o){
  89. var jsPath = this.o2.session.path;
  90. jsPath +="/"+levels.join("/")+".js";
  91. var loadAsync = (async!==false);
  92. _requireJs(jsPath, callback, loadAsync, compression, module);
  93. }else{
  94. o2.runCallback(callback, "success", [module]);
  95. }
  96. }
  97. };
  98. var _requireSequence = function(fun, module, thisLoaded, thisErrorLoaded, callback, async, compression){
  99. var m = module.shift();
  100. fun(m, {
  101. "onSuccess": function(m){
  102. thisLoaded.push(m);
  103. o2.runCallback(callback, "every", [m]);
  104. if (module.length){
  105. _requireSequence(fun, module, thisLoaded, thisErrorLoaded, callback, async, compression);
  106. }else{
  107. if (thisErrorLoaded.length){
  108. o2.runCallback(callback, "failure", [thisLoaded, thisErrorLoaded]);
  109. }else{
  110. o2.runCallback(callback, "success", [thisLoaded, thisErrorLoaded]);
  111. }
  112. }
  113. },
  114. "onFailure": function(){
  115. thisErrorLoaded.push(module[i]);
  116. o2.runCallback(callback, "failure", [thisLoaded, thisErrorLoaded]);
  117. }
  118. }, async, compression);
  119. };
  120. var _requireDisarray = function(fun, module, thisLoaded, thisErrorLoaded, callback, async, compression){
  121. for (var i=0; i<module.length; i++){
  122. fun(module[i], {
  123. "onSuccess": function(m){
  124. thisLoaded.push(m);
  125. o2.runCallback(callback, "every", [m]);
  126. if ((thisLoaded.length+thisErrorLoaded.length)===module.length){
  127. if (thisErrorLoaded.length){
  128. o2.runCallback(callback, "failure", [thisLoaded, thisErrorLoaded]);
  129. }else{
  130. o2.runCallback(callback, "success", [thisLoaded, thisErrorLoaded]);
  131. }
  132. }
  133. },
  134. "onFailure": function(){
  135. thisErrorLoaded.push(module[i]);
  136. o2.runCallback(callback, "failure", [thisLoaded, thisErrorLoaded]);
  137. }
  138. }, async, compression);
  139. }
  140. };
  141. var _require = function(module, callback, async, sequence, compression){
  142. var type = typeOf(module);
  143. if (type==="array"){
  144. var sql = !!sequence;
  145. var thisLoaded = [];
  146. var thisErrorLoaded = [];
  147. if (sql){
  148. _requireSequence(_requireSingle, module, thisLoaded, thisErrorLoaded, callback, async, compression);
  149. }else{
  150. _requireDisarray(_requireSingle, module, thisLoaded, thisErrorLoaded, callback, async, compression);
  151. }
  152. }
  153. if (type==="string"){
  154. _requireSingle(module, callback, async, compression);
  155. }
  156. };
  157. var _requireAppSingle = function(modules, callback, async, compression){
  158. var module = modules[0];
  159. var clazz = modules[1];
  160. var levels = module.split(".");
  161. var o = o2.xApplication;
  162. var i = 0;
  163. while (o && i<levels.length){
  164. o = o[levels[i]];
  165. i++
  166. }
  167. if (o) o = o[clazz || "Main"];
  168. if (!o){
  169. //levels.shift();
  170. var root = "x_component_"+levels.join("_");
  171. var clazzName = clazz || "Main";
  172. var path = "../"+root+"/"+clazzName.replace(/\./g, "/")+".js";
  173. var loadAsync = (async!==false);
  174. _requireJs(path, callback, loadAsync, compression);
  175. }else{
  176. o2.runCallback(callback, "success");
  177. }
  178. };
  179. var _requireApp = function(module, clazz, callback, async, sequence, compression){
  180. var type = typeOf(module);
  181. if (type==="array"){
  182. var sql = !!sequence;
  183. var thisLoaded = [];
  184. var thisErrorLoaded = [];
  185. if (sql){
  186. _requireSequence(_requireAppSingle, module, thisLoaded, thisErrorLoaded, callback, async, compression);
  187. }else{
  188. _requireDisarray(_requireAppSingle, module, thisLoaded, thisErrorLoaded, callback, async, compression);
  189. }
  190. }
  191. if (type==="string"){
  192. var modules = [module, clazz];
  193. _requireAppSingle(modules, callback, async, compression);
  194. }
  195. };
  196. JSON = window.JSON || {};
  197. var _json = JSON;
  198. _json.get = function(url, callback, async, nocache){
  199. var loadAsync = (async !== false);
  200. var noJsonCache = (nocache === true);
  201. url = (url.indexOf("?")!==-1) ? url+"&v="+o2.version.v : url+"?v="+o2.version.v;
  202. var json = null;
  203. var res = new Request.JSON({
  204. url: o2.filterUrl(url),
  205. secure: false,
  206. method: "get",
  207. noCache: noJsonCache,
  208. async: loadAsync,
  209. withCredentials: true,
  210. onSuccess: function(responseJSON, responseText){
  211. json = responseJSON;
  212. if (typeOf(callback).toLowerCase() === 'function'){
  213. callback(responseJSON, responseText);
  214. }else{
  215. o2.runCallback(callback, "success", [responseJSON, responseText]);
  216. }
  217. }.bind(this),
  218. onFailure: function(xhr){
  219. o2.runCallback(callback, "requestFailure", [xhr]);
  220. }.bind(this),
  221. onError: function(text, error){
  222. o2.runCallback(callback, "error", [text, error]);
  223. }.bind(this)
  224. });
  225. res.send();
  226. return json;
  227. };
  228. _json.getJsonp = function(url, callback, async, callbackKey){
  229. var loadAsync = (async !== false);
  230. var callbackKeyWord = callbackKey || "callback";
  231. url = (url.indexOf("?")!==-1) ? url+"&v="+o2.version.v : url+"?v="+o2.version.v;
  232. var res = new Request.JSONP({
  233. url: o2.filterUrl(url),
  234. secure: false,
  235. method: "get",
  236. noCache: true,
  237. async: loadAsync,
  238. callbackKey: callbackKeyWord,
  239. onSuccess: function(responseJSON, responseText){
  240. o2.runCallback(callback, "success",[responseJSON, responseText]);
  241. }.bind(this),
  242. onFailure: function(xhr){
  243. o2.runCallback(callback, "requestFailure",[xhr]);
  244. }.bind(this),
  245. onError: function(text, error){
  246. o2.runCallback(callback, "error",[text, error]);
  247. }.bind(this)
  248. });
  249. res.send();
  250. };
  251. var _loadLP = function(name){
  252. var jsPath = o2.session.path;
  253. jsPath = jsPath+"/lp/"+name+".js";
  254. var r = new Request({
  255. url: o2.filterUrl(jsPath),
  256. async: false,
  257. method: "get",
  258. onSuccess: function(responseText){
  259. try{
  260. Browser.exec(responseText);
  261. }catch (e){}
  262. },
  263. onFailure: function(xhr){
  264. throw "loadLP Error: "+xhr.responseText;
  265. }
  266. });
  267. r.send();
  268. };
  269. var _cacheUrls = [
  270. /jaxrs\/form\/workorworkcompleted\/.+/ig,
  271. // /jaxrs\/script/ig,
  272. /jaxrs\/script\/.+\/app\/.+\/imported/ig,
  273. /jaxrs\/script\/portal\/.+\/name\/.+\/imported/ig,
  274. /jaxrs\/script\/[\S\s]+\/application\/[\S\s]+\/imported/ig,
  275. /jaxrs\/page\/.+\/portal\/.+/ig
  276. // /jaxrs\/authentication/ig
  277. // /jaxrs\/statement\/.*\/execute\/page\/.*\/size\/.*/ig
  278. ];
  279. _restful = function(method, address, data, callback, async, withCredentials, cache){
  280. var loadAsync = (async !== false);
  281. var credentials = (withCredentials !== false);
  282. address = (address.indexOf("?")!==-1) ? address+"&v="+o2.version.v : address+"?v="+o2.version.v;
  283. //var noCache = cache===false;
  284. var noCache = !cache;
  285. //if (Browser.name == "ie")
  286. if (_cacheUrls.length){
  287. for (var i=0; i<_cacheUrls.length; i++){
  288. _cacheUrls[i].lastIndex = 0;
  289. if (_cacheUrls[i].test(address)){
  290. noCache = false;
  291. break;
  292. }
  293. }
  294. }
  295. //var noCache = false;
  296. var res = new Request.JSON({
  297. url: o2.filterUrl(address),
  298. secure: false,
  299. method: method,
  300. emulation: false,
  301. noCache: noCache,
  302. async: loadAsync,
  303. withCredentials: credentials,
  304. onSuccess: function(responseJSON, responseText){
  305. // var xToken = this.getHeader("authorization");
  306. // if (!xToken) xToken = this.getHeader("x-token");
  307. var xToken = this.getHeader("x-token");
  308. if (xToken){
  309. if (window.layout){
  310. if (!layout.session) layout.session = {};
  311. layout.session.token = xToken;
  312. }
  313. }
  314. o2.runCallback(callback, "success", [responseJSON]);
  315. },
  316. onFailure: function(xhr){
  317. o2.runCallback(callback, "requestFailure", [xhr]);
  318. }.bind(this),
  319. onError: function(text, error){
  320. o2.runCallback(callback, "error", [text, error]);
  321. }.bind(this)
  322. });
  323. res.setHeader("Content-Type", "application/json; charset=utf-8");
  324. res.setHeader("Accept", "text/html,application/json,*/*");
  325. if (window.layout) {
  326. if (layout["debugger"]){
  327. res.setHeader("x-debugger", "true");
  328. }
  329. if (layout.session && layout.session.user){
  330. if (layout.session.user.token) {
  331. res.setHeader("x-token", layout.session.user.token);
  332. res.setHeader("authorization", layout.session.user.token);
  333. }
  334. }
  335. }
  336. //Content-Type application/x-www-form-urlencoded; charset=utf-8
  337. res.send(data);
  338. return res;
  339. };
  340. var _release = function(o){
  341. var type = typeOf(o);
  342. switch (type){
  343. case "object":
  344. for (var k in o){
  345. //if (o[k] && o[k].destroy) o[k].destroy();
  346. o[k] = null;
  347. }
  348. break;
  349. case "array":
  350. for (var i=0; i< o.length; i++){
  351. _release(o[i]);
  352. if (o[i]) o[i] = null;
  353. }
  354. break;
  355. }
  356. };
  357. var _defineProperties = Object.defineProperties || function (obj, properties) {
  358. function convertToDescriptor(desc) {
  359. function hasProperty(obj, prop) {
  360. return Object.prototype.hasOwnProperty.call(obj, prop);
  361. }
  362. function isCallable(v) {
  363. // NB: modify as necessary if other values than functions are callable.
  364. return typeof v === "function";
  365. }
  366. if (typeof desc !== "object" || desc === null)
  367. throw new TypeError("bad desc");
  368. var d = {};
  369. if (hasProperty(desc, "enumerable"))
  370. d.enumerable = !!desc.enumerable;
  371. if (hasProperty(desc, "configurable"))
  372. d.configurable = !!desc.configurable;
  373. if (hasProperty(desc, "value"))
  374. d.value = desc.value;
  375. if (hasProperty(desc, "writable"))
  376. d.writable = !!desc.writable;
  377. if (hasProperty(desc, "get")) {
  378. var g = desc.get;
  379. if (!isCallable(g) && typeof g !== "undefined")
  380. throw new TypeError("bad get");
  381. d.get = g;
  382. }
  383. if (hasProperty(desc, "set")) {
  384. var s = desc.set;
  385. if (!isCallable(s) && typeof s !== "undefined")
  386. throw new TypeError("bad set");
  387. d.set = s;
  388. }
  389. if (("get" in d || "set" in d) && ("value" in d || "writable" in d))
  390. throw new TypeError("identity-confused descriptor");
  391. return d;
  392. }
  393. if (typeof obj !== "object" || obj === null)
  394. throw new TypeError("bad obj");
  395. properties = Object(properties);
  396. var keys = Object.keys(properties);
  397. var descs = [];
  398. for (var j = 0; j < keys.length; j++)
  399. descs.push([keys[j], convertToDescriptor(properties[keys[j]])]);
  400. for (var i = 0; i < descs.length; i++){
  401. if (Object.defineProperty && (Browser.name=="ie" && Browser.version!=8)){
  402. Object.defineProperty(obj, descs[i][0], descs[i][1]);
  403. }else{
  404. if (descs[i][1].value) obj[descs[i][0]] = descs[i][1].value;
  405. if (descs[i][1].get) obj["get"+descs[i][0].capitalize()] = descs[i][1].get;
  406. if (descs[i][1].set) obj["set"+descs[i][0].capitalize()] = descs[i][1].set;
  407. }
  408. }
  409. return obj;
  410. };
  411. if (!Array.prototype.findIndex) {
  412. Object.defineProperty(Array.prototype, 'findIndex', {
  413. value: function(predicate) {
  414. if (this == null) {
  415. throw new TypeError('"this" is null or not defined');
  416. }
  417. var o = Object(this);
  418. var len = o.length >>> 0;
  419. if (typeof predicate !== 'function') {
  420. throw new TypeError('predicate must be a function');
  421. }
  422. var thisArg = arguments[1];
  423. var k = 0;
  424. while (k < len) {
  425. var kValue = o[k];
  426. if (predicate.call(thisArg, kValue, k, o)) {
  427. return k;
  428. }
  429. k++;
  430. }
  431. return -1;
  432. }
  433. });
  434. }
  435. if (!Array.prototype.find) {
  436. Object.defineProperty(Array.prototype, 'find', {
  437. value: function(predicate) {
  438. if (this == null) {
  439. throw new TypeError('"this" is null or not defined');
  440. }
  441. var o = Object(this);
  442. var len = o.length >>> 0;
  443. if (typeof predicate !== 'function') {
  444. throw new TypeError('predicate must be a function');
  445. }
  446. var thisArg = arguments[1];
  447. var k = 0;
  448. while (k < len) {
  449. var kValue = o[k];
  450. if (predicate.call(thisArg, kValue, k, o)) {
  451. return kValue;
  452. }
  453. k++;
  454. }
  455. return undefined;
  456. }
  457. });
  458. }
  459. var _txt = function(v){
  460. var t = v.replace(/\</g, "&lt;");
  461. t = t.replace(/\</g, "&gt;");
  462. return t;
  463. };
  464. this.o2.Class = _Class;
  465. this.o2.require = _require;
  466. this.o2.requireApp = _requireApp;
  467. this.o2.JSON = _json;
  468. this.o2.loadLP = _loadLP;
  469. this.o2.restful = _restful;
  470. this.o2.release = _release;
  471. this.o2.defineProperties = _defineProperties;
  472. this.o2.txt = _txt;
  473. Object.repeatArray = function(o, count){
  474. var arr = [];
  475. for (var i=0; i<count; i++){
  476. arr.push(o)
  477. }
  478. return arr;
  479. }
  480. Date.implement({
  481. "getFromServer": function(){
  482. var d;
  483. o2.Actions.get("x_program_center").echo(function(json){
  484. d = Date.parse(json.data.serverTime);
  485. }, null, false);
  486. return d;
  487. }
  488. });
  489. })();
  490. o2.core = true;