zhuguifei
2026-03-10 2c1fd10c6fbabb8e9f0e9f07fe66fb36c008e883
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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
232
/**
* jQuery EasyUI 1.3.6
* Copyright (c) 2009-2014 www.jeasyui.com. All rights reserved.
*
* Licensed under the GPL or commercial licenses
* To use it on other terms please contact author: info@jeasyui.com
* http://www.gnu.org/licenses/gpl.txt
* http://www.jeasyui.com/license_commercial.php
*
* jQuery EasyUI window Extensions 1.0 beta
* jQuery EasyUI window 组件扩展
* jeasyui.extensions.window.js
* 二次开发 流云
* 最近更新:2014-04-09
*
* 依赖项:
*   1、jquery.jdirk.js v1.0 beta late
*   2、jeasyui.extensions.js v1.0 beta late
*   3、jeasyui.extensions.menu.js v1.0 beta late
*   4、jeasyui.extensions.panel.js v1.0 beta late
*
* Copyright (c) 2013-2014 ChenJianwei personal All rights reserved.
* http://www.chenjianwei.org
*/
(function ($, undefined) {
 
 
    $.fn.window.extensions = {};
 
 
    var initialize = function (target) {
        var t = $(target), state = $.data(target, "window"), opts = t.window("options"), win = t.window("window"), body = t.window("body");
        if (!opts._initialized) {
            t.window("header").on({
                dblclick: function () {
                    var opts = t.window("options");
                    if (opts.autoRestore) { if (opts.maximized) { t.window("restore"); } else if (opts.maximizable) { t.window("maximize"); } }
                },
                contextmenu: function (e) {
                    var opts = t.window("options");
                    if (opts.enableHeaderContextMenu) {
                        e.preventDefault();
                        var items = [
                            { text: "最大化", iconCls: "panel-tool-max", disabled: !opts.maximized && opts.maximizable ? false : true, onclick: function () { t.window("maximize"); } },
                            { text: "恢复", iconCls: "panel-tool-restore", disabled: opts.maximized ? false : true, onclick: function () { t.window("restore"); } },
                            "-",
                            { text: "关闭", iconCls: "panel-tool-close", disabled: !opts.closable, onclick: function () { t.window("close"); } }
                        ];
                        var headerContextMenu = $.array.likeArray(opts.headerContextMenu) ? opts.headerContextMenu : [];
                        if (headerContextMenu.length) { $.array.insertRange(items, 0, $.util.merge([], headerContextMenu, "-")); }
                        items = parseContextMenuMap(e, items, t);
                        $.easyui.showMenu({ items: items, left: e.pageX, top: e.pageY });
                    }
                }
            });
            if (opts.bodyCls) { body.addClass(opts.bodyCls); }
 
            if (opts.draggable) {
                var dragOpts = state.window.draggable("options"), cursor = dragOpts.cursor,
                    onBeforeDrag = dragOpts.onBeforeDrag, onStartDrag = dragOpts.onStartDrag, onStopDrag = dragOpts.onStopDrag, onDrag = dragOpts.onDrag;
                dragOpts.cursor = "default";
                dragOpts.onBeforeDrag = function (e) {
                    var ret = onBeforeDrag.apply(this, arguments);
                    if (ret == false || e.which != 1 || t.window("options").maximized) { return false; }
                    dragOpts.cursor = cursor;
                };
                dragOpts.onStartDrag = function () {
                    onStartDrag.apply(this, arguments);
                    t.window("body").addClass("window-body-hidden").children().addClass("window-body-hidden-proxy");
                };
                dragOpts.onStopDrag = function () {
                    onStopDrag.apply(this, arguments);
                    t.window("body").removeClass("window-body-hidden").children().removeClass("window-body-hidden-proxy");
                    dragOpts.cursor = "default";
                };
                dragOpts.onDrag = function (e) {
                    if (!opts.inContainer) { return onDrag.apply(this, arguments); }
                    var left = e.data.left, top = e.data.top,
                        p = win.parent(), root = p.is("body"),
                        scope = $.extend({}, root ? $.util.windowSize() : { width: p.innerWidth(), height: p.innerHeight() }),
                        width = $.isNumeric(opts.width) ? opts.width : win.outerWidth(),
                        height = $.isNumeric(opts.height) ? opts.height : win.outerHeight();
                    if (left < 0) { left = 0; }
                    if (top < 0) { top = 0; }
                    if (left + width > scope.width && left > 0) { left = scope.width - width; b = true; }
                    if (top + height > scope.height && top > 0) { top = scope.height - height; b = true; }
                    state.proxy.css({
                        display: 'block',
                        left: left,
                        top: top
                    });
                    return false;
                };
            }
            if (opts.resizable) {
                var resizableOpts = state.window.resizable("options"),
                    _onResize = resizableOpts.onResize, _onStopResize = resizableOpts.onStopResize;
                resizableOpts.onResize = function (e) {
                    if (!opts.minWidth && !opts.maxWidth && !opts.minHeight && !opts.maxHeight) {
                        return _onResize.apply(this, arguments);
                    }
                    state.proxy.css({ left: e.data.left, top: e.data.top });
                    var width = e.data.width, height = e.data.height,
                        minWidth = $.isNumeric(opts.minWidth) ? opts.minWidth : defaults.minHeight,
                        maxWidth = $.isNumeric(opts.maxWidth) ? opts.maxWidth : defaults.maxWidth,
                        minHeight = $.isNumeric(opts.minHeight) ? opts.minHeight : defaults.minHeight,
                        maxHeight = $.isNumeric(opts.maxHeight) ? opts.maxHeight : defaults.maxHeight;
                    if (width > opts.maxWidth) { width = maxWidth; resizable = true; }
                    if (width < opts.minWidth) { width = minWidth; resizable = true; }
                    if (height > opts.maxHeight) { height = maxHeight; resizable = true; }
                    if (height < opts.minHeight) { height = minHeight; resizable = true; }
                    state.proxy._outerWidth(width);
                    state.proxy._outerHeight(height);
                    return false;
                };
                resizableOpts.onStopResize = function (e) {
                    var ret = _onStopResize.apply(this, arguments);
                    if (t.window("options").maximized) {
                        t.window("restore").window("maximize");
                    }
                    return ret;
                };
            }
 
            opts._initialized = true;
        }
    };
 
    function parseContextMenuMap(e, menus, win) {
        return $.array.map(menus, function (value, index) {
            if (!value || $.util.isString(value)) { return value; }
            var ret = $.extend({}, value);
            ret.id = $.isFunction(value.id) ? value.id.call(ret, e, win) : value.id;
            ret.text = $.isFunction(value.text) ? value.text.call(ret, e, win) : value.text;
            ret.iconCls = $.isFunction(value.iconCls) ? value.iconCls.call(ret, e, win) : value.iconCls;
            ret.disabled = $.isFunction(value.disabled) ? value.disabled.call(ret, e, win) : value.disabled;
            ret.hideOnClick = $.isFunction(value.hideOnClick) ? value.hideOnClick.call(ret, e, win) : value.hideOnClick;
            ret.onclick = $.isFunction(value.onclick) ? function (e, item, menu) { value.onclick.call(this, e, win, item, menu); } : value.onclick;
            ret.handler = $.isFunction(value.handler) ? function (e, item, menu) { value.handler.call(this, e, win, item, menu); } : value.handler;
            if (ret.children && ret.children.length) { ret.children = parseContextMenuMap(e, ret.children, win); }
            return ret;
        });
    };
 
 
    var _window = $.fn.window;
    $.fn.window = function (options, param) {
        if (typeof options == "string") {
            return _window.apply(this, arguments);
        }
        options = options || {};
        return this.each(function () {
            var jq = $(this), hasInit = $.data(this, "window") ? true : false,
                opts = hasInit ? options : $.extend({}, $.fn.window.parseOptions(this), $.parser.parseOptions(this, [{
                    autoHCenter: "boolean", autoVCenter: "boolean", autoCloseOnEsc: "boolean",
                    autoRestore: "boolean", enableHeaderContextMenu: "boolean"
                }]), options);
            _window.call(jq, opts);
            initialize(this);
        });
    };
    $.union($.fn.window, _window);
 
 
 
    var methods = $.fn.window.extensions.methods = {};
    var defaults = $.fn.window.extensions.defaults = $.extend({}, $.fn.panel.extensions.defaults, {
 
        //  扩展 easyui-window 以及 easyui-dialog 控件的自定义属性,表示该窗口对象是否在屏幕大小调整的情况下自动进行左右居中,默认为 true。
        autoHCenter: false,
 
        //  扩展 easyui-window 以及 easyui-dialog 控件的自定义属性,表示该窗口对象是否在屏幕大小调整的情况下自动进行上下居中,默认为 true。
        autoVCenter: false,
 
        //  扩展 easyui-window 以及 easyui-dialog 控件的自定义属性,表示该窗口对象是否在按下 ESC,默认为 true。
        autoCloseOnEsc: true,
 
        //  扩展 easyui-window 以及 easyui-dialog 控件的自定义属性,表示该窗口是否在双击头部时自动最大化。
        autoRestore: true,
 
        //  扩展 easyui-window 以及 easyui-dialog 控件的自定义属性,表示是否启用该窗口的右键菜单。
        enableHeaderContextMenu: true,
 
        //  扩展 easyui-window 以及 easyui-dialog 控件的自定义属性,表示该窗口的右键菜单;
        //  这是一个数组格式对象,数组中的每一项都是一个 menu-item 元素;该 menu-item 元素格式定义如下:
        //      id:         表示菜单项的 id;
        //      text:       表示菜单项的显示文本;
        //      iconCls:    表示菜单项的左侧显示图标;
        //      disabled:   表示菜单项是否被禁用(禁用的菜单项点击无效);
        //      hideOnClick:    表示该菜单项点击后整个右键菜单是否立即自动隐藏;
        //      bold:           Boolean 类型值,默认为 false;表示该菜单项是否字体加粗;
        //      style:          JSON-Object 类型值,默认为 null;表示要附加到该菜单项的样式;
        //      handler:    表示菜单项的点击事件,该事件函数格式为 function(e, win, item, menu),其中 this 指向菜单项本身
        headerContextMenu: null
    });
 
    $.extend($.fn.window.defaults, defaults);
    $.extend($.fn.window.methods, methods);
 
    $(function () {
        //  设置当屏幕大小调整时,所有 easyui-window 或 easyui-dialog 窗口在属性 autoHCenter: true 或 autoVCenter: true 的情况下自动居中。
        $(window).resize(function () {
            $(".panel-body.window-body").each(function () {
                var win = $(this), opts = win.window("options");
                if (opts && opts.draggable) {
                    if (opts.autoHCenter || opts.autoVCenter) {
                        var method = opts.autoHCenter && opts.autoVCenter ? "center" : (opts.autoHCenter ? "hcenter" : "vcenter");
                        win.window(method);
                    } else if (opts.inContainer) { win.window("move"); }
                }
            });
        });
 
        //  在当前打开 modal:true 的 easyui-window 或者 easyui-dialog 时,按 ESC 键关闭顶层的 easyui-window 或者 easyui-dialog 对象。
        $(document).keydown(function (e) {
            if (e.which == 27) {
                $("div.window-mask:last").prevAll("div.panel.window:first").children(".panel-body.window-body").each(function () {
                    var win = $(this), opts = win.window("options");
                    if (opts && opts.closable && opts.autoCloseOnEsc && !win.window("header").find(".panel-tool a").attr("disabled")) {
                        $.util.exec(function () { win.window("close"); });
                    }
                });
            }
        });
 
        //  点击模式对话框(例如 easyui-messager、easyui-window、easyui-dialog)的背景遮蔽层使窗口闪动
        $("body").on("click", "div.window-mask:last", function (e) {
            $(this).prevAll("div.panel.window:first").shine();
        });
    });
 
})(jQuery);