index.js 10.1 KB
var messages = {
    text_info: "",
    text_error: "",
    init_plugin_error: ""
};
// 首先加载语言
loadLanguage(onLangReady, messages);

// 当语言文件加载成功,执行页面的其他逻辑
function onLangReady() {
    $().ready(function() {
        // 动态加载easyui
        loadEasyuiModules(["panel", "messager", "layout"], function() {
            appModel.loadMenuData();
        });
        // 页面的其他一些配置
        document.onkeydown = function() {
            if (event.keyCode == 8) {
                event.keyCode = 27;
            }
        };
        window.onbeforeunload = function() {
            //	alert("你点击了关闭浏览器,服务器资源即将销毁");
            var Url = "/do/trafficController/destructionResources";
            $.get(
                Url,
                null,
                function(data) {
                    if (!data.error) {
                        $.messager.alert(
                            messages.messager_alert_title,
                            "资源销毁失败!原因:" + data.feedback.Msg,
                            "error"
                        );
                    }
                },
                "json"
            );
        };
    });
}

function getMenuIcon(icon) {
    return "background-image:url(images/index/menu/" + icon + ".png)";
}
//登出功能
function loginout() {
    var loginoutUrl =
        "/do/trafficController/userLogout?t=" + new Date().getTime();
    $.get(
        loginoutUrl,
        null,
        function(data) {
            if (data.error) {
                // 获取语言
                var lang = $.i18n.normaliseLanguageCode();
                var loginUrl = "login.html";
                if (lang) {
                    loginUrl += "?lang=" + lang;
                }
                window.location.href = loginUrl;
            } else {
                $.messager.alert(
                    messages.messager_alert_title,
                    data.feedback.Msg,
                    "error"
                );
            }
        },
        "json"
    );
}

