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