base.js 29.3 KB
Newer Older
NoSubject's avatar
NoSubject 已提交
1 2
layout = window.layout || {};
layout.desktop = layout;
NoSubject's avatar
NoSubject 已提交
3
layout.desktop.type = "app";
NoSubject's avatar
NoSubject 已提交
4 5
var locate = window.location;
layout.protocol = locate.protocol;
R
roo00 已提交
6
layout.inBrowser = true;
NoSubject's avatar
NoSubject 已提交
7
layout.session = layout.session || {};
R
roo00 已提交
8 9
layout.debugger = (locate.href.toString().indexOf("debugger") !== -1);
layout.anonymous = (locate.href.toString().indexOf("anonymous") !== -1);
NoSubject's avatar
NoSubject 已提交
10 11 12
o2.xApplication = o2.xApplication || {};

o2.xDesktop = o2.xDesktop || {};
R
roo00 已提交
13
o2.xDesktop.requireApp = function (module, clazz, callback, async) {
NoSubject's avatar
NoSubject 已提交
14 15 16
    o2.requireApp(module, clazz, callback, async);
};

R
roo00 已提交
17
(function (layout) {
NoSubject's avatar
NoSubject 已提交
18
    layout.readys = [];
R
roo00 已提交
19 20 21
    layout.addReady = function () {
        for (var i = 0; i < arguments.length; i++) {
            if (o2.typeOf(arguments[i]) === "function") layout.readys.push(arguments[i]);
NoSubject's avatar
NoSubject 已提交
22 23
        }
    };
R
roo00 已提交
24
    var _requireApp = function (appNames, callback, clazzName) {
NoSubject's avatar
NoSubject 已提交
25 26
        var appPath = appNames.split(".");
        var baseObject = o2.xApplication;
R
roo00 已提交
27 28
        appPath.each(function (path, i) {
            if (i < (appPath.length - 1)) {
NoSubject's avatar
NoSubject 已提交
29
                baseObject[path] = baseObject[path] || {};
R
roo00 已提交
30
            } else {
NoSubject's avatar
NoSubject 已提交
31
                baseObject[path] = baseObject[path] || { "options": Object.clone(o2.xApplication.Common.options) };
NoSubject's avatar
NoSubject 已提交
32 33 34
            }
            baseObject = baseObject[path];
        }.bind(this));
NoSubject's avatar
NoSubject 已提交
35
        if (!baseObject.options) baseObject.options = Object.clone(o2.xApplication.Common.options);
NoSubject's avatar
NoSubject 已提交
36 37

        var _lpLoaded = false;
NoSubject's avatar
NoSubject 已提交
38

NoSubject's avatar
NoSubject 已提交
39
        o2.xDesktop.requireApp(appNames, "lp." + o2.language, {
NoSubject's avatar
NoSubject 已提交
40
            "onFailure": function () {
NoSubject's avatar
NoSubject 已提交
41
                o2.xDesktop.requireApp(appNames, "lp.zh-cn", null, false);
NoSubject's avatar
NoSubject 已提交
42 43
            }.bind(this)
        }, false);
R
roo00 已提交
44

NoSubject's avatar
NoSubject 已提交
45
        o2.xDesktop.requireApp(appNames, clazzName, function () {
NoSubject's avatar
NoSubject 已提交
46 47 48
            if (callback) callback(baseObject);
        });
    };
NoSubject's avatar
NoSubject 已提交
49
    var _createNewApplication = function (e, appNamespace, appName, options, statusObj, inBrowser, taskitem, notCurrent) {
F
fancy 已提交
50
        if (options) { options.event = e; } else { options = { "event": e }; }
NoSubject's avatar
NoSubject 已提交
51 52
        var app = new appNamespace["Main"](layout.desktop, options);
        app.desktop = layout.desktop;
NoSubject's avatar
NoSubject 已提交
53
        app.status = statusObj;
NoSubject's avatar
NoSubject 已提交
54 55
        app.inBrowser = !!(inBrowser || layout.inBrowser);

F
fancy 已提交
56
        if (layout.desktop.type === "layout") {
NoSubject's avatar
NoSubject 已提交
57 58 59 60 61 62 63 64 65 66 67
            app.appId = (options.appId) ? options.appId : ((appNamespace.options.multitask) ? appName + "-" + (new o2.widget.UUID()) : appName);
            app.options.appId = app.appId;

            if (!taskitem) taskitem = layout.desktop.createTaskItem(app);
            app.taskitem = taskitem;
            app.taskitem.app = app;

            app.isLoadApplication = true;
            app.load(!notCurrent);

            if (!layout.desktop.apps) layout.desktop.apps = {};
F
fancy 已提交
68
            if (layout.desktop.apps[app.appId]) {
NoSubject's avatar
NoSubject 已提交
69 70
                var tmpApp = layout.desktop.apps[app.appId];

F
fancy 已提交
71
            } else {
NoSubject's avatar
NoSubject 已提交
72 73
                layout.desktop.apps[app.appId] = app;
            }
R
roo00 已提交
74

NoSubject's avatar
NoSubject 已提交
75 76 77 78 79
            layout.desktop.appArr.push(app);
            layout.desktop.appCurrentList.push(app);
            if (!notCurrent) layout.desktop.currentApp = app;

            //app.taskitem = new MWF.xDesktop.Layout.Taskitem(app, this);
F
fancy 已提交
80
        } else {
NoSubject's avatar
NoSubject 已提交
81 82
            app.load(true);
            layout.app = app;
NoSubject's avatar
NoSubject 已提交
83
        }
NoSubject's avatar
NoSubject 已提交
84 85


NoSubject's avatar
NoSubject 已提交
86 87 88 89 90

        var mask = document.getElementById("appContentMask");
        if (mask) mask.destroy();
    };

R
roo00 已提交
91
    var _openWorkAndroid = function (options) {
NoSubject's avatar
NoSubject 已提交
92 93
        if (window.o2android && window.o2android.openO2Work) {
            if (options.workId) {
NoSubject's avatar
NoSubject 已提交
94
                window.o2android.openO2Work(options.workId, "", options.title || options.docTitle || "");
NoSubject's avatar
NoSubject 已提交
95
            } else if (options.workCompletedId) {
NoSubject's avatar
NoSubject 已提交
96
                window.o2android.openO2Work("", options.workCompletedId, options.title || options.docTitle || "");
NoSubject's avatar
NoSubject 已提交
97 98 99 100 101
            }
            return true;
        }
        return false;
    };
R
roo00 已提交
102
    var _openWorkIOS = function (options) {
NoSubject's avatar
NoSubject 已提交
103 104 105 106 107
        if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.openO2Work) {
            if (options.workId) {
                window.webkit.messageHandlers.openO2Work.postMessage({
                    "work": options.workId,
                    "workCompleted": "",
NoSubject's avatar
NoSubject 已提交
108
                    "title": options.title || options.docTitle || ""
NoSubject's avatar
NoSubject 已提交
109 110 111 112 113
                });
            } else if (options.workCompletedId) {
                window.webkit.messageHandlers.openO2Work.postMessage({
                    "work": "",
                    "workCompleted": options.workCompletedId,
NoSubject's avatar
NoSubject 已提交
114
                    "title": options.title || options.docTitle || ""
NoSubject's avatar
NoSubject 已提交
115 116 117 118 119 120
                });
            }
            return true;
        }
        return false;
    };
R
roo00 已提交
121
    var _openWorkHTML = function (options) {
NoSubject's avatar
NoSubject 已提交
122 123 124 125 126 127 128 129
        var uri = new URI(window.location.href);
        var redirectlink = uri.getData("redirectlink");
        if (!redirectlink) {
            redirectlink = encodeURIComponent(locate.pathname + locate.search);
        } else {
            redirectlink = encodeURIComponent(redirectlink);
        }
        if (options.workId) {
130
            window.location = o2.filterUrl("../x_desktop/workmobilewithaction.html?workid=" + options.workId + ((layout.debugger) ? "&debugger" : "") + "&redirectlink=" + redirectlink);
NoSubject's avatar
NoSubject 已提交
131
        } else if (options.workCompletedId) {
132
            window.location = o2.filterUrl("../x_desktop/workmobilewithaction.html?workcompletedid=" + options.workCompletedId + ((layout.debugger) ? "&debugger" : "") + "&redirectlink=" + redirectlink);
NoSubject's avatar
NoSubject 已提交
133 134
        }
    };
R
roo00 已提交
135
    var _openWork = function (options) {
NoSubject's avatar
NoSubject 已提交
136 137
        if (!_openWorkAndroid(options)) if (!_openWorkIOS(options)) _openWorkHTML(options);
    };
R
roo00 已提交
138 139
    var _openDocument = function (appNames, options, statusObj) {
        var title = typeOf(options) === "object" ? (options.docTitle || options.title) : "";
NoSubject's avatar
NoSubject 已提交
140
        title = title || "";
R
roo00 已提交
141 142
        var par = "app=" + encodeURIComponent(appNames) + "&status=" + encodeURIComponent((statusObj) ? JSON.encode(statusObj) : "") + "&option=" + encodeURIComponent((options) ? JSON.encode(options) : "");
        if (window.o2android && window.o2android.openO2CmsDocument) {
NoSubject's avatar
NoSubject 已提交
143
            window.o2android.openO2CmsDocument(options.documentId, title);
R
roo00 已提交
144 145 146
        } else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.openO2CmsDocument) {
            window.webkit.messageHandlers.openO2CmsDocument.postMessage({ "docId": options.documentId, "docTitle": title });
        } else {
147
            window.location = o2.filterUrl("../x_desktop/appMobile.html?" + par + ((layout.debugger) ? "&debugger" : ""));
NoSubject's avatar
NoSubject 已提交
148 149
        }
    };
