-
Notifications
You must be signed in to change notification settings - Fork 56
/
one-json.js
executable file
·93 lines (79 loc) · 2.58 KB
/
one-json.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
/*! ***************************************************************************
*
* evolutility-ui-jquery :: one-json.js
*
* View "one json" to edit one backbone model in JSON.
*
* https://github.com/evoluteur/evolutility-ui-jquery
* (c) 2017 Olivier Giulieri
*
*************************************************************************** */
Evol.ViewOne.JSON = Evol.View_One.extend({
events: {
'click > .evol-buttons > button': 'click_button',
'click .evol-title-toggle': 'click_toggle',
},
viewName: 'json',
icon: 'barcode', // glyphicon-barcode
render: function () {
var dom=Evol.DOM;
if(this.model){
var h = [],
jsonStr=JSON.stringify(this.model, null, 2);
h.push(dom.panelBegin({
id: 'p-json',
label:Evol.Format.capitalize(this.uiModel.name),
label2: 'JSON'
}, this.style+' evo-p-json', true)+
'<fieldset>'+
dom.label('uimjson', 'JSON')+
dom.input.textMJSON('uimjson', jsonStr, 16)+
'</fieldset>'+
dom.panelEnd());
this._renderButtons(h, 'json');
this.$el.html(h.join(''));
}else{
this.$el.html(dom.HTMLMsg(Evol.i18n.nodata, '', 'info'));
}
this.setData(this.model);
//this.custOn=false;
return this;
},
validate: function () {
var isValid=true,
data=this.getData(),
$fp=this._getDOMField().parent();
//this.clearMessages();
isValid=!Evol.DOM.addRemClass($fp, data===null, 'has-error');
this.$el.trigger('action', 'validate', {valid:isValid});
return isValid?[]:[Evol.i18n.validation.invalid];
},
getData: function () {
var jsonStr=this._getDOMField().val(),
obj;
if(jsonStr===''){
return jsonStr;
}
try{
obj=$.parseJSON(jsonStr);
}catch(err){
obj=null;
}
return obj;
},
setData: function (m) {
this.clearError()._getDOMField().val(JSON.stringify(m.toJSON(), null, 2));
return this.setTitle();
},
clear: function () {
this._getDOMField().val('');
return this;
},
clearError: function(){
this._getDOMField().parent().removeClass('has-error');
return this;
},
_getDOMField: function(){
return this.$('textarea');
}
});