1
0
Fork 0
fgdata/webgui/topics/Simulator/Properties.js

123 lines
3.7 KiB
JavaScript
Raw Normal View History

2015-01-25 12:02:20 +00:00
define([
2015-02-27 10:24:02 +00:00
'jquery', 'knockout', 'text!./Properties.html',
2015-01-25 12:02:20 +00:00
], function(jquery, ko, htmlString) {
2015-02-27 10:24:02 +00:00
2015-01-25 12:02:20 +00:00
function PropertyViewModel() {
var self = this;
2015-02-27 10:24:02 +00:00
var updateId = 0;
function update( id ) {
if( id != updateId ) return;
}
self.name = '';
2015-01-25 12:02:20 +00:00
self.value = ko.observable('');
self.children = ko.observableArray([]);
2015-02-27 10:24:02 +00:00
self.index = 0;
self.path = '';
self.hasChildren = false;
self.hasValue = false;
self.isExpanded = ko.observable(false);
self.isExpanded.subscribe(function(newValue) {
if (newValue) {
jquery.get('/json' + self.path, null, function(data) {
self.hasChildren = data.nChildren > 0;
if (typeof (data.value) != 'undefined') {
self.value(data.value);
self.hasValue = true;
} else {
self.value('');
self.hasValue = false;
}
var a = [];
data.children.forEach(function(prop) {
var p = new PropertyViewModel();
p.name = prop.name;
p.path = prop.path;
p.hasChildren = prop.nChildren > 0;
if (typeof (prop.value) != 'undefined') {
p.value(prop.value);
p.hasValue = true;
} else {
p.hasValue = false;
}
a.push(p);
});
self.children(a.sort(function(a, b) {
if (a.name == b.name) {
return a.index - b.index;
}
return a.name.localeCompare(b.name);
}));
});
} else {
self.children.removeAll();
}
});
self.toggle = function() {
self.isExpanded(!self.isExpanded());
}
self.valueEdit = function(prop, evt) {
var inplaceEditor = jquery(jquery('#inplace-editor-template').html());
var elem = jquery(evt.target);
elem.hide();
elem.after(inplaceEditor);
inplaceEditor.val(elem.text());
inplaceEditor.focus();
function endEdit(val) {
inplaceEditor.remove();
elem.show();
if (typeof (val) === 'undefined')
return;
var val = val.trim();
elem.text(val);
jquery.post('/json' + self.path, JSON.stringify({
value : val
}));
}
inplaceEditor.on('keyup', function(evt) {
switch (evt.key) {
case 'Esc':
endEdit();
break;
case 'Enter':
endEdit(inplaceEditor.val());
break;
}
});
inplaceEditor.blur(function() {
endEdit(inplaceEditor.val());
});
}
2015-01-25 12:02:20 +00:00
}
2015-02-27 10:24:02 +00:00
2015-01-25 12:02:20 +00:00
function ViewModel(params) {
var self = this;
2015-02-27 10:24:02 +00:00
self.root = new PropertyViewModel();
self.root.name = "root";
self.root.path = "/";
self.root.isExpanded(true);
self.properties = self.root.children;
2015-01-25 12:02:20 +00:00
}
2015-02-27 10:24:02 +00:00
// ViewModel.prototype.dispose = function() {
// }
2015-01-25 12:02:20 +00:00
// Return component definition
return {
viewModel : ViewModel,
template : htmlString
};
});