R
roo00 已提交
150 151 152
    var _openCms = function (appNames, options, statusObj) {
        var par = "app=" + encodeURIComponent(appNames) + "&status=" + encodeURIComponent((statusObj) ? JSON.encode(statusObj) : "") + "&option=" + encodeURIComponent((options) ? JSON.encode(options) : "");
        if (window.o2android && window.o2android.openO2CmsApplication) {
NoSubject's avatar
NoSubject 已提交
153
            window.o2android.openO2CmsApplication(options.columnId, options.title || "");
R
roo00 已提交
154
        } else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.openO2CmsApplication) {
NoSubject's avatar
NoSubject 已提交
155
            window.webkit.messageHandlers.openO2CmsApplication.postMessage(options.columnId);
R
roo00 已提交
156
        } else {
157
            window.location = o2.filterUrl("../x_desktop/appMobile.html?" + par + ((layout.debugger) ? "&debugger" : ""));
NoSubject's avatar
NoSubject 已提交
158 159
        }
    };
R
roo00 已提交
160 161 162
    var _openMeeting = function (appNames, options, statusObj) {
        var par = "app=" + encodeURIComponent(appNames) + "&status=" + encodeURIComponent((statusObj) ? JSON.encode(statusObj) : "") + "&option=" + encodeURIComponent((options) ? JSON.encode(options) : "");
        if (window.o2android && window.o2android.openO2Meeting) {
NoSubject's avatar
NoSubject 已提交
163
            window.o2android.openO2Meeting("");
R
roo00 已提交
164
        } else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.openO2Meeting) {
NoSubject's avatar
NoSubject 已提交
165
            window.webkit.messageHandlers.openO2Meeting.postMessage("");
R
roo00 已提交
166
        } else {
167
            window.location = o2.filterUrl("../x_desktop/appMobile.html?" + par + ((layout.debugger) ? "&debugger" : ""));
NoSubject's avatar
NoSubject 已提交
168 169 170
        }
    };

