-
Notifications
You must be signed in to change notification settings - Fork 15
/
jquery.column.js
211 lines (188 loc) · 6 KB
/
jquery.column.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
/*jslint devel: true, bitwise: true, regexp: true, browser: true, confusion: true, unparam: true, eqeq: true, white: true, nomen: true, plusplus: true, maxerr: 50, indent: 4 */
/*globals jQuery */
/*!
* Column
*
* Copyright (c) 2011-2015 Martijn W. van der Lee
* Licensed under the MIT.
*/
/* Emulate CSS3 style column on browsers that don't support it.
*/
;(function ($) {
"use strict";
$.fn.column = function(options) {
var settings = {
'count': 'auto',
'gap': 'normal',
'rule_color': '',
'rule_style': 'none',
'rule_width': 'medium',
'split': 'word',
'width': 'auto',
'after': null,
'before': null
},
measureEm = function(scope) {
var element = $('<div style="display:none;height:100em;margin:0;padding:0;border:0"/>').appendTo(scope),
px = element.height() / 100;
element.remove();
return px;
},
measureBorderWidth = function(type) {
var element = $('<div style="border:'+type+' solid transparent;height:0px"/>').appendTo('body'),
width = element.outerHeight() / 2;
element.remove();
return width;
},
borderWidths = {
thin: measureBorderWidth('thin'),
medium: measureBorderWidth('medium'),
thick: measureBorderWidth('thick')
},
indexOfRegExp = function(string, pattern, modifiers) {
var re = new RegExp(pattern, modifiers),
m = re.exec(string.valueOf());
return (m == null? -1 : m.index);
},
splitStrategies = {
word: function(node) {
var contents = [],
split;
do {
contents.push(node);
if (split = indexOfRegExp(node.nodeValue, '\\s+') + 1) {
if (split < node.length) {
node = node.splitText(split);
} else {
split = 0;
}
}
} while (split);
return contents;
},
sentence: function(node) {
var contents = [],
split;
do {
contents.push(node);
if (split = indexOfRegExp(node.nodeValue, '[.:!?]+') + 1) {
if (split < node.length) {
node = node.splitText(split);
} else {
split = 0;
}
}
} while (split);
return contents;
}
},
split = function(parent) {
var contents = [];
$(parent).contents().each( function(index, value) {
if (value.nodeType === 3) {
contents = contents.concat(splitStrategies[settings.split](value));
} else {
contents.push(value);
}
});
return contents;
};
if (typeof settings.before === 'function') {
settings.before.call();
}
var result = this.each(function() {
// Merge options
if (options) {
$.extend(settings, options);
}
// per-instance settings
var element = this,
content = $(this).html(), // entire bulk
contents = split(this),
gap_normal = measureEm(this);
// the active part
_resize(); // do once pre-load so we atleast have columns
$(window).resize(_resize).load(_resize);
// worker
function _resize() {
// Clear columns
$(element).find(">*").detach();
var column_gap = (settings.gap === parseFloat(settings.gap))? settings.gap : gap_normal,
rule_width = 0,
column_count,
column_width,
width;
if (settings.rule_style !== 'none') {
var rule_color = (settings.rule_color? settings.rule_color : $(element).css('color'));
rule_width = (settings.rule_width === parseFloat(settings.rule_width)? settings.rule_width : borderWidths[settings.rule_width]);
column_gap -= rule_width;
}
if (settings.width !== 'auto') {
column_count = Math.max(1, Math.floor(($(element).width() + rule_width) / (settings.width + column_gap)));
} else if (settings.count !== 'auto') {
column_count = Math.max(1, settings.count);
} else {
return;
}
width = $(element).width() - ((column_count - 1) * column_gap);
column_width = Math.floor(width / column_count);
// Setup columns
var left = 0;
for (var c = 0; c < column_count; ++c) {
var style = 'position:absolute;'
+ (c > 0? 'left:'+(left - Math.floor(column_gap / 2))+'px;' : '')
+ 'width:'+column_width+'px;'
+ (c > 0? 'padding-left:'+Math.ceil(column_gap / 2)+'px;' : '')
+ (c < column_count - 1? 'padding-right:'+Math.floor(column_gap / 2)+'px;' : '')
+ 'overflow:hidden;'
;
left += column_width;
left += column_gap;
if (c > 0 && settings.rule_style != 'none') {
style += 'border-left:'+rule_width+'px '+settings.rule_style+' '+rule_color+';';
left += rule_width;
}
$(element).append('<div style="'+style+'"/>');
}
// Determine height of total content in a single column
var first = $('div', element).first();
var height = first.html(content).height();
first.find(">*").detach();
var height_step = Math.ceil(height / column_count);
// Fill columns
var contents_length = contents.length;
var max_height = 0;
var i = 0;
for (var c = 0; c < column_count; ++c) {
var div = $('div', element).eq(c);
if (c < column_count - 1) {
// detect overflow
while (i < contents_length && div.height() <= height_step) {
div.append(contents[i++]);
}
// fill up rest of div
var div_height = div.height();
while (i < contents_length && div.height() == div_height) {
div.append(contents[i++]);
}
// retract last part of content
div.contents().last().detach();
--i;
} else {
// dump remaining content in the last column
while (i < contents_length) {
div.append(contents[i++]);
}
}
max_height = Math.max(max_height, div.height());
}
// Set all to the same height
$('div', element).add(element).css('height', max_height);
}
});
if (typeof settings.after === 'function') {
settings.after.call();
}
return result;
};
})(jQuery);