forked from GrapesJS/grapesjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
101 lines (86 loc) · 2.19 KB
/
index.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
import defaults from './config/config';
import ItemView from './view/ItemView';
import { isElement } from 'underscore';
module.exports = () => {
let em;
let layers;
let config = {};
return {
name: 'LayerManager',
init(opts = {}) {
config = { ...defaults, ...opts };
config.stylePrefix = opts.pStylePrefix;
em = config.em;
return this;
},
getConfig() {
return config;
},
onLoad() {
layers = new ItemView({
level: 0,
config,
opened: config.opened || {},
model: em.get('DomComponents').getWrapper()
});
em && em.on('component:selected', this.componentChanged);
this.componentChanged();
},
postRender() {
const elTo = config.appendTo;
const root = config.root;
root && this.setRoot(root);
if (elTo) {
const el = isElement(elTo) ? elTo : document.querySelector(elTo);
el.appendChild(this.render());
}
},
/**
* Set new root for layers
* @param {HTMLElement|Component|String} el Component to be set as the root
* @return {self}
*/
setRoot(el) {
layers.setRoot(el);
return this;
},
/**
* Get the root of layers
* @return {Component}
*/
getRoot() {
return layers.model;
},
/**
* Return the view of layers
* @return {View}
*/
getAll() {
return layers;
},
/**
* Triggered when the selected component is changed
* @private
*/
componentChanged(selected, opts = {}) {
if (opts.fromLayers) return;
const opened = em.get('opened');
const model = em.getSelected();
const scroll = config.scrollLayers;
let parent = model && model.collection ? model.collection.parent : null;
for (let cid in opened) opened[cid].set('open', 0);
while (parent) {
parent.set('open', 1);
opened[parent.cid] = parent;
parent = parent.collection ? parent.collection.parent : null;
}
if (model && scroll) {
const el = model.viewLayer && model.viewLayer.el;
el && el.scrollIntoView(scroll);
}
},
render() {
return layers.render().el;
}
};
};