-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoduly.js
More file actions
129 lines (95 loc) · 4.03 KB
/
Copy pathmoduly.js
File metadata and controls
129 lines (95 loc) · 4.03 KB
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
/*global jQuery */
/*!
* moduly.js 0.1
* https://github.com/PaulSpr/moduly
*
* Copyright 2014, Paul Sprangers http://paulsprangers.com
* Released under the WTFPL license
* http://www.wtfpl.net
*
* Date: Sat Dec 13 11:30:00 2014 +0100
*/
(function( $ ){
$.fn.moduly = function( options ) {
defaults = {
checkWidthMethod: 'width', // jQuery method to check width, possible values: width, innerWidth, outerWidth
};
var $this = $(this); // store the object
options = $.extend( {}, defaults, options ); // merge options
var minWidthPattern = /(\.moduly-min-width-)(\d+)/g;
var modulyNumberPattern = /(\d+)/;
var beforeSpacePattern = /([^\s]+)/;
var modulyMap = Array();
// Resize height of the columns
var resizeEvent = function () {
// 1) loop through elements with data-moduly-min-width
// 2) check if the width exceeds one of the breakpoints
// 3) if so, apply class
// 4) if not, remove that class
$('[data-moduly-min-width]').each( function(){
// get element width
var elWidth = parseInt($(this)[options.checkWidthMethod]());
// get all the min-width breakpoints
var breakpoints = String($(this).data('moduly-min-width')).split(',');
// loop through and apply moduly classes if applicable
for( var i = 0; i < breakpoints.length; i++){
var breakpointWidth = parseInt(breakpoints[i]);
if( elWidth >= breakpointWidth ){
$(this).addClass('moduly-min-width-'+breakpointWidth);
}
else{
$(this).removeClass('moduly-min-width-'+breakpointWidth);
}
}
});
};
// Init method, used to get the CSS and parse the rules
var init = function () {
sheets = document.styleSheets;
for(var c = 0; c < sheets.length; c++) {
var rules = sheets[c].rules || sheets[c].cssRules;
for(var r = 0; r < rules.length; r++) {
var selectorText = String(rules[r].selectorText);
if( selectorText.search(minWidthPattern) > -1){
// we should treat this rule
var selectorLastPart = selectorText.replace(minWidthPattern, '');
// get the part after the Moduly class selector
// first prepare by replaceing + and > with a space so we can get just the first part.
var elementSelector = selectorLastPart.replace('+', ' ');
elementSelector = elementSelector.replace('>', ' ');
// then match just the first part of the selector
elementSelector = elementSelector.match(beforeSpacePattern);
elementSelector = elementSelector[0];
// get just the Moduly width number
var breakpointWidth = selectorText.match(modulyNumberPattern);
breakpointWidth = breakpointWidth[0];
// bind data to elements
if( elementSelector && breakpointWidth ){
// check if breakpoint is already set on element
var breakpoints = String($(elementSelector).data('moduly-min-width')).split(',');
if( breakpoints.indexOf(String(breakpointWidth)) == -1 ){
// make sure undefined isn't included in breakpoint list
if( breakpoints == 'undefined' ){
breakpoints[0] = breakpointWidth;
}
else{
breakpoints.push(breakpointWidth);
}
// make new value to store in breakpoint
breakpointsNew = breakpoints.join();
$(elementSelector).attr('data-moduly-min-width', breakpointsNew);
}
}
}
}
}
resizeEvent();
}
// Call once to set initially
init();
// Call on resize. Opera debounces their resize by default.
$(window).resize(resizeEvent);
};
})( jQuery );
// call Moduly once
$(document).ready(function(){ $(document).moduly(); })