|
1 | | -/* |
2 | | - * Copyright 2013 The Polymer Authors. All rights reserved. |
3 | | - * Use of this source code is governed by a BSD-style |
4 | | - * license that can be found in the LICENSE file. |
5 | | - */ |
6 | | -(function(scope) { |
7 | | - |
8 | | - // magic words |
9 | | - |
10 | | - var PUBLISHED = '__published'; |
11 | | - var INSTANCE_ATTRIBUTES = '__instance_attributes'; |
12 | | - |
13 | | - // instance api for attributes |
14 | | - |
15 | | - var attributes = { |
16 | | - PUBLISHED: PUBLISHED, |
17 | | - INSTANCE_ATTRIBUTES: INSTANCE_ATTRIBUTES, |
18 | | - copyInstanceAttributes: function () { |
19 | | - var a$ = this[INSTANCE_ATTRIBUTES]; |
20 | | - for (var k in a$) { |
21 | | - this.setAttribute(k, a$[k]); |
22 | | - } |
23 | | - }, |
24 | | - // for each attribute on this, deserialize value to property as needed |
25 | | - takeAttributes: function() { |
26 | | - for (var i=0, a$=this.attributes, l=a$.length, a; (a=a$[i]) && i<l; i++) { |
27 | | - this.attributeToProperty(a.name, a.value); |
28 | | - } |
29 | | - }, |
30 | | - // if attribute 'name' is mapped to a property, deserialize |
31 | | - // 'value' into that property |
32 | | - attributeToProperty: function(name, value) { |
33 | | - // try to match this attribute to a property (attributes are |
34 | | - // all lower-case, so this is case-insensitive search) |
35 | | - var name = this.propertyForAttribute(name); |
36 | | - if (name) { |
37 | | - // filter out 'mustached' values, these are to be |
38 | | - // replaced with bound-data and are not yet values |
39 | | - // themselves |
40 | | - if (value.search(scope.bindPattern) >= 0) { |
41 | | - return; |
42 | | - } |
43 | | - // get original value |
44 | | - var defaultValue = this[name]; |
45 | | - // deserialize Boolean or Number values from attribute |
46 | | - var value = this.deserializeValue(value, defaultValue); |
47 | | - // only act if the value has changed |
48 | | - if (value !== defaultValue) { |
49 | | - // install new value (has side-effects) |
50 | | - this[name] = value; |
51 | | - } |
52 | | - } |
53 | | - }, |
54 | | - // return the published property matching name, or undefined |
55 | | - propertyForAttribute: function(name) { |
56 | | - // matchable properties must be published |
57 | | - var properties = Object.keys(this[PUBLISHED]); |
58 | | - // search for a matchable property |
59 | | - return properties[properties.map(lowerCase).indexOf(name.toLowerCase())]; |
60 | | - }, |
61 | | - // convert representation of 'stringValue' based on type of 'defaultValue' |
62 | | - deserializeValue: function(stringValue, defaultValue) { |
63 | | - return scope.deserializeValue(stringValue, defaultValue); |
64 | | - }, |
65 | | - serializeValue: function(value) { |
66 | | - if (typeof value != 'object' && value !== undefined) { |
67 | | - return value; |
68 | | - } |
69 | | - }, |
70 | | - propertyToAttribute: function(name) { |
71 | | - if (Object.keys(this[PUBLISHED]).indexOf(name) >= 0) { |
72 | | - var serializedValue = this.serializeValue(this[name]); |
73 | | - if (serializedValue !== undefined) { |
74 | | - this.setAttribute(name, serializedValue); |
75 | | - } |
76 | | - } |
77 | | - } |
78 | | - }; |
79 | | - |
80 | | - var lowerCase = String.prototype.toLowerCase.call.bind( |
81 | | - String.prototype.toLowerCase); |
82 | | - |
83 | | - // exports |
84 | | - |
85 | | - scope.api.instance.attributes = attributes; |
86 | | - |
87 | | -})(Polymer); |
| 1 | +/* |
| 2 | + * Copyright 2013 The Polymer Authors. All rights reserved. |
| 3 | + * Use of this source code is governed by a BSD-style |
| 4 | + * license that can be found in the LICENSE file. |
| 5 | + */ |
| 6 | +(function(scope) { |
| 7 | + |
| 8 | + // magic words |
| 9 | + |
| 10 | + var PUBLISHED = '__published'; |
| 11 | + var INSTANCE_ATTRIBUTES = '__instance_attributes'; |
| 12 | + |
| 13 | + // instance api for attributes |
| 14 | + |
| 15 | + var attributes = { |
| 16 | + PUBLISHED: PUBLISHED, |
| 17 | + INSTANCE_ATTRIBUTES: INSTANCE_ATTRIBUTES, |
| 18 | + copyInstanceAttributes: function () { |
| 19 | + var a$ = this[INSTANCE_ATTRIBUTES]; |
| 20 | + for (var k in a$) { |
| 21 | + this.setAttribute(k, a$[k]); |
| 22 | + } |
| 23 | + }, |
| 24 | + // for each attribute on this, deserialize value to property as needed |
| 25 | + takeAttributes: function() { |
| 26 | + for (var i=0, a$=this.attributes, l=a$.length, a; (a=a$[i]) && i<l; i++) { |
| 27 | + this.attributeToProperty(a.name, a.value); |
| 28 | + } |
| 29 | + }, |
| 30 | + // if attribute 'name' is mapped to a property, deserialize |
| 31 | + // 'value' into that property |
| 32 | + attributeToProperty: function(name, value) { |
| 33 | + // try to match this attribute to a property (attributes are |
| 34 | + // all lower-case, so this is case-insensitive search) |
| 35 | + var name = this.propertyForAttribute(name); |
| 36 | + if (name) { |
| 37 | + // filter out 'mustached' values, these are to be |
| 38 | + // replaced with bound-data and are not yet values |
| 39 | + // themselves |
| 40 | + if (value && value.search(scope.bindPattern) >= 0) { |
| 41 | + return; |
| 42 | + } |
| 43 | + // get original value |
| 44 | + var defaultValue = this[name]; |
| 45 | + // deserialize Boolean or Number values from attribute |
| 46 | + var value = this.deserializeValue(value, defaultValue); |
| 47 | + // only act if the value has changed |
| 48 | + if (value !== defaultValue) { |
| 49 | + // install new value (has side-effects) |
| 50 | + this[name] = value; |
| 51 | + } |
| 52 | + } |
| 53 | + }, |
| 54 | + // return the published property matching name, or undefined |
| 55 | + propertyForAttribute: function(name) { |
| 56 | + // matchable properties must be published |
| 57 | + var properties = Object.keys(this[PUBLISHED]); |
| 58 | + // search for a matchable property |
| 59 | + return properties[properties.map(lowerCase).indexOf(name.toLowerCase())]; |
| 60 | + }, |
| 61 | + // convert representation of 'stringValue' based on type of 'defaultValue' |
| 62 | + deserializeValue: function(stringValue, defaultValue) { |
| 63 | + return scope.deserializeValue(stringValue, defaultValue); |
| 64 | + }, |
| 65 | + serializeValue: function(value) { |
| 66 | + if (typeof value != 'object' && value !== undefined) { |
| 67 | + return value; |
| 68 | + } |
| 69 | + }, |
| 70 | + propertyToAttribute: function(name) { |
| 71 | + if (Object.keys(this[PUBLISHED]).indexOf(name) >= 0) { |
| 72 | + var serializedValue = this.serializeValue(this[name]); |
| 73 | + // boolean properties must reflect as boolean attributes |
| 74 | + if (typeof this.__proto__[name] === 'boolean') { |
| 75 | + if (serializedValue) { |
| 76 | + this.setAttribute(name, ''); |
| 77 | + } else { |
| 78 | + this.removeAttribute(name); |
| 79 | + } |
| 80 | + } else if (serializedValue !== undefined) { |
| 81 | + this.setAttribute(name, serializedValue); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + var lowerCase = String.prototype.toLowerCase.call.bind( |
| 88 | + String.prototype.toLowerCase); |
| 89 | + |
| 90 | + // exports |
| 91 | + |
| 92 | + scope.api.instance.attributes = attributes; |
| 93 | + |
| 94 | +})(Polymer); |
0 commit comments