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 |
+ 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 |
+ switch(widget.preferences.getItem(key)) { |
+ 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 |
+ value = parseInt(widget.preferences.getItem(key)); |
+ if(value == NaN) |
+ return 0; //On error return 0 |
+ else |
+ return value; |
+ } |
+ else //Sets |
+ widget.preferences.setItem(key, value); |
+ }, |
+ float: function(key, value) { //Gets or sets a float value |
+ if(value == undefined) { //Gets |
+ value = parseFloat(widget.preferences.getItem(key)); |
+ if(value == NaN) |
+ return 0; //On error return 0 |
+ 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 |
+ if(this.exist(key)) { |
+ value = widget.preferences.getItem(key); |
+ if(value.indexOf("\n") > -1) |
+ return value.split("\n"); |
+ else { |
+ return [value]; |
+ } |
+ } |
+ return new Array(); //Returns an empty array when no value exists |
+ } |
+ 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)) { |
+ case 'true': |
+ widget.preferences.setItem(key, 'false'); |
+ break; |
+ default: |
+ widget.preferences.setItem(key, 'true'); |
+ break; |
+ } |
+ }, |
+ exist: function(key) { //Says if a preference exists |
+ if(widget.preferences.getItem(key) != null && widget.preferences.getItem(key) != '') |
+ return true; |
+ return false; |
+ }, |
+ remove: function(key) { |
+ widget.preferences.removeItem(key); |
+ } |
+}; |