-
Notifications
You must be signed in to change notification settings - Fork 3
/
NsFormsModuleERD.js
335 lines (313 loc) · 9.35 KB
/
NsFormsModuleERD.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
define([
'jquery',
'underscore',
'backbone',
'marionette',
'backbone_forms',
'radio',
'config',
'autocompTree',
'fancytree',
'./NsFormsCustomFields',
'i18n',
'ListOfNestedModel',
], function ($, _, Backbone, Marionette, BackboneForm,Radio, config, AutocompTree ,ListOfNestedModel
){
return Marionette.ItemView.extend({
BBForm: null,
modelurl: null,
Name: null,
objecttype: null,
displayMode: null,
buttonRegion: null,
formRegion: null,
isNew: null,
id: null,
async: true,
template: 'app/ns_modules/ns_form/NsFormsModule.html',
regions: {
nsFormButtonRegion: '#NsFormButton'
},
initialize: function (options) {
//TODO Gestion Fields/Get à partir de la même url
this.BackboneFormsModel = BackboneForm.extend({
getValue: function(key) {
if (key) return this.fields[key].getValue();
var values = {};
_.each(this.fields, function(field) {
var val = field.getValue();
if (val === ''){ val =null;
}
values[field.key] = val;
});
return values;
},
});
this.radio = Radio.channel('froms');
this.modelurl = options.modelurl;
this.file = options.file;
this.name = options.name;
this.buttonRegion = options.buttonRegion;
this.formRegion = options.formRegion;
this.stationId = parseInt(options.stationId);
this.async = options.async || this.async;
if (options.id) {
this.id = options.id;
this.isNew = false;
}
else {
this.id = null;
this.isNew = true;
}
if (options.displayMode) {
this.displayMode = options.displayMode;
}
else {
this.displayMode = 'edit';
}
if (options.objecttype) {
this.objecttype = options.objecttype;
}
else {
this.objecttype = null;
}
this.objecttype = options.objecttype;
this.displaybuttons();
this.$el.i18n();
if (options.model) {
this.model = options.model;
this.BBForm = new BackboneForm({ model: this.model });
}
else {
this.initModel();
}
},
initModel: function (mode) {
//Initialisation du model et sema depuis l'url
var dipsMode = 'display';
if(mode){
dipsMode = mode;
}
this.model = new Backbone.Model();
var url = this.modelurl ;
var protoType = this.getProtocolType(url);
/*if (!this.isNew) {
url += this.id;
}
else if (this.modelurl) {
url += '0';
}*/
if(this.modelurl) {
$.ajax({
url: url,
context: this,
type: 'GET',
data: { FormName: this.name, ObjectType: this.objecttype,DisplayMode:this.displayMode },
dataType: 'json',
success: function (resp) {
this.model.schema = resp.schema;
var data = resp.data;
if(data && (! _.isEmpty(data))){
this.model.attributes = data;
} else {
this.model.attributes = resp.defaults || {};
}
if(this.model.schema.FK_TSta_ID){
this.model.set('FK_TSta_ID', this.stationId);
}
if (resp.fieldsets.length > 0) {
this.model.fieldsets = resp.fieldsets;
}
this.model.url = this.modelurl;
//this.model.trigger('change');
this.showForm();
if(data && (! _.isEmpty(data))){
this.radio.command('editState',{model: this.model});
}
if((dipsMode == 'display') && protoType =='old'){
this.displayState();
this.displayMode = 'display';
this.displaybuttons();
}
},
error: function (data) {
alert('error Getting Fields for current protocol ');
}
});
}
return false;
/*if(this.file) {
var self = this;
var deferreds = [];
var filePath ='./modules/input/data/' + this.file;
deferreds.push($.getJSON(filePath, function(data) {
self.model.schema = data.schema;
self.model.attributes = data.defaults;
if (data.fieldsets) {
self.model.fieldsets = data.fieldsets;
}
self.showForm();
}));
}*/
},
showForm: function () {
this.BBForm = new this.BackboneFormsModel({ model: this.model });
this.BBForm.render();
var formContent = this.BBForm.el;
if(this.id){
$(formContent).attr('id',this.id);
}
// format fields to have bootstrap style
$(formContent).find('fieldset>div').addClass('col-sm-3');
$(formContent).find('.wideField').each(function() {
var divContainer = $( this ).parent().parent().parent();
$(divContainer).removeClass('col-sm-3');
$(divContainer).addClass('col-sm-6');
});
$(formContent).find('input[type="text"]').addClass('form-control');
$(formContent).find('input[type="number"]').addClass('form-control');
$(formContent).find('select').addClass('form-control');
$(formContent).find('textarea').addClass('form-control');
$('#' + this.formRegion).html(this.BBForm.el);
var html = Marionette.Renderer.render(this.template);
$('#' + this.buttonRegion).html(html);
this.radio.command('updateForm');
this.displaybuttons();
this.createAutocompTree();
/*$('.timePicker').on('dp.show', function(e) {
$('input.timeInput').val('');
});*/
$('input.timeInput').attr('placeholder' ,'hh:mm');
},
createAutocompTree : function(e){
//var startId = $(e.target).attr('startId');
var elementsList = $('.autocompTree');
for(var i=0;i<elementsList.length;i++){
//$(e.target).autocompTree({
var startId = parseInt($(elementsList[i]).attr('startId')) + 204081;
// get current value
var currentVal = $(elementsList[i]).val();
$(elementsList[i]).autocompTree({
wsUrl: config.serverUrl+'/ThesaurusREADServices.svc/json',
//display: {displayValueName:'value', storedValueName: 'fullpath'},
webservices: 'fastInitForCompleteTree',
language: {hasLanguage:true, lng:"en"},
display: {
isDisplayDifferent: true,
suffixeId: '_value',
displayValueName: 'value',
storedValueName: 'fullpathTranslated'
},
startId: startId
});
// set current valua after applying autocompTree
$(elementsList[i]).val(currentVal);
}
},
/*
gethtml: function () {
this.BBForm = new BackboneForm({ model: this.model });
this.BBForm.render();
var formContent = this.BBForm.el;
//this.displaybuttons();
return formContent;
},
getbuttonhtml: function () {
this.displaybuttons();
return this.template;
},
*/
displaybuttons: function () {
if (this.displayMode == 'edit') {
$('#' + this.buttonRegion).find('#NsFormModuleSave').attr('style', 'display:');
$('#' + this.buttonRegion).find('#NsFormModuleClear').attr('style', 'display:');
$('#' + this.buttonRegion).find('#NsFormModuleEdit').attr('style', 'display:none');
}
else {
$('#' + this.buttonRegion).find('#NsFormModuleSave').attr('style', 'display:none');
$('#' + this.buttonRegion).find('#NsFormModuleClear').attr('style', 'display:none');
$('#' + this.buttonRegion).find('#NsFormModuleEdit').attr('style', 'display:');
}
// $('#' + this.buttonRegion).on('click #NsFormModuleSave', this.butClickSave);
$('#NsFormModuleSave').click($.proxy(this.butClickSave, this));
$('#NsFormModuleEdit').click($.proxy(this.butClickEdit, this));
$('#NsFormModuleClear').click($.proxy(this.butClickClear, this));
},
butClickSave: function (e) {
//e.preventDefault();
// TODO gérer l'appel AJAX
var errors = this.BBForm.commit();
var changedAttr = this.BBForm.model.changed;
if(!errors){
//this.model.set('id', null);
var staId = this.model.get('FK_TSta_ID');
if(staId){
this.model.set('FK_TSta_ID', parseInt(staId));
}
for (attr in this.model.attributes) {
var val = this.model.get(attr);
if (Array.isArray(val) ){
if (val[0] == 'true' && val.length == 0)
this.model.set(attr,1)
}
}
var self = this;
this.model.save([],{
dataType:"text",
async: this.async,
success:function(model, response) {
self.displayMode = 'display';
self.displaybuttons();
self.radio.command('successCommitForm', {id: response});
// update this.modelurl if we create a new instance of protocol
var tab = self.modelurl.split('/');
var ln = tab.length;
var newId = parseInt(response);
var currentProtoId = parseInt(tab[ln - 1]);
if (currentProtoId ===0){
var url ='';
for (var i=0; i<(ln -1);i++){
url += tab[i] +'/';
}
self.modelurl = url + newId;
}
},
error:function(request, status, error) {
alert('error in saving data');
}
});
}
return false;
},
butClickEdit: function (e) {
e.preventDefault();
this.displayMode = 'edit';
this.initModel('edit');
return false;
},
butClickClear: function (e) {
//this.displaybuttons();
var formContent = this.BBForm.el;
$(formContent).find('input').val('');
$(formContent).find('select').val('');
$(formContent).find('textarea').val('');
$(formContent).find('input[type="checkbox"]').attr('checked', false);
//this.displayMode = 'edit';
//initModel();
// TODO gérer l'appel AJAX
},
displayState : function(){
var formContent = this.BBForm.el;
$(formContent).find('input').attr('disabled','disabled');
$(formContent).find('select').attr('disabled','disabled');
$(formContent).find('textarea').attr('disabled','disabled');
},
getProtocolType : function(url){
var typeProtocol = 'new';
var tab = url.split('/');
var id = tab[tab.length -1];
if (parseInt(id) != 0 ) typeProtocol = 'old';
return typeProtocol;
}
});
});