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