juci-core: some cleanup in uci set/get/update code
authorReidar Cederqvist <reidar.cederqvist@gmail.com>
Thu, 3 May 2018 08:16:09 +0000 (10:16 +0200)
committerReidar Cederqvist <reidar.cederqvist@gmail.com>
Thu, 3 May 2018 08:16:09 +0000 (10:16 +0200)
juci/src/js/uci.js

index f3e850d..206571b 100644 (file)
                                if(this.uvalue !== this.schema.dvalue){
                                        this.uvalue = this.schema.dvalue;
                                        if(this.schema.type === Boolean && (this.schema.dvalue === true || this.schema.dvalue === false))
-                                               this.uvalue = this.schema.dvalue?"1":"2";
+                                               this.uvalue = this.schema.dvalue?"1":"0";
                                        this.is_dirty = true;
                                }
                        },
                                if(this.svalue != undefined) this.value = this.svalue;
                        },
                        $update: function(value, keep_user){
-                               if(this.ovalue instanceof Array){
-                                       // if user has modified value and we have keep user set then we do not discard his changes
-                                       // otherwise we also update uvalues
-                                       if(!keep_user || !this.dirty) {
-                                               this.uvalue = value;
-                                               this.dirty = false;
-                                       }
-                                       // store original value
-                                       this.ovalue = value;
-                               } else if(this.schema.type === Boolean){
+                               // if user has modified value and we have keep user set then we do not discard his changes
+                               // otherwise we also update uvalues
+                               if(!keep_user || !this.dirty) {
+                                       this.uvalue = value;
+                                       this.dirty = false;
+                               }
+                               if(this.schema.type === Boolean){
                                        // properly handle booleans
-                                       if(value == "on" || value == "off" || value == "yes" || value == "no" || value == "true" || value == "false")
-                                               this.ovalue = value;
-                                       else
+                                       if(value === true || value === false)
                                                this.ovalue = value?"1":"0";
+                                       else
+                                               this.ovalue = value;
                                } else {
                                        if(typeof value === "string") value = value.trim();
-                                       if(!keep_user || !this.dirty) {
-                                               this.uvalue = value;
-                                               this.dirty = false;
-                                       }
                                        this.ovalue = value;
                                }
                        },
                                if(val === null) val = "";
                                if(this.ovalue instanceof Array && !(val instanceof Array)) return;
 
-                               if(this.schema.type == Boolean){
+                               if(this.schema.type === Boolean){
                                        // properly handle booleans
                                        if(this.ovalue == "on" || this.ovalue == "off") { this.uvalue = (val)?"on":"off"; }
                                        else if(this.ovalue == "yes" || this.ovalue == "no") { this.uvalue = (val)?"yes":"no"; }
                                        else if(this.ovalue == "true" || this.ovalue == "false") { this.uvalue = (val)?"true":"false"; }
                                        else this.uvalue = val?"1":"0"; // uci uses 0 and 1 for true and false
-                               } else if(val instanceof Array) {
+                               } else if(this.schema.type === Array) {
                                        // assigning an Array to variable does not copy
                                        this.uvalue = Object.assign([], val);
                                } else {