Repository navigation
Expand file tree
/
Copy pathcomponent.html
More file actions
153 lines (149 loc) · 4.65 KB
/
Copy pathcomponent.html
File metadata and controls
153 lines (149 loc) · 4.65 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
<polymer-element name="ceci-dataarray" attributes="url" extends="ceci-element">
<ceci-definition>
{
"tags": ["data", "array"],
"name": "Data Array",
"description": "Pulls data from collections and provides an indexable list.",
"broadcasts": {
"currentValue": {
"label": "Value",
"description": "Current value changed."
}
},
"listeners": {
"next": {
"label": "Next Item",
"description": "Moves to the next item in the data."
},
"previous": {
"label": "Previous Item",
"description": "Moves to the previous item in the data."
}
},
"attributes": {
"index": {
"label": "Index",
"description": "Index of the array.",
"editable": "number",
"listener": true
},
"collection": {
"label": "Collection",
"description": "Collection to use.",
"editable": "select",
"options": [],
"listener": true
},
"field": {
"label": "Field",
"description": "Field of the collection to use.",
"editable": "select",
"options": [],
"listener": true
}
}
}
</ceci-definition>
<template>
<style>
:host {
display: block;
min-height: 50px;
}
</style>
<div id="container">{{_value}}</div>
<shadow></shadow>
<content></content>
</template>
<script>
function waitForCeciCollections (callback) {
var ceciCollections = document.querySelector('ceci-collections');
if (ceciCollections.getCollections) {
callback(ceciCollections);
}
else {
if (ceciCollections.async) {
ceciCollections.async(function (){
callback(ceciCollections);
});
}
else {
setTimeout(function () {
waitForCeciCollections(callback);
}, 10);
}
}
}
Polymer('ceci-dataarray', {
index: 0,
_value: '',
_data: [],
ready: function () {
this.super();
var ceciCollections = document.querySelector('ceci-collections');
ceciCollections.addEventListener('collectionchange', this.updateCollections.bind(this));
this.updateCollections(function () {
if (ceciCollections.getCollections().length) {
this.collection = this.collection || ceciCollections.getCollections()[0].name;
this.collectionChanged();
if (ceciCollections.getCollection(this.collection).getFields().length) {
this.field = this.field || ceciCollections.getCollection(this.collection).getFields()[0].name;
}
}
}.bind(this));
},
indexChanged: function () {
this._updateValue();
},
_updateValue: function () {
this._value = this._data[+this.index][this.field];
this.broadcast('currentValue', this._value);
},
fieldChanged: function () {
var that = this;
waitForCeciCollections(function (ceciCollections) {
if (that.collection &&
that.field &&
ceciCollections.getCollection(that.collection) &&
ceciCollections.getCollection(that.collection).getField(that.field)) {
var collection = ceciCollections.getCollection(that.collection);
that._data = collection.data;
that._updateValue();
}
});
},
collectionChanged: function () {
var that = this;
waitForCeciCollections(function (ceciCollections) {
var collection = ceciCollections.getCollection(that.collection);
var fields = collection.getFields();
that.ceci.attributes.field.options = [];
fields.forEach(function (field) {
that.ceci.attributes.field.options.push(field.name);
});
});
},
updateCollections: function (callback) {
var that = this;
waitForCeciCollections(function (ceciCollections) {
var collections = ceciCollections.getCollections();
that.ceci.attributes.collection.options = [];
collections.forEach(function (collection) {
that.ceci.attributes.collection.options.push(collection.name);
});
callback();
});
},
next: function () {
this.index = (+this.index + 1) % this._data.length;
},
previous: function () {
var i = (+this.index - 1) % this._data.length;
if (i < 0) {
i = this._data.length - 1;
}
this.index = i;
}
});
</script>
</polymer-element>