[134] | 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].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 | |
---|
| 24 | Model.prototype.addGroup = function (name) { |
---|
| 25 | this.groups.push(name); |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | Model.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 | |
---|
| 35 | Model.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 | |
---|
| 50 | Model.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 | |
---|
| 63 | Model.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 | |
---|
| 78 | Model.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 | |
---|
| 92 | Model.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 | |
---|
| 101 | Model.prototype._setByContext = function(value) |
---|
| 102 | { |
---|
| 103 | this._setByName(this._context, value); |
---|
| 104 | this._moveContext(); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | Model.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 | } |
---|