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

o2.xDesktop = o2.xDesktop || {};
o2.xDesktop.requireApp = function(module, clazz, callback, async){
    o2.requireApp(module, clazz, callback, async);
};
o2.addReady(function(){
    //兼容方法
    Element.implement({
        "makeLnk": function(options){}
    });
NoSubject's avatar
NoSubject 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32

    var loadingNode = $("loaddingArea");
    var loadeds = 0;
    var loadCount = 15;
    var size = document.body.getSize();
    var _loadProgressBar = function(){
        if (loadingNode){
            loadeds++;
            var p = (loadeds/loadCount)*size.x;
            loadingNode.set('morph', {duration: 300}).morph({"width": ""+p+"px"});
            if (loadeds>=loadCount) window.setTimeout(function(){loadingNode.destroy();}, 500);
        }
    };

NoSubject's avatar
NoSubject 已提交
33 34 35
    //异步载入必要模块
    layout.config = null;
    var modules = [ "MWF.xDesktop.Common", "MWF.xAction.RestActions" ];
NoSubject's avatar
NoSubject 已提交
36 37 38 39 40 41 42
    MWF.require(modules, {
        "onSuccess": function(){
            if (layout.config) _getDistribute(function(){ _load(); });
        },
        "onEvery": function(){
            _loadProgressBar();
        }
NoSubject's avatar
NoSubject 已提交
43
    });
NoSubject's avatar
NoSubject 已提交
44 45
    o2.getJSON("/x_desktop/res/config/config.json", function(config){
        _loadProgressBar();
NoSubject's avatar
NoSubject 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
        layout.config = config;
        if (MWF.xDesktop.getServiceAddress) _getDistribute(function(){ _load(); });
    });

    var _getDistribute = function(callback){
        if (layout.config.app_protocol==="auto"){
            layout.config.app_protocol = window.location.protocol;
        }
        MWF.xDesktop.getServiceAddress(layout.config, function(service, center){
            layout.serviceAddressList = service;
            layout.centerServer = center;
            if (callback) callback();
        }.bind(this));
    };

    var _load = function(){
R
roo00 已提交
62
        var _loadApp = function(json){
NoSubject's avatar
NoSubject 已提交
63 64 65 66 67 68 69
            //用户已经登录
            layout.user = json.data;
            layout.session = {};
            layout.session.user = json.data;
            (function(layout){
                var _loadResource = function(callback){
                    var isLoadedA = false;
NoSubject's avatar
NoSubject 已提交
70
                    var isLoadedB = false;
NoSubject's avatar
NoSubject 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
                    //var isLoadedC = false;

                    var lp = o2.session.path+"/lp/"+o2.language+".js";
                    var modules = [
                        "o2.xDesktop.Dialog",
                        "MWF.xDesktop.UserData",
                        "MWF.xDesktop.Access",
                        "MWF.widget.UUID",
                        "MWF.xDesktop.Menu",
                        "MWF.xDesktop.shortcut",
                        "MWF.widget.PinYin",
                        "MWF.xDesktop.Access",
                        "MWF.xDesktop.MessageMobile"
                    ];
                    //MWF.xDesktop.requireApp("Common", "", null, false);
                    var _check = function(){ if (isLoadedA && isLoadedB) if (callback) callback(); };

NoSubject's avatar
NoSubject 已提交
88
                    o2.load(["/o2_lib/mootools/plugin/mBox.min.js",lp], function(){_loadProgressBar(); isLoadedA = true; _check();});
NoSubject's avatar
NoSubject 已提交
89
                    o2.require("MWF.widget.Common", function(){
NoSubject's avatar
NoSubject 已提交
90 91 92 93 94 95 96 97
                        _loadProgressBar();
                        o2.require(modules, {
                            "onSuccess": function(){
                                o2.requireApp("Common", "", function(){_loadProgressBar(); isLoadedB = true; _check();})
                            },
                            "onEvery": function(){
                                _loadProgressBar();
                            }
NoSubject's avatar
NoSubject 已提交
98 99 100 101 102 103
                        });
                    });
                };

                var _loadContent =function(){
                    _loadResource(function(){
NoSubject's avatar
NoSubject 已提交
104 105 106
                        //window.status = window.status+layout.readys.length;
                        while (layout.readys && layout.readys.length){
                            layout.readys.shift().apply(window);
NoSubject's avatar
NoSubject 已提交
107
                        }
NoSubject's avatar
NoSubject 已提交
108 109 110
                        // for (var i=0; i<layout.readys.length; i++){
                        //     layout.readys[i].apply(window);
                        // }
NoSubject's avatar
NoSubject 已提交
111 112 113 114 115
                    });
                };

                _loadContent();
            })(layout);
R
roo00 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
        };
        //先判断用户是否登录
        MWF.Actions.get("x_organization_assemble_authentication").getAuthentication( function(json){
            //已经登录
            _loadApp( json );
        }.bind(this), function( json ){
            //允许匿名访问
            if( layout.anonymous ){
                _loadApp({
                    user : "anonymous",
                    session : {
                        user : {
                            name : "anonymous",
                            roleList : []
                        }
                    }
                });
            }else{
                //用户未经登录
                //打开登录页面
                var _loadResource = function(callback){
                    var isLoadedA = false;
                    var isLoadedB = false;
                    //var isLoadedC = false;
NoSubject's avatar
NoSubject 已提交
140

R
roo00 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154
                    var lp = o2.session.path+"/lp/"+o2.language+".js";
                    var modules = [
                        "o2.xDesktop.Dialog",
                        "MWF.xDesktop.UserData",
                        "MWF.xDesktop.Access",
                        "MWF.widget.UUID",
                        "MWF.xDesktop.Menu",
                        "MWF.xDesktop.shortcut",
                        "MWF.widget.PinYin",
                        "MWF.xDesktop.Access",
                        "MWF.xDesktop.MessageMobile"
                    ];
                    //MWF.xDesktop.requireApp("Common", "", null, false);
                    var _check = function(){ if (isLoadedA && isLoadedB) if (callback) callback(); };
NoSubject's avatar
NoSubject 已提交
155

R
roo00 已提交
156 157 158 159 160 161 162 163 164 165 166
                    o2.load(["/o2_lib/mootools/plugin/mBox.min.js",lp], function(){_loadProgressBar(); isLoadedA = true; _check();});
                    o2.require("MWF.widget.Common", function(){
                        _loadProgressBar();
                        o2.require(modules, {
                            "onSuccess":function(){
                                o2.requireApp("Common", "", function(){isLoadedB = true; _check();})
                            },
                            "onEvery": function(){
                                _loadProgressBar();
                            }
                        });
NoSubject's avatar
NoSubject 已提交
167
                    });
R
roo00 已提交
168 169 170
                };
                _loadResource(function(){
                    layout.openLogin();
NoSubject's avatar
NoSubject 已提交
171
                });
R
roo00 已提交
172
            }
NoSubject's avatar
NoSubject 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
        });

        layout.openLogin = function(){
            MWF.require("MWF.widget.Common", null, false);
            MWF.require("MWF.xDesktop.Authentication", function(){
                var authentication = new MWF.xDesktop.Authentication({
                    "onLogin": _load.bind(layout)
                });
                authentication.loadLogin(document.body);
            });
        };
    };
});