R
roo00 已提交
171 172 173
    var _openCalendar = function (appNames, options, statusObj) {
        var par = "app=" + encodeURIComponent(appNames) + "&status=" + encodeURIComponent((statusObj) ? JSON.encode(statusObj) : "") + "&option=" + encodeURIComponent((options) ? JSON.encode(options) : "");
        if (window.o2android && window.o2android.openO2Calendar) {
NoSubject's avatar
NoSubject 已提交
174
            window.o2android.openO2Calendar("");
R
roo00 已提交
175
        } else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.openO2Calendar) {
NoSubject's avatar
NoSubject 已提交
176
            window.webkit.messageHandlers.openO2Calendar.postMessage("");
R
roo00 已提交
177
        } else {
178
            window.location = o2.filterUrl("../x_desktop/appMobile.html?" + par + ((layout.debugger) ? "&debugger" : ""));
NoSubject's avatar
NoSubject 已提交
179 180
        }
    };
R
roo00 已提交
181 182
    var _openTaskCenter = function (appNames, options, statusObj) {
        var par = "app=" + encodeURIComponent(appNames) + "&status=" + encodeURIComponent((statusObj) ? JSON.encode(statusObj) : "") + "&option=" + encodeURIComponent((options) ? JSON.encode(options) : "");
NoSubject's avatar
NoSubject 已提交
183
        var tab = ((options && options.navi) ? options.navi : "task").toLowerCase();
R
roo00 已提交
184 185
        if (tab === "done") tab = "taskCompleted";
        if (tab === "readed") tab = "readCompleted";
NoSubject's avatar
NoSubject 已提交
186

R
roo00 已提交
187
        if (window.o2android && window.o2android.openO2WorkSpace) {
NoSubject's avatar
NoSubject 已提交
188
            window.o2android.openO2WorkSpace(tab);
R
roo00 已提交
189
        } else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.openO2WorkSpace) {
NoSubject's avatar
NoSubject 已提交
190
            window.webkit.messageHandlers.openO2WorkSpace.postMessage(tab);
R
roo00 已提交
191
        } else {
192
            window.location = o2.filterUrl("../x_desktop/appMobile.html?" + par + ((layout.debugger) ? "&debugger" : ""));
NoSubject's avatar
NoSubject 已提交
193 194 195
        }
    };

