Skip to content

Commit 97f271c

Browse files
committed
improve hideAll method,add backdrop feature to the plugin
1 parent 9f16e61 commit 97f271c

9 files changed

+136
-21
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ Create Popover and trigged by manual
142142
closeable:false,//display close button or not
143143
padding:true,//content padding
144144
type:'html',//content type, values:'html','iframe','async'
145-
url:''//if not empty ,plugin will load content by url
145+
url:''//if not empty ,plugin will load content by url,
146+
backdrop:false,//if backdrop is set to true, popover will use backdrop on open
146147
}
147148
```
148149

demo/index-dev.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,8 @@ <h2 >Input hints</h2>
440440
closeable:false,
441441
style:'',
442442
delay:300,
443-
padding:true
443+
padding:true,
444+
backdrop:false
444445
};
445446

446447

dist/jquery.webui-popover.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,12 @@
264264
transform: rotate(360deg);
265265
}
266266
}
267+
.webui-popover-backdrop {
268+
background-color: rgba(0, 0, 0, 0.65);
269+
width: 100%;
270+
height: 100%;
271+
position: fixed;
272+
top: 0;
273+
left: 0;
274+
z-index: 9998;
275+
}

dist/jquery.webui-popover.js

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,14 @@
5252
onHide: false
5353
};
5454

55+
56+
var popovers = [];
57+
var backdrop = $('<div class="webui-popover-backdrop"></div>');
5558
var _globalIdSeed = 0;
5659
var $document = $(document);
5760

5861

62+
5963
// The actual plugin constructor
6064
function WebuiPopover(element, options) {
6165
this.$element = $(element);
@@ -72,6 +76,7 @@
7276
this._name = pluginName;
7377
this._targetclick = false;
7478
this.init();
79+
popovers.push(this.$element);
7580
}
7681

7782
WebuiPopover.prototype = {
@@ -91,11 +96,27 @@
9196
}
9297
this._poped = false;
9398
this._inited = true;
99+
this._opened = false;
94100
this._idSeed = _globalIdSeed;
101+
if (this.options.backdrop) {
102+
backdrop.appendTo(document.body).hide();
103+
}
95104
_globalIdSeed++;
96105
},
97106
/* api methods and actions */
98107
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+
99120
this.hide();
100121
this.$element.data('plugin_' + pluginName, null);
101122
if (this.getTrigger() === 'click') {
@@ -108,6 +129,9 @@
108129
}
109130
},
110131
hide: function(event) {
132+
if (!this._opened) {
133+
return;
134+
}
111135
if (event) {
112136
event.preventDefault();
113137
event.stopPropagation();
@@ -122,6 +146,10 @@
122146
if (this.$target) {
123147
this.$target.removeClass('in').hide();
124148
}
149+
if (this.options.backdrop) {
150+
backdrop.hide();
151+
}
152+
this._opened = false;
125153
this.$element.trigger('hidden.' + pluginType);
126154

127155
if (this.options.onShow) {
@@ -137,7 +165,10 @@
137165
this[this.getTarget().hasClass('in') ? 'hide' : 'show']();
138166
},
139167
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+
141172
$document.trigger('hiddenAll.' + pluginType);
142173
},
143174
/*core method ,show popover */
@@ -148,6 +179,9 @@
148179
if (!this.options.multi) {
149180
this.hideAll();
150181
}
182+
if (this._opened) {
183+
return;
184+
}
151185
// use cache by default, if not cache setted , reInit the contents
152186
if (!this.getCache() || !this._poped || this.content === '') {
153187
this.content = '';
@@ -171,6 +205,10 @@
171205
}
172206

173207
this.bindBodyEvents();
208+
if (this.options.backdrop) {
209+
backdrop.show();
210+
}
211+
this._opened = true;
174212
},
175213
displayContent: function() {
176214
var
@@ -244,8 +282,6 @@
244282
}
245283
this._poped = true;
246284
this.$element.trigger('shown.' + pluginType);
247-
248-
249285
},
250286

251287
isTargetLoaded: function() {
@@ -672,16 +708,20 @@
672708
};
673709
}
674710
};
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+
677715
var webuiPopover = $.data(this, 'plugin_' + pluginName);
678716
if (!webuiPopover) {
679717
if (!options) {
680718
webuiPopover = new WebuiPopover(this, null);
681719
} else if (typeof options === 'string') {
682720
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+
}
685725
}
686726
} else if (typeof options === 'object') {
687727
webuiPopover = new WebuiPopover(this, options);
@@ -691,10 +731,12 @@
691731
if (options === 'destroy') {
692732
webuiPopover.destroy();
693733
} else if (typeof options === 'string') {
694-
webuiPopover[options]();
734+
results.push(webuiPopover[options]());
695735
}
696736
}
697737
});
738+
739+
return (results.length) ? results : $result;
698740
};
699741

700742
})(jQuery, window, document);

dist/jquery.webui-popover.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)