Index: files/preferences.js |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/files/preferences.js |
@@ -0,0 +1,83 @@ |
+const preferences = { |
+ string: function(key, value) { //Gets or sets a string value |
+ if(value == undefined) //Gets |
Felix Dahlke
2012/10/08 05:57:02
That's a bit dangerous, undefined could in fact ha
|
+ return widget.preferences.getItem(key); |
+ else //Sets |
+ widget.preferences.setItem(key, value); |
+ }, |
+ bool: function(key, value) { //Gets or sets a bool value |
+ if(value == undefined) { //Gets |
Felix Dahlke
2012/10/08 05:57:02
See comment about the same code above.
|
+ switch(widget.preferences.getItem(key)) { |
Felix Dahlke
2012/10/08 05:57:02
How about return key === "true" instead?
|
+ case 'true': |
+ return true; |
+ break; |
+ default: //Returns false if the value doesn't exist |
+ return false; |
+ break; |
+ } |
+ } |
+ else { //Sets |
+ if(value) |
+ widget.preferences.setItem(key, 'true'); |
+ else |
+ this.remove(key); //When set to false, remove the key to save space |
+ } |
+ }, |
+ int: function(key, value) { //Gets or sets a int value |
+ if(value == undefined) { //Gets |
Felix Dahlke
2012/10/08 05:57:02
See comment about the same code above.
|
+ value = parseInt(widget.preferences.getItem(key)); |
+ if(value == NaN) |
Felix Dahlke
2012/10/08 05:57:02
No, this doesn't work. Use isNan().
|
+ return 0; //On error return 0 |
Felix Dahlke
2012/10/08 05:57:02
I think it's dangerous to return 0 in case of an e
|
+ else |
+ return value; |
+ } |
+ else //Sets |
+ widget.preferences.setItem(key, value); |
+ }, |
+ float: function(key, value) { //Gets or sets a float value |
+ if(value == undefined) { //Gets |
Felix Dahlke
2012/10/08 05:57:02
See comment about the same code above.
|
+ value = parseFloat(widget.preferences.getItem(key)); |
+ if(value == NaN) |
Felix Dahlke
2012/10/08 05:57:02
See above, use isNan()
|
+ return 0; //On error return 0 |
Felix Dahlke
2012/10/08 05:57:02
See above, not sure if this is a good idea.
|
+ else |
+ return value; |
+ } |
+ else //Sets |
+ widget.preferences.setItem(key, value); |
+ }, |
+ array: function(key, value) { //Gets or sets an array in the preferences |
+ if(value == undefined) { //Gets |
Felix Dahlke
2012/10/08 05:57:02
See comment about the same code above.
|
+ if(this.exist(key)) { |
+ value = widget.preferences.getItem(key); |
+ if(value.indexOf("\n") > -1) |
Felix Dahlke
2012/10/08 05:57:02
This whole if/else can be replaced by: return valu
|
+ return value.split("\n"); |
+ else { |
+ return [value]; |
+ } |
+ } |
+ return new Array(); //Returns an empty array when no value exists |
Felix Dahlke
2012/10/08 05:57:02
Shorter: return []
|
+ } |
+ else if(value instanceof Array) { //Sets |
+ value = value.join("\n"); |
+ widget.preferences.setItem(key, value); |
+ } |
+ }, |
+ invert: function(key) { //Inverts a true/false setting |
+ switch(widget.preferences.getItem(key)) { |
Felix Dahlke
2012/10/08 05:57:02
This switch can be replaced with: widget.preferenc
|
+ case 'true': |
+ widget.preferences.setItem(key, 'false'); |
+ break; |
+ default: |
+ widget.preferences.setItem(key, 'true'); |
+ break; |
+ } |
+ }, |
+ exist: function(key) { //Says if a preference exists |
Felix Dahlke
2012/10/08 05:57:02
This will return false for a numerical value set t
|
+ if(widget.preferences.getItem(key) != null && widget.preferences.getItem(key) != '') |
+ return true; |
+ return false; |
+ }, |
+ remove: function(key) { |
+ widget.preferences.removeItem(key); |
+ } |
+}; |