Skip to content

Commit 16dbd1f

Browse files
committed
0.0.6
1 parent d89ec6d commit 16dbd1f

File tree

1 file changed

+37
-33
lines changed

1 file changed

+37
-33
lines changed

src/pbox.js

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
}
4545
};
4646

47-
var calTopBottom = function(){
47+
var calTopBottom = function () {
4848
if (options.align === "top") {
4949
top = elementTop;
5050
if (top + boxHeight > docClientHeight) {
@@ -70,7 +70,7 @@
7070
case "bottom":
7171
top = elementTop + elementOuterHeight + options.offset;
7272
calLeftRight();
73-
if (top + boxHeight > docClientHeight) {
73+
if (options.autoAdapt && top + boxHeight > docClientHeight) {
7474
top = undefined;
7575
bottom = docClientHeight - elementTop + options.offset;
7676
if (bottom + boxHeight > docClientHeight) {
@@ -81,7 +81,7 @@
8181
break;
8282
case "top":
8383
bottom = docClientHeight - elementTop + options.offset;
84-
if (bottom + boxHeight > docClientHeight) {
84+
if (options.autoAdapt && bottom + boxHeight > docClientHeight) {
8585
bottom = undefined;
8686
top = elementTop + elementOuterHeight + options.offset;
8787
if (top + boxHeight > docClientWidth) {
@@ -93,15 +93,15 @@
9393
break;
9494
case "left":
9595
right = ~~(docClientWidth - elementLeft + options.offset);
96-
if (right + boxWidth + options.offset > docClientWidth) {
96+
if (options.autoAdapt && right + boxWidth + options.offset > docClientWidth) {
9797
right = undefined;
9898
left = ~~(elementLeft + elementOuterWidth + options.offset);
9999
}
100100
calTopBottom();
101101
break;
102102
case "right":
103103
left = elementLeft + elementOuterWidth + options.offset;
104-
if (left + boxWidth + options.offset > docClientWidth) {
104+
if (options.autoAdapt && left + boxWidth + options.offset > docClientWidth) {
105105
left = undefined;
106106
right = docClientWidth - elementLeft + options.offset;
107107
}
@@ -139,20 +139,21 @@
139139
.provider("$pbox", [function () {
140140
// The default options for all popboxs.
141141
var defaultOptions = {
142-
placement : 'bottom',
143-
align : null,
144-
animation : false,
142+
placement: 'bottom',
143+
align: null,
144+
animation: false,
145145
popupDelay: 0,
146-
arrow : false,
147-
openClass : 'pbox-open',
146+
arrow: false,
147+
openClass: 'pbox-open',
148148
closeClass: 'pbox-close',
149-
autoClose : true,
150-
offset : 1,
151-
resolve : {}
149+
autoClose: true,
150+
offset: 1,
151+
autoAdapt: true,
152+
resolve: {}
152153
};
153154

154155
var globalOptions = {
155-
triggerClass : "pbox-trigger",
156+
triggerClass: "pbox-trigger",
156157
boxInstanceName: "boxInstance"
157158
};
158159

@@ -161,7 +162,7 @@
161162
};
162163

163164
var util = {
164-
hasClass : function (element, className) {
165+
hasClass: function (element, className) {
165166
return element.hasClass(className) || element.parents("." + className).length > 0;
166167
},
167168
hasClasses: function (element, classes) {
@@ -174,7 +175,7 @@
174175
});
175176
return result;
176177
},
177-
getTarget : function (event) {
178+
getTarget: function (event) {
178179
var $target = angular.element(event.target);
179180
if (!$target) {
180181
throw new Error("The event")
@@ -205,7 +206,7 @@
205206
"$wtPosition",
206207
function ($http, $document, $compile, $rootScope, $controller, $templateCache, $q, $injector, $timeout, $wtPosition) {
207208

208-
var $pbox = {}, $body = angular.element(document.body);
209+
var $pbox = {openedElement: null}, $body = angular.element(document.body);
209210

210211
function getTemplatePromise(options) {
211212
return options.template ? $q.when(options.template) :
@@ -228,25 +229,25 @@
228229
var _resultDeferred = $q.defer();
229230
var _openedDeferred = $q.defer();
230231
var _self = this;
231-
232+
this._id = new Date().getTime() + Math.random().toString(36).substr(2);
232233
this.resultDeferred = _resultDeferred;
233234
this.openedDeferred = _openedDeferred;
234235
this.result = _resultDeferred.promise;
235236
this.opened = _openedDeferred.promise;
236237
this._options = options;
237238
this._pboxElement = null;
238239
this._$target = $target;
239-
240-
$target.data(globalOptions.boxInstanceName, this);
240+
$target.data(globalOptions.boxInstanceName, _self);
241241

242242
BoxModal.prototype._remove = function () {
243-
_self._$target.removeData(globalOptions.boxInstanceName);
244-
_self._$target.removeClass(this._options.openClass);
245-
_self._pboxElement.remove();
243+
this._$target.removeData(globalOptions.boxInstanceName);
244+
this._$target.removeClass(this._options.openClass);
245+
this._pboxElement && this._pboxElement.remove();
246246
};
247247

248248
BoxModal.prototype._bindEvents = function () {
249-
$document.bind("mousedown.pbox", function (e) {
249+
var _self = this;
250+
$document.bind("click.pbox" + this._id, function (e) {
250251
var _eTarget = angular.element(e.target);
251252
if (util.hasClass(_eTarget, 'pbox')) {
252253
return;
@@ -264,30 +265,31 @@
264265
return;
265266
}
266267
}
267-
$document.unbind("mousedown.pbox");
268268
_self.close();
269269
});
270270
};
271271

272272
BoxModal.prototype.open = function (tpl, scope) {
273-
_self._pboxElement = angular.element('<div class="pbox"></div>');
274-
_self._pboxElement.html(tpl);
275-
_self._$target.addClass(_self._options.openClass);
276-
$compile(_self._pboxElement)(scope);
277-
$body.append(_self._pboxElement);
273+
this._pboxElement = angular.element('<div class="pbox"></div>');
274+
this._pboxElement.html(tpl);
275+
this._$target.addClass(this._options.openClass);
276+
//$pbox.openedElement = this._pboxElement;
277+
$compile(this._pboxElement)(scope);
278+
$body.append(this._pboxElement);
278279
$timeout(function () {
279280
$wtPosition.calculatePos(_self._options, $target, _self._pboxElement);
280281
});
281-
_self._bindEvents();
282+
this._bindEvents();
282283
};
283284

284285
BoxModal.prototype.close = function (result) {
285-
_self._remove();
286+
this._remove();
287+
$document.unbind("click.pbox" + this._id);
286288
_resultDeferred.resolve(result);
287289
};
288290

289291
BoxModal.prototype.dismiss = function (reason) {
290-
_self._remove();
292+
this._remove();
291293
_resultDeferred.reject(reason);
292294
}
293295
}
@@ -310,6 +312,8 @@
310312
var $target = util.getTarget(options.event);
311313
options.placement = $target.data("placement") ? $target.data("placement") : options.placement;
312314
options.align = $target.data("align") ? $target.data("align") : options.align;
315+
options.autoAdapt = $target.data("auto-adapt") !== undefined ? $target.data("auto-adapt") : options.autoAdapt;
316+
313317
if ($target.data(globalOptions.boxInstanceName)) {
314318
$target.data(globalOptions.boxInstanceName).close();
315319
//fix click error when user result.then();

0 commit comments

Comments
 (0)