|
52 | 52 | onHide: false |
53 | 53 | }; |
54 | 54 |
|
| 55 | + |
| 56 | + var popovers = []; |
| 57 | + var backdrop = $('<div class="webui-popover-backdrop"></div>'); |
55 | 58 | var _globalIdSeed = 0; |
56 | 59 | var $document = $(document); |
57 | 60 |
|
58 | 61 |
|
| 62 | + |
59 | 63 | // The actual plugin constructor |
60 | 64 | function WebuiPopover(element, options) { |
61 | 65 | this.$element = $(element); |
|
72 | 76 | this._name = pluginName; |
73 | 77 | this._targetclick = false; |
74 | 78 | this.init(); |
| 79 | + popovers.push(this.$element); |
75 | 80 | } |
76 | 81 |
|
77 | 82 | WebuiPopover.prototype = { |
|
91 | 96 | } |
92 | 97 | this._poped = false; |
93 | 98 | this._inited = true; |
| 99 | + this._opened = false; |
94 | 100 | this._idSeed = _globalIdSeed; |
| 101 | + if (this.options.backdrop) { |
| 102 | + backdrop.appendTo(document.body).hide(); |
| 103 | + } |
95 | 104 | _globalIdSeed++; |
96 | 105 | }, |
97 | 106 | /* api methods and actions */ |
98 | 107 | destroy: function() { |
| 108 | + var index = -1; |
| 109 | + |
| 110 | + for (var i = 0; i < popovers.length; i++) { |
| 111 | + if (popovers[i] === this.$element) { |
| 112 | + index = i; |
| 113 | + break; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + popovers.splice(index, 1); |
| 118 | + |
| 119 | + |
99 | 120 | this.hide(); |
100 | 121 | this.$element.data('plugin_' + pluginName, null); |
101 | 122 | if (this.getTrigger() === 'click') { |
|
108 | 129 | } |
109 | 130 | }, |
110 | 131 | hide: function(event) { |
| 132 | + if (!this._opened) { |
| 133 | + return; |
| 134 | + } |
111 | 135 | if (event) { |
112 | 136 | event.preventDefault(); |
113 | 137 | event.stopPropagation(); |
|
122 | 146 | if (this.$target) { |
123 | 147 | this.$target.removeClass('in').hide(); |
124 | 148 | } |
| 149 | + if (this.options.backdrop) { |
| 150 | + backdrop.hide(); |
| 151 | + } |
| 152 | + this._opened = false; |
125 | 153 | this.$element.trigger('hidden.' + pluginType); |
126 | 154 |
|
127 | 155 | if (this.options.onShow) { |
|
137 | 165 | this[this.getTarget().hasClass('in') ? 'hide' : 'show'](); |
138 | 166 | }, |
139 | 167 | hideAll: function() { |
140 | | - $('div.webui-popover').not('.webui-popover-fixed').removeClass('in').hide(); |
| 168 | + for (var i = 0; i < popovers.length; i++) { |
| 169 | + popovers[i].webuiPopover('hide'); |
| 170 | + } |
| 171 | + |
141 | 172 | $document.trigger('hiddenAll.' + pluginType); |
142 | 173 | }, |
143 | 174 | /*core method ,show popover */ |
|
148 | 179 | if (!this.options.multi) { |
149 | 180 | this.hideAll(); |
150 | 181 | } |
| 182 | + if (this._opened) { |
| 183 | + return; |
| 184 | + } |
151 | 185 | // use cache by default, if not cache setted , reInit the contents |
152 | 186 | if (!this.getCache() || !this._poped || this.content === '') { |
153 | 187 | this.content = ''; |
|
171 | 205 | } |
172 | 206 |
|
173 | 207 | this.bindBodyEvents(); |
| 208 | + if (this.options.backdrop) { |
| 209 | + backdrop.show(); |
| 210 | + } |
| 211 | + this._opened = true; |
174 | 212 | }, |
175 | 213 | displayContent: function() { |
176 | 214 | var |
|
244 | 282 | } |
245 | 283 | this._poped = true; |
246 | 284 | this.$element.trigger('shown.' + pluginType); |
247 | | - |
248 | | - |
249 | 285 | }, |
250 | 286 |
|
251 | 287 | isTargetLoaded: function() { |
|
672 | 708 | }; |
673 | 709 | } |
674 | 710 | }; |
675 | | - $.fn[pluginName] = function(options) { |
676 | | - return this.each(function() { |
| 711 | + $.fn[pluginName] = function(options, noInit) { |
| 712 | + var results = []; |
| 713 | + var $result = this.each(function() { |
| 714 | + |
677 | 715 | var webuiPopover = $.data(this, 'plugin_' + pluginName); |
678 | 716 | if (!webuiPopover) { |
679 | 717 | if (!options) { |
680 | 718 | webuiPopover = new WebuiPopover(this, null); |
681 | 719 | } else if (typeof options === 'string') { |
682 | 720 | if (options !== 'destroy') { |
683 | | - webuiPopover = new WebuiPopover(this, null); |
684 | | - webuiPopover[options](); |
| 721 | + if (!noInit) { |
| 722 | + webuiPopover = new WebuiPopover(this, null); |
| 723 | + results.push(webuiPopover[options]()); |
| 724 | + } |
685 | 725 | } |
686 | 726 | } else if (typeof options === 'object') { |
687 | 727 | webuiPopover = new WebuiPopover(this, options); |
|
691 | 731 | if (options === 'destroy') { |
692 | 732 | webuiPopover.destroy(); |
693 | 733 | } else if (typeof options === 'string') { |
694 | | - webuiPopover[options](); |
| 734 | + results.push(webuiPopover[options]()); |
695 | 735 | } |
696 | 736 | } |
697 | 737 | }); |
| 738 | + |
| 739 | + return (results.length) ? results : $result; |
698 | 740 | }; |
699 | 741 |
|
700 | 742 | })(jQuery, window, document); |
0 commit comments