forked from borbit/jquery.viewport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.viewport.js
230 lines (191 loc) · 6.52 KB
/
jquery.viewport.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
jQuery.Viewport 0.2.3
Makes an element as a handy viewport for displaying content
with absolute position. For all details and documentation:
http://borbit.github.com/jquery.viewport/
Copyright (c) 2011-2013 Serge Borbit <[email protected]>
Licensed under the MIT license
*/
(function($) {
$.widget('ui.viewport', {
options:{
binderClass: 'viewportBinder'
, contentClass: 'viewportContent'
, position: 'center'
, content: false
, height: false
, width: false
},
_create: function() {
var content = this.options.content;
if (content.tagName != null || $.isArray(content)) {
this.options.content = $(content);
}
this.viewport = createViewport(this.element, this.options);
this.viewport.adjust();
},
update: function() {
this.viewport.updateContentSize();
this.viewport.adjust();
},
adjust: function() {
this.viewport.adjust();
},
content: function() {
return this.viewport.content;
},
binder: function() {
return this.viewport.binder;
},
size: function(height, width) {
if (height == null || width == null) {
return this.viewport.contentSize;
}
this.viewport.setContentHeight(height);
this.viewport.setContentWidth(width);
},
height: function(height) {
if (height == null) {
return this.viewport.contentSize.height;
}
this.viewport.setContentHeight(height);
},
width: function(width) {
if (width == null) {
return this.viewport.contentSize.width;
}
this.viewport.setContentWidth(width);
}
});
var BINDER_CSS = {position: 'absolute', overflow: 'hidden'}
, ELEMENT_CSS = {position: 'relative', overflow: 'hidden'}
, CONTENT_CSS = {position: 'absolute'};
function createViewport(element, options) {
var contentPosition = {top: 0, left: 0}
, viewportSize = {height: 0, width: 0}
, contentSize = {height: 0, width: 0}
, centerH = true
, centerV = true
, diffH = 0
, diffW = 0;
var binder = $('<div/>').addClass(options.binderClass);
var content = $('<div/>').addClass(options.contentClass);
element.css(ELEMENT_CSS);
content.css(CONTENT_CSS);
binder.css(BINDER_CSS);
var contents = false;
if (!options.content && element.children().length) {
contents = element.children();
} else if (options.content) {
contents = options.content;
}
updateContentSize();
updateContentPosition();
if (contents) {
contents.detach();
content.append(contents);
}
binder.append(content).appendTo(element);
element.bind('dragstop', function(event, ui) {
if (contentPosition.top != ui.position.top) {
centerH = false;
}
if (contentPosition.left != ui.position.left) {
centerV = false;
}
contentPosition.left = ui.position.left;
contentPosition.top = ui.position.top;
});
function updateContentPosition() {
var position = options.position.split(' ');
if (~$.inArray('bottom', position)) {
centerV = false;
contentPosition.top = viewportSize.height - contentSize.height;
} else if (~$.inArray('top', position)) {
centerV = false;
contentPosition.top = 0;
}
if (~$.inArray('right', position)) {
centerH = false;
contentPosition.left = viewportSize.width - contentSize.width;
} else if (~$.inArray('left', position)) {
centerH = false;
contentPosition.left = 0;
}
}
function updateContentSize() {
if (options.width !== false && options.height !== false) {
content.height(options.height);
content.width(options.width);
} else if (contents !== false) {
content.height(contents.outerHeight());
content.width(contents.outerWidth());
}
contentSize.height = content.height();
contentSize.width = content.width();
}
var floor = Math.floor;
function adjust() {
viewportSize.height = element.height();
viewportSize.width = element.width();
var diff, newTop, newLeft;
if (viewportSize.height > contentSize.height) {
content.css('top', 0);
binder.css('height', contentSize.height);
binder.css('top', floor(viewportSize.height / 2) -
floor(contentSize.height / 2));
} else {
diff = contentSize.height - viewportSize.height;
binder.css('height', viewportSize.height + diff * 2);
binder.css('top', -diff);
if (centerV) {
contentPosition.top = floor(diff / 2);
content.css('top', contentPosition.top);
} else {
newTop = contentPosition.top + (diff - diffH);
newTop >= 0 || (newTop = 0);
contentPosition.top = newTop;
content.css('top', newTop);
}
diffH = diff;
}
if (viewportSize.width > contentSize.width) {
content.css('left', 0);
binder.css('width', contentSize.width);
binder.css('left', floor(viewportSize.width / 2) -
floor(contentSize.width / 2));
} else {
diff = contentSize.width - viewportSize.width;
binder.css('width', viewportSize.width + diff * 2);
binder.css('left', -diff);
if (centerH) {
contentPosition.left = floor(diff / 2);
content.css('left', contentPosition.left);
} else {
newLeft = contentPosition.left + (diff - diffW);
newLeft >= 0 || (newLeft = 0);
contentPosition.left = newLeft;
content.css('left', newLeft);
}
diffW = diff;
}
}
function setContentHeight(height) {
contentSize.height = height;
content.height(height);
}
function setContentWidth(width) {
contentSize.width = width;
content.width(width);
}
return {
adjust: adjust
, updateContentSize: updateContentSize
, setContentHeight: setContentHeight
, setContentWidth: setContentWidth
, contentSize: contentSize
, content: content
, binder: binder
};
}
})(jQuery);