colly_wyx
2018-05-29 d479d9fbcb37f8b861031c94a83d0e37761210c5
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
/* ==========================================================
 * sco.modal.js
 * http://github.com/terebentina/sco.js
 * ==========================================================
 * Copyright 2013 Dan Caragea.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ========================================================== */
 
/*jshint laxcomma:true, sub:true, browser:true, jquery:true, devel:true, eqeqeq:false */
/*global Spinner:true */
 
;(function($, undefined) {
    "use strict";
 
    var pluginName = 'scojs_modal';
 
    function Modal(options) {
        this.options = $.extend({}, $.fn[pluginName].defaults, options);
        this.$modal = $(this.options.target).attr('class', 'modal fade').hide();
        var self = this;
 
        function init() {
            if (self.options.title === '') {
                self.options.title = ' ';
            }
        };
 
        init();
    }
 
 
    $.extend(Modal.prototype, {
        show: function() {
            var self = this
                ,$backdrop;
 
            if (!this.options.nobackdrop) {
                $backdrop = $('.modal-backdrop');
            }
            if (!this.$modal.length) {
                this.$modal = $('<div class="modal fade" id="' + this.options.target.substr(1) + '"><div class="modal-header"><a class="close" href="#" data-dismiss="modal">×</a><h3>&nbsp;</h3></div><div class="inner"/></div>').appendTo(this.options.appendTo).hide();
            }
 
            this.$modal.find('.modal-header h3').html(this.options.title);
 
            if (this.options.cssclass !== undefined) {
                this.$modal.attr('class', 'modal fade ' + this.options.cssclass);
            }
 
            if (this.options.width !== undefined) {
                this.$modal.width(this.options.width);
            }
 
            if (this.options.left !== undefined) {
                this.$modal.css({'left': this.options.left});
            }
 
            if (this.options.height !== undefined) {
                this.$modal.height(this.options.height);
            }
 
            if (this.options.top !== undefined) {
                this.$modal.css({'top': this.options.top});
            }
 
            if (this.options.keyboard) {
                this.escape();
            }
 
            if (!this.options.nobackdrop) {
                if (!$backdrop.length) {
                    $backdrop = $('<div class="modal-backdrop fade" />').appendTo(this.options.appendTo);
                }
                $backdrop[0].offsetWidth; // force reflow
                $backdrop.addClass('in');
            }
 
            this.$modal.off('close.' + pluginName).on('close.' + pluginName, function() {
                self.close.call(self);
            });
            if (this.options.remote !== undefined && this.options.remote != '' && this.options.remote !== '#') {
                var spinner;
                if (typeof Spinner == 'function') {
                    spinner = new Spinner({color: '#3d9bce'}).spin(this.$modal[0]);
                }
                this.$modal.find('.inner').load(this.options.remote, function() {
                    if (spinner) {
                        spinner.stop();
                    }
                    if (self.options.cache) {
                        self.options.content = $(this).html();
                        delete self.options.remote;
                    }
                });
            } else {
                this.$modal.find('.inner').html(this.options.content);
            }
 
            this.$modal.show().addClass('in');
            return this;
        }
 
        ,close: function() {
            this.$modal.hide().off('.' + pluginName).find('.inner').html('');
            if (this.options.cssclass !== undefined) {
                this.$modal.removeClass(this.options.cssclass);
            }
            $(document).off('keyup.' + pluginName);
            $('.modal-backdrop').remove();
            if (typeof this.options.onClose === 'function') {
                this.options.onClose.call(this, this.options);
            }
            return this;
        }
 
        ,destroy: function() {
            this.$modal.remove();
            $(document).off('keyup.' + pluginName);
            $('.modal-backdrop').remove();
            this.$modal = null;
            return this;
        }
 
        ,escape: function() {
            var self = this;
            $(document).on('keyup.' + pluginName, function(e) {
                if (e.which == 27) {
                    self.close();
                }
            });
        }
    });
 
 
    $.fn[pluginName] = function(options) {
        return this.each(function() {
            var obj;
            if (!(obj = $.data(this, pluginName))) {
                var  $this = $(this)
                    ,data = $this.data()
                    ,opts = $.extend({}, options, data)
                    ;
                if ($this.attr('href') !== '' && $this.attr('href') != '#') {
                    opts.remote = $this.attr('href');
                }
                obj = new Modal(opts);
                $.data(this, pluginName, obj);
            }
            obj.show();
        });
    };
 
 
    $[pluginName] = function(options) {
        return new Modal(options);
    };
 
 
    $.fn[pluginName].defaults = {
        title: '&nbsp;'        // modal title
        ,target: '#modal'    // the modal id. MUST be an id for now.
        ,content: ''        // the static modal content (in case it's not loaded via ajax)
        ,appendTo: 'body'    // where should the modal be appended to (default to document.body). Added for unit tests, not really needed in real life.
        ,cache: false        // should we cache the output of the ajax calls so that next time they're shown from cache?
        ,keyboard: false
        ,nobackdrop: false
    };
 
 
    $(document).on('click.' + pluginName, '[data-trigger="modal"]', function() {
        $(this)[pluginName]();
        if ($(this).is('a')) {
            return false;
        }
    }).on('click.' + pluginName, '[data-dismiss="modal"]', function(e) {
        e.preventDefault();
        $(this).closest('.modal').trigger('close');
    });
})(jQuery);