This repository was archived by the owner on Sep 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathlist-view.html
161 lines (133 loc) · 4.82 KB
/
list-view.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
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
<!--
Copyright 2016 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<link rel="import" href="../polymer/polymer-element.html">
<link rel="import" href="action-emitter.html">
<script type="text/javascript">
var UniFlow = window.UniFlow || {};
/**
This mixin used by elements that need to render multiple models backed
by 'list' array. You may want to use ModelView to render individual
models in the list. The mixin supports element selection by setting predefined
$selected property on list elements.
### Example:
#### HTML:
<ul>
<template id="list-template" is="dom-repeat" items="[[list]]">
<li id="[[item.id]]">
<paper-checkbox checked="{{item.$selected}}">
<model-view state-path="[[statePath]].list.#[[index]]"></model-view>
</li>
</template>
</ul>
Selected: [[selectedCount]] items
<paper-button on-tap="onDeleteTap">Delete</paper-button>
#### JavaScript:
class ListElement extends UniFlow.StateAware(UniFlow.ListView(Polymer.Element)) {
static get is() { return "list-element"; }
onDeleteTap() {
this.deleteSelected();
}
}
customElements.define(ListElement.is, ListElement);
In the example above list view element is also state-aware, meaning it has its own place
in the application state tree. Assuming it has been declared as follows:
<list-element state-path="state.listElement"></list-element>
it will be rendering `state.listElement.list` and observing changes to it. Each `model-view`
within dom-repeat template will have `state-path` property set to
`state.listElement.list.#<index>` where `index` is the element's index in the array.
@polymer
@mixinFunction
@appliesMixin UniFlow.ActionEmitter
*/
UniFlow.ListView = Polymer.dedupingMixin((base) =>
class extends UniFlow.ActionEmitter(base) {
static get properties() {
return {
/**
* Array which data is to be rendered by the element.
*/
list: {
type: Array,
value: () => []
},
/**
* If element supports item selection (using meta-property $selected) then
* selectedCount property will keep track of number of selected items.
*/
selectedCount: {
type: Number,
notify: true,
value: 0,
},
/**
* Action name that will be emitted when deleteSelected method is called
* without parameter.
*/
deleteAction: String
}
}
static get observers() {
return [
'itemChanged_(list.*)',
]
}
/**
* Whenever list is set or mutated (elements added/removed), as well as
* meta-property $selected is modified, updates selectedCount.
* @param {!Object} change
* @private
*/
itemChanged_(change) {
if (this.get('list') &&
(change.path == 'list' ||
change.path == 'list.splices' ||
change.path.endsWith('$selected'))) {
this.updateSelectedCount_();
}
}
/**
* Updates selectedCount property of the element by iterating the list and
* counting each item that has meta-property $selected set.
* @private
*/
updateSelectedCount_() {
let selectedCount = 0;
this.get('list').forEach((elem) => {
selectedCount += elem.$selected ? 1 : 0;
});
this.selectedCount = selectedCount;
}
/**
* Emits deleteAction for each selected element in the list (for which
* meta-property $selected is set).
* @param {string=} deleteAction Action type for the action that will be emitted
* for each selected element. If not specified, `deleteAction` property of the element
* will be used.
*/
deleteSelected(deleteAction) {
deleteAction = deleteAction || this.deleteAction;
if (deleteAction) {
this.list.forEach(item => {
if (item.$selected) {
this.emitAction({
type: deleteAction,
item: item
});
}
});
} else {
console.warn('delete action is not defined');
}
}
});
</script>