R
roo00 已提交
196
    var _openApplicationMobile = function (appNames, options, statusObj) {
NoSubject's avatar
NoSubject 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
        switch (appNames) {
            case "process.Work":
                _openWork(options);
                break;
            case "cms.Document":
                _openDocument(appNames, options, statusObj);
                break;
            case "cms.Module":
                _openCms(appNames, options, statusObj);
                break;
            case "Meeting":
                _openMeeting(appNames, options, statusObj);
                break;
            case "Calendar":
                _openCalendar(appNames, options, statusObj);
                break;
            case "process.TaskCenter":
                _openTaskCenter(appNames, options, statusObj);
                break;
            default:
R
roo00 已提交
217 218 219
                var uri = new URI(window.location.href);
                var optionsStr = uri.getData("option");
                var statusStr = uri.getData("status");
220
                window.location = o2.filterUrl("../x_desktop/appMobile.html?app=" + appNames + "&option=" + (optionsStr || "") + "&status=" + (statusStr || "") + ((layout.debugger) ? "&debugger" : ""));
NoSubject's avatar
NoSubject 已提交
221
        }
NoSubject's avatar
NoSubject 已提交
222 223
    };

NoSubject's avatar
NoSubject 已提交
224
    var _openApplicationPC = function (appNames, options, statusObj) {
NoSubject's avatar
NoSubject 已提交
225
        delete options.docTitle;
NoSubject's avatar
NoSubject 已提交
226 227 228
        var par = "app=" + encodeURIComponent(appNames) + "&status=" + encodeURIComponent((statusObj) ? JSON.encode(statusObj) : "") + "&option=" + encodeURIComponent((options) ? JSON.encode(options) : "");
        switch (appNames) {
            case "process.Work":
229 230
                var url = "../x_desktop/work.html".toURI();
                if (options.draft){
231
                    url = "../x_desktop/app.html?" + par;
232 233
                }else{
                    url = url.setData(options).toString();
NoSubject's avatar
NoSubject 已提交
234
                }
235 236 237 238 239 240 241 242 243 244 245 246

                // if (options.workId) {
                //     url += "?workid=" + options.workId+"&"+par;
                //     //window.location = o2.filterUrl("../x_desktop/workmobilewithaction.html?workid=" + options.workId + ((layout.debugger) ? "&debugger" : "") + "&redirectlink=" + redirectlink);
                // } else if (options.workCompletedId) {
                //     url += "?workcompletedid=" + options.workCompletedId+"&"+par;;
                //     //window.location = o2.filterUrl("../x_desktop/workmobilewithaction.html?workcompletedid=" + options.workCompletedId + ((layout.debugger) ? "&debugger" : "") + "&redirectlink=" + redirectlink);
                // } else if (options.draftId){
                //     url += "?draftId=" + options.draftId+"&"+par;;
                // } else if (options.draft) {
                //     url = "../x_desktop/app.html?" + par;
                // }
NoSubject's avatar
NoSubject 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
                var job = (options.jobid || options.jobId || options.job);
                if (job) url += ((url.indexOf("?")!=-1) ? "&" : "?") + "jobid="+job;
                url +=((layout.debugger) ? "&debugger" : "");

                if (layout.app.$openWithSelf) {
                    return window.location = o2.filterUrl(url);
                } else {
                    return window.open(o2.filterUrl(url), par);
                }
                break;
            // case "cms.Document":
            //     _openDocument(appNames, options, statusObj);
            //     break;
            // case "cms.Module":
            //     _openCms(appNames, options, statusObj);
            //     break;
            // case "Meeting":
            //     _openMeeting(appNames, options, statusObj);
            //     break;
            // case "Calendar":
            //     _openCalendar(appNames, options, statusObj);
            //     break;
            // case "process.TaskCenter":
            //     _openTaskCenter(appNames, options, statusObj);
            //     break;
            default:
                //var par = "app=" + encodeURIComponent(appNames) + "&status=" + encodeURIComponent((statusObj) ? JSON.encode(statusObj) : "") + "&option=" + encodeURIComponent((options) ? JSON.encode(options) : "");
                if (layout.app.$openWithSelf) {
                    return window.location = o2.filterUrl("../x_desktop/app.html?" + par + ((layout.debugger) ? "&debugger" : ""));
                } else {
                    return window.open(o2.filterUrl("../x_desktop/app.html?" + par + ((layout.debugger) ? "&debugger" : "")), par);
                }
        }
    };

