-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.paving.js
279 lines (255 loc) · 6.34 KB
/
jquery.paving.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
(function($) {
var DEFAULT_OPTIONS = {
selector: '.stone',
marking: 'paved',
callback: null,
finish: null
};
var MILSEC_FINISH = 13;
/**
* jQuery.paving
* (c) 2012, Takashi Mizohata
* http://beatak.github.com/jquery-paving/
* MIT LICENSE
*/
$.fn.paving = function () {
var method, opts, args;
// determine method, opts and arguments
if (typeof arguments[0] === 'string') {
method = arguments[0].toLowerCase();
if (!arguments[1] || (typeof arguments[1] === 'object' && !isDOM(arguments[1]))) {
opts = arguments[1];
args = Array.prototype.slice.call(arguments, 2);
}
else {
opts = undefined;
args = Array.prototype.slice.call(arguments, 1);
}
}
else {
method = 'init';
opts = arguments[0];
args = Array.prototype.slice.call(arguments, 1);
}
// ==============================
var methods = {
init: function (i, elm) {
pave(this, opts);
return this;
},
append: function (i, elm) {
pave(this, opts, true);
return this;
},
add: function (i, elm) {
var $this = $(this);
var def = getDefaults($this, opts, true);
var $item = $(args[0]);
if ($item.length === 0) {
// console.log('nothing is to pave.');
return this;
}
if (typeof def.lefts === 'undefined') {
$.extend(def, buildFundamentals($this, $item.first()));
}
$this.append($item);
paveStone($item[0], def, args[1]);
if (typeof def.callback === 'function') {
def.callback($item[0], def.column);
}
finishPaving($this, def.finish, def.column);
return this;
},
clear: function (i, elm) {
clear(this, opts);
return this;
},
refresh: function (i, elm) {
clear(this, opts);
pave(this, opts);
return this;
}
};
return this.each(methods[method]);
};
// ==============================
var pave = function (parent, opts, isAppending) {
var i;
var len;
var elm;
var $items;
var $parent = $(parent);
var defaults = getDefaults($parent, opts);
if (typeof defaults.lefts === 'undefined') {
isAppending = false;
}
$items = $parent.find( buildSelector(defaults.selector, (isAppending ? defaults.marking : undefined)) );
if ($items.length === 0) {
// console.log('no elements to pave');
return;
}
if (!isAppending) {
// it looks a bit confusing, since all options are stored in defaults,
// but buildFundamentals() won't change things like selector nor callback/finish
$.extend(defaults, buildFundamentals($parent, $items.first()));
}
for (i = 0, len = $items.length; i < len; ++i) {
elm = paveStone($items[i], defaults);
if (typeof defaults.callback === 'function') {
defaults.callback(elm, defaults.column);
}
}
finishPaving($parent, defaults.finish, defaults.column);
};
var paveStone = function (item, defaults, index) {
var obj;
var container_offset = defaults.container_offset;
var column = defaults.column;
var lefts = defaults.lefts;
var marking = defaults.marking;
var column_len = defaults.column_len;
var count = defaults.count;
var $item = $(item);
var height = $item.outerHeight(true);
if (index !== undefined) {
index = parseInt(index, 10);
if (isNaN(index) || index >= column_len) {
index = undefined;
}
}
if (index === undefined) {
index = findColumnIndex(column);
}
// marking
obj = {
position: 'absolute',
left: container_offset.left + lefts[index],
top: container_offset.top + column[index]
};
$item.css(obj);
$item.data('height', height);
$item.attr('data-' + marking, 'true');
column[index] = column[index] + height;
++defaults.count;
return item;
};
var finishPaving = function ($parent, finish, column) {
$parent.height( column[findColumnIndex(column, true)] );
if (typeof finish !== 'function') {
return;
}
setTimeout(
function () {
finish(column);
},
MILSEC_FINISH
);
};
var getDefaults = function ($parent, opts, isNotModifying) {
var name;
var defaults = $parent.data('paving-defaults');
isNotModifying = isNotModifying || false;
if (!defaults) {
defaults = $.extend({}, DEFAULT_OPTIONS, opts || {});
$parent.data('paving-defaults', defaults);
}
else {
if (isNotModifying) {
defaults = $.extend({}, defaults);
}
for (name in opts) {
defaults[name] = opts[name];
}
}
return defaults;
};
var findColumnIndex = function (col, isTallest) {
var index;
var result = 0;
isTallest = isTallest || false;
loop:for (index in col) {
index = parseInt(index, 10);
if (index === 0) {
result = index;
if (isTallest) {
continue loop;
}
}
if (isTallest) {
if (col[index] > col[result]) {
result = index;
}
}
else {
if (col[index] === 0) {
result = index;
break loop;
}
if (col[index] < col[result]) {
result = index;
}
}
}
return result;
};
var buildSelector = function (selector, marking) {
var result;
if (marking) {
result = [selector, ':not([data-', marking, '])'].join('');
}
else {
result = selector;
}
return result;
};
var buildFundamentals = function ($parent, $firstItem) {
var container_width = $parent.width();
var item_width = $firstItem.outerWidth(true);
var container_offset = {
left: getInt($parent.css('padding-left')),
top: getInt($parent.css('padding-top'))
};
var lefts = [];
var column = {};
var column_len = Math.floor(container_width / item_width);
var i;
for (i = 0; i < column_len; ++i) {
column[i] = 0;
lefts[i] = item_width * i;
}
return {
container_offset: container_offset,
lefts: lefts,
column: column,
column_len: column_len,
count: 0
};
};
var getInt = function (val, def) {
var result = parseInt(val, 10);
def = def || 0;
if (isNaN(result)) {
result = def;
}
return result;
};
var isDOM = function (element) {
var result = false;
if (element.nodeType && element.nodeType === 1) {
result = true;
}
return result;
};
var clear = function (parent, opts) {
var $parent = $(parent);
var def = getDefaults($parent, opts, true);
$parent.find(def.selector)
.removeAttr('style')
.each(
function (i, elm) {
var data = $(elm).data();
delete data[def.marking];
}
);
};
})(jQuery);