/*
主要服务于word,excel,ppt,visio等文档的打开。
存在问题
1、保存的时候不弹出系统默认框
2、彻底分离各个模板
3、键盘处理 那些是快捷键 需要写流程图
*/
Namespace.register("U.MD.O");

//#region word创建,打开。

/**
* Word打开 之前通过第二个参数是guid进来的全部都要改
*
* @param   {object}    登录用户信息  
* @param   {object}    文档信息数组,如果为空,则创建空文档。
* @return  {object}    返回当前窗体 
*/
U.MD.O.word = function (userinfo, fileinfo, cb) {
    var _contentel,
        _fileid,
        _formel,
        _child
        ;
    //获取文档的id
    if (fileinfo && fileinfo.UserDirectoryID) {
        _fileid = fileinfo.UserDirectoryID; //获取文件id
    }
    if (!_fileid) { //第一次打开的文档的处理
        _fileid = Guid.newGuid();
        var _type = fileinfo.UserDirectoryExtendType;
        fileinfo = { "UserDirectoryID": _fileid, UserDirectoryExtendType: _type };
        if (_type == "un") {
            fileinfo.UsOffice = U.A.Request(US.CD, ([US.DB, "UseStudio_Disk", "GetFileContent", "6a8a235a-41c4-c3d2-8dd5-b6ee851e1967"])).value[0].UsOffice.unEscapeQuotes();
        } else {
            fileinfo.UsOffice = U.A.Request(US.CD, ([US.DB, "UseStudio_Disk", "GetFileContent", "d17a2910-11ba-df39-cd8a-5cac851c2148"])).value[0].UsOffice.unEscapeQuotes();
        }
        fileinfo.newfile = true;
    }
    //得到弹窗
    _formel = $("#UD_Word" + _fileid)[0];
    //未打开的创建加载处理
    if (!_formel) {
        //创建新窗口
        _contentel = $("#UD_Word").clone(true); //克隆word编辑器 包含头部菜单和功能及word编辑区域
        _child = $(_contentel[0]).Child(); //头部的区域
        _formel = new U.UF.UI.form(
            _child[0],
            _child[1],
            {
                "id": "UD_Word" + _fileid, "style": { "width": "90%", "height": "90%", "overflow": 'hidden' },
                "onresize": function () { }
            },
            {
                closecallback: function () {
                    U.A.Request(US.SCOKET, ["quitOffice", JSON.stringify({ docId: fileinfo.UserDirectoryID, pageId: US.pageId, userId: top.US.userInfo.userid, type: fileinfo.UserDirectoryExtendType == 'un' ? 'nav' : 'word' })], function (r) {
                        //                    console.log('退出文档');
                    });
                }
            },
            { "style": { "height": "36px" } }).form; //创建窗体
        U.MD.O.header(userinfo, _child[0], fileinfo); //创建header头部
        U.UF.DL.loading(_child[1]); //添加loading处理
        //添加编辑区域,异步加载iframe并执行回调
        U.MD.O.officeLoad(_child[1], "Word", function (iframe) {
            iframe.contentWindow.U.MD.O.W.load(fileinfo, userinfo); //word的处理
            $('#UD_Word' + _fileid + ' .U_MD_O_H_head_navigation_Title')[0].onchange = iframe.contentWindow.U.MD.O.W.updateName;
            //打开word成功后执行回调
            if (U.UF.C.isFunction(cb)) {
                cb(iframe.contentWindow);
            }
            U.UF.DL.uploading(_child[1]); //移除loading处理

        }); //创建Office编辑器 

    }
    //如果已经打开过的,直接置顶
    else {
        $('iframe', _formel)[0].contentWindow.U.MD.O.W.load(fileinfo, userinfo);
        U.UF.F.windowTopCenter(_formel);
    }
    return [_formel, fileinfo]; // 返回当前窗体
}

/**
* 导航建站
*
* @param   {element}    登录用户信息  
*/
U.MD.O.wordNav = function (el) {


}


//#endregion

//#region Excel初始化

