forked from evoluteur/evolutility-ui-jquery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
213 lines (192 loc) · 6.31 KB
/
Copy pathapp.js
File metadata and controls
213 lines (192 loc) · 6.31 KB
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
/*! ***************************************************************************
*
* evolutility :: app.js
*
* View "app" to manage the single page app for all objects/ui-models.
*
* https://github.com/evoluteur/evolutility
* Copyright (c) 2015, Olivier Giulieri
*
*************************************************************************** */
Evol.App = Backbone.View.extend({
events: {
//'click .evo-head-links2>li': 'click_entity'
},
options: {
//uiModelsObj: {},
elements:{
nav: '.evo-head-links',
nav2: '.evo-head-links2',
content: '#evol'
},
useRouter: true,
pageSize:20,
prefix: 'evol-'
},
initialize: function (opts) {
_.extend(this, this.options, opts);
this.uiModels = _.flatten(this.uiModelsObj);
this._tbs={};
this._ents={};
var es = this.elements;
//this.$nav = $(es.nav);
this.$nav2 = $(es.nav2);
//this.$content = $(es.content);
this.setupRouter();
},
//render: function() {
//this.$el.html(...
//this.$nav2.html(this._HTMLentities(this.uiModels));
//this.$content.html(...;
//return this;
//},
setupRouter: function(){
var that=this,
EvolRouter=Backbone.Router.extend ({
routes: {
'' : 'nav',
//':entity/:view/:id': 'nav',
//':entity/:view': 'nav',
//':entity': 'nav',
':entity(/:view)(/:id)': 'nav',
'*noroute': that.noRoute
},
nav: function(entity, view, id){
if(entity && that.uiModelsObj[entity]){
that.setEntity(entity, view, id);
}else {
// TODO !!!
//alert('Error: Invalid route.');
}
}
});
this.router = new EvolRouter();
Backbone.history.start();
},
setRoute: function(id, triggerRoute){
var cView = this._tbs[this._curEntity].curView;
if(cView){
Evol.Dico.setRoute(this.router, cView.getTitle(), cView.uiModel.id, cView.viewName, id, triggerRoute);
}else{
alert('Error: Invalid route.');
}
return this;
},
noRoute: function(route){
alert('Error: Invalid route "'+route+'".');
},
setEntity: function(eName, view, options){
var that=this,
tb=this._tbs[this._curEntity],
cbOK=function(){
that._setEntity(eName, view, options);
},
cbCancel=function(){
//TODO case w/ no/new model
if(tb && tb.curView){ // TODO this "if" should not be necessary
that.setRoute(tb.curView.model.id, false);
}
};
if(this._curEntity){
if(tb){
tb.proceedIfReady(cbOK, cbCancel);
}else{
//alert('Error calling proceedIfReady');
cbCancel();
}
}else{
cbOK();
}
},
_setEntity: function(eName, view, options){
var that=this;
view = view || 'list';
function cb(){
that._ents[eName].show().siblings().hide();
var tb=that._tbs[eName];
if(tb){
that._curEntity = eName;
if(tb.curView.viewName !== view){
tb.setView(view, false, false) //tb.setView(view, true, false)
.setTitle();
}
if(options){
if(tb.curView.cardinality==='1'){
tb.setModelById(options);
that.setRoute(options, false);
}else{
that.setRoute('', false);
}
}
}
}
if(this._ents[eName]){
cb();
}else{
var $v=$('<div data-eid="'+eName+'"></div>');
this._ents[eName]=$v;
this.$el.children().hide();
this.$el.append($v);
this.createEntity($v, this.uiModelsObj[eName], [], view, options, cb);
}
if(this._curEntity!==eName){
this.$nav2.find('>li>a').removeClass('sel')
.filter('[data-id="'+eName+'"]').addClass('sel');
this._curEntity=eName;
}
return this;
},
createEntity: function($v, uiModel, data, defaultView, options, cb){
var that=this,
lc = new Backbone.LocalStorage(this.prefix+uiModel.id),
M = Backbone.Model.extend({
localStorage: lc
}),
Ms = Backbone.Collection.extend({
model: M,
localStorage: lc
});
var ms = new Ms();
ms.fetch({
success: function(collection){
var m = ms.at(0),
config = {
el: $v,
mode: 'list',
model: m,
modelClass: M,
collection: ms,
collectionClass: Ms,
uiModel: uiModel,
pageSize: that.pageSize,
titleSelector: '#title',
style: that.style
};
if(defaultView){
config.defaultView = defaultView;
}
if(that.useRouter){
config.router = that.router;
}
var tb = new Evol.ViewToolbar(config).render();//.setTitle();
if(options && tb.cardinality==='1'){
tb.setModelById(options);
}
if(that._tbs){
that._tbs[uiModel.id] = tb;
}
if(cb){
cb(tb);
}
},
error: function(err){
alert('Error: invalid route.');
}
});
},
_HTMLentities: function (es) {
return _.map(es, function(e){
return '<li><a href="#' + e.id + '/list" data-id="' + e.id + '">' + e.entities + '</a></li>';
}).join('');
}
});