NoSubject's avatar
NoSubject 已提交
282
    layout.openApplication = function (e, appNames, options, statusObj, inBrowser, taskitem, notCurrent) {
F
fancy 已提交
283
        if (appNames.substring(0, 4) === "@url") {
NoSubject's avatar
NoSubject 已提交
284
            var url = appNames.replace(/\@url\:/i, "");
F
fancy 已提交
285
            var a = new Element("a", { "href": url, "target": "_blank" });
NoSubject's avatar
NoSubject 已提交
286 287 288 289 290 291
            a.click();
            a.destroy();
            a = null;
            return true;
        }

R
roo00 已提交
292 293
        if (layout.app) {
            if (layout.mobile) {
NoSubject's avatar
NoSubject 已提交
294
                _openApplicationMobile(appNames, options, statusObj);
R
roo00 已提交
295
            } else {
NoSubject's avatar
NoSubject 已提交
296
                return _openApplicationPC(appNames, options, statusObj);
NoSubject's avatar
NoSubject 已提交
297 298 299 300 301 302 303
                // var par = "app=" + encodeURIComponent(appNames) + "&status=" + encodeURIComponent((statusObj) ? JSON.encode(statusObj) : "") + "&option=" + encodeURIComponent((options) ? JSON.encode(options) : "");
                //
                // if (layout.app.$openWithSelf) {
                //     return window.location = o2.filterUrl("../x_desktop/app.html?" + par + ((layout.debugger) ? "&debugger" : ""));
                // } else {
                //     return window.open(o2.filterUrl("../x_desktop/app.html?" + par + ((layout.debugger) ? "&debugger" : "")), par);
                // }
NoSubject's avatar
NoSubject 已提交
304
            }
R
roo00 已提交
305
        } else {
NoSubject's avatar
NoSubject 已提交
306
            var appPath = appNames.split(".");
R
roo00 已提交
307 308
            var appName = appPath[appPath.length - 1];
            _requireApp(appNames, function (appNamespace) {
NoSubject's avatar
NoSubject 已提交
309 310
                var appId = (options && options.appId) ? options.appId : ((appNamespace.options.multitask) ? "" : appName);

311
                //if (appId && layout.desktop.apps && layout.desktop.apps[appId] && layout.desktop.apps[appId].window){
F
fancy 已提交
312
                if (appId && layout.desktop.apps && layout.desktop.apps[appId]) {
NoSubject's avatar
NoSubject 已提交
313
                    layout.desktop.apps[appId].setCurrent();
F
fancy 已提交
314
                } else {
NoSubject's avatar
NoSubject 已提交
315
                    if (options) options.appId = appId;
F
fancy 已提交
316
                    _createNewApplication(e, appNamespace, appName, (options || { "appId": appId }), statusObj, inBrowser, taskitem, notCurrent);
NoSubject's avatar
NoSubject 已提交
317
                }
NoSubject's avatar
NoSubject 已提交
318 319 320 321
            }.bind(this));
        }
    };

R
roo00 已提交
322
    layout.refreshApp = function (app) {
NoSubject's avatar
NoSubject 已提交
323 324 325 326 327 328 329 330 331
        var status = app.recordStatus();

        var uri = new URI(window.location.href);
        var appNames = uri.getData("app");
        var optionsStr = uri.getData("option");
        var statusStr = uri.getData("status");
        if (status) statusStr = JSON.encode(status);

        var port = uri.get("port");
U
unknown 已提交
332
        window.location = o2.filterUrl(uri.get("scheme") + "://" + uri.get("host") + ((port) ? ":" + port + "/" : "") + uri.get("directory ") + "?app=" + encodeURIComponent(appNames) + "&status=" + encodeURIComponent(statusStr) + "&option=" + encodeURIComponent((optionsStr) ? JSON.encode(optionsStr) : "") + ((layout.debugger) ? "&debugger" : ""));
NoSubject's avatar
NoSubject 已提交
333 334
    };

