/
ui.js
174 lines (161 loc) · 4.69 KB
/
ui.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
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
(function() {
var Cookie = {
read: function(name) {
var re = new RegExp('(?:^|\\s|;)' + name + '=([^;]+)');
return decodeURIComponent((document.cookie.match(re) || [])[1] || '');
},
write: function(name, value, maxAge) {
value = value.toString();
document.cookie = [
encodeURIComponent(name) + '=' + encodeURIComponent(value),
'Max-Age: ' + (value === '' ? -1 : (isNaN(maxAge) ? 2592000 : maxAge))
].join('; ');
}
};
function addEvent(el, type, listener) {
if (!el) return;
if (el.addEventListener) {
el.addEventListener(type, listener, false);
}
else if (el.attachEvent) {
el.attachEvent('on' + type, function() {
return listener.call(el, window.event);
});
}
}
function byId(id) {
return document.getElementById(id);
}
function byTag(tag, el) {
if (!el) return [];
return el.getElementsByTagName(tag);
}
function handle(el) {
var callback, node, clone, anchor, target, list, input;
switch (el.nodeName.toLowerCase()) {
case '#text':
// fall through
case 'img':
return handle(el.parentNode);
case 'a':
if (/callback/.test(el.className)) {
callback = byId('icallback');
if (callback) {
callback.value = el.title;
el.blur();
return true;
}
}
if (/new/.test(el.className)) {
for (node = el.parentNode; node; node = node.parentNode) {
if (node.nodeName.toLowerCase() != 'li') continue;
clone = node.cloneNode(true);
anchor = node.nextSibling;
list = node.parentNode;
if (anchor) list.insertBefore(clone, anchor);
else list.appendChild(clone);
input = byTag('input', clone)[0];
input.value = '';
input.focus();
byTag('input', clone)[1].checked = false;
Cufon.CSS.removeClass(list, 'no-del');
return true;
}
}
if (/del/.test(el.className)) {
for (node = el.parentNode; node; node = node.parentNode) {
if (node.nodeName.toLowerCase() != 'li') continue;
list = node.parentNode;
list.removeChild(node);
if (byTag('li', list).length < 2) {
Cufon.CSS.addClass(list, 'no-del');
}
return true;
}
}
if (/more/.test(el.className)) {
target = byId(el.href.replace(/^.*#/, ''));
if (target) {
Cufon.CSS.removeClass(target, 'hidden');
Cufon.CSS.addClass(el, 'hidden');
return true;
}
}
return false;
}
return false;
}
function fetch() {
var list, template, container, node, el, i, l;
list = Cookie.read('cu_dom');
if (list) {
list = decodeURIComponent(list).split(',');
template = byTag('li', byId('security'))[0];
for (i = 0, l = list.length; i < l; ++i) {
el = template.cloneNode(true);
byTag('input', el)[0].value = list[i].replace(/\s/g, '');
byTag('input', el)[1].checked = true;
container = template.parentNode;
container.insertBefore(el, template);
Cufon.CSS.removeClass(container, 'no-del');
}
}
list = Cookie.read('cu_gly');
if (list) {
list = decodeURIComponent(list).split(',');
for (i = 0, l = list.length; i < l; ++i) {
el = byId(list[i]);
if (el) {
el.checked = true;
for (node = el.parentNode; node; node = node.parentNode) {
if (node.nodeName.toLowerCase() == 'dl') {
if (/hidden/.test(node.className)) {
Cufon.CSS.removeClass(node, 'hidden');
handle(byId('more-extra-glyphs'));
}
break;
}
}
}
}
}
el = byId('icustomGlyphs');
if (el) el.value = (Cookie.read('cu_cly') || '');
el = byId('iuseGlyphCSSRange');
if (el) el.checked = (Cookie.read('cu_lyr') == 'yes');
}
function store(e) {
var form = byId('generator'), inputs, input, list;
inputs = form.elements['domains[]']
if (inputs.nodeType) inputs = [ inputs ];
list = [];
for (i = 0; input = inputs[i]; ++i) {
if (input.value === '' || !byTag('input', input.parentNode)[1].checked) continue;
list.push(input.value);
}
Cookie.write('cu_dom', list);
inputs = form.elements['glyphs[]'];
if (inputs.nodeType) inputs = [ inputs ];
list = [];
for (i = 0; input = inputs[i]; ++i) {
if (input.checked) list.push(input.id);
}
Cookie.write('cu_gly', list);
input = byId('icustomGlyphs');
Cookie.write('cu_cly', input ? input.value : '');
input = byId('iuseGlyphCSSRange');
Cookie.write('cu_lyr', (input && input.checked) ? 'yes' : '');
}
addEvent(document, 'click', function(e) {
if (!handle(e.target || e.srcElement)) return;
if (e.preventDefault) e.preventDefault();
else e.cancelBubble = true;
if (e.stopPropagation) e.stopPropagation();
else e.returnValue = false;
});
addEvent(window, 'load', function() {
if (!byId('generator')) return;
addEvent(byId('generator'), 'submit', store);
fetch();
});
})();