source: js/viewer-f0/js/Structures/Model.js @ 134

Last change on this file since 134 was 134, checked in by mmichalski, 10 years ago

First version of viewer without neuron part - fix

File size: 2.7 KB
Line 
1function Model() {
2    this.groups = [];//tabliza z nazwami grup
3    this._properties = {};//obiekt z propertisami
4    this._context = "";
5}
6
7Model.prototype.setModel = function(data)
8{
9    var nodes = data[0].children;
10
11    for (var i = 0; i < nodes.length; i++) {
12        var node = $(nodes[i]);
13        if (node.prop("tagName") == "GROUP")
14            this.addGroup(node.attr("NAME"));
15        else {
16            this.addProperties(node);
17
18        }
19    }
20    var keys = Object.keys(this._properties);
21    this._context = keys[0];
22}
23
24Model.prototype.addGroup = function (name) {
25    this.groups.push(name);
26}
27
28Model.prototype.showGroups = function () {
29    console.log("Gropus");
30    for (var obj in this.groups)
31        console.log("\t", this.groups[obj]);
32    console.log("\n");
33}
34
35Model.prototype.addProperties = function (data) {
36    var properties = {};
37
38    $(data).each(function () {
39        $.each(this.attributes, function () {
40            properties[this.name] = this.value;
41        });
42
43    });
44
45    properties["VALUE"] = typeof(properties["DEF"]) === 'undefined' ? 0 : properties["DEF"];
46
47    this._properties[properties["ID"]] = properties;
48}
49
50Model.prototype.showProperties = function () {
51    console.log("Properites");
52    for (var k in this._properties) {
53        console.log(k);
54        var prop = this._properties[k];
55        for (var key in prop) {
56            console.log("\t", key, prop[key]);
57        }
58        console.log("\n");
59    }
60
61}
62
63Model.prototype.setValue = function(name, value)
64{
65    if(arguments.length == 0)   //use default value
66        this._moveContext();
67    else if(arguments.length == 1)   //use context
68        this._setByContext(name);
69    else if (arguments.length == 2){ //set by name
70        this._setByName(name, value);
71        this._moveContext();
72    }
73    else
74        console.log("Error in function SetValue in Model");
75
76}
77
78Model.prototype._checkValue = function(name, value)
79{
80    var property = this._properties[name];
81    if(property.hasOwnProperty("MIN") && property.hasOwnProperty("MAX"))
82    {
83        if(parseFloat(property["MIN"]) > parseFloat(value) || parseFloat(property["MAX"]) < parseFloat(value))
84        {
85            //throw new Error("Value for \"" + name + "\" has incorrect value");
86        }
87    }
88
89    return true;
90}
91
92Model.prototype._setByName = function(name, value)
93{
94    if(this._checkValue(name, value)){
95        this._properties[name]["VALUE"] = value;
96        this._context = name;
97    }
98
99}
100
101Model.prototype._setByContext = function(value)
102{
103    this._setByName(this._context, value);
104    this._moveContext();
105}
106
107Model.prototype._moveContext = function()
108{
109    var keys = Object.keys(this._properties);
110    var i = keys.indexOf(this._context);
111    this._context = typeof(keys[i+1]) !== "undefined" ? keys[i+1] : this._context;
112}
Note: See TracBrowser for help on using the repository browser.