forked from eduardoboucas/include-media-export
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinclude-media.js
103 lines (85 loc) · 2.52 KB
/
include-media.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
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = factory();
} else {
root.im = factory();
}
}(this, function () {
var element = document.body;
var autoUpdate = true;
var breakpoints = false;
function setElement(newElement) {
element = newElement;
}
function setUpdateMode(updateMode) {
if (updateMode == 'manual') {
autoUpdate = false;
readBreakpoints();
}
}
function readBreakpoints() {
if (window.getComputedStyle && (window.getComputedStyle(element, '::after').content !== '')) {
var data = window.getComputedStyle(element, '::after').content;
try {
breakpoints = JSON.parse(removeQuotes(data));
} catch (err) {}
} else {
breakpoints = false;
}
}
function isBreakpointActive(breakpoint) {
if (autoUpdate) {
readBreakpoints();
}
return breakpoints.hasOwnProperty(breakpoint) && breakpoints[breakpoint].active;
}
function isBreakpointNotActive(breakpoint) {
return !isBreakpointActive(breakpoint);
}
function getActiveBreakpoint() {
if (autoUpdate) {
readBreakpoints();
}
var largest = {name: false, value: 0};
for (var breakpoint in breakpoints) {
if (breakpoints.hasOwnProperty(breakpoint)) {
var breakpointValue = parseFloat(breakpoints[breakpoint].value);
if (breakpoints[breakpoint].active && (breakpointValue > largest.value)) {
largest = {name: breakpoint, value: breakpointValue};
}
}
}
return largest.name;
}
function getBreakpointValue(breakpoint, asNumber) {
if (autoUpdate) {
readBreakpoints();
}
if (!breakpoints || !breakpoints.hasOwnProperty(breakpoint)) {
return false;
}
return asNumber ? parseFloat(breakpoints[breakpoint].value) : breakpoints[breakpoint].value;
}
/**
*
* Function by Les James (@lesjames) taken from https://css-tricks.com/making-sass-talk-to-javascript-with-json/
*
**/
function removeQuotes(string) {
if (typeof string === 'string' || string instanceof String) {
string = string.replace(/[']/g, '"').replace(/\\|^[\s\S]{0,1}|[\s\S]$/g, '');
}
return string;
}
return {
setElement: setElement,
setUpdateMode: setUpdateMode,
greaterThan: isBreakpointActive,
lessThan: isBreakpointNotActive,
getActive: getActiveBreakpoint,
getValue: getBreakpointValue,
update: readBreakpoints
};
}));