PageSourceSearch

http://8.498.net/Public/js/dialog.js

js 498.net collected 2026-10-02 15:22:29 UTC 10,463 bytes, 265 lines download raw bytes

1var Modal = (function ($) {
2    var $body = $(document.body),
3        BackDrop = (function () {
4            var $backDrop,
5                count = 0,
6                create = function () {
7                    $backDrop = $('<div class="modal-backdrop fade in"></div>').appendTo($body);
8                };
9
10            return {
11                show: function () {
12                    !$backDrop && create();
13                    $backDrop[0].style.display = 'block';
14                    count++;
15                },
16                hide: function () {
17                    count--;
18                    if (!count) {
19                        $backDrop.remove();
20                        $backDrop = undefined;
21                    }
22                }
23            }
24        })(),
25        getTpl = function () {
26            return ['<div class="modal fade" style="z-index: 1055;" data-backdrop="false" data-show="false" data-keyboard="false">',
27                '    <div class="modal-dialog">',
28                '        <div class="modal-content">',
29                '            <div class="modal-header">',
30                '                <h4 class="modal-title"></h4>',
31                '            </div>',
32                '            <div class="modal-body"></div>',
33                '            <div class="modal-footer"></div>',
34                '        </div>',
35                '    </div>',
36                '</div>'].join("");
37        },
38        initModal = function (that, opts) {
39            var $modal = createModal(that);
40            that.setTitle(opts.title);
41            that.setContent(opts.content);
42            that.addButtons(opts.buttons);
43            that.setWidth(opts.width);
44            bindHandler(that, opts);
45            $modal.modal();//调用bootstrap的Modal组件
46            $modal.trigger('contentReady');
47        },
48        createModal = function (that) {
49            var $modal = that.$modal = $(getTpl()).appendTo($body);
50            that.$modalTitle = $modal.find('.modal-title');
51            that.$modalBody = $modal.find('.modal-body');
52            that.$modalFooter = $modal.find('.modal-footer');
53            return $modal;
54        },
55        bindHandler = function (that, opts) {
56            var $modal = that.$modal;
57            typeof opts.onContentChange === 'function' && $modal.on('contentChange', $.proxy(opts.onContentChange, that));
58            typeof opts.onContentReady === 'function' && $modal.on('contentReady', $.proxy(opts.onContentReady, that));
59            typeof opts.onModalShow === 'function' && $modal.on('modalShow', $.proxy(opts.onModalShow, that));
60            typeof opts.onModalHide === 'function' && $modal.on('modalHide', $.proxy(opts.onModalHide, that));
61            $modal.on('show.bs.modal', function () {
62                $modal.trigger('modalShow');
63            });
64            $modal.on('hidden.bs.modal', function () {
65                $modal.trigger('modalHide');
66            });
67        },
68        getDefaultBtnCallbackProxy = function (callbackName) {
69            return function () {
70                var opts = this.options,
71                    callback = opts[callbackName] && typeof opts[callbackName] === 'function' ? opts[callbackName] : '';
72
73                return callback && callback.apply(this, arguments);
74            }
75        };
76
77    function ModalDialog(options) {
78        this.options = this.getOptions(options);
79        this.$modal = undefined;
80        this.$modalTitle = undefined;
81        this.$modalBody = undefined;
82        this.$modalFooter = undefined;
83        this.state = undefined;
84    }
85
86    ModalDialog.defaults = {
87        title: '',
88        content: '',
89        width: 600,
90        buttons: [
91            {
92                html: '<button type="button" class="btn btn-sm btn-primary btn-ok">确定</button>',
93                selector: '.btn-ok',
94                callback: getDefaultBtnCallbackProxy('onOk')
95            },
96            {
97                html: '<button type="button" class="btn btn-sm btn-default btn-cancel">取消</button>',
98                selector: '.btn-cancel',
99                callback: getDefaultBtnCallbackProxy('onCancel')
100            }
101        ],
102        onOk: $.noop,
103        onCancel: $.noop,
104        onContentReady: $.noop,
105        onContentChange: $.noop,//content替换之后的回调
106        onModalShow: $.noop,
107        onModalHide: $.noop//modal关闭之后的回调
108    };
109
110    $.extend(ModalDialog.prototype, {
111        getOptions: function (options) {
112            return $.extend({}, ModalDialog.defaults, options || {});
113        },
114        setOptions: function (options) {
115            return $.extend(this.options, options || {});
116        },
117        open: function (state) {
118            this.state = state;
119            !this.$modal && initModal(this, this.options);
120            BackDrop.show();
121            this.$modal.modal('show');
122        },
123        hide: function () {
124            var $modal = this.$modal;
125            $modal.modal('hide');
126            $modal.one('hidden.bs.modal', function () {
127                BackDrop.hide();
128            });
129        },
130        setTitle: function (title, html) {
131            this.$modalTitle[html === true ? 'html' : 'text'](title);
132        },
133        setContent: (function () {
134            var init = true;
135            return function (content) {
136                this.$modalBody.html(content);
137                !init ? this.$modal.trigger('contentChange') : (init = false);
138            }
139        })(),
140        addButtons: function (buttons) {
141            var buttons = !$.isArray(buttons) ? [] : buttons,
142                that = this,
143                htmlS = [];
144            buttons.forEach(function (btn) {
145                htmlS.push(btn.html);
146
147                btn.selector && that.$modal.on('click', btn.selector, $.proxy(function (e) {
148
149                    var self = this,
150                        $btn = $(e.currentTarget);
151
152                    //先禁用按钮
153                    $btn[0].disabled = true;
154
155                    var callback = typeof btn.callback === 'function' ? btn.callback : '',
156                        ret = callback && callback.apply(self, arguments);
157
158                    if (ret === false) {
159                        $btn[0].disabled = false;
160                        return;
161                    }
162
163                    if (typeof(ret) === 'object' && 'done' in ret && typeof ret['done'] === 'function') {
164                        //异步任务只有在成功回调的时候关闭Modal
165                        ret.done(function () {
166                            that.hide();
167                        }).always(function () {
168                            $btn[0].disabled = false;
169                        });
170                    } else {
171                        $btn[0].disabled = false;
172                        that.hide();
173                    }
174
175                }, that));
176            });
177
178            this.$modalFooter.prepend($(htmlS.join('')));
179        },
180        hideButton: function (selector) {
181            selector && this.$modalFooter.find(selector).hide();
182        },
183        showButton: function (selector) {
184            selector && this.$modalFooter.find(selector).show();
185        },
186        setWidth: function (width) {
187            this.$modal.find('.modal-dialog').css('width', width);
188        }
189    });
190
191    //针对这些方法统一加个校验
192    ['setTitle', 'setContent', 'addButtons', 'hideButton', 'showButton', 'setWidth'].forEach(function (name) {
193        var _old = ModalDialog.prototype[name];
194        ModalDialog.prototype[name] = function () {
195            if (!arguments.length || !this.$modal) {
196                return;
197            }
198            return _old.apply(this, arguments);
199        }
200    });
201
202    return ModalDialog;
203})(jQuery);
204
205var Alert, Confirm;
206(function () {
207    var modal,
208        Proxy = function (isAlert) {
209            return function () {
210                if (arguments.length != 1 && arguments.length != 2) return;
211                var title = typeof arguments[0] === 'string' && arguments[0] || arguments[0].title || '',
212                    msg = typeof arguments[0] === 'string' && arguments[0] || arguments[0].msg || '',
213                    onOk = typeof arguments[0] === 'object' && typeof arguments[0].onOk === 'function' && arguments[0].onOk,
214                    onCancel = typeof arguments[0] === 'object' && typeof arguments[0].onCancel === 'function' && arguments[0].onCancel,
215                    width = typeof arguments[0] === 'object' && arguments[0].width || 400,
216                    _onModalShow = function () {
217                        this.setWidth(width);
218                        this.setContent(msg);
219                        this[(isAlert ? 'hide' : 'show') + 'Button']('.btn-cancel');
220                    },
221                    _onModalHide = function () {
222                        this.setContent('');
223                    };
224                if ( arguments.length == 1 && (msg == title) ) {
225                    title = '';
226                }
227                if ( arguments.length == 2 ) {
228                    msg = typeof arguments[1] === 'string' && arguments[1] || arguments[1].msg || '';
229                }
230
231                //延迟初始化modal
232                if(!modal) {
233                    modal = new Modal({
234                        'title': title || '操作提示',
235                        onModalShow: _onModalShow,
236                        onModalHide: _onModalHide,
237                        onContentReady: function(){
238                            this.$modalBody.css({
239                                'padding-top': '30px',
240                                'padding-bottom': '30px'
241                            })
242                        }
243                    });
244                } else {
245                    var $modal = modal.$modal;
246                    //如果modal已经初始化则需要重新监听事件
247                    $modal.off('modalShow modalHide');
248                    $modal.off('modalShow modalHide');
249                    $modal.on('modalShow', $.proxy(_onModalShow, modal));
250                    $modal.on('modalHide', $.proxy(_onModalHide, modal));
251                }
252
253                modal.setOptions({
254                    onOk: onOk || $.noop,
255                    onCancel: onCancel || $.noop
256                });
257
258                modal.open();
259                $(".modal-footer button").focus();
260            }
261        };
262
263    Alert = Proxy(true);
264    Confirm = Proxy();
265})();

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.