This repository has been archived by the owner on Apr 8, 2019. It is now read-only.
/
layout-view.js
348 lines (290 loc) · 8.99 KB
/
layout-view.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
(function() {
// An item in composer
var ComposerApplicationView = Backbone.View.extend({
tagName : "li",
// TODO: Can this be removed, an empty method ?
initialize : function() {
},
render : function() {
this.template = _.template($("#portlet-template").html());
this.$el.html(this.template(this.model.toJSONForRenderer()));
this.$el.attr("id", "");
this.$el.attr("data-name", this.model.getName());
return this;
}
});
// Composer
var ComposerView = Backbone.View.extend({
el : $("#composers"),
initialize : function(options) {
var options = options || {};
this.model = new ComposerContainer(null, {
url : options.fetchPortletURL
});
this.listenTo(this.model, 'addChild.eXo.Container', this.onAddChild);
this.model.fetch();
},
getModel: function() {
return this.model;
},
onAddChild : function(child) {
var container = $('#portlet-list');
var view = new ComposerApplicationView({model : child});
container.append(view.render().$el);
//Enable draggable
$(view.$el).draggable({
connectToSortable: ".sortable",
revert: "invalid",
helper: "clone"
});
}
});
var ApplicationView = Backbone.View.extend({
tagName: "div",
className: "portlet",
initialize: function() {
this.model.on('change', this.updateContent, this);
this.model.fetchContent();
},
render: function() {
this.template = _.template($("#application-template").html());
this.$el.html(this.template(this.model.toJSONForRenderer()));
this.$el.attr("id", this.model.getId());
return this;
},
updateContent: function() {
var id = this.model.getId();
var selector = "#" + id + " div";
$(selector).html(this.model.getContent());
}
});
//
var LayoutView = Backbone.View.extend({
el : '.pageBody',
events : {
"click .close" : "deleteApp"
},
initialize : function(options) {
// Trigger adding D&D ability to Zone and Application elements
this.setupDnD();
var options = options || {};
this.editUrl = options.editUrl;
// Build model from current DOM
var model = this.buildModel();
// Setup model
this.setModel(model);
this.snapshotModel = this.model;
},
setModel : function(model) {
// Stop listening to events on the old model
this.stopListening();
// Assign to new model
this.model = model;
// Listen to add/remove events on the new model
this.listenTo(this.model, 'addChild.eXo.Container', this.onAddChild);
this.listenTo(this.model, 'removeChild.eXo.Container', this.onRemoveChild);
return this;
},
// Using JQuery UI - Sortable to initialize Draggable & Droppable elements
setupDnD : function() {
this.$(".sortable").sortable({
connectWith : ".sortable",
tolerance : "pointer",
placeholder : "portlet-placeholder",
revert : true,
update : (function(view) {
return function() {
view.dropApp.apply(view, arguments);
};
})(this)
});
},
/*
* Listen to model changes
*/
onAddChild : function(child, container) {
var $cont = $('#' + container.getId());
var $app = $('#' + child.getId());
var prev = container.at(child.getIndex() - 1);
if(!$app.html()) {
//Create new view of application
var appView = new ApplicationView({model: child});
appView = appView.render();
$app = $(appView.$el);
}
if (prev) {
$app.insertAfter($('#' + prev.getId()));
} else {
$cont.prepend($app);
}
$cont.removeClass('emptyContainer');
},
onRemoveChild : function(child, container) {
var $cont = $("#" + container.getId());
var $app = $cont.children('#' + child.getId());
$app.remove();
if (container.isEmpty()) {
$cont.addClass('emptyContainer');
}
},
/*
* Listen to DOM event
*/
// Drag and drop
dropApp : function(event, ui) {
var dragObj = $(ui.item);
var cont = this.model.getDescendant(dragObj.closest('.sortable').attr('id'));
var prev = dragObj.prev('.portlet');
var idx = 0;
if (prev.length) {
idx = $('#' + cont.getId() + ' > .portlet').index(prev.get(0)) + 1;
}
// Modify the model
if(!dragObj.attr("id")) {
//Add new application
var composerView = window.editorView.getComposerView();
var application = composerView.getModel().findChildByName(dragObj.attr("data-name"))[0];
//Clone and generate id for new application
var newChild = application.clone();
newChild.setId(newChild.getName() + new Date().getTime());
cont.addChild(newChild, {at: idx});
//Remove dropped item
$(ui.item).remove();
} else {
cont.addChild(ui.item.attr('id'), {
at : idx
});
}
// Update snapshot
this.snapshotModel = this.model;
},
// Listen to clicking on SAVE button
save : function() {
// Delegate to MODEL#save
var view = this;
this.model.save().done(function($data) {
if ($data.code == 200) {
// model saving success
window.location.href = view.model.url;
} else {
// model saving error
alert("error: " + data.message);
}
}).error(function($error) {
// network error
alert("error on connect to server");
});
return this;
},
// An event handler for deleting a window.
// Find the target window ID and container ID
// then use them to modify corresponding models
deleteApp : function(e) {
var appId = $(e.target).closest('div.portlet').attr('id');
var containerId = $(e.target).closest('div.sortable').attr('id');
var container = this.model.getDescendant(containerId);
container.removeChild(appId);
// Update snapshot
this.snapshotModel = this.model;
},
switchLayout : function(layoutData) {
// Temporarily hide the old layout
this.$el.each(function() {
var id = $(this).attr('id');
$(this).attr('id', id + '-old');
});
var tmp = $('<div ></div>').html(this.$el.html());
$('body').append(tmp.hide());
// Apply the new layout template
this.$el.html(layoutData.html);
// Retrieve this before building the new model
var snapshot = this.snapshotModel;
// Build new model according to new layout
var model = this.buildModel();
this.setModel(model);
if (layoutData.layout_id) {
this.model.setLayoutId(layoutData.layout_id);
}
this.setupDnD();
// Start switching layout
snapshot.switchLayout(this.model);
// remove old layout
tmp.remove();
return this;
},
// Build model from DOM
buildModel : function() {
var model = new PageContainer({
id : 'layoutId'
}, {
url : this.editUrl
});
this.$el.find('.sortable').each(function() {
var cont = new Container({
id : this.id
});
$(this).children('.portlet').each(function() {
var app = new Application({
'id' : this.id
});
cont.addChild(app);
});
model.addChild(cont);
});
return model;
}
});
// The root container view of Layout Edition mode
var EditorView = Backbone.View.extend({
el : '.LAYOUT-EDITION',
events : {
"click .switch" : "switchLayout",
"click #saveLayout" : "saveLayout"
},
initialize : function() {
// Be sure that the element LAYOUT-EDITION has already been available in DOM
if (this.$el.hasClass("LAYOUT-EDITION")) {
// Initialize LayoutView
this.layoutView = new LayoutView({
editUrl : this.$el.attr('data-editURL')
});
// Composer
var composerRoot = this.$("#composers");
this.composerView = new ComposerView({
fetchPortletURL : composerRoot.attr("data-url")
});
// End composer
}
},
getComposerView: function() {
return this.composerView;
},
// Delegate to the LayoutView save
saveLayout : function() {
this.layoutView.save();
},
// Clicked on Swich layout button
switchLayout : function(e) {
var anchor = e.target;
var href = $(anchor).attr('href');
e.preventDefault();
$.ajax({
url : href,
dataType : "json",
success : function(result) {
if (result.code != 200) {
alert("change layout failure!");
return false;
}
// Delegate to LayoutView
var layoutView = window.editorView.layoutView;
layoutView.switchLayout(result.data);
}
});
}
});
// Trigger to initialize the LAYOUT EDITION mode
$(function() {
window.editorView = new EditorView();
});
})();