o2.core.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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: 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. module = module.replace("MWF", "o2");
  76. var levels = module.split(".");
  77. if (levels[levels.length-1]==="*") levels[levels.length-1] = "package";
  78. levels.shift();
  79. var jsPath = this.o2.session.path;
  80. jsPath +="/"+levels.join("/")+".js";
  81. var loadAsync = (async!==false);
  82. _requireJs(jsPath, callback, loadAsync, compression, module);
  83. };
  84. var _requireSequence = function(fun, module, thisLoaded, thisErrorLoaded, callback, async, compression){
  85. var m = module.shift();
  86. fun(m, {
  87. "onSuccess": function(m){
  88. thisLoaded.push(m);
  89. o2.runCallback(callback, "every", [m]);
  90. if (module.length){
  91. _requireSequence(module, thisLoaded, thisErrorLoaded, callback);
  92. }else{
  93. if (thisErrorLoaded.length){
  94. o2.runCallback(callback, "failure", [thisLoaded, thisErrorLoaded]);
  95. }else{
  96. o2.runCallback(callback, "success", [thisLoaded, thisErrorLoaded]);
  97. }
  98. }
  99. },
  100. "onFailure": function(){
  101. thisErrorLoaded.push(module[i]);
  102. o2.runCallback(callback, "failure", [thisLoaded, thisErrorLoaded]);
  103. }
  104. }, async, compression);
  105. };
  106. var _requireDisarray = function(fun, module, thisLoaded, thisErrorLoaded, callback, async, compression){
  107. for (var i=0; i<module.length; i++){
  108. fun(module[i], {
  109. "onSuccess": function(m){
  110. thisLoaded.push(m);
  111. o2.runCallback(callback, "every", [m]);
  112. if ((thisLoaded.length+thisErrorLoaded.length)===module.length){
  113. if (thisErrorLoaded.length){
  114. o2.runCallback(callback, "failure", [thisLoaded, thisErrorLoaded]);
  115. }else{
  116. o2.runCallback(callback, "success", [thisLoaded, thisErrorLoaded]);
  117. }
  118. }
  119. },
  120. "onFailure": function(){
  121. thisErrorLoaded.push(module[i]);
  122. o2.runCallback(callback, "failure", [thisLoaded, thisErrorLoaded]);
  123. }
  124. }, async, compression);
  125. }
  126. };
  127. var _require = function(module, callback, async, sequence, compression){
  128. var type = typeOf(module);
  129. if (type==="array"){
  130. var sql = !!sequence;
  131. var thisLoaded = [];
  132. var thisErrorLoaded = [];
  133. if (sql){
  134. _requireSequence(_requireSingle, module, thisLoaded, thisErrorLoaded, callback, async, compression);
  135. }else{
  136. _requireDisarray(_requireSingle, module, thisLoaded, thisErrorLoaded, callback, async, compression);
  137. }
  138. }
  139. if (type==="string"){
  140. _requireSingle(module, callback, async, compression);
  141. }
  142. };
  143. var _requireAppSingle = function(modules, callback, async, compression){
  144. var module = modules[0];
  145. var clazz = modules[1];
  146. var levels = module.split(".");
  147. //levels.shift();
  148. var root = "x_component_"+levels.join("_");
  149. var clazzName = clazz || "Main";
  150. var path = "/"+root+"/"+clazzName.replace(/\./g, "/")+".js";
  151. var loadAsync = (async!==false);
  152. _requireJs(path, callback, loadAsync, compression);
  153. };
  154. var _requireApp = function(module, clazz, callback, async, sequence, compression){
  155. var type = typeOf(module);
  156. if (type==="array"){
  157. var sql = !!sequence;
  158. var thisLoaded = [];
  159. var thisErrorLoaded = [];
  160. if (sql){
  161. _requireSequence(_requireAppSingle, module, thisLoaded, thisErrorLoaded, callback, async, compression);
  162. }else{
  163. _requireDisarray(_requireAppSingle, module, thisLoaded, thisErrorLoaded, callback, async, compression);
  164. }
  165. }
  166. if (type==="string"){
  167. var modules = [module, clazz];
  168. _requireAppSingle(modules, callback, async, compression);
  169. }
  170. };
  171. var _json = JSON;
  172. _json.get = function(url, callback, async, nocache){
  173. var loadAsync = (async !== false);
  174. var noJsonCache = (nocache === true);
  175. url = (url.indexOf("?")!==-1) ? url+"&v="+o2.version.v : url+"?v="+o2.version.v;
  176. var json = null;
  177. var res = new Request.JSON({
  178. url: url,
  179. secure: false,
  180. method: "get",
  181. noCache: noJsonCache,
  182. async: loadAsync,
  183. withCredentials: true,
  184. onSuccess: function(responseJSON, responseText){
  185. json = responseJSON;
  186. if (typeOf(callback).toLowerCase() === 'function'){
  187. callback(responseJSON, responseText);
  188. }else{
  189. o2.runCallback(callback, "success", [responseJSON, responseText]);
  190. }
  191. }.bind(this),
  192. onFailure: function(xhr){
  193. o2.runCallback(callback, "requestFailure", [xhr]);
  194. }.bind(this),
  195. onError: function(text, error){
  196. o2.runCallback(callback, "error", [text, error]);
  197. }.bind(this)
  198. });
  199. res.send();
  200. return json;
  201. };
  202. _json.getJsonp = function(url, callback, async, callbackKey){
  203. var loadAsync = (async !== false);
  204. var callbackKeyWord = callbackKey || "callback";
  205. url = (url.indexOf("?")!==-1) ? url+"&v="+o2.version.v : url+"?v="+o2.version.v;
  206. var res = new Request.JSONP({
  207. url: url,
  208. secure: false,
  209. method: "get",
  210. noCache: true,
  211. async: loadAsync,
  212. callbackKey: callbackKeyWord,
  213. onSuccess: function(responseJSON, responseText){
  214. o2.runCallback(callback, "success",[responseJSON, responseText]);
  215. }.bind(this),
  216. onFailure: function(xhr){
  217. o2.runCallback(callback, "requestFailure",[xhr]);
  218. }.bind(this),
  219. onError: function(text, error){
  220. o2.runCallback(callback, "error",[text, error]);
  221. }.bind(this)
  222. });
  223. res.send();
  224. };
  225. var _loadLP = function(name){
  226. var jsPath = o2.session.path;
  227. jsPath = jsPath+"/lp/"+name+".js";
  228. var r = new Request({
  229. url: jsPath,
  230. async: false,
  231. method: "get",
  232. onSuccess: function(responseText){
  233. try{
  234. Browser.exec(responseText);
  235. }catch (e){}
  236. },
  237. onFailure: function(xhr){
  238. throw "loadLP Error: "+xhr.responseText;
  239. }
  240. });
  241. r.send();
  242. };
  243. _restful = function(method, address, data, callback, async, withCredentials, cache){
  244. var loadAsync = (async !== false);
  245. var credentials = (withCredentials !== false);
  246. address = (address.indexOf("?")!==-1) ? address+"&v="+o2.version.v : address+"?v="+o2.version.v;
  247. var noCache = !cache;
  248. var res = new Request.JSON({
  249. url: address,
  250. secure: false,
  251. method: method,
  252. emulation: false,
  253. noCache: noCache,
  254. async: loadAsync,
  255. withCredentials: credentials,
  256. onSuccess: function(responseJSON, responseText){
  257. // var xToken = this.getHeader("authorization");
  258. // if (!xToken) xToken = this.getHeader("x-token");
  259. // if (xToken){
  260. // if (layout){
  261. // if (!layout.session) layout.session = {};
  262. // layout.session.token = xToken;
  263. // }
  264. // }
  265. o2.runCallback(callback, "success", [responseJSON])
  266. },
  267. onFailure: function(xhr){
  268. o2.runCallback(callback, "requestFailure", [xhr]);
  269. }.bind(this),
  270. onError: function(text, error){
  271. o2.runCallback(callback, "error", [text, error]);
  272. }.bind(this)
  273. });
  274. res.setHeader("Content-Type", "application/json; charset=utf-8");
  275. res.setHeader("Accept", "text/html,application/json,*/*");
  276. if (window.layout) {
  277. if (layout["debugger"]){
  278. res.setHeader("x-debugger", "true");
  279. }
  280. if (layout.session && layout.session.user){
  281. if (layout.session.user.token) {
  282. res.setHeader("x-token", layout.session.user.token);
  283. res.setHeader("authorization", layout.session.user.token);
  284. }
  285. }
  286. }
  287. //Content-Type application/x-www-form-urlencoded; charset=utf-8
  288. res.send(data);
  289. return res;
  290. };
  291. var _release = function(o){
  292. var type = typeOf(o);
  293. switch (type){
  294. case "object":
  295. for (var k in o){
  296. //if (o[k] && o[k].destroy) o[k].destroy();
  297. o[k] = null;
  298. }
  299. break;
  300. case "array":
  301. for (var i=0; i< o.length; i++){
  302. _release(o[i]);
  303. if (o[i]) o[i] = null;
  304. }
  305. break;
  306. }
  307. };
  308. var _defineProperties = Object.defineProperties || function (obj, properties) {
  309. function convertToDescriptor(desc) {
  310. function hasProperty(obj, prop) {
  311. return Object.prototype.hasOwnProperty.call(obj, prop);
  312. }
  313. function isCallable(v) {
  314. // NB: modify as necessary if other values than functions are callable.
  315. return typeof v === "function";
  316. }
  317. if (typeof desc !== "object" || desc === null)
  318. throw new TypeError("bad desc");
  319. var d = {};
  320. if (hasProperty(desc, "enumerable"))
  321. d.enumerable = !!desc.enumerable;
  322. if (hasProperty(desc, "configurable"))
  323. d.configurable = !!desc.configurable;
  324. if (hasProperty(desc, "value"))
  325. d.value = desc.value;
  326. if (hasProperty(desc, "writable"))
  327. d.writable = !!desc.writable;
  328. if (hasProperty(desc, "get")) {
  329. var g = desc.get;
  330. if (!isCallable(g) && typeof g !== "undefined")
  331. throw new TypeError("bad get");
  332. d.get = g;
  333. }
  334. if (hasProperty(desc, "set")) {
  335. var s = desc.set;
  336. if (!isCallable(s) && typeof s !== "undefined")
  337. throw new TypeError("bad set");
  338. d.set = s;
  339. }
  340. if (("get" in d || "set" in d) && ("value" in d || "writable" in d))
  341. throw new TypeError("identity-confused descriptor");
  342. return d;
  343. }
  344. if (typeof obj !== "object" || obj === null)
  345. throw new TypeError("bad obj");
  346. properties = Object(properties);
  347. var keys = Object.keys(properties);
  348. var descs = [];
  349. for (var j = 0; j < keys.length; j++)
  350. descs.push([keys[j], convertToDescriptor(properties[keys[j]])]);
  351. for (var i = 0; i < descs.length; i++){
  352. if (Object.defineProperty && (Browser.name=="ie" && Browser.version!=8)){
  353. Object.defineProperty(obj, descs[i][0], descs[i][1]);
  354. }else{
  355. if (descs[i][1].value) obj[descs[i][0]] = descs[i][1].value;
  356. if (descs[i][1].get) obj["get"+descs[i][0].capitalize()] = descs[i][1].get;
  357. if (descs[i][1].set) obj["set"+descs[i][0].capitalize()] = descs[i][1].set;
  358. }
  359. }
  360. return obj;
  361. };
  362. var _txt = function(v){
  363. var t = v.replace(/\</g, "&lt;");
  364. t = t.replace(/\</g, "&gt;");
  365. return t;
  366. };
  367. this.o2.Class = _Class;
  368. this.o2.require = _require;
  369. this.o2.requireApp = _requireApp;
  370. this.o2.JSON = _json;
  371. this.o2.loadLP = _loadLP;
  372. this.o2.restful = _restful;
  373. this.o2.release = _release;
  374. this.o2.defineProperties = _defineProperties;
  375. this.o2.txt = _txt;
  376. Object.repeatArray = function(o, count){
  377. var arr = [];
  378. for (var i=0; i<count; i++){
  379. arr.push(o)
  380. }
  381. return arr;
  382. }
  383. })();
  384. o2.core = true;