catalog-data.html
2.69 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
<link rel="import" href="../../bower_components/iron-ajax/iron-ajax.html">
<dom-module id="catalog-data">
<template>
<iron-ajax id="req" url="/catalog.json" method="get" handle-as="json" on-response="_handleResponse"></iron-ajax>
</template>
</dom-module>
<script>
(function() {
var _data = {};
var _els = [];
var _generateMap = function(list) {
var out = {};
for (var i = 0; i < list.length; i++) {
out[list[i].name] = list[i];
}
return out;
};
var _setPackageData = function(data) {
_data = data || {};
if (data) {
_data.packageMap = _generateMap(data.packages);
_data.elementMap = _generateMap(data.elements);
_data.guideMap = _generateMap(data.guides);
_data.behaviorMap = {};
_data.elements.forEach(function(el) {
el.behaviors.forEach(function(be) {
_data.behaviorMap[be] = el.name;
});
});
}
_els.forEach(function(el){ el.load(_data); });
};
Polymer({
is: 'catalog-data',
ready: function() {
this.load(_data);
},
attached: function() {
if (_els.length === 0 && !_data.packages) { this.$.req.generateRequest(); }
_els.push(this);
},
detached: function() {
_els.splice(_els.indexOf(this), 1);
},
properties: {
packages: {
type: Array,
readOnly: true,
notify: true
},
packageMap: {
type: Object,
readOnly: true,
notify: true
},
elements: {
type: Array,
readOnly: true,
notify: true
},
elementMap: {
type: Object,
readOnly: true,
notify: true
},
guides: {
type: Array,
readOnly: true,
notify: true
},
guideMap: {
type: Object,
readOnly: true,
notify: true
},
tags: {
type: Object,
readOnly: true,
notify: true
},
behaviorMap: {
type: Object,
readOnly: true,
notify: true
}
},
load: function(data) {
if (data.packages) {
this._setPackages(data.packages);
this._setPackageMap(data.packageMap);
this._setElements(data.elements);
this._setElementMap(data.elementMap);
this._setGuides(data.guides);
this._setGuideMap(data.guideMap);
this._setBehaviorMap(data.behaviorMap);
this._setTags(data.tags);
}
},
_handleResponse: function(_, req) {
_setPackageData(req.response);
}
});
})();
</script>