R
roo00 已提交
335
    layout.load = function (appNames, options, statusObj) {
NoSubject's avatar
NoSubject 已提交
336 337
        // layout.message = new o2.xDesktop.MessageMobile();
        // layout.message.load();
NoSubject's avatar
NoSubject 已提交
338 339 340

        layout.apps = [];
        layout.node = $("layout");
R
roo00 已提交
341
        var appName = appNames, m_status = statusObj, option = options;
NoSubject's avatar
NoSubject 已提交
342 343

        var topWindow = window.opener;
R
roo00 已提交
344 345
        if (topWindow) {
            try {
NoSubject's avatar
NoSubject 已提交
346 347
                if (!appName) appName = topWindow.layout.desktop.openBrowserApp;
                if (!m_status) m_status = topWindow.layout.desktop.openBrowserStatus;
R
roo00 已提交
348 349
                if (!option) option = topWindow.layout.desktop.openBrowserOption;
            } catch (e) { }
NoSubject's avatar
NoSubject 已提交
350
        }
R
roo00 已提交
351
        layout.openApplication(null, appName, option || {}, m_status);
NoSubject's avatar
NoSubject 已提交
352
    };
NoSubject's avatar
NoSubject 已提交
353

354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
    // layout.getAppUrlMapping = function(url){
    //     return this.getUrlMapping(url, "app");
    // };
    // layout.getCenterUrlMapping = function(url){
    //     return this.getUrlMapping(url,"center");
    // };
    // layout.getWebUrlMapping = function(url){
    //     return this.getUrlMapping(url, "web");
    // };
    // layout.getUrlMapping = function(url, type){
    //     var urlContextMapping = layout.config.urlMapping;
    //     if (urlContextMapping && urlContextMapping.app){
    //         var href = url || window.location.href.toString();
    //
    //         for (var k in urlContextMapping[type]){
    //             var regex = new RegExp(k);
    //             if (regex.test(href)){
    //                 return urlContextMapping[type][k];
    //             }
    //         }
    //     }
    //     return "";
    // };

R
roo00 已提交
378
})(layout);
NoSubject's avatar
NoSubject 已提交
379 380 381 382 383 384 385 386 387 388 389

