forked from kudago/waterfall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.waterfall.js
231 lines (190 loc) · 5.37 KB
/
jquery.waterfall.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
/*
Simple min-height-masonry layout plugin.
Like masonry column shift, but works.
*/
;(function ($){
var Waterfall = function (el, opts){
this.element = $(el);
this._create(opts)
}
$.extend(Waterfall.prototype, {
options: {
itemSelector: null,
colMinWidth: 300,
defaultContainerWidth: $(window).width(),
colClass: null,
autoresize: true
},
_create: function (opts) {
var self = this,
o = self.options = $.extend({}, self.options, opts);
self.container = self.element;
var colClass = o.colClass ? o.colClass : 'wf-column';
if (self.container.children().hasClass(colClass)) {
//Columns init
self.items = $('.' + colClass, self.container).children();
} else {
//Items init
self.items = o.itemSelector ? $(o.itemSelector, self.container) : self.container.children();
}
self._resetColumns();
o.colMinWidth = opts.colMinWidth || parseInt(self.items.css("min-width")) || o.colMinWidth;
self.reflow();
if (o.autoresize) {
$(window).resize(self.reflow.bind(self))
}
},
//==========================API
//getset options
getOption: function (name) {
return this.options && this.options[name];
},
getOptions: function () {
return this.options;
},
setOption: function (name, value) {
if (this.options) {this.options[name] = value;}
this._resetColumns().reflow();
return this;
},
setOptions: function (opts) {
this.options = $.extend(this.options, opts);
this._resetColumns().reflow()
return this;
},
//Ensures column number correct, reallocates items
reflow: function () {
var self = this, o = self.options,
neededCols = self._countNeededColumns();
if (neededCols == self.container.children().length) return; //prevent recounting if columns enough
self.items.detach();
self._ensureColumns(neededCols)._refill();
return self;
},
//Inserts new item(s)
add: function (itemSet) {
var self = this, o = self.options, cols = self.container.children();
itemSet = $(itemSet);
itemSet.each(function (i, el) {
var $item = $(el);
self._getMinCol(cols).append($item);
})
self.items = self.items.add(itemSet);
return self;
},
//========================= Techs
//calc needed number of columns
_countNeededColumns: function () {
var self = this, o = self.options;
return ~~((self.container.width() || o.defaultContainerWidth) / o.colMinWidth) || 1;
},
//Just ensures that columns has correct classes etc
_resetColumns: function(){
var self = this;
self.items.detach();
self.container.children().remove();
return self;
},
//ensures only number of columns exist
_ensureColumns: function (num) {
var self = this, o = self.options
num = num || 1,
columns = self.container.children();
if (columns.length < num) {
var str = '';
for (var i = 0; i < num - columns.length; i++ ){
str += self._columnTpl();
}
self.container.append(str);
} else if ( columns.length > num) {
columns.slice(- columns.length + num).remove();
}
columns = self.container.children();
columns.css({
"width": 100 / columns.length +"%",
"display": "inline-block",
"vertical-align": "top"
})
return self;
},
_columnTpl: function () {
return '<div class="wf-column ' + (this.options.colClass || '') + '"></div>';
},
//Redistributes items by columns
_refill: function () {
var self = this, o = self.options;
//for each item place it correctly
self.items.each(function (i, el) {
var $e = $(el),
col = $e.data("float") || $e.data("column");
if (col){
switch(col){
case "left":
case "first":
self.container.children().first().append($e)
break;
case "right":
case "last":
self.container.children().last().append($e)
break;
default:
self.container.children().eq(Math.max(Math.min(col, self.container.children().length) - 1, 0)).append($e)
}
} else {
self._getMinCol(self.container.children()).append($e);
}
})
return self;
},
//returns column with minimal height
_getMinCol: function (cols) {
var minH = Infinity, minCol = cols.first(), minColNum = 0;
//fill min heights
cols.each(function (colNum, col){
var $col = $(col);
var h = $col.height();
if (h < minH) {
minH = h;
minColNum = colNum;
}
});
return cols.eq(minColNum);
},
})
$.fn.waterfall = function (arg, arg2) {
if (typeof arg == "string") {//Call API method
return $(this).each(function (i, el) {
$(el).data("waterfall")[arg](arg2);
})
} else {
return $(this).each(function (i, el) {
var wf = new Waterfall(el, arg);
if (!$(el).data("waterfall")) $(el).data("waterfall", wf);
})
}
}
//Simple options parser. The same as $.fn.data(), or element.dataset but for zepto
$.parseDataAttributes = function(el){
var data = {};
if (el.dataset) {
$.extend(data, el.dataset);
} else {
[].forEach.call(el.attributes, function(attr) {
if (/^data-/.test(attr.name)) {
var camelCaseName = attr.name.substr(5).replace(/-(.)/g, function ($0, $1) {
return $1.toUpperCase();
});
data[camelCaseName] = attr.value;
}
});
}
return data;
}
$(function () {
$(".waterfall").each(function (i, e){
var $e = $(e),
opts = $.parseDataAttributes(e);
$e.waterfall(opts);
});
})
})(window.jQuery || window.Zepto);