forked from germanysbestkeptsecret/Wookmark-jQuery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.wookmark.js
283 lines (235 loc) · 8.72 KB
/
jquery.wookmark.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*!
jQuery Wookmark plugin
@name jquery.wookmark.js
@author Christoph Ono ([email protected] or @gbks)
@author Sebastian Helzle ([email protected] or @sebobo)
@version 1.1.1
@date 4/14/2013
@category jQuery plugin
@copyright (c) 2009-2013 Christoph Ono (www.wookmark.com)
@license Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
*/
(function($){
var Wookmark, defaultOptions, __bind;
__bind = function(fn, me) {
return function() {
return fn.apply(me, arguments);
};
};
// Wookmark default options
defaultOptions = {
align: 'center',
container: $('body'),
offset: 2,
autoResize: false,
itemWidth: 0,
flexibleWidth: 0,
resizeDelay: 50,
onLayoutChanged: undefined
};
Wookmark = (function(options) {
function Wookmark(handler, options) {
this.handler = handler;
// Layout variables.
this.columns = null;
this.containerWidth = null;
this.resizeTimer = null;
this.direction = 'left';
$.extend(true, this, defaultOptions, options);
// Bind methods
this.update = __bind(this.update, this);
this.onResize = __bind(this.onResize, this);
this.destroy = __bind(this.destroy, this);
this.getItemWidth = __bind(this.getItemWidth, this);
this.layout = __bind(this.layout, this);
this.layoutFull = __bind(this.layoutFull, this);
this.layoutColumns = __bind(this.layoutColumns, this);
this.bindEvents = __bind(this.bindEvents, this);
this.clear = __bind(this.clear, this);
this.bindEvents();
};
// Method for updating the plugins options
Wookmark.prototype.update = function(options) {
$.extend(true, this, options);
};
// This timer ensures that layout is not continuously called as window is being dragged.
Wookmark.prototype.onResize = function() {
clearTimeout(this.resizeTimer);
this.resizeTimer = setTimeout(this.layout, this.resizeDelay);
};
Wookmark.prototype.destroy = function() {
// clear event listeners and time outs
this.clear();
// remove saved columns to force full layout refresh in case of reinstantiation
this.columns = null;
// reset styles of grid items and container
this.handler.attr("style","");
this.container.attr("style","");
};
// Method to get the standard item width
Wookmark.prototype.getItemWidth = function() {
if (this.itemWidth === undefined || this.itemWidth === 0) {
return this.handler.eq(0).outerWidth();
}
else if (typeof this.itemWidth == 'string' && this.itemWidth.indexOf('%') >= 0) {
return parseFloat(this.itemWidth) / 100 * this.container.width();
}
return this.itemWidth;
};
// Method to get the flexible item width
Wookmark.prototype.getFlexibleWidth = function() {
var containerWidth = this.container.width(),
flexibleWidth = this.flexibleWidth;
if (typeof flexibleWidth == 'string' && flexibleWidth.indexOf('%') >= 0) {
flexibleWidth = parseFloat(flexibleWidth) / 100 * containerWidth;
flexibleWidth -= this.handler.eq(0).outerWidth() - this.handler.eq(0).innerWidth();
}
var columns = Math.floor(1 + containerWidth / (flexibleWidth + this.offset)),
columnWidth = (containerWidth - (columns - 1) * this.offset) / columns;
return Math.floor(columnWidth);
};
// Main layout method.
Wookmark.prototype.layout = function() {
// Do nothing if container isn't visible
if(!this.container.is(":visible")) {
return;
}
// Calculate flexible item width if option is set
if (this.flexibleWidth) {
this.itemWidth = this.getFlexibleWidth();
// Stretch items to fill calculated width
this.handler.css('width', this.itemWidth);
}
// Calculate basic layout parameters.
var columnWidth = this.getItemWidth() + this.offset,
containerWidth = this.container.width(),
columns = Math.floor((containerWidth + this.offset) / columnWidth),
offset = 0,
maxHeight = 0;
// Use less columns if there are to few items
columns = Math.min(columns, this.handler.length);
// Calculate the offset based on the alignment of columns to the parent container
if (this.align == 'left' || this.align == 'right') {
offset = Math.floor((columns / columnWidth + this.offset) / 2);
} else {
offset = Math.round((containerWidth - (columns * columnWidth - this.offset)) / 2);
}
// Get direction for positioning
this.direction = this.align == 'right' ? 'right' : 'left';
// If container and column count hasn't changed, we can only update the columns.
if(this.columns != null && this.columns.length == columns) {
maxHeight = this.layoutColumns(columnWidth, offset);
} else {
maxHeight = this.layoutFull(columnWidth, columns, offset);
}
// Set container height to height of the grid.
this.container.css('height', maxHeight);
if (this.onLayoutChanged !== undefined && typeof this.onLayoutChanged === 'function') {
this.onLayoutChanged();
}
};
/**
* Perform a full layout update.
*/
Wookmark.prototype.layoutFull = function(columnWidth, columns, offset) {
var item, top, left, i = 0, k = 0 , j = 0,
length = this.handler.length,
shortest = null, shortestIndex = null,
itemCSS = {position: 'absolute'},
sideOffset, heights = [],
leftAligned = this.align == 'left' ? true : false;
this.columns = [];
// Prepare arrays to store height of columns and items.
while (heights.length < columns) {
heights.push(0);
this.columns.push([]);
}
// Loop over items.
for (; i < length; i++ ) {
item = this.handler.eq(i);
// Find the shortest column.
shortest = heights[0];
shortestIndex = 0;
for (k = 0; k < columns; k++) {
if (heights[k] < shortest) {
shortest = heights[k];
shortestIndex = k;
}
}
// stick to left side if alignment is left and this is the first column
if (shortestIndex == 0 && leftAligned) {
sideOffset = 0;
} else {
sideOffset = shortestIndex * columnWidth + offset;
}
// Position the item.
itemCSS[this.direction] = sideOffset;
itemCSS.top = shortest;
item.css(itemCSS);
// Update column height and store item in shortest column
heights[shortestIndex] += item.outerHeight() + this.offset;
this.columns[shortestIndex].push(item);
}
// Return longest column
return Math.max.apply(Math, heights);
};
/**
* This layout method only updates the vertical position of the
* existing column assignments.
*/
Wookmark.prototype.layoutColumns = function(columnWidth, offset) {
var heights = [],
i = 0, k = 0,
column, item, itemCSS, sideOffset;
for (; i < this.columns.length; i++) {
heights.push(0);
column = this.columns[i];
sideOffset = i * columnWidth + offset;
for (k = 0; k < column.length; k++) {
item = column[k];
itemCSS = {
top: heights[i]
};
itemCSS[this.direction] = sideOffset;
item.css(itemCSS);
heights[i] += item.outerHeight() + this.offset;
}
}
// Return longest column
return Math.max.apply(Math, heights);
};
/**
* Bind event listeners
*/
Wookmark.prototype.bindEvents = function() {
// Listen to resize event if requested.
if (this.autoResize) {
$(window).unbind('resize.wookmark').bind('resize.wookmark', this.onResize);
this.container.unbind('refreshWookmark').bind('refreshWookmark', this.onResize);
};
this.container.unbind('destroyWookmark').bind('destroyWookmark', this.destroy);
};
/**
* Clear event listeners and time outs.
*/
Wookmark.prototype.clear = function() {
clearTimeout(this.resizeTimer);
$(window).unbind('resize.wookmark', this.onResize);
this.container.unbind('refreshWookmark', this.onResize);
};
return Wookmark;
})();
$.fn.wookmark = function(options) {
// Create a wookmark instance if not available
if (!this.wookmarkInstance) {
this.wookmarkInstance = new Wookmark(this, options || {});
} else {
this.wookmarkInstance.update(options || {});
}
this.wookmarkInstance.bindEvents();
// Apply layout
this.wookmarkInstance.layout();
// Display items (if hidden) and return jQuery object to maintain chainability
return this.show();
};
})(jQuery);