|
44 | 44 | } |
45 | 45 | }; |
46 | 46 |
|
47 | | - var calTopBottom = function(){ |
| 47 | + var calTopBottom = function () { |
48 | 48 | if (options.align === "top") { |
49 | 49 | top = elementTop; |
50 | 50 | if (top + boxHeight > docClientHeight) { |
|
70 | 70 | case "bottom": |
71 | 71 | top = elementTop + elementOuterHeight + options.offset; |
72 | 72 | calLeftRight(); |
73 | | - if (top + boxHeight > docClientHeight) { |
| 73 | + if (options.autoAdapt && top + boxHeight > docClientHeight) { |
74 | 74 | top = undefined; |
75 | 75 | bottom = docClientHeight - elementTop + options.offset; |
76 | 76 | if (bottom + boxHeight > docClientHeight) { |
|
81 | 81 | break; |
82 | 82 | case "top": |
83 | 83 | bottom = docClientHeight - elementTop + options.offset; |
84 | | - if (bottom + boxHeight > docClientHeight) { |
| 84 | + if (options.autoAdapt && bottom + boxHeight > docClientHeight) { |
85 | 85 | bottom = undefined; |
86 | 86 | top = elementTop + elementOuterHeight + options.offset; |
87 | 87 | if (top + boxHeight > docClientWidth) { |
|
93 | 93 | break; |
94 | 94 | case "left": |
95 | 95 | right = ~~(docClientWidth - elementLeft + options.offset); |
96 | | - if (right + boxWidth + options.offset > docClientWidth) { |
| 96 | + if (options.autoAdapt && right + boxWidth + options.offset > docClientWidth) { |
97 | 97 | right = undefined; |
98 | 98 | left = ~~(elementLeft + elementOuterWidth + options.offset); |
99 | 99 | } |
100 | 100 | calTopBottom(); |
101 | 101 | break; |
102 | 102 | case "right": |
103 | 103 | left = elementLeft + elementOuterWidth + options.offset; |
104 | | - if (left + boxWidth + options.offset > docClientWidth) { |
| 104 | + if (options.autoAdapt && left + boxWidth + options.offset > docClientWidth) { |
105 | 105 | left = undefined; |
106 | 106 | right = docClientWidth - elementLeft + options.offset; |
107 | 107 | } |
|
139 | 139 | .provider("$pbox", [function () { |
140 | 140 | // The default options for all popboxs. |
141 | 141 | var defaultOptions = { |
142 | | - placement : 'bottom', |
143 | | - align : null, |
144 | | - animation : false, |
| 142 | + placement: 'bottom', |
| 143 | + align: null, |
| 144 | + animation: false, |
145 | 145 | popupDelay: 0, |
146 | | - arrow : false, |
147 | | - openClass : 'pbox-open', |
| 146 | + arrow: false, |
| 147 | + openClass: 'pbox-open', |
148 | 148 | closeClass: 'pbox-close', |
149 | | - autoClose : true, |
150 | | - offset : 1, |
151 | | - resolve : {} |
| 149 | + autoClose: true, |
| 150 | + offset: 1, |
| 151 | + autoAdapt: true, |
| 152 | + resolve: {} |
152 | 153 | }; |
153 | 154 |
|
154 | 155 | var globalOptions = { |
155 | | - triggerClass : "pbox-trigger", |
| 156 | + triggerClass: "pbox-trigger", |
156 | 157 | boxInstanceName: "boxInstance" |
157 | 158 | }; |
158 | 159 |
|
|
161 | 162 | }; |
162 | 163 |
|
163 | 164 | var util = { |
164 | | - hasClass : function (element, className) { |
| 165 | + hasClass: function (element, className) { |
165 | 166 | return element.hasClass(className) || element.parents("." + className).length > 0; |
166 | 167 | }, |
167 | 168 | hasClasses: function (element, classes) { |
|
174 | 175 | }); |
175 | 176 | return result; |
176 | 177 | }, |
177 | | - getTarget : function (event) { |
| 178 | + getTarget: function (event) { |
178 | 179 | var $target = angular.element(event.target); |
179 | 180 | if (!$target) { |
180 | 181 | throw new Error("The event") |
|
205 | 206 | "$wtPosition", |
206 | 207 | function ($http, $document, $compile, $rootScope, $controller, $templateCache, $q, $injector, $timeout, $wtPosition) { |
207 | 208 |
|
208 | | - var $pbox = {}, $body = angular.element(document.body); |
| 209 | + var $pbox = {openedElement: null}, $body = angular.element(document.body); |
209 | 210 |
|
210 | 211 | function getTemplatePromise(options) { |
211 | 212 | return options.template ? $q.when(options.template) : |
|
228 | 229 | var _resultDeferred = $q.defer(); |
229 | 230 | var _openedDeferred = $q.defer(); |
230 | 231 | var _self = this; |
231 | | - |
| 232 | + this._id = new Date().getTime() + Math.random().toString(36).substr(2); |
232 | 233 | this.resultDeferred = _resultDeferred; |
233 | 234 | this.openedDeferred = _openedDeferred; |
234 | 235 | this.result = _resultDeferred.promise; |
235 | 236 | this.opened = _openedDeferred.promise; |
236 | 237 | this._options = options; |
237 | 238 | this._pboxElement = null; |
238 | 239 | this._$target = $target; |
239 | | - |
240 | | - $target.data(globalOptions.boxInstanceName, this); |
| 240 | + $target.data(globalOptions.boxInstanceName, _self); |
241 | 241 |
|
242 | 242 | 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(); |
246 | 246 | }; |
247 | 247 |
|
248 | 248 | BoxModal.prototype._bindEvents = function () { |
249 | | - $document.bind("mousedown.pbox", function (e) { |
| 249 | + var _self = this; |
| 250 | + $document.bind("click.pbox" + this._id, function (e) { |
250 | 251 | var _eTarget = angular.element(e.target); |
251 | 252 | if (util.hasClass(_eTarget, 'pbox')) { |
252 | 253 | return; |
|
264 | 265 | return; |
265 | 266 | } |
266 | 267 | } |
267 | | - $document.unbind("mousedown.pbox"); |
268 | 268 | _self.close(); |
269 | 269 | }); |
270 | 270 | }; |
271 | 271 |
|
272 | 272 | 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); |
278 | 279 | $timeout(function () { |
279 | 280 | $wtPosition.calculatePos(_self._options, $target, _self._pboxElement); |
280 | 281 | }); |
281 | | - _self._bindEvents(); |
| 282 | + this._bindEvents(); |
282 | 283 | }; |
283 | 284 |
|
284 | 285 | BoxModal.prototype.close = function (result) { |
285 | | - _self._remove(); |
| 286 | + this._remove(); |
| 287 | + $document.unbind("click.pbox" + this._id); |
286 | 288 | _resultDeferred.resolve(result); |
287 | 289 | }; |
288 | 290 |
|
289 | 291 | BoxModal.prototype.dismiss = function (reason) { |
290 | | - _self._remove(); |
| 292 | + this._remove(); |
291 | 293 | _resultDeferred.reject(reason); |
292 | 294 | } |
293 | 295 | } |
|
310 | 312 | var $target = util.getTarget(options.event); |
311 | 313 | options.placement = $target.data("placement") ? $target.data("placement") : options.placement; |
312 | 314 | 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 | + |
313 | 317 | if ($target.data(globalOptions.boxInstanceName)) { |
314 | 318 | $target.data(globalOptions.boxInstanceName).close(); |
315 | 319 | //fix click error when user result.then(); |
|
0 commit comments