-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmy-colorrepeat.html
110 lines (91 loc) · 3.59 KB
/
my-colorrepeat.html
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
<!-- Imports polymer -->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-list/iron-list.html">
<!-- The my-colorrepeat component uses a dom-repeat iterator -->
<dom-module id="my-colorrepeat">
<!-- The styling for the elements -->
<style>
:host my-color {
height: 24px;
width: 24px;
--my-background-color: gray;
}
:host my-color[color='red'] {
--my-background-color: red;
}
:host my-color[color='blue'] {
--my-background-color: blue;
}
:host .my-list-button {
display: inline-block;
width: 200px;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
background-color: #999;
cursor: pointer;
padding: 2px 10px;
}
.my-list-button:hover {
background-color: #bbb;
}
#my-color-description {
font-weight: bold;
}
</style>
<!-- Defines element markup -->
<template is="dom-bind">
<div class="my-list-button" on-click="_switchColor">CLICK TO SWITCH COLOR</div>
<div id="my-color-description">All Circles Should Be RED</div>
<template id="my-dom-repeat" is="dom-repeat" items="[[listItems]]" as="item">
<div>
<my-color color$="{{item.color}}" class="my-colortype"></my-color>
<span>{{item.label}}</span>
</div>
</template>
</template>
<!-- Registers the my-colorrepeat custom element -->
<script>
(function () {
Polymer({
is: 'my-colorrepeat',
properties: {
color: {
type: String,
reflectToAttribute: true
}
},
ready: function () {
this.listItems = [];
this.itemSource = redItems;
this._getItems();
},
_getItems: function () {
//////// REMOVE OLD ITEMS FROM THE LIST
while (this.listItems.length) this.pop("listItems");
// this.splice("listItems",0,this.listItems.length); // I've tried both deleting all items by pop and splice.
//////// AT THIS POINT, I WOULD BE FINE WITH TELLING THE LIST TO REINITIALIZE THE CSS
//////// IT WOULD BE EVEN BETTER IF THERE WAS AN "empty()" FUNCTION TO CALL THAT WOULD EMPTY THE LIST AND DESTROY THE CSS
//////// HOWEVER, THE CSS SHOULD BE DELETED AS EACH ITEM IS DESTROYED.
//////// ADD NEW ITEMS TO THE LIST
for (var i = 0; i < this.itemSource.length; i++) {
var item = this.itemSource[i];
this.push("listItems", item);
}
},
// Simply a function to toggle between the red and blue item lists.
_switchColor: function() {
console.log("switch colors");
if (this.itemSource==blueItems) {
this.$["my-color-description"].innerHTML="All Circles Should Be RED";
this.itemSource = redItems;
} else {
this.$["my-color-description"].innerHTML="All Circles Should Be BLUE";
this.itemSource = blueItems;
}
this._getItems();
}
});
})();
</script>
</dom-module>