07d13c9c
isisadmin
polymer catalog
|
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
|
<link rel="import" href="../catalog-element/catalog-element.html">
<script>
(function() {
var _instances = [];
var _store = function() {
if (_instances.length) {
localStorage['catalog.cart'] = JSON.stringify(_instances[0].items);
}
};
var _retrieve = function() {
try {
return JSON.parse(localStorage['catalog.cart'] || '[]');
} catch (e) {
console.log('error retrieving catalog data from localstorage.', e);
return [];
}
};
var _add = function(name) {
var el = document.createElement('catalog-element');
el.name = name;
if (!el.data) {
return;
}
var check = _instances[0];
var insertAt;
if (check.items.length === 0 || check.items[0].name > name) {
insertAt = 0;
} else {
for (var i = 0; i < check.items.length; i++) {
if (name > check.items[i].name && (!check.items[i + 1] || name < check.items[i+1].name)) {
//console.log(name, ">", check.items[i].name);
insertAt = i + 1;
break;
}
}
}
_instances.forEach(function(instance) {
instance.splice('items',insertAt,0,el.data);
instance.fire('item-added', {name: el.name, element: el.data}, {bubbles: false});
});
_store();
return el.data;
};
var _remove = function(el) {
var check = _instances[0];
var removeAt = -1;
for (var i = 0; i < check.items.length; i++) {
if (check.items[i] === el || check.items[i].name === el) {
removeAt = i;
}
}
if (removeAt >= 0) {
_instances.forEach(function(instance) {
instance.splice('items',removeAt,1);
instance.fire('item-removed', {name: el.name, element: el.data}, {bubbles: false});
});
_store();
} else {
return false;
}
};
Polymer({
is: 'catalog-cart',
properties: {
items: {
type: Array,
notify: true,
value: function() {
return _retrieve();
}
}
},
created: function() {
_instances.push(this);
},
add: function(name) {
if (this.includes(name)) return false;
return _add(name);
},
remove: function(name) {
return _remove(name);
},
includes: function(el) {
for (var i = 0; i < this.items.length; i++) {
if (this.items[i] === el || this.items[i].name === el) return true;
}
return false;
}
});
})();
</script>
|