/+junk/gjs_values

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/%2Bjunk/gjs_values

« back to all changes in this revision

Viewing changes to main.js

  • Committer: Gustav Hartvigsson
  • Date: 2014-11-22 19:23:17 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20141122192317-9v9r3l475xtld0q1
initial messy code

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
const Lang = imports.lang;
 
2
 
 
3
const Values = imports.values;
 
4
 
 
5
function main (argv) {
 
6
  
 
7
}
 
8
 
 
9
 
 
10
 
 
11
const Values = new Lang.Class ({
 
12
  Name: 'Values',
 
13
  Extends: GObject.Object,
 
14
  Signals: {
 
15
    'change': {
 
16
      param_types: [GObject.TYPE_STRING, GObject.TYPE_VARIANT]
 
17
    }
 
18
  },
 
19
  
 
20
  /**
 
21
   * Name to keeyp track of what object we are using.
 
22
   */
 
23
  domain: "Non",
 
24
  
 
25
  /** @structure
 
26
   * Holds the holds of the tells that listners can listen to.
 
27
   */
 
28
  _tellers: {},
 
29
  
 
30
  /** @structure
 
31
   * Holds a list of lintners in a multi map:
 
32
   * <code>
 
33
   { "tell1": [callback0, callback1, callback2], "tell2": [callback4]}
 
34
   </code>
 
35
   */
 
36
  _listners: {},
 
37
  
 
38
  /** @structure
 
39
   * holds the definitions of values with their name, type, and optionally
 
40
   * user defined data that may be needed when dealing with getting and
 
41
   * setting of the values:
 
42
   * <code>
 
43
    {"value1": {type: "string", user_data: {any}},
 
44
     "value2": {type: "boolean", user_data: {any}}
 
45
    }
 
46
   </code>
 
47
   */
 
48
  _value_defs: {},
 
49
  
 
50
  /** @structure
 
51
   * Holds the data of the values in an encopsinated way, this makes it easyer
 
52
   * to deal with the values.
 
53
   * <code>
 
54
    {"value1": data, "value2": data}
 
55
   </code>
 
56
   */
 
57
  _values: {},
 
58
  
 
59
  
 
60
  _value_setters: {
 
61
    "x.y.z": {type: "number", setter: function (value) {
 
62
      this.x_y_z = value;
 
63
    }},
 
64
    
 
65
    "a.b.c": {type: "number", setter: function (value) {
 
66
      this.a_b_c = value;
 
67
    }}
 
68
    
 
69
    
 
70
  },
 
71
  
 
72
    
 
73
  _value_getters: {
 
74
    "x.y.z": {type: "number", getter: function () {
 
75
      return this.x_y_z;
 
76
    }},
 
77
    
 
78
    "a.b.c": {type: "number", getter: function () {
 
79
      return this.x_y_z;
 
80
    }}
 
81
  },
 
82
  
 
83
  /** @method
 
84
   * adds a setter to a value.
 
85
   *
 
86
   * @param name The name of the value to add a setter to.
 
87
   * @func the function that is used for setting of data.
 
88
   */
 
89
  add_setter: function (name, func) {
 
90
    
 
91
  },
 
92
  
 
93
  _init: function (params) {
 
94
    this.parent (params);
 
95
    
 
96
    
 
97
    if (params != undefined) {
 
98
      if (params["domain"] != undefined) {
 
99
        this.domain = params["domain"];
 
100
      } else {
 
101
        print ("It is recomended that a Values object has a domain.");
 
102
      }
 
103
      
 
104
      if (params["tellers"] != undefined) {
 
105
        this._tellers = params["tellers"]
 
106
      }
 
107
    }
 
108
  },
 
109
  
 
110
  /** @method
 
111
   * Method to get the value stored.
 
112
   */
 
113
  get_value: function (name) {
 
114
    if (this._value_setters[name] == undefined) {
 
115
      print ("value \"" + name + "\" does not exist in the Values object.");
 
116
      return null; 
 
117
    }
 
118
    
 
119
    return _value_getters[name][getter] ();
 
120
  },
 
121
  
 
122
  /** @method
 
123
   * Sets a value.
 
124
   */
 
125
  set_value: function (name, value) {
 
126
    
 
127
    if (this._value_setters[name] == undefined) {
 
128
      print ("value \"" + name + "\" does not exist in the Values object.");
 
129
      return; 
 
130
    }
 
131
    
 
132
    if (this._value_setters[name]["type"] != typeof (value)) {
 
133
      print ("The value \"" + name + "\" is of the wrong type, should be a " +
 
134
      "\"" + this._value_setters[name]["type"] + "\".");
 
135
      return;
 
136
    }
 
137
    
 
138
    let ret_variant = null;
 
139
    
 
140
    switch (this._value_setters[name]["type"]) {
 
141
      case "number":
 
142
        ret_variant = GLib.Variant.new_int32 (value);
 
143
        break;
 
144
      case "boolean":
 
145
        ret_variant = GLib.Variant.new_boolean (value);
 
146
        break;
 
147
      case "string":
 
148
        ret_variant = GLib.Variant.new_string (value);
 
149
      default:
 
150
        print ("Balls!");
 
151
        return;
 
152
    }
 
153
    print ("Not balls!");
 
154
    this.emit ("change", name, ret_variant);
 
155
  }
 
156
  
 
157
});
 
158
 
 
159
main (ARGV);