-
Notifications
You must be signed in to change notification settings - Fork 1
/
responsive-elements.coffee
142 lines (106 loc) · 2.88 KB
/
responsive-elements.coffee
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
###*
* Responsive Elements
*
* Inspired by https://github.com/kumailht/responsive-elements
*
* @copyright Corey Worrell 2014
* @version 1.1.1
###
do ->
window.re = new ->
b = this
# Configuration
b.config =
attr: 'data-respond'
widthAttr: 'data-width'
refreshRate: 50
rootFontSize: parseInt (getComputedStyle document.documentElement).fontSize
baseFontSize: 16
start: 100
end: 900
interval: 50
# DOM elements
b.dom =
window: window
elements: document.querySelectorAll "[#{b.config.attr}]"
# Utilities
util =
debounce: (func, wait, immediate) ->
->
context = @
args = arguments
later = ->
timeout = null;
if ! immediate
result = func.apply context, args
callNow = immediate and ! timeout
clearTimeout timeout
timeout = setTimeout later, wait
if callNow
result = func.apply context, args
result
# Bind listeners
bind = ->
b.dom.window.addEventListener 'load', respondAll
b.dom.window.addEventListener 'resize', debounceRespondAll
# Unbind listeners
unbind = ->
b.dom.window.removeEventListener 'load', respondAll, false
b.dom.window.removeEventListener 'resize', debounceRespondAll, false
# Respond all elements
respondAll = (e) ->
for element in b.dom.elements
respond element
# Respond with a debounce
debounceRespondAll = util.debounce respondAll, b.config.refreshRate
# Respond single element
respond = (element) ->
params = parseParams (element.getAttribute b.config.attr)
data = makeData element.offsetWidth, params
element.setAttribute b.config.widthAttr, (data.join ' ')
# Make responsive data
makeData = (width, params) ->
data = []
i = if params.interval > params.start then params.interval else ~~(params.start / params.interval) * params.interval
width = width * (b.config.baseFontSize / b.config.rootFontSize)
while i <= params.end
data.push "gt#{i}" if i < width
data.push "lt#{i}" if i >= width
i += params.interval
data
# Parse parameters
parseParams = (string) ->
params =
start: b.config.start
end: b.config.end
interval: b.config.interval
matches = string.match /([a-zA-Z]+|[0-9]+)/g
if ! matches
return params
for match, i in matches by 2
switch match[0].toLowerCase()
when 's' then params.start = +matches[i + 1]
when 'e' then params.end = +matches[i + 1]
when 'i' then params.interval = +matches[i + 1]
params
# Public methods
# Set configuration
b.setConfig = (config) ->
for key, value of config
b.config[key] = value if b.config[key]?
b.dom.elements = document.querySelectorAll "[#{b.config.attr}]"
b
# Enable
b.enable = ->
bind()
b
# Disable
b.disable = ->
unbind()
b
# Refresh
b.refresh = ->
respondAll()
b
# Return self
b