Commit a4559f9114360adb8994438ec510ce7d176b9b0c

Authored by Igor Kulikov
1 parent c8b5b2f1

JSON Forms improvements.

@@ -82,6 +82,7 @@ function JsonForm($compile, $templateCache, $mdColorPicker) { @@ -82,6 +82,7 @@ function JsonForm($compile, $templateCache, $mdColorPicker) {
82 val = undefined; 82 val = undefined;
83 } 83 }
84 selectOrSet(key, scope.model, val); 84 selectOrSet(key, scope.model, val);
  85 + scope.formProps.model = scope.model;
85 }, 86 },
86 onColorClick: function(event, key, val) { 87 onColorClick: function(event, key, val) {
87 scope.showColorPicker(event, val); 88 scope.showColorPicker(event, val);
@@ -27,39 +27,90 @@ class ThingsboardRcSelect extends React.Component { @@ -27,39 +27,90 @@ class ThingsboardRcSelect extends React.Component {
27 this.onDeselect = this.onDeselect.bind(this); 27 this.onDeselect = this.onDeselect.bind(this);
28 this.onBlur = this.onBlur.bind(this); 28 this.onBlur = this.onBlur.bind(this);
29 this.onFocus = this.onFocus.bind(this); 29 this.onFocus = this.onFocus.bind(this);
30 - let emptyValue = this.props.form.schema.type === 'array'? [] : null;  
31 this.state = { 30 this.state = {
32 - currentValue: this.props.value || emptyValue, 31 + currentValue: this.keyToCurrentValue(this.props.value, this.props.form.schema.type === 'array'),
33 items: this.props.form.items, 32 items: this.props.form.items,
34 focused: false 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 onSelect(value, option) { 89 onSelect(value, option) {
39 if(this.props.form.schema.type === 'array') { 90 if(this.props.form.schema.type === 'array') {
40 let v = this.state.currentValue; 91 let v = this.state.currentValue;
41 - v.push(value); 92 + v.push(this.keyToCurrentValue(value.key, false));
42 this.setState({ 93 this.setState({
43 currentValue: v 94 currentValue: v
44 }); 95 });
45 - this.props.onChangeValidate(v); 96 + this.props.onChangeValidate(this.arrayValues(v));
46 } else { 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 onDeselect(value, option) { 103 onDeselect(value, option) {
53 if (this.props.form.schema.type === 'array') { 104 if (this.props.form.schema.type === 'array') {
54 let v = this.state.currentValue; 105 let v = this.state.currentValue;
55 - let index = v.indexOf(value); 106 + let index = this.keyIndex(v, value.key);
56 if (index > -1) { 107 if (index > -1) {
57 v.splice(index, 1); 108 v.splice(index, 1);
58 } 109 }
59 this.setState({ 110 this.setState({
60 currentValue: v 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,6 +156,7 @@ class ThingsboardRcSelect extends React.Component {
105 combobox={this.props.form.combobox} 156 combobox={this.props.form.combobox}
106 disabled={this.props.form.readonly} 157 disabled={this.props.form.readonly}
107 value={this.state.currentValue} 158 value={this.state.currentValue}
  159 + labelInValue={true}
108 onSelect={this.onSelect} 160 onSelect={this.onSelect}
109 onDeselect={this.onDeselect} 161 onDeselect={this.onDeselect}
110 onFocus={this.onFocus} 162 onFocus={this.onFocus}
@@ -63,11 +63,15 @@ class ThingsboardSchemaForm extends React.Component { @@ -63,11 +63,15 @@ class ThingsboardSchemaForm extends React.Component {
63 63
64 this.onChange = this.onChange.bind(this); 64 this.onChange = this.onChange.bind(this);
65 this.onColorClick = this.onColorClick.bind(this); 65 this.onColorClick = this.onColorClick.bind(this);
  66 + this.hasConditions = false;
66 } 67 }
67 68
68 onChange(key, val) { 69 onChange(key, val) {
69 //console.log('SchemaForm.onChange', key, val); 70 //console.log('SchemaForm.onChange', key, val);
70 this.props.onModelChange(key, val); 71 this.props.onModelChange(key, val);
  72 + if (this.hasConditions) {
  73 + this.forceUpdate();
  74 + }
71 } 75 }
72 76
73 onColorClick(event, key, val) { 77 onColorClick(event, key, val) {
@@ -81,8 +85,11 @@ class ThingsboardSchemaForm extends React.Component { @@ -81,8 +85,11 @@ class ThingsboardSchemaForm extends React.Component {
81 console.log('Invalid field: \"' + form.key[0] + '\"!'); 85 console.log('Invalid field: \"' + form.key[0] + '\"!');
82 return null; 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 return <Field model={model} form={form} key={index} onChange={onChange} onColorClick={onColorClick} mapper={mapper} builder={this.builder}/> 94 return <Field model={model} form={form} key={index} onChange={onChange} onColorClick={onColorClick} mapper={mapper} builder={this.builder}/>
88 } 95 }