-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flex-grid.js
112 lines (97 loc) · 2.94 KB
/
flex-grid.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
(() => {
const DEBUG = (window.FlexGrid && window.FlexGrid.debug) || false;
const COMBINED = {
g: ["gx", "gy"],
};
const ALIASES = {
start: "flex-start",
end: "flex-end",
};
const GRID_ATTRS = ["g", "gx", "gy", "xs", "sm", "md", "lg", "xl", "xxl", "align", "justify", "mw", "unit", "direction", "wrap"];
class FlexGrid extends HTMLElement {
constructor() {
// Always call super first in constructor
super();
}
static log(message) {
if (!DEBUG) {
return;
}
console.log("[flex-grid] " + message);
}
static get observedAttributes() {
return GRID_ATTRS;
}
static updateStyle(inst, attr, newVal) {
let style = inst.getAttribute("style");
let rules = [];
if (style) {
rules = style.split(";");
}
let attrs = [attr];
if (ALIASES[newVal]) {
newVal = ALIASES[newVal];
}
if (COMBINED[attr]) {
attrs = COMBINED[attr];
}
for (let i = 0; i < attrs.length; i++) {
let dashAttr = "--" + attrs[i];
rules = rules.filter((v) => {
return !v.includes(dashAttr);
});
rules.push(dashAttr + ":" + newVal);
}
inst.setAttribute("style", rules.join(";"));
}
attributeChangedCallback(attr, oldVal, newVal) {
FlexGrid.updateStyle(this, attr, newVal);
}
connectedCallback() {
// This is actually called AFTER attributeChangedCallback
}
}
class FlexCol extends HTMLElement {
constructor() {
super();
}
static get observedAttributes() {
return ["o", "w", "mw", "col", "align", "justify", "grow", "d-xs", "d-sm", "d-md", "d-lg", "d-xl", "d-xxl"];
}
attributeChangedCallback(attr, oldVal, newVal) {
FlexGrid.updateStyle(this, attr, newVal);
}
connectedCallback() {}
}
class FlexContainer extends HTMLElement {
constructor() {
super();
}
static get observedAttributes() {
return GRID_ATTRS;
}
attributeChangedCallback(attr, oldVal, newVal) {
let grid = this.querySelector("flex-grid");
if (grid) {
FlexGrid.updateStyle(grid, attr, newVal);
}
}
connectedCallback() {
// If there is no flex-grid, create it
if (this.querySelector("flex-grid") === null) {
this.innerHTML = "<flex-grid>" + this.innerHTML + "</flex-grid>";
}
let grid = this.querySelector("flex-grid");
Array.prototype.slice.call(this.attributes).forEach((item) => {
if (!GRID_ATTRS.includes(item.name)) {
return;
}
FlexGrid.updateStyle(grid, item.name, item.value);
grid.setAttribute(item.name, item.value);
});
}
}
customElements.define("flex-grid", FlexGrid);
customElements.define("flex-col", FlexCol);
customElements.define("flex-container", FlexContainer);
})();