OLD | NEW |
(Empty) | |
| 1 |
| 2 |
| 3 (function() { |
| 4 |
| 5 // monostate data |
| 6 var metaDatas = {}; |
| 7 var metaArrays = {}; |
| 8 |
| 9 Polymer.IronMeta = Polymer({ |
| 10 |
| 11 is: 'iron-meta', |
| 12 |
| 13 properties: { |
| 14 |
| 15 /** |
| 16 * The type of meta-data. All meta-data of the same type is stored |
| 17 * together. |
| 18 * |
| 19 * @attribute type |
| 20 * @type String |
| 21 * @default 'default' |
| 22 */ |
| 23 type: { |
| 24 type: String, |
| 25 value: 'default', |
| 26 observer: '_typeChanged' |
| 27 }, |
| 28 |
| 29 /** |
| 30 * The key used to store `value` under the `type` namespace. |
| 31 * |
| 32 * @attribute key |
| 33 * @type String |
| 34 * @default '' |
| 35 */ |
| 36 key: { |
| 37 type: String, |
| 38 observer: '_keyChanged' |
| 39 }, |
| 40 |
| 41 /** |
| 42 * The meta-data to store or retrieve. |
| 43 * |
| 44 * @attribute value |
| 45 * @type * |
| 46 * @default this |
| 47 */ |
| 48 value: { |
| 49 type: Object, |
| 50 notify: true, |
| 51 observer: '_valueChanged' |
| 52 }, |
| 53 |
| 54 /** |
| 55 * If true, `value` is set to the iron-meta instance itself. |
| 56 * |
| 57 * @attribute self |
| 58 * @type Boolean |
| 59 * @default false |
| 60 */ |
| 61 self: { |
| 62 type: Boolean, |
| 63 observer: '_selfChanged' |
| 64 }, |
| 65 |
| 66 /** |
| 67 * Array of all meta-data values for the given type. |
| 68 * |
| 69 * @property list |
| 70 * @type Array |
| 71 */ |
| 72 list: { |
| 73 type: Array, |
| 74 notify: true |
| 75 } |
| 76 |
| 77 }, |
| 78 |
| 79 /** |
| 80 * Only runs if someone invokes the factory/constructor directly |
| 81 * e.g. `new Polymer.IronMeta()` |
| 82 */ |
| 83 factoryImpl: function(config) { |
| 84 if (config) { |
| 85 for (var n in config) { |
| 86 switch(n) { |
| 87 case 'type': |
| 88 case 'key': |
| 89 case 'value': |
| 90 this[n] = config[n]; |
| 91 break; |
| 92 } |
| 93 } |
| 94 } |
| 95 }, |
| 96 |
| 97 created: function() { |
| 98 // TODO(sjmiles): good for debugging? |
| 99 this._metaDatas = metaDatas; |
| 100 this._metaArrays = metaArrays; |
| 101 }, |
| 102 |
| 103 _keyChanged: function(key, old) { |
| 104 this._resetRegistration(old); |
| 105 }, |
| 106 |
| 107 _valueChanged: function(value) { |
| 108 this._resetRegistration(this.key); |
| 109 }, |
| 110 |
| 111 _selfChanged: function(self) { |
| 112 if (self) { |
| 113 this.value = this; |
| 114 } |
| 115 }, |
| 116 |
| 117 _typeChanged: function(type) { |
| 118 this._unregisterKey(this.key); |
| 119 if (!metaDatas[type]) { |
| 120 metaDatas[type] = {}; |
| 121 } |
| 122 this._metaData = metaDatas[type]; |
| 123 if (!metaArrays[type]) { |
| 124 metaArrays[type] = []; |
| 125 } |
| 126 this.list = metaArrays[type]; |
| 127 this._registerKeyValue(this.key, this.value); |
| 128 }, |
| 129 |
| 130 /** |
| 131 * Retrieves meta data value by key. |
| 132 * |
| 133 * @method byKey |
| 134 * @param {String} key The key of the meta-data to be returned. |
| 135 * @returns * |
| 136 */ |
| 137 byKey: function(key) { |
| 138 return this._metaData && this._metaData[key]; |
| 139 }, |
| 140 |
| 141 _resetRegistration: function(oldKey) { |
| 142 this._unregisterKey(oldKey); |
| 143 this._registerKeyValue(this.key, this.value); |
| 144 }, |
| 145 |
| 146 _unregisterKey: function(key) { |
| 147 this._unregister(key, this._metaData, this.list); |
| 148 }, |
| 149 |
| 150 _registerKeyValue: function(key, value) { |
| 151 this._register(key, value, this._metaData, this.list); |
| 152 }, |
| 153 |
| 154 _register: function(key, value, data, list) { |
| 155 if (key && data && value !== undefined) { |
| 156 data[key] = value; |
| 157 list.push(value); |
| 158 } |
| 159 }, |
| 160 |
| 161 _unregister: function(key, data, list) { |
| 162 if (key && data) { |
| 163 if (key in data) { |
| 164 var value = data[key]; |
| 165 delete data[key]; |
| 166 this.arrayDelete(list, value); |
| 167 } |
| 168 } |
| 169 } |
| 170 |
| 171 }); |
| 172 |
| 173 /** |
| 174 `iron-meta-query` can be used to access infomation stored in `iron-meta`. |
| 175 |
| 176 Examples: |
| 177 |
| 178 If I create an instance like this: |
| 179 |
| 180 <iron-meta key="info" value="foo/bar"></iron-meta> |
| 181 |
| 182 Note that keyUrl="foo/bar" is the metadata I've defined. I could define more |
| 183 attributes or use child nodes to define additional metadata. |
| 184 |
| 185 Now I can access that element (and it's metadata) from any `iron-meta-query`
instance: |
| 186 |
| 187 var value = new Polymer.IronMetaQuery({key: 'info'}).value; |
| 188 |
| 189 @group Polymer Iron Elements |
| 190 @element iron-meta-query |
| 191 */ |
| 192 Polymer.IronMetaQuery = Polymer({ |
| 193 |
| 194 is: 'iron-meta-query', |
| 195 |
| 196 properties: { |
| 197 |
| 198 /** |
| 199 * The type of meta-data. All meta-data of the same type is stored |
| 200 * together. |
| 201 * |
| 202 * @attribute type |
| 203 * @type String |
| 204 * @default 'default' |
| 205 */ |
| 206 type: { |
| 207 type: String, |
| 208 value: 'default', |
| 209 observer: '_typeChanged' |
| 210 }, |
| 211 |
| 212 /** |
| 213 * Specifies a key to use for retrieving `value` from the `type` |
| 214 * namespace. |
| 215 * |
| 216 * @attribute key |
| 217 * @type String |
| 218 */ |
| 219 key: { |
| 220 type: String, |
| 221 observer: '_keyChanged' |
| 222 }, |
| 223 |
| 224 /** |
| 225 * The meta-data to store or retrieve. |
| 226 * |
| 227 * @attribute value |
| 228 * @type * |
| 229 * @default this |
| 230 */ |
| 231 value: { |
| 232 type: Object, |
| 233 notify: true, |
| 234 readOnly: true |
| 235 }, |
| 236 |
| 237 /** |
| 238 * Array of all meta-data values for the given type. |
| 239 * |
| 240 * @property list |
| 241 * @type Array |
| 242 */ |
| 243 list: { |
| 244 type: Array, |
| 245 notify: true |
| 246 } |
| 247 |
| 248 }, |
| 249 |
| 250 /** |
| 251 * Actually a factory method, not a true constructor. Only runs if |
| 252 * someone invokes it directly (via `new Polymer.IronMeta()`); |
| 253 */ |
| 254 constructor: function(config) { |
| 255 if (config) { |
| 256 for (var n in config) { |
| 257 switch(n) { |
| 258 case 'type': |
| 259 case 'key': |
| 260 this[n] = config[n]; |
| 261 break; |
| 262 } |
| 263 } |
| 264 } |
| 265 }, |
| 266 |
| 267 created: function() { |
| 268 // TODO(sjmiles): good for debugging? |
| 269 this._metaDatas = metaDatas; |
| 270 this._metaArrays = metaArrays; |
| 271 }, |
| 272 |
| 273 _keyChanged: function(key) { |
| 274 this._setValue(this._metaData && this._metaData[key]); |
| 275 }, |
| 276 |
| 277 _typeChanged: function(type) { |
| 278 this._metaData = metaDatas[type]; |
| 279 this.list = metaArrays[type]; |
| 280 if (this.key) { |
| 281 this._keyChanged(this.key); |
| 282 } |
| 283 }, |
| 284 |
| 285 /** |
| 286 * Retrieves meta data value by key. |
| 287 * |
| 288 * @method byKey |
| 289 * @param {String} key The key of the meta-data to be returned. |
| 290 * @returns * |
| 291 */ |
| 292 byKey: function(key) { |
| 293 return this._metaData && this._metaData[key]; |
| 294 } |
| 295 |
| 296 }); |
| 297 |
| 298 })(); |
OLD | NEW |