(function(layout){
    layout.readys = [];
    layout.addReady = function(){
        for (var i = 0; i<arguments.length; i++){
            if (o2.typeOf(arguments[i])==="function") layout.readys.push(arguments[i]);
        }
    };
    var _requireApp = function(appNames, callback, clazzName){
        var appPath = appNames.split(".");
        var baseObject = o2.xApplication;
        appPath.each(function(path, i){
            if (i<(appPath.length-1)){
                baseObject[path] = baseObject[path] || {};
            }else {
                baseObject[path] = baseObject[path] || {"options": Object.clone(MWF.xApplication.Common.options)};
            }
            baseObject = baseObject[path];
        }.bind(this));
        if (!baseObject.options) baseObject.options = Object.clone(MWF.xApplication.Common.options);

        var _lpLoaded = false;
        MWF.xDesktop.requireApp(appNames, "lp."+o2.language, {
            "failure": function(){
                MWF.xDesktop.requireApp(appNames, "lp.zh-cn", null, false);
            }.bind(this)
        }, false);
        MWF.xDesktop.requireApp(appNames, clazzName, function(){
            if (callback) callback(baseObject);
        });
    };
    var _createNewApplication = function(e, appNamespace, appName, options, statusObj){
        var app = new appNamespace["Main"](this, options);
        app.desktop = layout;
        app.inBrowser = true;
        app.status = statusObj;
        app.load(true);

        var appId = appName;
        if (options.appId){
            appId = options.appId;
        }else{
            if (appNamespace.options.multitask) appId = appId+"-"+(new MWF.widget.UUID());
        }
        app.appId = appId;
        layout.app = app;
NoSubject's avatar
NoSubject 已提交
232
        layout.desktop.currentApp = app;
NoSubject's avatar
NoSubject 已提交
233 234 235 236 237 238 239 240

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

    var _openWorkAndroid = function(options){
        if (window.o2android && window.o2android.openO2Work) {
            if (options.workId) {
NoSubject's avatar
NoSubject 已提交
241
                window.o2android.openO2Work(options.workId, "", options.title || "");
NoSubject's avatar
NoSubject 已提交
242
            } else if (options.workCompletedId) {
NoSubject's avatar
NoSubject 已提交
243
                window.o2android.openO2Work("", options.workCompletedId, options.title || "");
NoSubject's avatar
NoSubject 已提交
244 245 246 247 248 249 250 251 252 253 254
            }
            return true;
        }
        return false;
    };
    var _openWorkIOS = function(options){
        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 已提交
255
                    "title": options.title || ""
NoSubject's avatar
NoSubject 已提交
256 257 258 259 260
                });
            } else if (options.workCompletedId) {
                window.webkit.messageHandlers.openO2Work.postMessage({
                    "work": "",
                    "workCompleted": options.workCompletedId,
NoSubject's avatar
NoSubject 已提交
261
                    "title": options.title || ""
NoSubject's avatar
NoSubject 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
                });
            }
            return true;
        }
        return false;
    };
    var _openWorkHTML = function(options){
        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) {
            window.location = "workmobilewithaction.html?workid=" + options.workId + ((layout.debugger)? "&debugger":"") + "&redirectlink=" + redirectlink;
        } else if (options.workCompletedId) {
            window.location = "workmobilewithaction.html?workcompletedid=" + options.workCompletedId + ((layout.debugger)? "&debugger":"") + "&redirectlink=" + redirectlink;
        }
    };
    var _openWork = function(options){
        if (!_openWorkAndroid(options)) if (!_openWorkIOS(options)) _openWorkHTML(options);
    };
    var _openDocument = function(appNames, options, statusObj){
NoSubject's avatar
NoSubject 已提交
286 287
        var title = typeOf( options ) === "object" ? ( options.docTitle || options.title ) : "";
        title = title || "";
NoSubject's avatar
NoSubject 已提交
288 289 290 291 292 293 294 295 296 297 298 299
        var par = "app="+encodeURIComponent(appNames)+"&status="+encodeURIComponent((statusObj)? JSON.encode(statusObj) : "")+"&option="+encodeURIComponent((options)? JSON.encode(options) : "");
        if (window.o2android && window.o2android.openO2CmsDocument){
            window.o2android.openO2CmsDocument(options.documentId, title);
        }else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.openO2CmsDocument){
            window.webkit.messageHandlers.openO2CmsDocument.postMessage({"docId":options.documentId,"docTitle":title});
        }else{
            window.location = "appMobile.html?"+par + ((layout.debugger)? "&debugger":"");
        }
    };
    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 已提交
