-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpim-demo-element.html
198 lines (181 loc) · 5.58 KB
/
pim-demo-element.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
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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="x-breadcrumb.html">
<link rel="import" href="x-crumb.html">
<dom-module id="pim-demo-element">
<!--
This demo is explained in https://github.com/Juicy/x-breadcrumb/issues/6
-->
<template>
<x-breadcrumb orientation='vertical'>
<dom-repeat>
<template is="dom-repeat" items="{{path}}" id="tmpl">
<x-crumb
ghost="false"
value="{{item.Label}}"
search-query="{{item.SearchQuery}}"
item="{{item}}"
dropdown="true"
active$="{{item.IsActive}}"
ghost$="{{item.IsGhost}}"
listid="tmpl-[[index]]">
<ul>
<dom-repeat>
<template is="dom-repeat" items="{{searchFilter(item.Siblings, item.SearchQuery)}}">
<li on-click="siblingClicked" sibling="{{item}}">{{item.Name}}</li>
</template>
</dom-repeat>
<li on-click="addClicked" crumb="{{item}}">+ Add a new type</li>
</ul>
</x-crumb>
</template>
</dom-repeat>
</x-breadcrumb>
</template>
<script>
(function() {
'use strict';
var db = {
Name: "Products",
Children: [{
Name: "Food",
Children: [{
Name: "Dairy",
Children: [{
Name: "Milk",
Children: [{
Name: "Coffee milk 5 ML",
Children: []
}, {
Name: "Milk 1 L",
Children: []
}]
}]
}, {
Name: "Meat",
Children: []
}]
}, {
Name: "Metal",
Children: [{
Name: "Screws",
Children: [{
Name: "Phillips flat head",
Children: []
}]
}]
}]
};
function addParentReference(parent) {
for (var i = 0; i < parent.Children.length; i++) {
parent.Children[i].Parent = parent;
if (parent.Children[i].Children) {
addParentReference(parent.Children[i]);
}
}
}
addParentReference(db);
function findProductByName(name, parent) {
if (!parent) {
parent = db;
}
var found;
for (var i = 0; i < parent.Children.length; i++) {
if (parent.Children[i].Name == name) {
found = parent.Children[i];
} else if (parent.Children[i].Children) {
found = findProductByName(name, parent.Children[i]);
}
if (found) {
return found;
}
}
}
Polymer({
is: 'pim-demo-element',
properties: {
path: {
type: Array,
value: []
}
},
ready: function() {
var product = findProductByName("Food");
//var product = findProductByName("Milk");
this.rebuildBreadcrumb(product);
},
searchFilter: function(siblings, query) {
var out = [];
var re = new RegExp("^" + query, "i");
for (var i = siblings.length - 1; i >= 0; i--) {
if (re.test(siblings[i].Name) == true) {
out.push(siblings[i]);
}
}
console.log("search result for", query, out);
return out;
},
searchChanged: function(event, query) {
var crumb = event.target.item;
crumb.SearchItem = query;
var ttt = this.$.tmpl.querySelectorAll('.ttt');
console.log("ttt", tttd);
//.render()
},
siblingClicked: function(event) {
var sibling = event.target.sibling;
var product = findProductByName(sibling.Name);
this.rebuildBreadcrumb(product);
},
addClicked: function(event) {
var crumb = event.target.crumb;
this.rebuildBreadcrumb(crumb.Product.Parent, true);
},
rebuildBreadcrumb: function(product, isAdd) {
//Since Prodcuts is a graph, and a breadcrumb is a flat structure,
//I need an array to show a path to a product (e.g. "Milk"):
// Food -- inactive, not ghost
// Diary -- inactive, not ghost
// Milk -- active, not ghost
// 2 subtypes of milk - inactive, ghost
this.splice('path', 0, this.path.length);
if (isAdd) {
this.unshift('path', {
Product: {
Parent: product
},
Label: "Add a new type",
SearchQuery: "",
Siblings: product.Children,
IsGhost: false,
IsActive: true
});
} else {
this.unshift('path', {
Product: {
Parent: product
},
Label: product.Children.length + " sub types",
SearchQuery: "",
Siblings: product.Children,
IsGhost: true,
IsActive: false
});
}
var current = product;
while (current.Name !== "Products") {
this.unshift('path', {
Product: current,
Label: current.Name,
SearchQuery: "",
Siblings: current.Parent.Children,
IsGhost: false,
IsActive: (current == product && !isAdd),
});
current = current.Parent;
}
console.log("Breadcrumb path to", product.Name, this.path);
}
});
})();
</script>
</dom-module>