forked from ecoutu/eco-json-schema-form
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheco-json-schema-input.html
More file actions
183 lines (155 loc) · 4.86 KB
/
Copy patheco-json-schema-input.html
File metadata and controls
183 lines (155 loc) · 4.86 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<link rel="import" href="../polymer/polymer.html"/>
<link rel="import" href="../iron-flex-layout/iron-flex-layout.html"/>
<!--
`eco-json-schema-input` takes in a JSON schema of type number and string and
contains a `paper-input`, exposing a `value` property that represents the schema.
Validation is handled for strings and number/integers by mapping JSON schema
validation keywords to `paper-input` attributes; form elements will automatically
try and validate themselves as users provide input:
Please see the `eco-json-schema-object` documentation for further information.
@group eco Elements
@element eco-json-schema-input
@demo demo/index.html
-->
<dom-module id="eco-json-schema-input">
<link rel="import" href="../paper-input/paper-input.html"/>
<template>
<style>
:host {
display: block;
}
</style>
<paper-input id="input"
value="{{value}}"
auto-validate>
</paper-input>
</template>
<script>
Polymer({
is: 'eco-json-schema-input',
properties: {
schema: {
type: Object,
observer: '_schemaChanged'
},
value: {
type: String,
notify: true,
value: function () {
return '';
}
},
error: {
type: String,
observer: '_errorChanged',
value: null
}
},
ready: function () {
},
detached: function () {
},
_schemaChanged: function () {
var schema = this.schema;
var inputEl = this.$.input;
if (this._isSchemaNumber(schema.type)) {
inputEl.type = 'number';
if (schema.multipleOf) {
inputEl.step = schema.multipleOf;
}
if (!isNaN(schema.maximum)) {
if (schema.exclusiveMaximum) {
inputEl.max = schema.maximum - (schema.multipleOf || 1);
} else {
inputEl.max = schema.maximum;
}
}
if (!isNaN(schema.minimum)) {
if (schema.exclusiveMinimum) {
inputEl.min = schema.minimum + (schema.multipleOf || 1);
} else {
inputEl.min = schema.minimum;
}
}
}
if (this._isSchemaString(schema.type)) {
if (schema.format === 'date-time') {
inputEl.type = 'datetime-local';
inputEl.alwaysFloatLabel = true; // label doesn't float when value not set
} else if (schema.format === 'email') {
inputEl.type = 'email';
} else if (schema.format === 'hostname') {
inputEl.type = 'text';
} else if (schema.format === 'ipv4') {
inputEl.type = 'text';
} else if (schema.format === 'ipv6') {
inputEl.type = 'text';
} else if (schema.format === 'uri') {
inputEl.type = 'url';
} else {
inputEl.type = 'text';
}
if (schema.maxLength || schema.minLength) {
inputEl.charCounter = true;
}
if (schema.maxLength) {
inputEl.maxlength = schema.maxLength;
}
if (schema.minLength) {
inputEl.minlength = schema.minLength;
}
if (schema.pattern) {
inputEl.pattern = schema.pattern;
}
}
if (schema.component && schema.component.properties) {
Object.keys(schema.component.properties).forEach(function(prop) {
inputEl[prop] = schema.component.properties[prop];
});
}
if (schema.title) {
inputEl.label = schema.title;
}
},
_errorChanged: function() {
if (this.error) {
this.$.input.errorMessage = this.error;
this.$.input.invalid = true;
} else {
this.$.input.invalid = false;
this.$.input.errorMessage = null;
}
},
_isSchemaValue: function (type) {
return this._isSchemaBoolean(type) || this._isSchemaNumber(type) || this._isSchemaString(type);
},
_isSchemaBoolean: function (type) {
if (Array.isArray(type)) {
return type.indexOf('boolean') !== -1;
} else {
return type === 'boolean';
}
},
_isSchemaNumber: function(type) {
if (Array.isArray(type)) {
return type.indexOf('number') !== -1 || type.indexOf('integer') !== -1;
} else {
return type === 'number' || type === 'integer';
}
},
_isSchemaString: function (type) {
if (Array.isArray(type)) {
return type.indexOf('string') !== -1;
} else {
return type === 'string';
}
},
_isSchemaObject: function (type) {
return type === 'object';
},
_isSchemaArray: function (type) {
return type === 'array';
}
});
</script>
</dom-module>