300
            window.o2android.openO2CmsApplication(options.columnId, options.title || "");
NoSubject's avatar
NoSubject 已提交
301 302 303
        }else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.openO2CmsApplication){
            window.webkit.messageHandlers.openO2CmsApplication.postMessage(options.columnId);
        }else{
R
roo00 已提交
304
            window.location = "appMobile.html?"+par + ((layout.debugger)? "&debugger":"");
NoSubject's avatar
NoSubject 已提交
305 306 307 308 309 310 311 312 313
        }
    };
    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){
            window.o2android.openO2Meeting("");
        }else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.openO2Meeting){
            window.webkit.messageHandlers.openO2Meeting.postMessage("");
        }else{
R
roo00 已提交
314
            window.location = "appMobile.html?"+par + ((layout.debugger)? "&debugger":"");
NoSubject's avatar
NoSubject 已提交
315 316 317 318 319 320 321 322 323 324
        }
    };

    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){
            window.o2android.openO2Calendar("");
        }else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.openO2Calendar){
            window.webkit.messageHandlers.openO2Calendar.postMessage("");
        }else{
R
roo00 已提交
325
            window.location = "appMobile.html?"+par + ((layout.debugger)? "&debugger":"");
NoSubject's avatar
NoSubject 已提交
326 327 328 329 330 331 332 333 334 335 336 337 338
        }
    };
    var _openTaskCenter = function(appNames, options, statusObj){
        var par = "app="+encodeURIComponent(appNames)+"&status="+encodeURIComponent((statusObj)? JSON.encode(statusObj) : "")+"&option="+encodeURIComponent((options)? JSON.encode(options) : "");
        var tab = ((options && options.navi) ? options.navi : "task").toLowerCase();
        if (tab==="done") tab = "taskCompleted";
        if (tab==="readed") tab = "readCompleted";

        if (window.o2android && window.o2android.openO2WorkSpace){
            window.o2android.openO2WorkSpace(tab);
        }else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.openO2WorkSpace){
            window.webkit.messageHandlers.openO2WorkSpace.postMessage(tab);
        }else{
R
roo00 已提交
339
            window.location = "appMobile.html?"+par + ((layout.debugger)? "&debugger":"");
NoSubject's avatar
NoSubject 已提交
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
        }
    };

    var _openApplicationMobile = function(appNames, options, statusObj){
        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 已提交
364 365 366
                var uri = new URI(window.location.href);
                var optionsStr = uri.getData("option");
                var statusStr = uri.getData("status");
NoSubject's avatar
NoSubject 已提交
367 368
                window.location = "appMobile.html?app="+appNames+"&option="+(optionsStr || "")+"&status="+(statusStr || "") + ((layout.debugger)? "&debugger":"");
        }
