Commit a4559f9114360adb8994438ec510ce7d176b9b0c

Authored by Igor Kulikov
1 parent c8b5b2f1

JSON Forms improvements.

... ... @@ -82,6 +82,7 @@ function JsonForm($compile, $templateCache, $mdColorPicker) {
82 82 val = undefined;
83 83 }
84 84 selectOrSet(key, scope.model, val);
  85 + scope.formProps.model = scope.model;
85 86 },
86 87 onColorClick: function(event, key, val) {
87 88 scope.showColorPicker(event, val);
... ...
... ... @@ -27,39 +27,90 @@ class ThingsboardRcSelect extends React.Component {
27 27 this.onDeselect = this.onDeselect.bind(this);
28 28 this.onBlur = this.onBlur.bind(this);
29 29 this.onFocus = this.onFocus.bind(this);
30   - let emptyValue = this.props.form.schema.type === 'array'? [] : null;
31 30 this.state = {
32   - currentValue: this.props.value || emptyValue,
  31 + currentValue: this.keyToCurrentValue(this.props.value, this.props.form.schema.type === 'array'),
33 32 items: this.props.form.items,
34 33 focused: false
35 34 };
36 35 }
37 36
  37 + keyToCurrentValue(key, isArray) {
  38 + var currentValue = isArray ? [] : null;
  39 + if (isArray) {
  40 + var keys = key;
  41 + if (keys) {
  42 + for (var i = 0; i < keys.length; i++) {
  43 + currentValue.push({key: keys[i], label: this.labelFromKey(keys[i])});
  44 + }
  45 + }
  46 + } else {
  47 + currentValue = {key: key, label: this.labelFromKey(key)};
  48 + }
  49 + return currentValue;
  50 + }
  51 +
  52 + labelFromKey(key) {
  53 + let label = key || '';
  54 + if (key) {
  55 + for (var i=0;i<this.props.form.items.length;i++) {
  56 + var item = this.props.form.items[i];
  57 + if (item.value === key) {
  58 + label = item.label;
  59 + break;
  60 + }
  61 + }
  62 + }
  63 + return label;
  64 + }
  65 +
  66 + arrayValues(items) {
  67 + var v = [];
  68 + if (items) {
  69 + for (var i = 0; i < items.length; i++) {
  70 + v.push(items[i].key);
  71 + }
  72 + }
  73 + return v;
  74 + }
  75 +
  76 + keyIndex(values, key) {
  77 + var index = -1;
  78 + if (values) {
  79 + for (var i = 0; i < values.length; i++) {
  80 + if (values[i].key === key) {
  81 + index = i;
  82 + break;
  83 + }
  84 + }
  85 + }
  86 + return index;
  87 + }
  88 +
38 89 onSelect(value, option) {
39 90 if(this.props.form.schema.type === 'array') {
40 91 let v = this.state.currentValue;
41   - v.push(value);
  92 + v.push(this.keyToCurrentValue(value.key, false));
42 93 this.setState({
43 94 currentValue: v
44 95 });
45   - this.props.onChangeValidate(v);
  96 + this.props.onChangeValidate(this.arrayValues(v));
46 97 } else {
47   - this.setState({currentValue: value});
48   - this.props.onChangeValidate({target: {value: value}});
  98 + this.setState({currentValue: this.keyToCurrentValue(value.key, false)});
  99 + this.props.onChangeValidate({target: {value: value.key}});
49 100 }
50 101 }
51 102
52 103 onDeselect(value, option) {
53 104 if (this.props.form.schema.type === 'array') {
54 105 let v = this.state.currentValue;
55   - let index = v.indexOf(value);
  106 + let index = this.keyIndex(v, value.key);
56 107 if (index > -1) {
57 108 v.splice(index, 1);
58 109 }
59 110 this.setState({
60 111 currentValue: v
61 112 });
62   - this.props.onChangeValidate(v);
  113 + this.props.onChangeValidate(this.arrayValues(v));
63 114 }
64 115 }
65 116
... ... @@ -105,6 +156,7 @@ class ThingsboardRcSelect extends React.Component {
105 156 combobox={this.props.form.combobox}
106 157 disabled={this.props.form.readonly}
107 158 value={this.state.currentValue}
  159 + labelInValue={true}
108 160 onSelect={this.onSelect}
109 161 onDeselect={this.onDeselect}
110 162 onFocus={this.onFocus}
... ...
... ... @@ -63,11 +63,15 @@ class ThingsboardSchemaForm extends React.Component {
63 63
64 64 this.onChange = this.onChange.bind(this);
65 65 this.onColorClick = this.onColorClick.bind(this);
  66 + this.hasConditions = false;
66 67 }
67 68
68 69 onChange(key, val) {
69 70 //console.log('SchemaForm.onChange', key, val);
70 71 this.props.onModelChange(key, val);
  72 + if (this.hasConditions) {
  73 + this.forceUpdate();
  74 + }
71 75 }
72 76
73 77 onColorClick(event, key, val) {
... ... @@ -81,8 +85,11 @@ class ThingsboardSchemaForm extends React.Component {
81 85 console.log('Invalid field: \"' + form.key[0] + '\"!');
82 86 return null;
83 87 }
84   - if(form.condition && eval(form.condition) === false) {
85   - return null;
  88 + if(form.condition) {
  89 + this.hasConditions = true;
  90 + if (eval(form.condition) === false) {
  91 + return null;
  92 + }
86 93 }
87 94 return <Field model={model} form={form} key={index} onChange={onChange} onColorClick={onColorClick} mapper={mapper} builder={this.builder}/>
88 95 }
... ...