/**
* Excel打开
*
* @param   {object}     登录用户信息 
* @param   {object}     文档信息数组
* @return  {object}     返回当前窗体
*/
U.MD.O.excel = function (userinfo, fileinfo) {
    var _contentel,
        _fileid,
        _formel,
        _child
        ;
    //获取文档的id
    if (fileinfo && fileinfo.UserDirectoryID) {
        _fileid = fileinfo.UserDirectoryID; //获取文件id
    }
    if (!_fileid) { //第一次打开的文档的处理
        _fileid = Guid.newGuid();
        fileinfo = { "UserDirectoryID": _fileid };
    }


    //得到弹窗
    _formel = $("#UD_Excel" + _fileid)[0];
    //未打开的创建加载处理
    if (!_formel) {
        //创建新窗口
        _contentel = $("#UD_Excel").clone(true); //克隆word编辑器 包含头部菜单和功能及word编辑区域
        _contentel[0].id = ""; //清除id
        _child = $(_contentel[0]).Child(); //头部的区域
        _formel = new U.UF.UI.form(_child[0], _child[1], { "id": "UD_Excel" + _fileid, "style": { "width": "90%", "height": "90%", "overflow": 'hidden' } }, {}, { "style": { "height": "36px" } }).form; //创建窗体
        U.UF.DL.loading(_child[1]); //添加loading处理
        U.MD.O.header(userinfo, _child[0], fileinfo); //创建header头部
        //添加编辑区域,异步加载iframe并执行回调
        U.MD.O.officeLoad(_child[1], "Excel", function (iframe) {
            U.UF.DL.uploading(_child[1]); //移除loading处理
            iframe.contentWindow.U.MD.O.E.load(fileinfo, userinfo, iframe); //word的处理
        }); //创建Office编辑器 
    }
    //如果已经打开过的,直接置顶
    else {
        U.UF.F.windowTopCenter(_formel);
    }
    return _formel; // 返回当前窗体
}

//#endregion

//#region 此函数只是用来创建word,excel,PPT等的编辑器,没有包含word,excel,PPT等的功能。word,excel,PPT的功能包含在异步函数U.MD.O.asynWord中,统一创建office的编辑iframe。

/**
* 创建Office窗体
*
* @param  {element}    创建office的容器元素
* @param  {object}    登录用户信息
* @param  {object}    文档信息数组 或 null,用于显示文件内容
* @param  {str}       类型 Word || Excel||ppt|others...
* @param  {function} 回调函数
*/
U.MD.O.officeLoad = function (el, type, cb) {
    var _file, //文件的路径
        _iframe //iframe的名字
        ;
    //初始化应用的路径。
    switch (type) {
        case "Word": //word的处理
            _file = "WordEditArea.htm";
            break;
        case "Excel": //excel的处理
            _file = "ExcelEditArea.htm";
            //            type = 'ExcelCanvas';
            break;
        case "PPT": //ppt的处理
            _file = "PPT.htm";
            break;
        default: break;
    }

    //创建容纳word,Excel,PPT等的iframe。
    _iframe = $$("iframe", {
        "frameBorder": "0",
        "style": {
            "border": "0"
        }, //创建office
        "src": US.MAINDOMAIN + "/Controls/Office/" + type + "/" + _file,
        "width": "100%",
        "height": "100%"
    }, el);

    //异步加载iframe  添加office功能
    U.UF.DL.iframeLoad(_iframe, function () {
        $("body", _iframe.contentWindow.document)[0].onmousedown = function () {
            U.UF.F.topWindow(el, true);
        }
        cb(_iframe);
    });
}

/**
* 用户头部生成
*
* @param  {object}   登录用户信息
* @param  {element}  头部元素 
* @param {object}   文档信息数组
*/
U.MD.O.header = function (userinfo, el, fileinfo) {
    var _filename,
        _hreadel;
    //获取文件名
    if (fileinfo && (fileinfo["UserallDirectoryName"] || fileinfo["UserFilesName"])) { //文件存在的时候文件的名字
        _filename = fileinfo["UserallDirectoryName"] || fileinfo["UserFilesName"];
    }
    //不存在文件的文件名
    else {
        _filename = "未命名文档(点击编辑)...";
    }
    $("input", el)[0].value = _filename; //文件名
    //头部处理
    _hreadel = $$("div", { "className": "U_MD_O_H_Editor" }, el.children[2].children[4]);
    $$("img", {
        "onerror": U.MD.C.imgError,
        "onerror": U.MD.C.imgError,
        "src": U.MD.C.getHeadImage(userinfo.UserThumbnailImageHead)
    }, _hreadel);
    $$("div", { "className": "U_MD_O_H_head_navigation_username", "innerHTML": userinfo.UserNickName || "" }, el.children[2].children[4]);
}

//#endregion

//#region 统一区域

/**
* 只需要随便传递一个元素,则可以获取指定的Window操作层
*
* @param  {element}   Ifrmae下的元素
* @return {boject}   指定的Window操作层,即iframe中的windows对象
*/
U.MD.O.getOfficeWindow = function (el) {
    // 如果没有参数,则获取当前事件聚焦的对象。
    el = el || event.srcElement; //获取事件的源对象 
    return $("iframe", $(el).Parent({ usform: "true" }) || el)[0].contentWindow; //却换到指定的window操作层
}

//#endregion