<script>
    window.nav = new Vue({
        data: {
            activeIndex: 'home',
            contextPath: '${contextPath?default("")}',
            homePath: '${homePath?default("")}',
            loadingTip: '',
            loadingBar: '',
            tip: {
                show: function (msg) {
                    var message = "<i class='el-icon-loading'></i> 正在加载 ..."
                    if (msg) {
                        message = "<i class='el-icon-loading'></i> " + msg
                    }
                    if (!nav.loadingTip) {
                        nav.loadingTip = nav.$message({
                            type: '',
                            duration: 0,
                            dangerouslyUseHTMLString: true,
                            message: message
                        });
                    } else {
                        nav.loadingTip.message = message;
                    }
                },
                close: function () {
                    if (nav.loadingTip) {
                        nav.loadingTip.close();
                        nav.loadingTip = '';
                    }
                }
            },
            bar: {
                show: function () {
                    if (!nav.loadingBar) {
                        nav.loadingBar = nav.$message({
                            type: '',
                            duration: 0,
                            customClass: 'loading-bar',
                            dangerouslyUseHTMLString: true,
                            message: '<i class="bar" style="width: 90%"></i>'
                        });
                    } else {
                        nav.loadingBar.message = '<i class="bar" style="width: 90%"></i>'
                    }
                },
                finish: function () {
                    if (nav.loadingBar) {
                        nav.loadingBar.message = '<i class="bar" style="width: 100%"></i>'
                        setTimeout(function(){
                            if (nav.loadingBar) {
                                nav.loadingBar.close();
                                nav.loadingBar = '';
                            }
                        },500);
                    }
                },
                error: function () {
                    if (nav.loadingBar) {
                        nav.loadingBar.message = '<i class="bar error" style="width: 100%"></i>'
                        setTimeout(function(){
                            if(nav.loadingBar){
                                nav.loadingBar.close();
                                nav.loadingBar = '';
                            }
                        },500);
                    }
                }
            }
        },
        methods: {
            i: function (message, callback) {
                this.$message({
                    type: "info",
                    showClose: true,
                    message: message,
                    duration: 1500,
                    onClose: callback
                });
            },
            e: function (message, callback) {
                this.$message({
                    type: "error",
                    showClose: true,
                    message: message,
                    duration: 1500,
                    onClose: callback
                });
            },
            w: function (message, callback) {
                this.$message({
                    type: "warning",
                    showClose: true,
                    message: message,
                    duration: 1500,
                    onClose: callback
                });
            },
            s: function (message, callback) {
                this.$message({
                    type: "success",
                    showClose: true,
                    message: message,
                    duration: 1500,
                    onClose: callback
                });
            },
            toOpen: function (url) {
                nav.tip.show();
                var url = url.substring(0, 1) == "/" ? url.substring(1) : url;
                $("body").append($("<a id='wb-open' href='" + this.contextPath + "/" + url + "' target='_self' style='dispaly:none;'></a>"))
                document.getElementById("wb-open").click();
                $("#wb-open").remove();
            },
            toOpenNew: function (url) {
                var url = url.substring(0, 1) == "/" ? url.substring(1) : url;
                $("body").append($("<a id='wb-open' href='" + this.contextPath + "/" + url + "' target='_blank' style='dispaly:none;'></a>"))
                document.getElementById("wb-open").click();
                $("#wb-open").remove();
            },
            toHome: function () {
                nav.tip.show();
                location.href = this.contextPath + "/"
            },
            /**
             * 滚动屏蔽至顶部
             */
            scrollToTop: function () {
                var distance = document.documentElement.scrollTop || document.body.scrollTop;
                var step = distance / 10;

                (function jump() {
                    if (distance > 0) {
                        distance -= step;
                        window.scrollTo(0, distance);
                        setTimeout(jump, 10)
                    }
                })();
            },
            /**
             * 控制任一目标滚动到顶部
             * select jquery对象
             */
            scrollToTop: function (select) {
                var distance = $(select).scrollTop();
                var step = distance / 10;

                (function jump() {
                    if (distance > 0) {
                        distance -= step;
                        $(select).scrollTop(distance);
                        setTimeout(jump, 10)
                    } else {
                        $(select).scrollTop(distance);
                    }
                })();
            }
        }
    });
</script>