This repository was archived by the owner on Jul 9, 2024. It is now read-only.
forked from DimShadoWWW/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
219 lines (178 loc) · 5.76 KB
/
Copy patheco-json-schema-input.html
File metadata and controls
219 lines (178 loc) · 5.76 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<link rel="import" href="../polymer/polymer.html"/>
<link rel="import" href="../iron-flex-layout/iron-flex-layout.html"/>
<link rel="import" href="../app-localize-behavior/app-localize-behavior.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"/>
<link rel="import" href="../paper-styles/typography.html"/>
<template>
<style>
paper-input{ --paper-input-container:{width:300px;}; }
</style>
<paper-input class="Eco-Json-input" id="input"
class="flex"
value="{{value}}"
auto-validate
label="[[label]]" >
</paper-input>
</template>
<script>
class EcoJsonSchemaInput extends Polymer.Element {
static get is() {
return 'eco-json-schema-input';
}
static get behaviors() {
return [Polymer.AppLocalizeBehavior]
}
static get properties() {
return {
language: {
value: 'en'
},
resources: {
value: function () {
return {};
}
},
schema: {
type: Object,
observer: '_schemaChanged'
},
value: {
type: String,
notify: true,
value: function () {
return '';
}
},
error: {
type: String,
observer: '_errorChanged',
value: null
}
}
}
_schemaChanged() {
var schema = this.schema;
var inputEl = this.$.input;
if (schema.required) {
inputEl.required = true;
}
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 === 'date') {
inputEl.type = 'date';
} 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];
});
}
// Ravi S commenting out as we do not need label to float by default
//inputEl.alwaysFloatLabel = true; // label doesn't float when value not set
if (schema.title) {
inputEl.label = schema.title;
}
}
_errorChanged() {
if (this.error) {
this.$.input.errorMessage = this.error;
this.$.input.invalid = true;
} else {
this.$.input.invalid = false;
this.$.input.errorMessage = null;
}
}
_isSchemaValue(type) {
return this._isSchemaBoolean(type) || this._isSchemaNumber(type) || this._isSchemaString(type);
}
_isSchemaBoolean(type) {
if (Array.isArray(type)) {
return type.indexOf('boolean') !== -1;
} else {
return type === 'boolean';
}
}
_isSchemaNumber(type) {
if (Array.isArray(type)) {
return type.indexOf('number') !== -1 || type.indexOf('integer') !== -1;
} else {
return type === 'number' || type === 'integer';
}
}
_isSchemaString(type) {
if (Array.isArray(type)) {
return type.indexOf('string') !== -1;
} else {
return type === 'string';
}
}
_isSchemaObject(type) {
return type === 'object';
}
_isSchemaArray(type) {
return type === 'array';
}
}
window.customElements.define(EcoJsonSchemaInput.is, EcoJsonSchemaInput);
</script>
</dom-module>