base.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. layout = window.layout || {};
  2. layout.desktop = layout;
  3. layout.desktop.type = "app";
  4. var locate = window.location;
  5. layout.protocol = locate.protocol;
  6. layout.inBrowser = true;
  7. layout.session = layout.session || {};
  8. layout.debugger = (locate.href.toString().indexOf("debugger") !== -1);
  9. layout.anonymous = (locate.href.toString().indexOf("anonymous") !== -1);
  10. o2.xApplication = o2.xApplication || {};
  11. o2.xDesktop = o2.xDesktop || {};
  12. o2.xDesktop.requireApp = function (module, clazz, callback, async) {
  13. o2.requireApp(module, clazz, callback, async);
  14. };
  15. (function (layout) {
  16. layout.readys = [];
  17. layout.addReady = function () {
  18. for (var i = 0; i < arguments.length; i++) {
  19. if (o2.typeOf(arguments[i]) === "function") layout.readys.push(arguments[i]);
  20. }
  21. };
  22. var _requireApp = function (appNames, callback, clazzName) {
  23. var appPath = appNames.split(".");
  24. var baseObject = o2.xApplication;
  25. appPath.each(function (path, i) {
  26. if (i < (appPath.length - 1)) {
  27. baseObject[path] = baseObject[path] || {};
  28. } else {
  29. baseObject[path] = baseObject[path] || { "options": Object.clone(o2.xApplication.Common.options) };
  30. }
  31. baseObject = baseObject[path];
  32. }.bind(this));
  33. if (!baseObject.options) baseObject.options = Object.clone(o2.xApplication.Common.options);
  34. var _lpLoaded = false;
  35. o2.xDesktop.requireApp(appNames, "lp." + o2.language, {
  36. "failure": function () {
  37. o2.xDesktop.requireApp(appNames, "lp.zh-cn", null, false);
  38. }.bind(this)
  39. }, false);
  40. o2.xDesktop.requireApp(appNames, clazzName, function () {
  41. if (callback) callback(baseObject);
  42. });
  43. };
  44. var _createNewApplication = function (e, appNamespace, appName, options, statusObj, inBrowser, taskitem, notCurrent) {
  45. if (options){ options.event = e; }else{ options = {"event": e}; }
  46. var app = new appNamespace["Main"](layout.desktop, options);
  47. app.desktop = layout.desktop;
  48. app.status = statusObj;
  49. app.inBrowser = !!(inBrowser || layout.inBrowser);
  50. if (layout.desktop.type === "layout"){
  51. app.appId = (options.appId) ? options.appId : ((appNamespace.options.multitask) ? appName + "-" + (new o2.widget.UUID()) : appName);
  52. app.options.appId = app.appId;
  53. if (!taskitem) taskitem = layout.desktop.createTaskItem(app);
  54. app.taskitem = taskitem;
  55. app.taskitem.app = app;
  56. // if (layout.viewMode=="Default"){
  57. // if (!notCurrent){
  58. // app.load(!notCurrent);
  59. // }else{
  60. // app.isLoadApplication = false;
  61. // }
  62. // }else{
  63. // app.load(!notCurrent);
  64. // }
  65. app.isLoadApplication = true;
  66. app.load(!notCurrent);
  67. if (!layout.desktop.apps) layout.desktop.apps = {};
  68. if (layout.desktop.apps[app.appId]){
  69. var tmpApp = layout.desktop.apps[app.appId];
  70. }else{
  71. layout.desktop.apps[app.appId] = app;
  72. }
  73. layout.desktop.appArr.push(app);
  74. layout.desktop.appCurrentList.push(app);
  75. if (!notCurrent) layout.desktop.currentApp = app;
  76. //app.taskitem = new MWF.xDesktop.Layout.Taskitem(app, this);
  77. }else{
  78. app.load(true);
  79. layout.app = app;
  80. }
  81. var mask = document.getElementById("appContentMask");
  82. if (mask) mask.destroy();
  83. };
  84. var _openWorkAndroid = function (options) {
  85. if (window.o2android && window.o2android.openO2Work) {
  86. if (options.workId) {
  87. window.o2android.openO2Work(options.workId, "", options.title || "");
  88. } else if (options.workCompletedId) {
  89. window.o2android.openO2Work("", options.workCompletedId, options.title || "");
  90. }
  91. return true;
  92. }
  93. return false;
  94. };
  95. var _openWorkIOS = function (options) {
  96. if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.openO2Work) {
  97. if (options.workId) {
  98. window.webkit.messageHandlers.openO2Work.postMessage({
  99. "work": options.workId,
  100. "workCompleted": "",
  101. "title": options.title || ""
  102. });
  103. } else if (options.workCompletedId) {
  104. window.webkit.messageHandlers.openO2Work.postMessage({
  105. "work": "",
  106. "workCompleted": options.workCompletedId,
  107. "title": options.title || ""
  108. });
  109. }
  110. return true;
  111. }
  112. return false;
  113. };
  114. var _openWorkHTML = function (options) {
  115. var uri = new URI(window.location.href);
  116. var redirectlink = uri.getData("redirectlink");
  117. if (!redirectlink) {
  118. redirectlink = encodeURIComponent(locate.pathname + locate.search);
  119. } else {
  120. redirectlink = encodeURIComponent(redirectlink);
  121. }
  122. if (options.workId) {
  123. window.location = "workmobilewithaction.html?workid=" + options.workId + ((layout.debugger) ? "&debugger" : "") + "&redirectlink=" + redirectlink;
  124. } else if (options.workCompletedId) {
  125. window.location = "workmobilewithaction.html?workcompletedid=" + options.workCompletedId + ((layout.debugger) ? "&debugger" : "") + "&redirectlink=" + redirectlink;
  126. }
  127. };
  128. var _openWork = function (options) {
  129. if (!_openWorkAndroid(options)) if (!_openWorkIOS(options)) _openWorkHTML(options);
  130. };
  131. var _openDocument = function (appNames, options, statusObj) {
  132. var title = typeOf(options) === "object" ? (options.docTitle || options.title) : "";
  133. title = title || "";
  134. var par = "app=" + encodeURIComponent(appNames) + "&status=" + encodeURIComponent((statusObj) ? JSON.encode(statusObj) : "") + "&option=" + encodeURIComponent((options) ? JSON.encode(options) : "");
  135. if (window.o2android && window.o2android.openO2CmsDocument) {
  136. window.o2android.openO2CmsDocument(options.documentId, title);
  137. } else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.openO2CmsDocument) {
  138. window.webkit.messageHandlers.openO2CmsDocument.postMessage({ "docId": options.documentId, "docTitle": title });
  139. } else {
  140. window.location = "appMobile.html?" + par + ((layout.debugger) ? "&debugger" : "");
  141. }
  142. };
  143. var _openCms = function (appNames, options, statusObj) {
  144. var par = "app=" + encodeURIComponent(appNames) + "&status=" + encodeURIComponent((statusObj) ? JSON.encode(statusObj) : "") + "&option=" + encodeURIComponent((options) ? JSON.encode(options) : "");
  145. if (window.o2android && window.o2android.openO2CmsApplication) {
  146. window.o2android.openO2CmsApplication(options.columnId, options.title || "");
  147. } else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.openO2CmsApplication) {
  148. window.webkit.messageHandlers.openO2CmsApplication.postMessage(options.columnId);
  149. } else {
  150. window.location = "appMobile.html?" + par + ((layout.debugger) ? "&debugger" : "");
  151. }
  152. };
  153. var _openMeeting = function (appNames, options, statusObj) {
  154. var par = "app=" + encodeURIComponent(appNames) + "&status=" + encodeURIComponent((statusObj) ? JSON.encode(statusObj) : "") + "&option=" + encodeURIComponent((options) ? JSON.encode(options) : "");
  155. if (window.o2android && window.o2android.openO2Meeting) {
  156. window.o2android.openO2Meeting("");
  157. } else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.openO2Meeting) {
  158. window.webkit.messageHandlers.openO2Meeting.postMessage("");
  159. } else {
  160. window.location = "appMobile.html?" + par + ((layout.debugger) ? "&debugger" : "");
  161. }
  162. };
  163. var _openCalendar = function (appNames, options, statusObj) {
  164. var par = "app=" + encodeURIComponent(appNames) + "&status=" + encodeURIComponent((statusObj) ? JSON.encode(statusObj) : "") + "&option=" + encodeURIComponent((options) ? JSON.encode(options) : "");
  165. if (window.o2android && window.o2android.openO2Calendar) {
  166. window.o2android.openO2Calendar("");
  167. } else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.openO2Calendar) {
  168. window.webkit.messageHandlers.openO2Calendar.postMessage("");
  169. } else {
  170. window.location = "appMobile.html?" + par + ((layout.debugger) ? "&debugger" : "");
  171. }
  172. };
  173. var _openTaskCenter = function (appNames, options, statusObj) {
  174. var par = "app=" + encodeURIComponent(appNames) + "&status=" + encodeURIComponent((statusObj) ? JSON.encode(statusObj) : "") + "&option=" + encodeURIComponent((options) ? JSON.encode(options) : "");
  175. var tab = ((options && options.navi) ? options.navi : "task").toLowerCase();
  176. if (tab === "done") tab = "taskCompleted";
  177. if (tab === "readed") tab = "readCompleted";
  178. if (window.o2android && window.o2android.openO2WorkSpace) {
  179. window.o2android.openO2WorkSpace(tab);
  180. } else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.openO2WorkSpace) {
  181. window.webkit.messageHandlers.openO2WorkSpace.postMessage(tab);
  182. } else {
  183. window.location = "appMobile.html?" + par + ((layout.debugger) ? "&debugger" : "");
  184. }
  185. };
  186. var _openApplicationMobile = function (appNames, options, statusObj) {
  187. switch (appNames) {
  188. case "process.Work":
  189. _openWork(options);
  190. break;
  191. case "cms.Document":
  192. _openDocument(appNames, options, statusObj);
  193. break;
  194. case "cms.Module":
  195. _openCms(appNames, options, statusObj);
  196. break;
  197. case "Meeting":
  198. _openMeeting(appNames, options, statusObj);
  199. break;
  200. case "Calendar":
  201. _openCalendar(appNames, options, statusObj);
  202. break;
  203. case "process.TaskCenter":
  204. _openTaskCenter(appNames, options, statusObj);
  205. break;
  206. default:
  207. var uri = new URI(window.location.href);
  208. var optionsStr = uri.getData("option");
  209. var statusStr = uri.getData("status");
  210. window.location = "appMobile.html?app=" + appNames + "&option=" + (optionsStr || "") + "&status=" + (statusStr || "") + ((layout.debugger) ? "&debugger" : "");
  211. }
  212. };
  213. layout.openApplication = function (e, appNames, options, statusObj, inBrowser, taskitem, notCurrent) {
  214. if (appNames.substring(0, 4)==="@url"){
  215. var url = appNames.replace(/\@url\:/i, "");
  216. var a = new Element("a", {"href": url, "target": "_blank"});
  217. a.click();
  218. a.destroy();
  219. a = null;
  220. return true;
  221. }
  222. if (layout.app) {
  223. if (layout.mobile) {
  224. _openApplicationMobile(appNames, options, statusObj);
  225. } else {
  226. var par = "app=" + encodeURIComponent(appNames) + "&status=" + encodeURIComponent((statusObj) ? JSON.encode(statusObj) : "") + "&option=" + encodeURIComponent((options) ? JSON.encode(options) : "");
  227. if (layout.app.$openWithSelf) {
  228. return window.location = "app.html?" + par + ((layout.debugger) ? "&debugger" : "");
  229. } else {
  230. return window.open("app.html?" + par + ((layout.debugger) ? "&debugger" : ""), par);
  231. }
  232. }
  233. } else {
  234. var appPath = appNames.split(".");
  235. var appName = appPath[appPath.length - 1];
  236. _requireApp(appNames, function (appNamespace) {
  237. var appId = (options && options.appId) ? options.appId : ((appNamespace.options.multitask) ? "" : appName);
  238. if (appId && layout.desktop.apps && layout.desktop.apps[appId] && layout.desktop.apps[appId].window){
  239. layout.desktop.apps[appId].setCurrent();
  240. }else{
  241. if (options) options.appId = appId;
  242. _createNewApplication(e, appNamespace, appName, (options || {"appId": appId}), statusObj, inBrowser, taskitem, notCurrent);
  243. }
  244. // if (appNamespace.options.multitask){
  245. // if (options && options.appId){
  246. // if (layout.desktop.apps[options.appId]){
  247. // layout.desktop.apps[options.appId].setCurrent();
  248. // }else {
  249. // _createNewApplication(e, appNamespace, appName, options, obj, inBrowser);
  250. // }
  251. // }else{
  252. // _createNewApplication(e, appNamespace, appName, options, obj, inBrowser);
  253. // }
  254. // }else{
  255. // if (layout.desktop.apps[appName]){
  256. // layout.desktop.apps[appName].setCurrent();
  257. // }else{
  258. // _createNewApplication(e, appNamespace, appName, options, obj, inBrowser);
  259. // }
  260. // }
  261. // _createNewApplication(e, appNamespace, appName, (options || {}), statusObj, inBrowser);
  262. }.bind(this));
  263. }
  264. };
  265. layout.refreshApp = function (app) {
  266. var status = app.recordStatus();
  267. var uri = new URI(window.location.href);
  268. var appNames = uri.getData("app");
  269. var optionsStr = uri.getData("option");
  270. var statusStr = uri.getData("status");
  271. if (status) statusStr = JSON.encode(status);
  272. var port = uri.get("port");
  273. window.location = uri.get("scheme") + "://" + uri.get("host") + ((port) ? ":" + port + "/" : "") + uri.get("directory ") + "?app=" + encodeURIComponent(appNames) + "&status=" + encodeURIComponent(statusStr) + "&option=" + encodeURIComponent((options) ? JSON.encode(options) : "") + ((layout.debugger) ? "&debugger" : "");
  274. };
  275. layout.load = function (appNames, options, statusObj) {
  276. // layout.message = new o2.xDesktop.MessageMobile();
  277. // layout.message.load();
  278. layout.apps = [];
  279. layout.node = $("layout");
  280. var appName = appNames, m_status = statusObj, option = options;
  281. var topWindow = window.opener;
  282. if (topWindow) {
  283. try {
  284. if (!appName) appName = topWindow.layout.desktop.openBrowserApp;
  285. if (!m_status) m_status = topWindow.layout.desktop.openBrowserStatus;
  286. if (!option) option = topWindow.layout.desktop.openBrowserOption;
  287. } catch (e) { }
  288. }
  289. layout.openApplication(null, appName, option || {}, m_status);
  290. };
  291. })(layout);
  292. o2.addReady(function () {
  293. // layout.desktop = new (new Class({Implements: [Options, Events]}))();
  294. // layout.desktop.openApplication = layout.openApplication;
  295. // layout.desktop.refreshApp = layout.refreshApp;
  296. // layout.desktop.load = layout.load;
  297. //兼容方法
  298. Element.implement({
  299. "makeLnk": function (options) { }
  300. });
  301. layout.desktop.addEvent = function(type, e, d){
  302. window.addEvent(type, e, d);
  303. };
  304. layout.desktop.addEvents = function(e){
  305. window.addEvents(e);
  306. };
  307. var loadingNode = $("loaddingArea");
  308. var loadeds = 0;
  309. var loadCount = 16;
  310. var size = document.body.getSize();
  311. var _closeLoadingNode = function () {
  312. if (loadingNode) {
  313. loadingNode.destroy();
  314. loadingNode = null;
  315. }
  316. };
  317. var _loadProgressBar = function (complete) {
  318. if (loadingNode) {
  319. if (complete) {
  320. loadingNode.setStyles({ "width": "" + size.x + "px" });
  321. //loadingNode.set('morph', {duration: 100}).morph({"width": ""+size.x+"px"});
  322. window.setTimeout(_closeLoadingNode, 500);
  323. } else {
  324. loadeds++;
  325. var p = (loadeds / loadCount) * size.x;
  326. loadingNode.setStyles({ "width": "" + p + "px" });
  327. //loadingNode.set('morph', {duration: 100}).morph({"width": ""+p+"px"});
  328. if (loadeds >= loadCount) window.setTimeout(_closeLoadingNode, 500);
  329. }
  330. }
  331. };
  332. //异步载入必要模块
  333. layout.config = null;
  334. var configLoaded = false;
  335. var lpLoaded = false;
  336. var commonLoaded = false;
  337. var lp = o2.session.path + "/lp/" + o2.language + ".js";
  338. o2.load(lp, function () {
  339. _loadProgressBar();
  340. lpLoaded = true;
  341. if (configLoaded && commonLoaded && lpLoaded) _getDistribute(function () { _load(); });
  342. });
  343. var modules = ["o2.xDesktop.Common", "o2.xDesktop.Actions.RestActions", "o2.xAction.RestActions"];
  344. o2.require(modules, {
  345. "onSuccess": function () {
  346. commonLoaded = true;
  347. if (configLoaded && commonLoaded && lpLoaded) _getDistribute(function () { _load(); });
  348. },
  349. "onEvery": function () {
  350. _loadProgressBar();
  351. }
  352. });
  353. o2.getJSON("/x_desktop/res/config/config.json", function (config) {
  354. _loadProgressBar();
  355. layout.config = config;
  356. configLoaded = true
  357. if (configLoaded && commonLoaded && lpLoaded) _getDistribute(function () { _load(); });
  358. });
  359. var _getDistribute = function (callback) {
  360. if (layout.config.app_protocol === "auto") {
  361. layout.config.app_protocol = window.location.protocol;
  362. }
  363. o2.xDesktop.getServiceAddress(layout.config, function (service, center) {
  364. layout.serviceAddressList = service;
  365. layout.centerServer = center;
  366. layout.desktop.serviceAddressList = service;
  367. layout.desktop.centerServer = center;
  368. _loadProgressBar();
  369. if (callback) callback();
  370. }.bind(this));
  371. };
  372. var _load = function () {
  373. var _loadApp = function (json) {
  374. //用户已经登录
  375. layout.user = json.data;
  376. //layout.session = {};
  377. layout.session.user = json.data;
  378. layout.session.token = json.data.token;
  379. layout.desktop.session = layout.session;
  380. (function (layout) {
  381. var _loadResource = function (callback) {
  382. var isLoadedA = false;
  383. var isLoadedB = false;
  384. //var isLoadedC = false;
  385. var modules = [
  386. "o2.xDesktop.Dialog",
  387. "o2.xDesktop.UserData",
  388. "o2.xDesktop.Access",
  389. "o2.widget.UUID",
  390. "o2.xDesktop.Menu",
  391. "o2.xDesktop.Authentication",
  392. // "o2.xDesktop.shortcut",
  393. "o2.widget.PinYin",
  394. "o2.xDesktop.Access"
  395. // "o2.xDesktop.MessageMobile"
  396. ];
  397. //o2.xDesktop.requireApp("Common", "", null, false);
  398. var _check = function () { if (isLoadedA && isLoadedB) if (callback) callback(); };
  399. o2.load(["/o2_lib/mootools/plugin/mBox.min.js"], function () { _loadProgressBar(); isLoadedA = true; _check(); });
  400. o2.require("o2.widget.Common", function () {
  401. _loadProgressBar();
  402. o2.require(modules, {
  403. "onSuccess": function () {
  404. o2.requireApp("Common", "", function () { _loadProgressBar(); isLoadedB = true; _check(); })
  405. },
  406. "onEvery": function () {
  407. _loadProgressBar();
  408. }
  409. });
  410. });
  411. };
  412. var _loadContent = function () {
  413. _loadResource(function () {
  414. _loadProgressBar(true);
  415. while (layout.readys && layout.readys.length) {
  416. layout.readys.shift().apply(window);
  417. }
  418. });
  419. };
  420. _loadContent();
  421. })(layout);
  422. };
  423. //先判断用户是否登录
  424. o2.Actions.get("x_organization_assemble_authentication").getAuthentication(function (json) {
  425. //已经登录
  426. _loadProgressBar();
  427. _loadApp(json);
  428. }.bind(this), function (json) {
  429. _loadProgressBar();
  430. //允许匿名访问
  431. if (layout.anonymous) {
  432. _loadProgressBar(true);
  433. _loadApp({
  434. user: "anonymous",
  435. session: {
  436. user: {
  437. name: "anonymous",
  438. roleList: []
  439. }
  440. }
  441. });
  442. } else {
  443. //用户未经登录
  444. //打开登录页面
  445. var _loadResource = function (callback) {
  446. var isLoadedA = false;
  447. var isLoadedB = false;
  448. //var isLoadedC = false;
  449. //var lp = o2.session.path+"/lp/"+o2.language+".js";
  450. var modules = [
  451. "o2.xDesktop.Dialog",
  452. "o2.xDesktop.UserData",
  453. "o2.xDesktop.Access",
  454. "o2.widget.UUID",
  455. "o2.xDesktop.Menu",
  456. //"o2.xDesktop.shortcut",
  457. "o2.widget.PinYin",
  458. "o2.xDesktop.Access",
  459. //"o2.xDesktop.MessageMobile"
  460. ];
  461. //o2.xDesktop.requireApp("Common", "", null, false);
  462. var _check = function () { if (isLoadedA && isLoadedB) if (callback) callback(); };
  463. o2.load(["/o2_lib/mootools/plugin/mBox.min.js"], function () { _loadProgressBar(); isLoadedA = true; _check(); });
  464. o2.require("o2.widget.Common", function () {
  465. _loadProgressBar();
  466. o2.require(modules, {
  467. "onSuccess": function () {
  468. o2.requireApp("Common", "", function () { isLoadedB = true; _check(); })
  469. },
  470. "onEvery": function () {
  471. _loadProgressBar();
  472. }
  473. });
  474. });
  475. };
  476. _loadResource(function () {
  477. _loadProgressBar(true);
  478. layout.openLogin();
  479. });
  480. }
  481. });
  482. layout.openLogin = function () {
  483. o2.require("o2.widget.Common", null, false);
  484. o2.require("o2.xDesktop.Authentication", function () {
  485. layout.authentication = new o2.xDesktop.Authentication({
  486. "style": "flat",
  487. "onLogin": _load.bind(layout)
  488. });
  489. layout.authentication.loadLogin(document.body);
  490. var loadingNode = $("browser_loading");
  491. if (loadingNode) loadingNode.fade("out");
  492. });
  493. };
  494. };
  495. });