This repository has been archived by the owner on Apr 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
/
layout-model.js
191 lines (177 loc) · 4.45 KB
/
layout-model.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
(function() {
/**
*
*/
var LayoutComponent = Backbone.Model.extend({
initialize : function() {
this.set({
_parent : null,
draggable : false,
droppable : false
});
},
isAllowDropping : function(dragObj) {
return false;
},
getType : function() {
return 'LayoutComponent';
},
getId : function() {
return this.get('id') || this.cid;
},
getParent : function() {
return this.get('_parent');
},
getRoot : function() {
var parent = this, root;
do {
root = parent;
parent = parent.getParent();
} while (parent);
return root;
},
getIndex : function() {
if (this.getParent()) {
return this.getParent().indexOf(this);
} else {
return -1;
}
}
});
/**
*
*/
var Application = LayoutComponent.extend({
initialize : function() {
LayoutComponent.prototype.initialize.call(this);
this.set({
draggable : true
});
},
getType : function() {
return 'Application';
}
});
/**
*
*/
var Container = LayoutComponent.extend({
initialize : function() {
LayoutComponent.prototype.initialize.call(this);
this.set({
draggable : true,
droppable : true,
// Should not access directly to those internal attributes
_childrens : new Backbone.Collection(),
// Allow type 'Application' and 'Container'to be dropped
_childTypes : [ Application.prototype.getType(), 'Container' ]
});
var _this = this;
this.get('_childrens').on('add', function(child) {
_this.trigger('addChild.eXo.Container', child, _this);
});
this.get('_childrens').on('remove', function(child) {
_this.trigger('removeChild.eXo.Container', child, _this);
});
},
isChildSupported : function(dragObj) {
// Check for supported types
if (dragObj && $.isFunction(dragObj.getType)) {
var type = $.trim(dragObj.getType());
return this.get('droppable') && $.inArray(type, this.get('_childTypes')) != -1;
} else {
return false;
}
},
getType : function() {
return 'Container';
},
/**
* methods on childrens
*/
//
addChild : function(child, idx) {
child = typeof child == 'string' ? this.getChild(child) : child;
if (child && this.isChildSupported(child)) {
var _this = this;
this.listenTo(child, 'addChild.eXo.Container', function(addedChild, container) {
_this.trigger('addChild.eXo.Container', addedChild, container);
});
this.listenTo(child, 'removeChild.eXo.Container', function(addedChild, container) {
_this.trigger('removeChild.eXo.Container', addedChild, container);
});
if (child.getParent() != null) {
child.getParent().removeChild(child, true);
}
child.set('_parent', this);
this.get('_childrens').add(child, {
at : idx
});
}
return this;
},
//
removeChild : function(child, silent) {
child = typeof child == 'string' ? this.getChild(child) : child;
if (child && child.getParent() === this) {
child.set('_parent', null);
this.get('_childrens').remove(child, {'silent' : silent});
this.stopListening(child);
}
return this;
},
getChild : function(id) {
return this.get('_childrens').get(id);
},
indexOf : function(child) {
child = typeof child == 'string' ? this.getChild(child) : child;
return this.get('_childrens').indexOf(child);
},
at : function(idx) {
return this.get('_childrens').at(idx);
},
getDescendant : function(id) {
var child = this.getChild(id);
if (!child) {
var cont = this.get('_childrens').find(function(elem) {
if ($.isFunction(elem.getDescendant)) {
return elem.getDescendant.call(elem, id);
}
});
cont != null && (child = cont.getChild(id));
}
return child;
},
//
addChildType : function(childType) {
var type = $.trim(childType);
var types = this.get('_childTypes');
if ($.inArray(type, types) == -1) {
types.put(type);
this.trigger('addChildType.eXo.Container', type);
}
return this;
},
//
removeChildType : function(childType) {
var type = $.trim(childType);
var types = this.get('_childTypes');
var idx = $.inArray(type, types);
if (idx != -1) {
types.splice(idx, 1);
this.trigger('removeChildType.eXo.Container', type);
}
return this;
}
});
var layoutDef = {
'LayoutComponent' : LayoutComponent,
'Application' : Application,
'Container' : Container
};
if (typeof window.require === "function" && window.require.amd) {
return layoutDef;
} else {
return $.extend(window, layoutDef);
}
})();