审查视图

public/assets/js/backend.js 11.9 KB
1
define(['fast', 'template', 'moment'], function (Fast, Template, Moment) {
Karson authored
2 3 4 5 6 7 8
    var Backend = {
        api: {
            sidebar: function (params) {
                colorArr = ['red', 'green', 'yellow', 'blue', 'teal', 'orange', 'purple'];
                $colorNums = colorArr.length;
                badgeList = {};
                $.each(params, function (k, v) {
9
                    $url = Fast.api.fixurl(k);
Karson authored
10
11
                    if ($.isArray(v)) {
Karson authored
12 13 14
                        $nums = typeof v[0] !== 'undefined' ? v[0] : 0;
                        $color = typeof v[1] !== 'undefined' ? v[1] : colorArr[(!isNaN($nums) ? $nums : $nums.length) % $colorNums];
                        $class = typeof v[2] !== 'undefined' ? v[2] : 'label';
15
                    } else {
Karson authored
16 17 18 19 20 21 22 23
                        $nums = v;
                        $color = colorArr[(!isNaN($nums) ? $nums : $nums.length) % $colorNums];
                        $class = 'label';
                    }
                    //必须nums大于0才显示
                    badgeList[$url] = $nums > 0 ? '<small class="' + $class + ' pull-right bg-' + $color + '">' + $nums + '</small>' : '';
                });
                $.each(badgeList, function (k, v) {
24
                    var anchor = top.window.$("li a[addtabs][url='" + k + "']");
Karson authored
25
                    if (anchor) {
Karson authored
26 27
                        top.window.$(".pull-right-container", anchor).html(v);
                        top.window.$(".nav-addtabs li a[node-id='" + anchor.attr("addtabs") + "'] .pull-right-container").html(v);
Karson authored
28 29 30
                    }
                });
            },
Karson authored
31
            addtabs: function (url, title, icon) {
Karson authored
32
                var dom = "a[url='{url}']"
Karson authored
33 34 35 36
                var leftlink = top.window.$(dom.replace(/\{url\}/, url));
                if (leftlink.size() > 0) {
                    leftlink.trigger("click");
                } else {
37
                    url = Fast.api.fixurl(url);
Karson authored
38 39 40 41 42 43 44 45 46
                    leftlink = top.window.$(dom.replace(/\{url\}/, url));
                    if (leftlink.size() > 0) {
                        var event = leftlink.parent().hasClass("active") ? "dblclick" : "click";
                        leftlink.trigger(event);
                    } else {
                        var baseurl = url.substr(0, url.indexOf("?") > -1 ? url.indexOf("?") : url.length);
                        leftlink = top.window.$(dom.replace(/\{url\}/, baseurl));
                        //能找到相对地址
                        if (leftlink.size() > 0) {
Karson authored
47 48
                            icon = typeof icon !== 'undefined' ? icon : leftlink.find("i").attr("class");
                            title = typeof title !== 'undefined' ? title : leftlink.find("span:first").text();
Karson authored
49 50
                            leftlink.trigger("fa.event.toggleitem");
                        }
51
                        var navnode = top.window.$(".nav-tabs ul li a[node-url='" + url + "']");
Karson authored
52 53 54 55 56
                        if (navnode.size() > 0) {
                            navnode.trigger("click");
                        } else {
                            //追加新的tab
                            var id = Math.floor(new Date().valueOf() * Math.random());
Karson authored
57 58
                            icon = typeof icon !== 'undefined' ? icon : 'fa fa-circle-o';
                            title = typeof title !== 'undefined' ? title : '';
59 60 61 62
                            top.window.$("<a />").append('<i class="' + icon + '"></i> <span>' + title + '</span>').prop("href", url).attr({
                                url: url,
                                addtabs: id
                            }).addClass("hide").appendTo(top.window.document.body).trigger("click");
Karson authored
63 64 65 66
                        }
                    }
                }
            },
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
            closetabs: function (url) {
                if (typeof url === 'undefined') {
                    top.window.$("ul.nav-addtabs li.active .close-tab").trigger("click");
                } else {
                    var dom = "a[url='{url}']"
                    var navlink = top.window.$(dom.replace(/\{url\}/, url));
                    if (navlink.size() === 0) {
                        url = Fast.api.fixurl(url);
                        navlink = top.window.$(dom.replace(/\{url\}/, url));
                        if (navlink.size() === 0) {
                        } else {
                            var baseurl = url.substr(0, url.indexOf("?") > -1 ? url.indexOf("?") : url.length);
                            navlink = top.window.$(dom.replace(/\{url\}/, baseurl));
                            //能找到相对地址
                            if (navlink.size() === 0) {
                                navlink = top.window.$(".nav-tabs ul li a[node-url='" + url + "']");
                            }
                        }
                    }
                    if (navlink.size() > 0 && navlink.attr('addtabs')) {
                        top.window.$("ul.nav-addtabs li#tab_" + navlink.attr('addtabs') + " .close-tab").trigger("click");
                    }
                }
            },
91 92 93 94 95 96 97 98 99 100 101 102
            replaceids: function (elem, url) {
                //如果有需要替换ids的
                if (url.indexOf("{ids}") > -1) {
                    var ids = 0;
                    var tableId = $(elem).data("table-id");
                    if (tableId && $(tableId).size() > 0 && $(tableId).data("bootstrap.table")) {
                        var Table = require("table");
                        ids = Table.api.selectedids($(tableId)).join(",");
                    }
                    url = url.replace(/\{ids\}/g, ids);
                }
                return url;
103 104 105
            },
            refreshmenu: function () {
                top.window.$(".sidebar-menu").trigger("refresh");
106
            },
107
            gettablecolumnbutton: function (options) {
108
                if (typeof options.tableId !== 'undefined' && typeof options.fieldIndex !== 'undefined' && typeof options.buttonIndex !== 'undefined') {
109 110
                    var tableOptions = $("#" + options.tableId).bootstrapTable('getOptions');
                    if (tableOptions) {
111 112 113 114 115 116 117 118 119 120 121 122 123
                        var columnObj = null;
                        $.each(tableOptions.columns, function (i, columns) {
                            $.each(columns, function (j, column) {
                                if (typeof column.fieldIndex !== 'undefined' && column.fieldIndex === options.fieldIndex) {
                                    columnObj = column;
                                    return false;
                                }
                            });
                            if (columnObj) {
                                return false;
                            }
                        });
                        if (columnObj) {
124
                            return columnObj['buttons'][options.buttonIndex];
125 126 127
                        }
                    }
                }
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
                return null;
            },
        },
        init: function () {
            //公共代码
            //添加ios-fix兼容iOS下的iframe
            if (/iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream) {
                $("html").addClass("ios-fix");
            }
            //配置Toastr的参数
            Toastr.options.positionClass = Config.controllername === 'index' ? "toast-top-right-index" : "toast-top-right";
            //点击包含.btn-dialog的元素时弹出dialog
            $(document).on('click', '.btn-dialog,.dialogit', function (e) {
                var that = this;
                var options = $.extend({}, $(that).data() || {});
                var url = Backend.api.replaceids(that, $(that).attr('href'));
                var title = $(that).attr("title") || $(that).data("title") || $(that).data('original-title');
                var button = Backend.api.gettablecolumnbutton(options);
                if (button && typeof button.callback === 'function') {
                    options.callback = button.callback;
                }
149 150
                if (typeof options.confirm !== 'undefined') {
                    Layer.confirm(options.confirm, function (index) {
151
                        Backend.api.open(url, title, options);
152 153 154
                        Layer.close(index);
                    });
                } else {
155
                    Backend.api.open(url, title, options);
156
                }
Karson authored
157
                return false;
158
            });
159
            //点击包含.btn-addtabs的元素时新增选项卡
Karson authored
160
            $(document).on('click', '.btn-addtabs,.addtabsit', function (e) {
161
                var that = this;
162
                var options = $.extend({}, $(that).data() || {});
163 164
                var url = Backend.api.replaceids(that, $(that).attr('href'));
                var title = $(that).attr("title") || $(that).data("title") || $(that).data('original-title');
165 166
                if (typeof options.confirm !== 'undefined') {
                    Layer.confirm(options.confirm, function (index) {
167
                        Backend.api.addtabs(url, title);
168 169 170
                        Layer.close(index);
                    });
                } else {
171
                    Backend.api.addtabs(url, title);
172
                }
Karson authored
173
                return false;
Karson authored
174
            });
175
            //点击包含.btn-ajax的元素时发送Ajax请求
Karson authored
176
            $(document).on('click', '.btn-ajax,.ajaxit', function (e) {
177
                var that = this;
178
                var options = $.extend({}, $(that).data() || {});
179 180
                if (typeof options.url === 'undefined' && $(that).attr("href")) {
                    options.url = $(that).attr("href");
Karson authored
181
                }
182
                options.url = Backend.api.replaceids(this, options.url);
183 184 185 186
                var success = typeof options.success === 'function' ? options.success : null;
                var error = typeof options.error === 'function' ? options.error : null;
                delete options.success;
                delete options.error;
187 188 189 190 191 192 193
                var button = Backend.api.gettablecolumnbutton(options);
                if (button) {
                    if (typeof button.success === 'function') {
                        success = button.success;
                    }
                    if (typeof button.error === 'function') {
                        error = button.error;
194 195
                    }
                }
196 197 198 199
                //如果未设备成功的回调,设定了自动刷新的情况下自动进行刷新
                if (!success && typeof options.tableId !== 'undefined' && typeof options.refresh !== 'undefined' && options.refresh) {
                    $("#" + options.tableId).bootstrapTable('refresh');
                }
200 201 202 203 204 205 206 207
                if (typeof options.confirm !== 'undefined') {
                    Layer.confirm(options.confirm, function (index) {
                        Backend.api.ajax(options, success, error);
                        Layer.close(index);
                    });
                } else {
                    Backend.api.ajax(options, success, error);
                }
Karson authored
208
                return false;
Karson authored
209
            });
210 211
            //修复含有fixed-footer类的body边距
            if ($(".fixed-footer").size() > 0) {
Karson authored
212
                $(document.body).css("padding-bottom", $(".fixed-footer").outerHeight());
213
            }
214 215 216 217
            //修复不在iframe时layer-footer隐藏的问题
            if ($(".layer-footer").size() > 0 && self === top) {
                $(".layer-footer").show();
            }
218
            //tooltip和popover
219 220 221 222
            if (!('ontouchstart' in document.documentElement)) {
                $('body').tooltip({selector: '[data-toggle="tooltip"]'});
            }
            $('body').popover({selector: '[data-toggle="popover"]'});
Karson authored
223 224
        }
    };
225
    Backend.api = $.extend(Fast.api, Backend.api);
226 227
    //将Template渲染至全局,以便于在子框架中调用
    window.Template = Template;
228 229
    //将Moment渲染至全局,以便于在子框架中调用
    window.Moment = Moment;
Karson authored
230 231
    //将Backend渲染至全局,以便于在子框架中调用
    window.Backend = Backend;
Karson authored
232
233
    Backend.init();
Karson authored
234 235
    return Backend;
});