Values

class webix.Values()

Values mixin

References

helpers
assert(), copy(), isUndefined(), log().

Referenced by

views
htmlform(), toolbar().

External references

Official documentation page.

Code

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
webix.Values = {
    $init:function(){
        this.elements = {};
    },
    focus:function(name){
        if (name){
            webix.assert(this.elements[name],"unknown input name: "+name);
            this._focus(this.elements[name]);
        } else{
            for(var n in this.elements){
                if(this._focus(this.elements[n]))
                    return true;
            }
        }
    },
    _focus: function(target){
        if (target && target.focus){
            target.focus();
            return true;
        }
    },
    setValues:function(data, update){
        if (this._settings.complexData)
            data = webix.CodeParser.collapseNames(data);

        this._inner_setValues(data, update);
    },
    _inner_setValues:function(data, update){
        this._is_form_dirty = update;
        //prevent onChange calls from separate controls
        this.blockEvent();

        if (!update || !this._values)
            this._values = {};

        if (webix.debug_render)
            webix.log("Render: "+this.name+"@"+this._settings.id);

        for (var name in data)
            if (!this.elements[name])
                this._values[name] = data[name];

        for (var name in this.elements){
            var input = this.elements[name];
            if (input){
                if (!webix.isUndefined(data[name]))
                    input.setValue(data[name]);
                else if (!update && input._allowsClear)
                    input.setValue("");
                this._values[name] = input.getValue();
            }
        }

        this.unblockEvent();
        this.callEvent("onValues",[]);
    },
    isDirty:function(){
        if (this._is_form_dirty) return true;
        if (this.getDirtyValues(1) === 1)
            return true;

        return false;
    },
    setDirty:function(flag){
        this._is_form_dirty = flag;
        if (!flag)
            this._values = this._inner_getValues();
    },
    getDirtyValues:function(){
        var result = {};
        if (this._values){
            for (var name in this.elements){
                var value = this.elements[name].getValue();
                if (this._values[name] != value){
                    result[name] = value;
                    //FIXME - used by isDirty
                    if (arguments[0])
                        return 1;
                }
            }
        }
        return result;
    },
    getCleanValues:function(){
        return this._values;
    },
    getValues:function(filter){
        var data = this._inner_getValues(filter);
        if (this._settings.complexData)
            data = webix.CodeParser.expandNames(data);

        return data;
    },
    _inner_getValues:function(filter){
        //get original data
        var success,
            elem = null,
            data = (this._values?webix.copy(this._values):{});

        //update properties from linked controls
        for (var name in this.elements){
            elem = this.elements[name];
            success = true;
            if(filter){
                if(typeof filter == "object"){
                    if(filter.hidden === false)
                        success = elem.isVisible();
                    if(success && filter.disabled === false)
                        success = elem.isEnabled();
                }
                else
                    success = filter.call(this,elem);
            }
            if(success)
                data[name] = elem.getValue();
            else
                delete data[name]; //in case of this._values[name]
        }
        return data;
    },
    clear:function(){
        this._is_form_dirty = false;
        var data = {};
        for (var name in this.elements)
            if (this.elements[name]._allowsClear)
                data[name] = this.elements[name]._settings.defaultValue||"";

        this._inner_setValues(data);
    },
    markInvalid: function(name, state){
        // remove 'invalid' mark
        if(state === false){
            this._clear_invalid(name);
        }
        // add 'invalid' mark
        else{
            // set invalidMessage
            if(typeof state == "string"){
                var input = this.elements[name];
                if(input)
                    input._settings.invalidMessage = state;
            }
            this._mark_invalid(name);
        }
    },
    _mark_invalid:function(id){
        var input = this.elements[id];
        if (id && input){
            this._clear_invalid(id,true);
            webix.html.addCss(input._viewobj, "webix_invalid");
            input._settings.invalid = true;
            var message = input._settings.invalidMessage;
            if(typeof message === "string" && input.setBottomText)
                input.setBottomText();
        }
    },
    _clear_invalid:function(id,silent){
        var input = this.elements[id];
        if(id && input && input.$view && input._settings.invalid){
            webix.html.removeCss(input._viewobj, "webix_invalid");
            input._settings.invalid = false;
            var message = input._settings.invalidMessage;
            if(typeof message === "string" && !silent && input.setBottomText)
                input.setBottomText();
        }
    }
};