o2.addReady(function () {
    // layout.desktop = new (new Class({Implements: [Options, Events]}))();
    // layout.desktop.openApplication = layout.openApplication;
    // layout.desktop.refreshApp = layout.refreshApp;
    // layout.desktop.load = layout.load;

    //兼容方法
    Element.implement({
        "makeLnk": function (options) { }
    });
F
fancy 已提交
390
    layout.desktop.addEvent = function (type, e, d) {
NoSubject's avatar
NoSubject 已提交
391 392
        window.addEvent(type, e, d);
    };
F
fancy 已提交
393
    layout.desktop.addEvents = function (e) {
NoSubject's avatar
NoSubject 已提交
394 395 396 397 398
        window.addEvents(e);
    };

    var loadingNode = $("loaddingArea");
    var loadeds = 0;
399
    var loadCount = 4;
NoSubject's avatar
NoSubject 已提交
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
    var size = document.body.getSize();
    var _closeLoadingNode = function () {
        if (loadingNode) {
            loadingNode.destroy();
            loadingNode = null;
        }
    };
    var _loadProgressBar = function (complete) {
        if (loadingNode) {
            if (complete) {
                loadingNode.setStyles({ "width": "" + size.x + "px" });
                //loadingNode.set('morph', {duration: 100}).morph({"width": ""+size.x+"px"});
                window.setTimeout(_closeLoadingNode, 500);
            } else {
                loadeds++;
                var p = (loadeds / loadCount) * size.x;
                loadingNode.setStyles({ "width": "" + p + "px" });
                //loadingNode.set('morph', {duration: 100}).morph({"width": ""+p+"px"});
                if (loadeds >= loadCount) window.setTimeout(_closeLoadingNode, 500);
            }
        }
    };

NoSubject's avatar
NoSubject 已提交
423 424 425 426 427 428
    var _setLayoutService = function(service, center){
        layout.serviceAddressList = service;
        layout.centerServer = center;
        layout.desktop.serviceAddressList = service;
        layout.desktop.centerServer = center;
    };
NoSubject's avatar
NoSubject 已提交
429
    var _getDistribute = function (callback) {
NoSubject's avatar
NoSubject 已提交
430
        debugger;
NoSubject's avatar
NoSubject 已提交
431 432 433
        if (layout.config.app_protocol === "auto") {
            layout.config.app_protocol = window.location.protocol;
        }
NoSubject's avatar
NoSubject 已提交
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459

        if (layout.config.configMapping && layout.config.configMapping[window.location.host]){
            var mapping = layout.config.configMapping[window.location.host];
            if (mapping.servers){
                layout.serviceAddressList = mapping.servers;
                layout.desktop.serviceAddressList = mapping.servers;
                if (mapping.center) center = (o2.typeOf(mapping.center)==="array") ? mapping.center[0] : mapping.center;
                layout.centerServer = center;
                layout.desktop.centerServer = center;
                if (callback) callback();
            }else{
                if (mapping.center) layout.config.center = (o2.typeOf(mapping.center)==="array") ? mapping.center : [mapping.center];
                o2.xDesktop.getServiceAddress(layout.config, function (service, center) {
                    _setLayoutService(service, center);
                    _loadProgressBar();
                    if (callback) callback();
                }.bind(this));
            }
        }else{
            o2.xDesktop.getServiceAddress(layout.config, function (service, center) {
                _setLayoutService(service, center);
                _loadProgressBar();
                if (callback) callback();
            }.bind(this));
        }

NoSubject's avatar
NoSubject 已提交
460 461 462 463 464
    };

    var _load = function () {
        var _loadApp = function (json) {
            //用户已经登录
NoSubject's avatar
NoSubject 已提交
465 466 467 468 469 470 471
            if (json){
                layout.user = json.data;
                layout.session = layout.session || {};
                layout.session.user = json.data;
                layout.session.token = json.data.token;
                layout.desktop.session = layout.session;
            }
NoSubject's avatar
NoSubject 已提交
472

NoSubject's avatar
NoSubject 已提交
473 474
            _loadProgressBar(true);
            while (layout.readys && layout.readys.length) {
NoSubject's avatar
NoSubject 已提交
475
                console.log("load app ...")
NoSubject's avatar
NoSubject 已提交
476 477
                layout.readys.shift().apply(window);
            }
NoSubject's avatar
NoSubject 已提交
478
        };
S
st 已提交
479 480 481 482

        //修改支持x-token
        var uri = new URI(window.location.href);
        var options = uri.get("data");
F
fancy 已提交
483
        if (options["x-token"]) {
S
st 已提交
484 485 486
            Cookie.write("x-token", options["x-token"]);
        }

NoSubject's avatar
NoSubject 已提交
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
        layout.sessionPromise = {
            "resolveList": [],
            "rejectList": [],
            "init": function(resolve, reject){
                if (resolve) this.resolveList.push(resolve);
                if (reject) this.rejectList.push(reject);
                this.status = "pending";
                this.resolveReturn = this;

                //先判断用户是否登录
                o2.Actions.get("x_organization_assemble_authentication").getAuthentication(function (json) {
                    this.status = "fulfilled";
                    this.resolveReturn = json.data;
                    this.runResolve(this.resolveReturn);
                }.bind(this), function (xhr, text, error) {
                    this.status = "rejected";
                    this.resolveReturn = {"xhr": xhr, "text": text, "error": error};
                    this.runReject(this.resolveReturn);
                }.bind(this));
            },
            "runResolve": function(json){
                while (this.resolveList.length){
                    var r = this.resolveList.shift()(this.resolveReturn);
                    if (r) this.resolveReturn = r;
                }
            },
            "runReject": function(json){
                while (this.rejectList.length){
                    var r = this.rejectList.shift()(json);
                    if (r) this.resolveReturn = r;
                }
            },
            "then": function(resolve, reject){
                if (resolve) this.resolveList.push(resolve);
                if (reject) this.rejectList.push(reject);
                switch (this.status){
                    case "fulfilled":
                        this.runResolve();
                        break;
                    case "rejected":
                        this.runReject();
                        break;
                    default:
                    //nothing
                }
                return this;
            }
        }
NoSubject's avatar
NoSubject 已提交
535

NoSubject's avatar
NoSubject 已提交
536
        layout.sessionPromise.init(function(data){
NoSubject's avatar
NoSubject 已提交
537
            //已经登录
NoSubject's avatar
NoSubject 已提交
538
            layout.user = data;
NoSubject's avatar
NoSubject 已提交
539
            layout.session = layout.session || {};
NoSubject's avatar
NoSubject 已提交
540 541
            layout.session.user = data;
            layout.session.token = data.token;
NoSubject's avatar
NoSubject 已提交
542
            layout.desktop.session = layout.session;
543
            _loadApp();
NoSubject's avatar
NoSubject 已提交
544
        }, function(){
NoSubject's avatar
NoSubject 已提交
545 546
            //允许匿名访问
            if (layout.anonymous) {
NoSubject's avatar
NoSubject 已提交
547 548 549 550 551 552
                var data = { user: "anonymous", session: { user: { name: "anonymous", roleList: [] } } };
                layout.user = data;
                layout.session = layout.session || {};
                layout.session.user = data;
                layout.session.token = data.token;
                layout.desktop.session = layout.session;
553
                _loadApp();
NoSubject's avatar
NoSubject 已提交
554
            } else {
NoSubject's avatar
NoSubject 已提交
555 556 557 558 559 560
                _loadProgressBar(true);
                if (layout.yqwx) {
                    layout.openLoginQywx();
                } else {
                    layout.openLogin();
                }
NoSubject's avatar
NoSubject 已提交
561 562
            }
        });
NoSubject's avatar
NoSubject 已提交
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602

        // //先判断用户是否登录
        // o2.Actions.get("x_organization_assemble_authentication").getAuthentication(function (json) {
        //     //已经登录
        //     //_loadProgressBar();
        //
        //     layout.user = json.data;
        //     layout.session = layout.session || {};
        //     layout.session.user = json.data;
        //     layout.session.token = json.data.token;
        //     layout.desktop.session = layout.session;
        //
        //     //_loadApp(json);
        // }.bind(this), function (json) {
        //     //_loadProgressBar();
        //     //允许匿名访问
        //     if (layout.anonymous) {
        //         //_loadProgressBar(true);
        //         // _loadApp({
        //         //     data : {
        //         //         user: "anonymous",
        //         //         session: {
        //         //             user: {
        //         //                 name: "anonymous",
        //         //                 roleList: []
        //         //             }
        //         //         }
        //         //     }
        //         // });
        //     } else {
        //         _loadProgressBar(true);
        //         if (layout.yqwx) {
        //             layout.openLoginQywx();
        //         } else {
        //             layout.openLogin();
        //         }
        //     }
        // });


NoSubject's avatar
NoSubject 已提交
603 604

        layout.openLogin = function () {
NoSubject's avatar
NoSubject 已提交
605 606 607 608 609 610 611
            layout.authentication = new o2.xDesktop.Authentication({
                "style": "flat",
                "onLogin": _load.bind(layout)
            });
            layout.authentication.loadLogin(document.body);
            var loadingNode = $("browser_loading");
            if (loadingNode) loadingNode.fade("out");
NoSubject's avatar
NoSubject 已提交
612
        };
F
fancy 已提交
613 614 615 616 617

        layout.openLoginQywx = function () {
            console.log("开始login。。。。。。。。。。。。。");
            var uri = locate.href.toURI();

NoSubject's avatar
NoSubject 已提交
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
            console.log("执行单点。。。。。。。。。。");
            var action = new MWF.xDesktop.Actions.RestActions("", "x_organization_assemble_authentication", "");
            action.getActions = function (actionCallback) {
                this.actions = { "sso": { "uri": "/jaxrs/qiyeweixin/code/{code}", "method": "GET" } };
                if (actionCallback) actionCallback();
            };
            action.invoke({
                "name": "sso", "async": true, "parameter": { "code": uri.getData("code") }, "success": function (json) {
                    console.log("单点成功。");
                    console.log(json);
                    //基础数据。。。。
                    layout.session.user = json.data;
                    //
                    _load();

                }.bind(this), "failure": function (xhr, text, error) {
                    var n = document.getElementById("loaddingArea");
                    if (n) { n.destroy(); }
                    document.id("layout").set("html", "<div>企业微信单点异常!</div>")
                }.bind(this)
            });
F
fancy 已提交
639
        };
NoSubject's avatar
NoSubject 已提交
640
    };
NoSubject's avatar
NoSubject 已提交
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678

    //异步载入必要模块
    layout.config = null;
    var configLoaded = false;
    var lpLoaded = false;
    var commonLoaded = false;
    var lp = o2.session.path + "/lp/" + o2.language + ".js";

    if (o2.session.isDebugger && (o2.session.isMobile || layout.mobile)) o2.load("../o2_lib/eruda/eruda.js");

    var loadModuls = function () {
        _loadProgressBar();
        lpLoaded = true;

        var modules = ["o2.xDesktop.$all"];
        o2.require(modules, {
            "onSuccess": function () {
                commonLoaded = true;
                if (configLoaded && commonLoaded && lpLoaded) _getDistribute(function () { _load(); });
            },
            "onEvery": function () {
                _loadProgressBar();
            }
        });
    }

    if (!o2.LP){
        o2.load(lp, loadModuls);
    }else{
        loadModuls();
    }

    o2.getJSON("../x_desktop/res/config/config.json", function (config) {
        _loadProgressBar();
        layout.config = config;
        configLoaded = true;
        if (configLoaded && commonLoaded && lpLoaded) _getDistribute(function () { _load(); });
    });
NoSubject's avatar
NoSubject 已提交
679
});