|
| 1 | +<!-- |
| 2 | +Defines behaviors usable for Energy2Use (namespace e2uBehaviors). |
| 3 | +
|
| 4 | +Especially, holds methods to create GUI elements for the array |
| 5 | +elements and helpers to handle the JSON schema. |
| 6 | +
|
| 7 | + |
| 8 | +
|
| 9 | +--> |
| 10 | + |
| 11 | +<script> |
| 12 | + // initialize behavior namespace if it not yet exists |
| 13 | + window.E2uBehaviors = window.E2uBehaviors || {}; |
| 14 | + //console.log('Init behavior E2uBehaviors'); |
| 15 | + /** |
| 16 | + * This behavior holds all code to read a JSON schema. |
| 17 | + */ |
| 18 | + /** @polymerBehavior E2uBehaviors.JSONSchema */ |
| 19 | + E2uBehaviors.JSONSchema = { |
| 20 | + created: function() { |
| 21 | + //console.log("Behavior created"); |
| 22 | + }, |
| 23 | + registered: function() { |
| 24 | + //console.log("Behavior registered"); |
| 25 | + }, |
| 26 | + getArrayKey: function getArrayKey(myArray, myIndex) { |
| 27 | + var collection = Polymer.Collection.get(myArray); |
| 28 | + var keyIndexHash = collection.getKey(myArray[myIndex]); |
| 29 | + return keyIndexHash; |
| 30 | + }, |
| 31 | + getArrayIndex: function getArrayIndex(myArray, myKey) { |
| 32 | + var collection = Polymer.Collection.get(myArray); |
| 33 | + var item = collection.getItem(myKey); |
| 34 | + var index = myArray.indexOf(item); |
| 35 | + return index; |
| 36 | + }, |
| 37 | + /** |
| 38 | + * Create an element to be inserted into the generated form. |
| 39 | + * @parameter schemaProperty |
| 40 | + * An object describing how to create the element. It must have the following properties: |
| 41 | + * - property - a property name (path) representing the current property in an underlying object |
| 42 | + * For arrays, we have the value of the $arrKey property in the array elements. |
| 43 | + * - schema - the JSON schema describing the value to be created |
| 44 | + * - component - describe the component to be used, with name=component name and |
| 45 | + * valueProperty=property to set the value on the element |
| 46 | + */ |
| 47 | + _createElement: function createElement(schemaProperty) { |
| 48 | + var ctx = this; |
| 49 | + var pName = schemaProperty.property; |
| 50 | + var cName = schemaProperty.component.name; |
| 51 | + var schema = schemaProperty.schema; |
| 52 | + //var eleProps = |
| 53 | + var el = ctx.create(cName, { |
| 54 | + label: schemaProperty.label |
| 55 | + }); |
| 56 | + if (cName == 'e2u-json-object-editor') { |
| 57 | + // if we have an object, add the schema information. |
| 58 | + el.schema = schema; |
| 59 | + el.schemaProperty = schemaProperty; |
| 60 | + el._level += 1; |
| 61 | + el.setAttribute('style', 'margin-left:' + el._level * 10 + 'px'); |
| 62 | + el.addEventListener('value-changed', ctx._objectUp('value.' + pName, el)); |
| 63 | + } else if (cName == 'e2u-json-array-editor') { |
| 64 | + // if we have an array, add the schema information. |
| 65 | + el.schema = schema; |
| 66 | + el.schemaProperty = schemaProperty; |
| 67 | + el._level += 1; |
| 68 | + el.setAttribute('style', 'margin-left:' + el._level * 10 + 'px'); |
| 69 | + el.addEventListener('value-changed', ctx._objectUp('value.' + pName, el)); |
| 70 | + } else { |
| 71 | + // else set id to the property name and set an eventlistener to |
| 72 | + // use the valueUp() method which sets the subproperty. |
| 73 | + el.setAttribute('id', pName); |
| 74 | + el.addEventListener('value-changed', ctx._valueUp('value.' + pName)); |
| 75 | + // Since we are editing a simple property, we check for |
| 76 | + var inputEl = el.$.input; |
| 77 | + inputEl.setAttribute('onfocusout', 'validate()'); |
| 78 | + if (this._isSchemaNumber(schema.type)) { |
| 79 | + inputEl.type = 'number'; |
| 80 | + |
| 81 | + if (schema.multipleOf) { |
| 82 | + inputEl.step = schema.multipleOf; |
| 83 | + } |
| 84 | + |
| 85 | + if (!isNaN(schema.maximum)) { |
| 86 | + if (schema.exclusiveMaximum) { |
| 87 | + inputEl.max = schema.maximum - (schema.multipleOf || 1); |
| 88 | + } else { |
| 89 | + inputEl.max = schema.maximum; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + if (!isNaN(schema.minimum)) { |
| 94 | + if (schema.exclusiveMinimum) { |
| 95 | + inputEl.min = schema.minimum + (schema.multipleOf || 1); |
| 96 | + } else { |
| 97 | + inputEl.min = schema.minimum; |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + if (this._isSchemaString(schema.type)) { |
| 103 | + if (schema.format === 'date-time') { |
| 104 | + inputEl.type = 'datetime-local'; |
| 105 | + inputEl.alwaysFloatLabel = true; // label doesn't float when value not set |
| 106 | + } else if (schema.format === 'date') { |
| 107 | + inputEl.type = 'date'; |
| 108 | + } else if (schema.format === 'email') { |
| 109 | + inputEl.type = 'email'; |
| 110 | + } else if (schema.format === 'hostname') { |
| 111 | + inputEl.type = 'text'; |
| 112 | + } else if (schema.format === 'ipv4') { |
| 113 | + inputEl.type = 'text'; |
| 114 | + } else if (schema.format === 'ipv6') { |
| 115 | + inputEl.type = 'text'; |
| 116 | + } else if (schema.format === 'uri') { |
| 117 | + inputEl.type = 'url'; |
| 118 | + } else { |
| 119 | + inputEl.type = 'text'; |
| 120 | + } |
| 121 | + |
| 122 | + if (schema.maxLength || schema.minLength) { |
| 123 | + inputEl.charCounter = true; |
| 124 | + } |
| 125 | + |
| 126 | + if (schema.maxLength) { |
| 127 | + inputEl.maxlength = schema.maxLength; |
| 128 | + } |
| 129 | + |
| 130 | + if (schema.minLength) { |
| 131 | + inputEl.minlength = schema.minLength; |
| 132 | + } |
| 133 | + |
| 134 | + if (schema.pattern) { |
| 135 | + inputEl.pattern = schema.pattern; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + if (schema.component && schema.component.properties) { |
| 140 | + Object.keys(schema.component.properties).forEach(function(prop) { |
| 141 | + inputEl[prop] = schema.component.properties[prop]; |
| 142 | + }); |
| 143 | + } |
| 144 | + |
| 145 | + if (schema.title) { |
| 146 | + inputEl.label = schema.title; |
| 147 | + } |
| 148 | + } |
| 149 | + return el; |
| 150 | + }, |
| 151 | + _deepClone: function(o) { |
| 152 | + return JSON.parse(JSON.stringify(o)); |
| 153 | + }, |
| 154 | + /* Is the schema fragment representing an enumeration? */ |
| 155 | + _isSchemaEnum: function(schema) { |
| 156 | + return !!schema.enum; |
| 157 | + }, |
| 158 | + /* Is the schema fragment representing a simple value? */ |
| 159 | + _isSchemaValue: function(type) { |
| 160 | + return this._isSchemaBoolean(type) || this._isSchemaNumber(type) || this._isSchemaString(type); |
| 161 | + }, |
| 162 | + /* Is the schema fragment representing a boolean? */ |
| 163 | + _isSchemaBoolean: function(type) { |
| 164 | + if (Array.isArray(type)) { |
| 165 | + return type.indexOf('boolean') !== -1; |
| 166 | + } else { |
| 167 | + return type === 'boolean'; |
| 168 | + } |
| 169 | + }, |
| 170 | + _isSchemaNumber: function(type) { |
| 171 | + if (Array.isArray(type)) { |
| 172 | + return type.indexOf('number') !== -1 || type.indexOf('integer') !== -1; |
| 173 | + } else { |
| 174 | + return type === 'number' || type === 'integer'; |
| 175 | + } |
| 176 | + }, |
| 177 | + _isSchemaString: function(type) { |
| 178 | + if (Array.isArray(type)) { |
| 179 | + return type.indexOf('string') !== -1; |
| 180 | + } else { |
| 181 | + return type === 'string'; |
| 182 | + } |
| 183 | + }, |
| 184 | + _isSchemaObject: function(type) { |
| 185 | + return type === 'object'; |
| 186 | + }, |
| 187 | + _isSchemaArray: function(type) { |
| 188 | + return type === 'array'; |
| 189 | + }, |
| 190 | + /** |
| 191 | + * This is a fallback method. |
| 192 | + */ |
| 193 | + _schemaPropertyChanged: function(event, detail) { |
| 194 | + console.log('_schemaPropertyChanged(' + event + ',' + detail + ')'); |
| 195 | + }, |
| 196 | + /** |
| 197 | + * Debug method only useful for output of strange elements |
| 198 | + */ |
| 199 | + safeJSONString: function(obj, indent) { |
| 200 | + var censor = function censor(censor) { |
| 201 | + var i = 0; |
| 202 | + var seenValues = []; |
| 203 | + return function(key, value) { |
| 204 | + if (seenValues.indexOf(value) >= 0) { |
| 205 | + return '[Circular]'; |
| 206 | + } |
| 207 | + seenValues.push(value); |
| 208 | + // if (i >= 29) // seems to be a harded maximum of 30 serialized objects? |
| 209 | + // return '[Unknown]'; |
| 210 | + ++i; // so we know we aren't using the original object anymore |
| 211 | + return value; |
| 212 | + } |
| 213 | + }; |
| 214 | + return JSON.stringify(obj, censor(obj), indent); |
| 215 | + } |
| 216 | + }; |
| 217 | +</script> |
0 commit comments