2
const GObject = imports.gi.GObject;
3
const GLib = imports.gi.GLib;
4
const Lang = imports.lang;
7
const Values = new Lang.Class ({
9
Extends: GObject.Object,
12
param_types: [GObject.TYPE_STRING, GObject.TYPE_VARIANT]
17
* Name to keeyp track of what object we are using.
22
* Holds the holds of the tells that listners can listen to.
27
* Holds a list of lintners in a multi map:
29
{ "tell1": [callback0, callback1, callback2], "tell2": [callback4]}
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:
39
{"value1": {type: "string", user_data: {any}},
40
"value2": {type: "boolean", user_data: {any}}
47
* Holds the data of the values in an encopsinated way, this makes it easyer
48
* to deal with the values.
50
{"value1": data, "value2": data}
57
* adds a setter to a value.
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.
62
* @return true on succses
63
* @return false on fail
65
add_setter: function (name, func) {
66
if (name == undefined ||
68
this._value_defs[name] == undefined ||
69
!(func instanceof Function)) {
75
_init: function (params) {
79
if (params != undefined) {
80
if (params["domain"] != undefined) {
81
this.domain = params["domain"];
83
print ("It is recomended that a Values object has a domain.");
86
if (params["tellers"] != undefined) {
87
this._tellers = params["tellers"]
93
* Method to get the value stored.
95
get_value: function (name) {
96
if (this._value_setters[name] == undefined) {
97
print ("value \"" + name + "\" does not exist in the Values object.");
101
return _value_getters[name][getter] ();
107
set_value: function (name, value) {
109
if (this._value_setters[name] == undefined) {
110
print ("value \"" + name + "\" does not exist in the Values object.");
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"] + "\".");
120
let ret_variant = null;
122
switch (this._value_setters[name]["type"]) {
124
ret_variant = GLib.Variant.new_int32 (value);
127
ret_variant = GLib.Variant.new_boolean (value);
130
ret_variant = GLib.Variant.new_string (value);
135
print ("Not balls!");
136
this.emit ("change", name, ret_variant);