-
Notifications
You must be signed in to change notification settings - Fork 0
/
catalog.js
199 lines (189 loc) · 7.72 KB
/
catalog.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
var init = (function() {
var res = Resource.res;
var lang = Resource.lang;
var Meta = function(meta) {
var getProp = function(prop) {
return meta[prop+'['+lang()+']'] || meta[prop];
};
var self = {};
[ 'title', 'type', 'description', 'option', 'documentation'
].forEach(function(prop) {
self[prop] = getProp(prop) || res('NO_'+prop.toUpperCase());
});
return self;
};
var SmartKeywordInstruction = function() {
var self = {};
self.node = res('INSTRUCTION_SMARKLET');
document.body.appendChild(self.node);
new GNN.UI.Draggable(self.node);
new GNN.UI.Observer(
GNN.UI.$('instruction-smarklet-close'),
'onclick',
function(e) { e.stop(); self.deactivate(); });
var hideAll = function() {
GNN.UI.$('instruction-menu').style.visibility = 'hidden';
GNN.UI.$('instruction-dialog').style.visibility = 'hidden';
GNN.UI.$('instruction-keyword').style.visibility = 'hidden';
};
var transitions = [
function() { // show menu
GNN.UI.$('instruction-menu').style.visibility = 'visible';
GNN.UI.$('instruction-dialog').style.visibility = 'hidden';
GNN.UI.$('instruction-keyword').style.visibility = 'hidden';
},
function() { // show dialog
GNN.UI.$('instruction-dialog').style.visibility = 'visible';
},
function() { // swap dialog
GNN.UI.$('instruction-dialog').style.visibility = 'hidden';
GNN.UI.$('instruction-keyword').style.visibility = 'visible';
}
];
var transit = function() {
transitions[self.state]();
self.state = (self.state+1) % transitions.length;
};
self.activate = function(target) {
self.deactivate();
if (target) {
var pos = GNN.UI.getPosition(target);
pos.y += target.offsetHeight + 30;
self.node.style.left = pos.x+'px';
self.node.style.top = pos.y+'px';
}
self.node.style.visibility = 'visible';
var [ m, d ] = [
GNN.UI.$('instruction-menu'), GNN.UI.$('instruction-dialog')
];
self.node.style.width = (m.offsetWidth+d.offsetWidth)+'px';
self.node.style.height = (m.offsetHeight+d.offsetHeight)+'px';
self.state = 0;
self.timer = setInterval(function(){ transit(); }, 1500);
transit();
};
self.deactivate = function() {
clearInterval(self.timer);
self.node.style.visibility = 'hidden';
hideAll();
};
return self;
};
var instruction;
var Description = function(file, meta) {
var desc = res('DESCRIPTION', meta.description);
if (!meta.permalink && meta.documentation) {
var p = { permalink: file };
desc.appendChild(res('DETAIL', GNN.URI.params(p)));
}
return desc;
};
var showdown = function() {
if (typeof Showdown == 'undefined') return null;
if (!Showdown.converter) return null;
return new Showdown.converter();
};
var documentation = function(div, uri) {
var iframeDoc = function() {
div.appendChild(res('DOCUMENTATION_FRAME', uri));
};
var dom = new GNN.URI(uri).domain;
var sd = showdown();
if ((!dom || dom == '.' || dom == GNN.URI.location().domain) &&
/\.md$/.test(uri) && sd) {
// FIXME?: support other browsers
var req = new XMLHttpRequest();
req.open('GET', uri);
req.onreadystatechange = function(e) {
if (req.readyState == 4 && req.status == 200) {
var text = sd.makeHtml(req.responseText);
div.appendChild(res('DOCUMENTATION_NODE', text));
}
};
req.send(null);
} else {
iframeDoc();
}
};
var Catalog = function(catalog, file, permalink) {
var [ re1, re2 ] = [ new RegExp("\\\\",'g'), new RegExp("'",'g') ];
var e = function(str) {
return str.replace(re1, '\\\\').replace(re2, "\\'");
};
var meta = new Meta(catalog.list[file]);
meta.permalink = permalink;
var actionText = "javascript:[ \
'" + e(meta.title) + "','" + e(file) + "','" + e(catalog.base) +
"'," + !!catalog.nocache + ", \
function(d,e,c,b,s,n){ \
[e,c,b,s,n]=this; \
e=d.createElement('script'); \
e.onload=function(){GNN.Let={name:n,arg:'";
actionText = actionText.replace(/\s+/g,'');
var hiddenText = "'},GNN.lib(b,c),GNN.use(s)}; \
e.src=b+'lib.js'; \
e.charset='utf-8'; \
d.body.appendChild(e); \
}].reverse()[0](document)";
hiddenText = hiddenText.replace(/\s+/g,'');
return {
meta: meta,
toNode: function() {
var id = (meta.type=='smarklet'?'SMARK':'BOOKMARK');
id = 'LINK_' + id + 'LET';
var form = res(id, file, actionText, hiddenText);
if (meta.type == 'smarklet') {
var input = form.input;
var help = form.help;
new GNN.UI.Observer(help, 'onclick', function(e) {
e.stop();
instruction.activate(input);
});
new GNN.UI.Observer(input, 'onfocus', function(e) {
e.stop();
e.event.target.blur();
});
form = form.form;
}
var desc = new Description(file, meta);
var entry = res('ENTRY', file, meta, desc, form);
if (permalink && meta.documentation) {
documentation(entry, meta.documentation);
}
return entry;
}
};
};
var addDebugButton = function(div, d) {
div.appendChild(res('SELECTOR_DEBUG', d));
}
var addCatalog = function(div, catalog, file, permalink) {
var div2 = new Catalog(catalog, file, permalink).toNode();
if (div2) div.appendChild(div2);
}
return function(div, header) {
instruction = new SmartKeywordInstruction();
div = GNN.UI.$(div);
header = GNN.UI.$(header);
header.appendChild(res('SELECTOR_LANG'));
var uri = GNN.URI.location();
new GNN.JSONP(new GNN.URI('./catalog.json'), function(catalog) {
if (catalog.debug) {
addDebugButton(div, uri.params.debug);
if (uri.params.debug) {
catalog.base = catalog.debug.base;
catalog.nocache = catalog.debug.nocache;
}
}
for (var file in catalog.list) {
if (!uri.params.permalink ||
uri.params.permalink == GNN.URI.encode(file)) {
addCatalog(div, catalog, file, uri.params.permalink);
}
}
if (uri.params.permalink) {
div.appendChild(res('BACK_TO_LIST'));
}
});
}
})();