function MainPageModel() {
    var self = this;
    var mainFrameObj; //主IFrame框架
    var isLeftMenuShowed = true;
    var panelWestWidth = null; //左侧框架的宽度

    self.menuData = ko.observable(null); //菜单
    self.appSettings = new Array(); //程序设置
    self.selectedMenuLevel1 = ko.observable(null); //选择的第一级目录

    //选择的一级菜单下的二级菜单列表
    self.selectedMenuItems = ko.computed(function() {
        if (self.selectedMenuLevel1() != null)
            return self.selectedMenuLevel1().children;
        else {
            return [];
        }
    }, self);

    /*-----------------------------------菜单相关的逻辑-------------------------*/
    //当选择的二级菜单变化时,动态显示或隐藏左侧的菜单
    self.selectedMenuItems.subscribe(function(newValue) {
        var mainLayout = $("#mainLayout");
        var panelWest = null;
        var panelCenter = null;
        try {
            panelWest = mainLayout.layout("panel", "west");
            panelCenter = mainLayout.layout("panel", "center");
        } catch (e) {}
        if (!panelWest || !panelCenter) {
            return;
        }
        //保存最初的左侧菜单框架的宽度
        if (!panelWestWidth) {
            panelWestWidth = panelWest.width();
        }
        var centerHeight = panelCenter.height();
        var centerWidth = panelCenter.width();
        if (newValue != null && newValue.length > 0) {
            //只在左侧菜单不显示的时候才显示菜单
            if (!isLeftMenuShowed) {
                panelWest.panel("open");
                panelCenter.panel("resize", {
                    height: centerHeight,
                    width: centerWidth - panelWestWidth
                });
                panelCenter.panel("move", {
                    left: panelWestWidth
                });
                isLeftMenuShowed = true;
            }
        } else {
            if (isLeftMenuShowed) {
                panelWest.panel("close");
                panelCenter.panel("resize", {
                    height: centerHeight,
                    width: centerWidth + panelWestWidth
                });
                panelCenter.panel("move", {
                    left: 0
                });
                isLeftMenuShowed = false;
            }
        }
    });

    //加载菜单数据的
    self.loadMenuData = function() {
        // 确定现在的语言,加载对应的菜单
        var lang = $.i18n.normaliseLanguageCode();
        var menuUrl = "i18n/menu_" + lang + ".json";
        $.getJSON(menuUrl, function(data) {
            if (data) {
                self.menuData(data);
                //默认选择首页
                self.selectedMenuLevel1(data.menus[0]);
                //发送数据
                self.loadData();
                showPage(data.menus[0].url);
                //第一次加载,设置首页的图标样式
                $(".menu_level1_icon:eq(0)").addClass("selectedMenuLevel1");
            }
        });
    };

    self.loadData = function() {
        $.get(
            "/do/trafficController/setTrafficCamera?command=resultdata_send&value=1"
        );
    };

    self.stopLoadData = function() {
        $.get(
            "/do/trafficController/setTrafficCamera?command=resultdata_send&value=0"
        );
    };

    //选择一级菜单,显示左侧菜单
    self.selectMenuLv1 = function(menu, event) {
        //选中单击的一级菜单
        self.selectedMenuLevel1(menu);
        if (menu.id == "index") {
            self.loadData();
        } else {
            self.stopLoadData();
        }

        if (menu.url) {
            showPage(menu.url);
        } else {
            //当有二级菜单时,选中二级菜单下的第一项
            setTimeout(function() {
                var firstLevel2MenuItem = $(".menuLv2Item:eq(0)");
                if (firstLevel2MenuItem.length > 0) {
                    firstLevel2MenuItem.trigger("click");
                }
            }, 200);
        }
        //一菜菜单图标
        $(".menu_level1_icon").removeClass("selectedMenuLevel1");
        $(event.currentTarget)
            .find(".menu_level1_icon")
            .addClass("selectedMenuLevel1");
    };

    //选择左侧的二级菜单
    self.selectMenuLv2 = function(menu, event) {
        if (menu.url) {
            showPage(menu.url);
        }
        //二级菜单选中的效果
        $(event.currentTarget)
            .toggleClass("selected")
            .siblings()
            .removeClass("selected");
        //默认选中第一个菜单
        var firstChildMenu = $(event.currentTarget).find(
            "li.menuLv3Item:eq(0)"
        );
        if (firstChildMenu.length > 0) {
            firstChildMenu.trigger("click");
        }
    };

    //选择左侧的三级菜单
    self.selectMenuLv3 = function(menu, event) {
        if (menu.url && !menu.disabled) {
            showPage(menu.url);
            //三级菜单选中效果
            $(".menuLv3Item").removeClass("selected");
            $(event.currentTarget).toggleClass("selected");
        }
    };
    self.isLv1MenuDisabled = function(menuLv1) {
        //当用户不是admin时,不显示用户管理菜单
        if (menuLv1.id == "userManage" || menuLv1.id === "settings") {
            var loginedUser = '<%= getSessionVar("loginedUser") %>';
            console.log("当前登录用户: " + loginedUser);
            var isAdmin = loginedUser != "admin";
            return !isAdmin;
        } else {
            //当一级菜单没有url,且没有子菜单项时才禁用
            return !menuLv1.url && !menuLv1.children;
        }
    };

    self.isLv3MenuDisabled = function(menuLv3) {
        //当相机模式是公交时,显示“线路编辑”菜单
        if (menuLv3.id == "roadList") {
            return !menuLv3.disabled && cameraRunMode != 3;
        } else {
            return menuLv3.disabled;
        }
    };

    //获取应用设置
    self.getSetting = function(key) {
        return self.appSettings[key] || null;
    };
    //设置应用设置项
    self.setSetting = function(key, value) {
        if (!key) return;
        self.appSettings[key] = value;
    };

    //计算可用的子项(菜单)的数量
    self.isLevel2MenuValid = function(url, children) {
        var count = 0;
        if (url) return true;
        if (children && children.length) {
            for (var i = 0; i < children.length; i++) {
                if (!children[i].disabled) {
                    count++;
                }
            }
        }
        return count > 0;
    };

    //加载页面
    function showPage(url) {
        getMainFrameObj();
        var currentUrl = mainFrameObj.attr("src");
        if (currentUrl != url) {
            mainFrameObj.attr("src", url);
        }
    }

    function getMainFrameObj() {
        if (!mainFrameObj) {
            mainFrameObj = $("#mainPageFrame");
        }
        return mainFrameObj;
    }
}

// 获取相机运行模式?
var cameraRunMode = -1;
// 加载相机运行模式
getCameraBusinessMode(function(mode) {
    cameraRunMode = mode;
    // 保存相机运行模式
    appModel.setSetting("captureMode", cameraRunMode);
});

//初始化主页ViewModel
var appModel = new MainPageModel();
//绑定模型
ko.applyBindings(appModel);

//初始化信息推送的Ocx
function initResultOcx() {
    var ip = window.location.hostname;
    var result = infoOcxObj.Init(ip, 41066);
    if (result != 1) {
        $.messager.alert(
            messages.text_error,
            messages.init_plugin_error,
            "error"
        );
        console.log(messages.init_plugin_error + "(ip:" + ip + ",port:41066)");
		console.log("RecResultOcx.ocx Init()");
    } else {
		console.log("RecResultOcx.ocx Init() return 1");
	}
}

//反初始化信息推送的Ocx
function uninitResultOcx() {
	console.log("RecResultOcx.ocx Uninit()");
    infoOcxObj.Uninit();
}

function getInfoOcx() {
    return document.getElementById("infoOcxObj");
}

window.onbeforeunload = function() {
    infoOcxObj.Uninit();
};