Skip to content

Commit d89ec6d

Browse files
committed
0.0.5
1 parent fc6c725 commit d89ec6d

File tree

2 files changed

+78
-77
lines changed

2 files changed

+78
-77
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-wtpbox",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"main": "src/pbox.js",
55
"dependencies": {
66
"angular": "~1.2.25",

src/pbox.js

Lines changed: 77 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
calculatePos: function (options, $element, $boxElement) {
88
var elementTop = $element.offset().top,
99
elementLeft = $element.offset().left,
10-
docOuterWidth = $document.outerWidth(),
11-
docOuterHeight = $document.outerHeight(),
10+
docClientWidth = document.documentElement.clientWidth,
11+
docClientHeight = document.documentElement.clientHeight,
12+
1213
elementOuterWidth = $element.outerWidth(),
1314
elementOuterHeight = $element.outerHeight(),
1415
boxWidth = $boxElement.outerWidth(true),
@@ -20,91 +21,91 @@
2021
left = options.left;
2122
right = options.right;
2223
} else {
23-
switch (options.placement) {
24-
case "bottom":
25-
top = elementTop + elementOuterHeight + options.offset;
26-
if (options.align === "left") {
27-
left = elementLeft;
28-
} else if (options.align === "right") {
29-
left = elementLeft + elementOuterWidth - boxWidth;
24+
var calLeftRight = function () {
25+
if (options.align === "left") {
26+
left = elementLeft;
27+
if (left + boxWidth > docClientWidth) {
28+
left = docClientWidth - boxWidth - options.offset;
3029
}
31-
else {
32-
left = elementLeft - boxWidth / 2 + elementOuterWidth / 2;
30+
} else if (options.align === "right") {
31+
right = docClientWidth - elementLeft - elementOuterWidth;
32+
left = undefined;
33+
if (right + boxWidth > docClientWidth) {
34+
right = options.offset;
3335
}
34-
36+
}
37+
else {
38+
left = elementLeft - boxWidth / 2 + elementOuterWidth / 2;
3539
if (left < 0) {
3640
left = options.offset;
41+
} else if (left + boxWidth > docClientWidth) {
42+
left = docClientWidth - boxWidth - options.offset;
3743
}
38-
if (left + boxWidth > docOuterWidth) {
39-
left = docOuterWidth - boxWidth - options.offset;
40-
}
41-
if (top + boxHeight > docOuterHeight) {
42-
top = elementTop - boxHeight - options.offset;
44+
}
45+
};
46+
47+
var calTopBottom = function(){
48+
if (options.align === "top") {
49+
top = elementTop;
50+
if (top + boxHeight > docClientHeight) {
51+
top = docClientHeight - boxHeight;
4352
}
44-
break;
45-
case "top":
46-
top = elementTop - boxHeight - options.offset;
47-
left = elementLeft;
48-
if (options.align === "left") {
49-
left = elementLeft;
50-
} else if (options.align === "right") {
51-
left = elementLeft + elementOuterWidth - boxWidth;
53+
} else if (options.align === "bottom") {
54+
bottom = docClientHeight - elementTop - elementOuterHeight;
55+
if (bottom + boxHeight > docClientHeight) {
56+
bottom = docClientHeight - boxHeight;
5257
}
53-
else {
54-
left = elementLeft - boxWidth / 2 + elementOuterWidth / 2;
58+
}
59+
else {
60+
top = ~~(elementTop - boxHeight / 2 + elementOuterHeight / 2);
61+
if (top < 0) {
62+
top = options.offset;
63+
} else if (top + boxHeight > docClientHeight) {
64+
top = docClientHeight - boxHeight + options.offset;
5565
}
66+
}
67+
};
5668

57-
if (left < 0) {
58-
left = options.offset;
59-
}
60-
if (left + boxWidth > docOuterWidth) {
61-
left = docOuterWidth - boxWidth - options.offset;
69+
switch (options.placement) {
70+
case "bottom":
71+
top = elementTop + elementOuterHeight + options.offset;
72+
calLeftRight();
73+
if (top + boxHeight > docClientHeight) {
74+
top = undefined;
75+
bottom = docClientHeight - elementTop + options.offset;
76+
if (bottom + boxHeight > docClientHeight) {
77+
top = options.offset;
78+
bottom = undefined;
79+
}
6280
}
63-
if (top < boxHeight) {
81+
break;
82+
case "top":
83+
bottom = docClientHeight - elementTop + options.offset;
84+
if (bottom + boxHeight > docClientHeight) {
85+
bottom = undefined;
6486
top = elementTop + elementOuterHeight + options.offset;
87+
if (top + boxHeight > docClientWidth) {
88+
top = undefined;
89+
bottom = docClientHeight - boxHeight + options.offset;
90+
}
6591
}
92+
calLeftRight();
6693
break;
6794
case "left":
68-
right = ~~(docOuterWidth - elementLeft + options.offset);
69-
if (right + boxWidth + options.offset > docOuterWidth) {
95+
right = ~~(docClientWidth - elementLeft + options.offset);
96+
if (right + boxWidth + options.offset > docClientWidth) {
7097
right = undefined;
7198
left = ~~(elementLeft + elementOuterWidth + options.offset);
7299
}
73-
if (options.align === "top") {
74-
top = elementTop;
75-
} else if (options.align === "bottom") {
76-
top = elementTop + elementOuterHeight - boxHeight;
77-
}
78-
else {
79-
top = ~~(elementTop - boxHeight / 2 + elementOuterHeight / 2);
80-
}
81-
82-
if (top < 0) {
83-
top = options.offset;
84-
} else if (top + boxHeight > docOuterHeight) {
85-
top = docOuterHeight - boxHeight - options.offset;
86-
}
100+
calTopBottom();
87101
break;
88102
case "right":
89103
left = elementLeft + elementOuterWidth + options.offset;
90-
if (left + boxWidth + options.offset > docOuterWidth) {
104+
if (left + boxWidth + options.offset > docClientWidth) {
91105
left = undefined;
92-
right = docOuterWidth - elementLeft + options.offset;
93-
}
94-
if (options.align === "top") {
95-
top = elementTop;
96-
} else if (options.align === "bottom") {
97-
top = elementTop + elementOuterHeight - boxHeight;
98-
}
99-
else {
100-
top = elementTop - boxHeight / 2 + elementOuterHeight / 2;
101-
}
102-
103-
if (top < 0) {
104-
top = options.offset;
105-
} else if (top + boxHeight > docOuterHeight) {
106-
top = docOuterHeight - boxHeight - options.offset;
106+
right = docClientWidth - elementLeft + options.offset;
107107
}
108+
calTopBottom();
108109
break;
109110
default:
110111
break;
@@ -138,20 +139,20 @@
138139
.provider("$pbox", [function () {
139140
// The default options for all popboxs.
140141
var defaultOptions = {
141-
placement: 'bottom',
142-
align: null,
143-
animation: false,
142+
placement : 'bottom',
143+
align : null,
144+
animation : false,
144145
popupDelay: 0,
145-
arrow: false,
146-
openClass: 'pbox-open',
146+
arrow : false,
147+
openClass : 'pbox-open',
147148
closeClass: 'pbox-close',
148-
autoClose: true,
149-
offset: 1,
150-
resolve: {}
149+
autoClose : true,
150+
offset : 1,
151+
resolve : {}
151152
};
152153

153154
var globalOptions = {
154-
triggerClass: "pbox-trigger",
155+
triggerClass : "pbox-trigger",
155156
boxInstanceName: "boxInstance"
156157
};
157158

@@ -160,7 +161,7 @@
160161
};
161162

162163
var util = {
163-
hasClass: function (element, className) {
164+
hasClass : function (element, className) {
164165
return element.hasClass(className) || element.parents("." + className).length > 0;
165166
},
166167
hasClasses: function (element, classes) {
@@ -173,7 +174,7 @@
173174
});
174175
return result;
175176
},
176-
getTarget: function (event) {
177+
getTarget : function (event) {
177178
var $target = angular.element(event.target);
178179
if (!$target) {
179180
throw new Error("The event")

0 commit comments

Comments
 (0)