NoSubject's avatar
NoSubject 已提交
369 370 371 372
    };

    layout.openApplication = function(e, appNames, options, statusObj){
        if (layout.app){
NoSubject's avatar
NoSubject 已提交
373 374 375 376 377 378
            if (layout.mobile){
                _openApplicationMobile(appNames, options, statusObj);
            }else{
                var par = "app="+encodeURIComponent(appNames)+"&status="+encodeURIComponent((statusObj)? JSON.encode(statusObj) : "")+"&option="+encodeURIComponent((options)? JSON.encode(options) : "");
                return window.open("app.html?"+par + ((layout.debugger)? "&debugger":""), "_blank");
            }
NoSubject's avatar
NoSubject 已提交
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
        }else{
            var appPath = appNames.split(".");
            var appName = appPath[appPath.length-1];

            _requireApp(appNames, function(appNamespace){
                _createNewApplication(e, appNamespace, appName, options, statusObj);
            }.bind(this));
        }
    };

    layout.refreshApp = function(app){
        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");
NoSubject's avatar
NoSubject 已提交
399
        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":"");
NoSubject's avatar
NoSubject 已提交
400 401
    };

NoSubject's avatar
NoSubject 已提交
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
    layout.load =function(appNames, options, statusObj){
        layout.message = new MWF.xDesktop.MessageMobile();
        layout.message.load();

        layout.apps = [];
        layout.node = $("layout");
        var appName=appNames, m_status=statusObj, option=options;

        var topWindow = window.opener;
        if (topWindow){
            try{
                if (!appName) appName = topWindow.layout.desktop.openBrowserApp;
                if (!m_status) m_status = topWindow.layout.desktop.openBrowserStatus;
                if (!option)  option = topWindow.layout.desktop.openBrowserOption;
            }catch(e){}
        }
        layout.openApplication(null, appName, option||{}, m_status);
    }

NoSubject's avatar
NoSubject 已提交
421
})(layout);