-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathla.js
213 lines (201 loc) · 7.17 KB
/
la.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
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = factory();
} else {
root.LayoutArchitect = factory();
}
}(typeof self !== 'undefined' ? self : this, function () {
return function (rootContainer, allBlocks, initialState) {
if (!rootContainer) throw new Error('Please provide a HTML element as first argument!');
if (!allBlocks) throw new Error('Please provide a list of strings as a second argument!');
const $ = tag => document.createElement(tag);
const $a = (e, attr, value) => e.setAttribute(attr, value);
const blocks = filterBlocksBasedOnTheInitialState();
const laContainer = $('DIV');
const listeners = [];
let state = initialState || null;
let selectedCallback;
$a(laContainer, 'class', 'la');
function filterBlocksBasedOnTheInitialState() {
const all = allBlocks.slice();
if (!initialState) return all;
const foundInTree = (function parseEl(found, item) {
if (item.elements.length === 0) {
found.push(item.name);
} else {
item.elements.forEach(i => parseEl(found, i));
}
return found;
})([], initialState);
return all.filter(i => foundInTree.indexOf(i) === -1);
}
function createBlock(name) {
return { name: name, elements: [] }
}
function addItem(item, direction, where) {
return selectBlock().then(blockName => createBlock(blockName)).then(block => {
if (item.elements.length === 0) {
item.direction = direction;
item.elements = where === 'after' ? [
createBlock(item.name),
block
] : [
block,
createBlock(item.name)
];
delete item.name;
} else {
if (item.direction !== direction) {
const oldElements = {
direction: item.direction,
elements: item.elements
}
item.elements = where === 'after' ?
[ oldElements, block ] :
[ block, oldElements ]
} else {
where === 'after' ?
item.elements.push(block) :
item.elements = [block].concat(item.elements);
}
item.direction = direction;
}
})
}
function removeItem(parent, item) {
if (!parent) {
state = null;
if (blocks.indexOf(item.name) === -1) {
blocks.push(item.name);
}
return;
}
const index = parent.elements.findIndex(i => i === item);
if (index > -1) {
parent.elements.splice(index, 1);
if (parent.elements.length === 1) {
if (parent.elements[0].elements.length > 0) {
parent.direction = parent.elements[0].direction;
parent.elements = parent.elements[0].elements;
} else {
parent.name = parent.elements[0].name;
parent.elements = [];
delete parent.direction;
}
}
blocks.push(item.name);
}
}
function addLinks(container, operations, item, parent) {
return operations.map(linkData => {
const a = $('A');
$a(a, 'data-op', linkData[0]);
$a(a, 'href', 'javascript:void(0);');
$a(a, 'class', linkData[2]);
a.innerHTML = linkData[1];
a.item = item;
a.parent = parent;
return a;
}).forEach(link => container.appendChild(link));
}
function renderItem(item, parent) {
const e = $('DIV');
$a(e, 'class', 'la-block');
if (item.elements.length === 0) {
e.innerHTML = '<div class="la-name">' + item.name + '</div>';
addLinks(e, [ ['remove', '<svg width="14" height="14" viewBox="0 0 1792 1792"><path d="M1490 1322q0 40-28 68l-136 136q-28 28-68 28t-68-28l-294-294-294 294q-28 28-68 28t-68-28l-136-136q-28-28-28-68t28-68l294-294-294-294q-28-28-28-68t28-68l136-136q28-28 68-28t68 28l294 294 294-294q28-28 68-28t68 28l136 136q28 28 28 68t-28 68l-294 294 294 294q28 28 28 68z"/></svg>', 'la-remove'], ], item, parent);
} else {
e.innerHTML = '<div class="la-children" style="grid-template-' + (item.direction === 'vertical' ? 'rows' : 'columns') + ': repeat(' + item.elements.length + ', 1fr);"></div>';
item.elements.forEach(i => e.querySelector('.la-children').appendChild(renderItem(i, item)))
}
addLinks(e, [
['horizontal:left', '', 'la-left'],
['vertical:top', '', 'la-top'],
['vertical:bottom', '', 'la-bottom'],
['horizontal:right', '', 'la-right']
], item, parent);
return e;
}
function createBlockSelector() {
const e = $('DIV');
blocks.forEach(blockName => {
const link = $('A');
$a(link, 'href', 'javascript:void(0);');
$a(link, 'data-op', 'select');
link.item = blockName;
link.innerHTML = '<svg width="10" height="10" viewBox="0 0 1792 1792"><path d="M1600 736v192q0 40-28 68t-68 28h-416v416q0 40-28 68t-68 28h-192q-40 0-68-28t-28-68v-416h-416q-40 0-68-28t-28-68v-192q0-40 28-68t68-28h416v-416q0-40 28-68t68-28h192q40 0 68 28t28 68v416h416q40 0 68 28t28 68z"/></svg> ' + blockName;
e.appendChild(link);
});
$a(e, 'class', 'la-selector');
return e;
}
function selectBlock() {
render(createBlockSelector(laContainer));
return new Promise(done => {
selectedCallback = blockName => {
const index = blocks.indexOf(blockName);
if (index > -1) {
blocks.splice(index, 1);
}
done(blockName);
};
});
}
function render(content) {
content = content ? content : renderItem(state);
while (laContainer.firstChild) { laContainer.removeChild(laContainer.firstChild); }
laContainer.appendChild(content);
}
function notify() {
listeners.forEach(l => l(state));
}
function selectInitialBlock() {
selectBlock().then(blockName => {
state = createBlock(blockName);
render();
notify();
});
}
laContainer.addEventListener('click', event => {
let operation = event.target.getAttribute('data-op');
const item = event.target.item;
const parent = event.target.parent;
if (operation && item) {
if (operation === 'remove') {
removeItem(parent, item);
if (state) {
render();
} else {
selectInitialBlock();
}
notify();
} else if (operation === 'select') {
selectedCallback(item);
} else {
if (blocks.length === 0) return;
operation = operation.split(':');
addItem(item, operation[0], operation[1] === 'right' || operation[1] === 'bottom' ? 'after' : 'before').then(() => {
render();
notify();
});
}
}
});
state ? render() : selectInitialBlock();
rootContainer.appendChild(laContainer);
return {
onChange: cb => {
listeners.push(cb);
},
change: newState => {
state = newState;
render();
},
get() {
return state;
}
}
};
}));