-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathlocalstarage.js
86 lines (78 loc) · 2.38 KB
/
localstarage.js
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
var localStarage = function() {
var LSPREFIX = 'localstarage-';
var STARCLASS = 'localstarage';
var STARREDCLASS = 'starred';
var CLONESUFFIX = '-clone';
var EMPTYHTML = '(None yet)';
var supportsStorage = function () {
try {
return !!localStorage.getItem;
} catch (e) {
return false;
}
}();
var addClone = function(item, starredList, onClone) {
if (starredList.innerHTML == EMPTYHTML) {
starredList.innerHTML = '';
}
var starredNode = item.cloneNode(true);
starredNode.id += CLONESUFFIX;
starredList.appendChild(starredNode);
// Events don't copy :(
starredNode.querySelectorAll('.' + STARCLASS)[0].onclick = item.querySelectorAll('.' + STARCLASS)[0].onclick;
onClone.call(null, starredNode);
}
var removeClone = function(item) {
var starredNode = document.getElementById(item.id + CLONESUFFIX);
starredNode.parentNode.removeChild(starredNode);
}
var checkEmpty = function(starredList) {
if (starredList.querySelectorAll('.' + STARREDCLASS).length == 0) {
starredList.innerHTML = EMPTYHTML;
}
}
var addStar = function(item, options) {
var lsKey = LSPREFIX + item.id;
var star = document.createElement('span');
star.className = STARCLASS;
star.innerHTML = '★';
star.onclick = function(event) {
event.preventDefault();
if (!star.classList.contains(STARREDCLASS)) {
star.classList.add(STARREDCLASS);
localStorage.setItem(lsKey, 'starred');
if (options.starredList) {
addClone(item, options.starredList, options.onClone);
}
} else {
star.className = STARCLASS;
localStorage.removeItem(lsKey);
if (options.starredList) {
removeClone(item);
checkEmpty(options.starredList);
}
}
};
item.insertBefore(star, item.firstChild);
if (localStorage.getItem(lsKey)) {
star.classList.add(STARREDCLASS);
if (options.starredList) {
addClone(item, options.starredList, options.onClone);
}
}
}
return {
init: function(list, options) {
if (!supportsStorage) return false;
var items = list.childNodes;
for (var i = 0; i < items.length; i++) {
if (items[i].nodeType == 1) {
addStar(items[i], options);
}
}
if (options.starredList) {
checkEmpty(options.starredList);
}
}
}
}();