/
dorm.js
329 lines (273 loc) · 8.85 KB
/
dorm.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* dorm.js v0.0.1, (c) Damir Sultanov - http://fronteed.com, http://git.io/vCUlz */
/* global define, module */
(function (isNode, nil) {
// detect node.js env
try {
isNode = Object.prototype.toString.call(global.process) === '[object process]'
} catch (exception) {}
// cache globals
var root = isNode ? global : window
var rootNode = !isNode && document ? document.documentElement : nil
var types = {desktop: nil, mobile: nil, phone: nil, tablet: nil, tv: nil, console: nil, bot: nil}
// dorm.js instance
var Dorm = function (init) {
init && this.init({}, rootNode)
}
// initiate dorm.js
Dorm.prototype.init = function (options, domNode) {
if (domNode && domNode.nodeType && !(domNode.nodeType === 1 || domNode.nodeType === 9)) {
domNode = nil
}
options = this.assign({
prefix: 'dorm',
classes: true,
// tv: nil,
// console: nil,
// bot: nil,
useragent: !isNode && root.navigator ? root.navigator.userAgent : null,
},
this.getAttribute(),
this.getOptions(),
this.getOptions(options)
)
// parse current userAgent
var result = this.parse(options.useragent, options)
var resultHelper
// normalize according options
for (var type in types) {
if (options[type] === false || options[type] === nil) {
result[type] = nil
} else if (typeof options[type] === 'string') {
result[type] = result[options[type]]
}
}
for (var resultItem in result) {
if (result[resultItem] === true && types[resultItem] !== undefined) {
// redirects
if (!isNode && root.location) {
resultHelper = options[resultItem + 'redirect']
if (resultHelper) {
root.location.replace(resultHelper)
}
}
// callbacks
resultHelper = options[resultItem + 'callback']
if (resultHelper && typeof (resultHelper) === 'function') {
resultHelper(result, options)
}
}
}
if (rootNode && options.classes) {
var rootClass = this.trim(rootNode.className)
for (var resultClass in result) {
if (result[resultClass] !== nil) {
rootClass += ' ' + (options.prefix ? options.prefix + '-' : '') + (!result[resultClass] ? 'not-' : '') + (resultClass === 'os' ? result[resultClass] : resultClass)
}
}
rootNode.className = this.trim(rootClass)
}
return result
}
// parse userAgent and return object
Dorm.prototype.parse = function (string, options, result) {
string = typeof (string) === 'string' ? this.trim(string) : nil
result = {}
if (string) {
var os
var phone
var tablet
var secondary
var legacy = 'amoi|bada|jasmine|maemo|meego|nokia\\D\\d{2}|palm|pocket|sailfish|series.[10-90]|symb'
var check = function (keyword) {
return new RegExp(keyword, 'i').test(string)
}
// detect consoles
if (check('car(.*?)sys|func(.*?)titan|nintendo|playstation|sony(.*?)ps|wii|xbox')) {
result.console = secondary = true
// detect tvs
} else if (check('aftb|(apple|google|hbb|internet.|net|pov_|power|smart|sonyd|web)(.*?)tv|boxee|ce-html|dlink(.*?)dsm|dlnadoc|dongle|espial|kylo|loewe|net(box|cast)|roku|thom|tube|tv |viera')) {
result.tv = secondary = true
// detect bots
} else if (check('anonym|bot|cisco|crawl|gr(a|u)b|http|search|seek|spider|worm')) {
result.bot = true
}
// detect os
if (check('ios|ip(ad|hone|od)')) {
os = 'ios'
if (!secondary) {
if (check('pad')) {
tablet = true
} else {
phone = true
}
}
// android
} else if (check('android|kindle|silk')) {
if (secondary) {
if (check('android')) {
os = 'android'
}
} else {
os = 'android'
if ((check('mobile|opera (mini|mobi)|(xl|xm)\\d{2}\\D') &&
!check('Odys(.*?)(space|xpress)|allview(.*?)(speed|city)|kobo|msi enjoy|nabi|nook|note|pocketbook|t-hub|xelio') &&
!check('gt-p\\d{2}|sc-\\d{2}\\D')) ||
check('sdk b')) {
phone = true
} else {
tablet = true
}
}
// blackberry
} else if (check('blackberry|bb10|rim(?!ent)')) {
os = 'blackberry'
if (!secondary) {
if (check('tablet')) {
tablet = true
} else {
phone = true
}
}
// firefox os
} else if (check('\\((mobile|tablet);') && check('; rv:')) {
os = 'foxos'
if (!secondary) {
if (check('tablet')) {
tablet = true
} else {
phone = true
}
}
// windows
// } else if (check('cygwin|windows|win[ \\d]') && !check('darwin')) {
} else if (check('os\\/2|win') && !check('darwin')) {
os = 'windows'
if (!secondary) {
if (check(legacy)) {
phone = true
} else {
if (check('phone|wpdesktop')) {
phone = true
} else if (check('tablet') || (check('touch') && check('iemobile'))) {
tablet = true
} else if (check('mobi')) {
phone = true
}
}
}
// mac
} else if (check('darwin|mac|os[ _-]?x')) {
os = 'mac'
// third-party os
} else {
if (!secondary && (check(legacy) || check('midp|mobi|puffin'))) {
phone = true
}
if (check('bada|\\D{3}bsd|hp(.*?)ux|(ir|un)ix|linux|maemo|meego|sailfish|sunos|tizen')) {
os = 'nix'
}
}
// specific ids
if (check('folio|pad|slate|tab(?!out|rec)')) {
phone = false
tablet = true
} else if (check('juc|(pad|voda)fone')) {
phone = true
tablet = false
}
// specific brands
if (!secondary && !phone && !tablet && (!os || os === 'nix') && check('htc|mot[o -]|nokia|samsung|sonyeric|xda')) {
phone = true
}
// desktop
if (os === 'windows' && check('(win|wow|x)64') || result.bot && !check('mobi')) {
phone = tablet = false
}
// detect phones and tablets
if (phone || tablet || secondary) {
result.mobile = true
if (phone) {
result.phone = phone
} else if (tablet) {
result.tablet = tablet
}
} else {
if (!check('mac|nix|win', os)) {
os = nil
}
result.desktop = true
}
// save os
if (os) {
result.os = os
}
}
for (var type in types) {
result[type] = !!result[type]
}
return result
}
// get data-dorm attribute
Dorm.prototype.getAttribute = function (data) {
data = {}
if (rootNode) {
var attribute = rootNode.getAttribute('data-dorm')
if (attribute) {
attribute = this.trim(attribute).split(/\s*,\s*/)
for (var i = 0; i < attribute.length; i++) {
var attributeItem = attribute[i].split(/\s*:\s*/)
var attributeName = attributeItem[0].toLowerCase()
var attributeValue = attributeItem[1]
if (attributeItem.length > 1) {
if (attributeName === 'prefix') {
attributeValue = attributeValue === 'false' || !attributeValue ? '' : (attributeValue === 'true' ? nil : attributeValue)
} else if (attributeName === 'classes') {
attributeValue = attributeValue === 'true'
}
attributeValue === 'null' && (attributeValue = nil)
attributeValue !== nil && (data[attributeName] = attributeValue)
}
}
}
}
return data
}
// get window.dormOptions or custom options
Dorm.prototype.getOptions = function (options, result) {
options = options || (!isNode ? (root.dormOptions || {}) : {})
result = {}
for (var option in options) {
result[option.toLowerCase()] = options[option]
}
return result
}
// cross-browser Object.assign() replacement
Dorm.prototype.assign = function (target) {
for (var index = 1, key, src; index < arguments.length; ++index) {
src = arguments[index]
for (key in src) {
if (Object.prototype.hasOwnProperty.call(src, key)) {
target[key] = src[key]
}
}
}
return target
}
// cross-browser String.trim() replacement
Dorm.prototype.trim = function (variable) {
return (variable + '').replace(/^\s+|\s+$/g, '')
}
// exports
if (typeof define === 'function' && define.amd) {
// export for amd fans
define(function () {
return new Dorm()
})
} else if (typeof module !== 'undefined' && module.exports) {
// export in Node.js style
module.exports = new Dorm()
} else {
// export to global and launch
!isNode && (root.dorm = new Dorm(true))
}
})(false, null)