This repository was archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray-composer.html
More file actions
209 lines (177 loc) · 4.38 KB
/
array-composer.html
File metadata and controls
209 lines (177 loc) · 4.38 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
<!--
@license
Copyright (c) 2015 Khaos-Coders. All rights reserved.
This code may only be used under the BSD style license found at http://khaoscoders.github.io/LICENSE.txt
The complete set of authors may be found at http://khaoscoders.github.io/AUTHORS.txt
The complete set of contributors may be found at http://khaoscoders.github.io/CONTRIBUTORS.txt
Code distributed by Khaos-Coders as part of this project is also
subject to an additional IP rights grant found at http://khaoscoders.github.io/PATENTS.txt
-->
<link rel="import" href="../polymer/polymer.html">
<!--
This element helps composing arrays on-the-fly via data-bindings.
Many elements take arrays as parameters, `dom-repeat` for example.
If you can't - or simply don't want to - create the array manually,
this element is what you need!
Thanks to the nature of data-bindings the array will always contain
the latest field values.
You can bind up to 10 fields to this element and in return you get
an one-dimensional array containing all fields values.
Example:
<array-composer array="{{outputArray}}"
length="3"
f0="[[field0]]"
f1="[[field1]]"
f2="[[field2]]">
</array-composer>
Use cases:
On the main page, where you can have data-bindings using `dom-bind`
template, but can't create arrays to bind to.
Any other case where you're in need of a fast way to compose an array
from a few fields.
@demo
-->
<script>
Polymer({
is: 'array-composer',
properties: {
/**
* The output array will be set to this parameter.
*/
array: {
type: Object,
notify: true,
readOnly: true
},
/**
* The number of fields in the array.
*/
length: {
value: 0,
type: Number
},
/**
* First field placed at array index 0
*/
f0: {
type: Object,
value: null
},
/**
* Second field placed at array index 1
*/
f1: {
type: Object,
value: null
},
/**
* Third field placed at array index 2
*/
f2: {
type: Object,
value: null
},
/**
* Fourth field placed at array index 3
*/
f3: {
type: Object,
value: null
},
/**
* Fifth field placed at array index 4
*/
f4: {
type: Object,
value: null
},
/**
* Sixth field placed at array index 5
*/
f5: {
type: Object,
value: null
},
/**
* Seventh field placed at array index 6
*/
f6: {
type: Object,
value: null
},
/**
* Eighth field placed at array index 7
*/
f7: {
type: Object,
value: null
},
/**
* Ninth field placed at array index 8
*/
f8: {
type: Object,
value: null
},
/**
* Tenth field placed at array index 9
*/
f9: {
type: Object,
value: null
},
/**
* Define the fields which will cause the arrays
* data-binding to be notified on change
*/
updateOn: {
type: String,
value: 'f0,f1,f2,f3,f4,f5,f6,f7,f8,f9'
}
},
observers: [
'_lengthChanged(length)',
'_f0Changed(f0)',
'_f1Changed(f1)',
'_f2Changed(f2)',
'_f3Changed(f3)',
'_f4Changed(f4)',
'_f5Changed(f5)',
'_f6Changed(f6)',
'_f7Changed(f7)',
'_f8Changed(f8)',
'_f9Changed(f9)'
],
_fieldChanged: function(index, value) {
// Update a field
if (index<this.length) {
var arr = new Array(this.length);
for(var i=0; i<this.length; i++) {
arr[i]=this['f'+i];
}
arr[index]=value;
if (this.updateOn && this.updateOn.indexOf('f'+index) > -1)
this._setArray(arr);
}
},
_f0Changed: function() { this._fieldChanged(0, this.f0); },
_f1Changed: function() { this._fieldChanged(1, this.f1); },
_f2Changed: function() { this._fieldChanged(2, this.f2); },
_f3Changed: function() { this._fieldChanged(3, this.f3); },
_f4Changed: function() { this._fieldChanged(4, this.f4); },
_f5Changed: function() { this._fieldChanged(5, this.f5); },
_f6Changed: function() { this._fieldChanged(6, this.f6); },
_f7Changed: function() { this._fieldChanged(7, this.f7); },
_f8Changed: function() { this._fieldChanged(8, this.f8); },
_f9Changed: function() { this._fieldChanged(9, this.f9); },
_lengthChanged: function() {
// Compose fields to array
var arr = new Array(this.length);
for(var i=0; i<this.length; i++) {
arr[i]=this['f'+i];
}
// Always update binding
this._setArray(arr);
}
});
</script>