1 |
wakaba |
1.1 |
if (!self.SW) self.SW = {}; |
2 |
|
|
|
3 |
|
|
SW.CurrentDocument = new SAMI.Class (function () { |
4 |
|
|
var self = this; |
5 |
|
|
var path = location.pathname; |
6 |
|
|
path = path.replace (/;([^;\/]*)$/, function (_, v) { |
7 |
|
|
self.param = decodeURIComponent (v.replace (/\+/g, '%2F')); |
8 |
|
|
return ''; |
9 |
|
|
}); |
10 |
|
|
path = path.replace (/\$([^$\/]*)$/, function (_, v) { |
11 |
|
|
self.dollar = decodeURIComponent (v.replace (/\+/g, '%2F')); |
12 |
|
|
return ''; |
13 |
|
|
}); |
14 |
|
|
path = path.replace (/\/([^\/]*)$/, function (_, v) { |
15 |
|
|
self.name = decodeURIComponent (v.replace (/\+/g, '%2F')); |
16 |
|
|
return ''; |
17 |
|
|
}); |
18 |
|
|
path = path.replace (/\/([^\/]*)$/, function (_, v) { |
19 |
|
|
self.area = decodeURIComponent (v.replace (/\+/g, '%2F')); |
20 |
|
|
return ''; |
21 |
|
|
}); |
22 |
|
|
this.wikiPath = path; |
23 |
|
|
}, { |
24 |
|
|
constructURL: function (area, name, dollar, param) { |
25 |
|
|
var p = this.wikiPath; |
26 |
|
|
|
27 |
|
|
area = area || this.area; |
28 |
|
|
p += '/' + encodeURIComponent (area).replace (/%2F/g, '+'); |
29 |
|
|
|
30 |
|
|
name = name || this.name; |
31 |
|
|
p += '/' + encodeURIComponent (name).replace (/%2F/g, '+'); |
32 |
|
|
|
33 |
|
|
dollar = dollar === undefined ? this.dollar : dollar; |
34 |
|
|
if (dollar != null) p += '$' + encodeURIComponent (dollar).replace (/%2F/g, '+'); |
35 |
|
|
|
36 |
|
|
param = param === undefined ? this.param : param; |
37 |
|
|
if (param != null) p += ';' + encodeURIComponent (param).replace (/%2F/g, '+'); |
38 |
|
|
|
39 |
|
|
return p; |
40 |
|
|
} // constructURL |
41 |
|
|
}); // CurrentDocument |
42 |
|
|
|
43 |
wakaba |
1.4 |
SW.CurrentDocument.getDocumentId = function () { |
44 |
|
|
var el = document.body; |
45 |
|
|
if (!el) return null; |
46 |
|
|
|
47 |
|
|
var value = el.getAttribute ('data-doc-id'); |
48 |
|
|
if (!value) return null; |
49 |
|
|
|
50 |
|
|
return parseInt (value); |
51 |
|
|
}; // getDocumentId |
52 |
|
|
|
53 |
wakaba |
1.1 |
SW.CurrentDocument.getInstance = function () { |
54 |
|
|
if (!SW.CurrentDocument._instance) { |
55 |
|
|
SW.CurrentDocument._instance = new SW.CurrentDocument; |
56 |
|
|
} |
57 |
|
|
return SW.CurrentDocument._instance; |
58 |
|
|
}; // getInstance |
59 |
|
|
|
60 |
|
|
SW.SearchResult = new SAMI.Class (function (source) { |
61 |
|
|
this.parse (source); |
62 |
|
|
}, { |
63 |
|
|
parse: function (source) { |
64 |
|
|
this.entries = new SAMI.List (source.split (/\x0D?\x0A/)).map (function (v) { |
65 |
|
|
if (v == '') return; |
66 |
|
|
return new SW.SearchResult.Entry (v.split (/\t/, 3)); |
67 |
|
|
}).grep (function (v) { return v }); |
68 |
|
|
}, // parse |
69 |
|
|
|
70 |
|
|
toOL: function () { |
71 |
|
|
var ol = document.createElement ('ol'); |
72 |
wakaba |
1.6 |
ol.className = 'swe-links'; |
73 |
wakaba |
1.1 |
this.entries.forEach (function (entry) { |
74 |
|
|
ol.appendChild (entry.toLI ()); |
75 |
|
|
}); |
76 |
|
|
return ol; |
77 |
|
|
} // toOL |
78 |
|
|
}); // SearchResult |
79 |
|
|
|
80 |
|
|
SW.SearchResult.Entry = new SAMI.Class (function (v) { |
81 |
wakaba |
1.5 |
this.score = v[0]; |
82 |
|
|
this.docId = v[1]; |
83 |
|
|
this.docName = v[2]; |
84 |
wakaba |
1.1 |
}, { |
85 |
|
|
toLI: function () { |
86 |
|
|
var li = document.createElement ('li'); |
87 |
|
|
li.innerHTML = '<a href="">xxx</a>'; |
88 |
|
|
li.firstChild.firstChild.data = this.docName; |
89 |
|
|
li.firstChild.href = SW.CurrentDocument.getInstance ().constructURL |
90 |
|
|
('n', this.docName, this.docId); |
91 |
|
|
return li; |
92 |
|
|
} // toLI |
93 |
wakaba |
1.4 |
}); // SearchResult.Entry |
94 |
|
|
|
95 |
wakaba |
1.5 |
SW.Neighbors = new SAMI.Class (function (id, source) { |
96 |
|
|
this.documentId = id; |
97 |
wakaba |
1.4 |
this.parse (source); |
98 |
|
|
}, { |
99 |
|
|
parse: function (source) { |
100 |
wakaba |
1.5 |
var id = this.documentId; |
101 |
wakaba |
1.4 |
this.entries = new SAMI.List (source.split (/\x0D?\x0A/)).map (function (v) { |
102 |
|
|
if (v == '') return; |
103 |
wakaba |
1.5 |
return new SW.Neighbors.Entry (v.split (/\t/, 2), id); |
104 |
wakaba |
1.4 |
}).grep (function (v) { return v }); |
105 |
|
|
}, // parse |
106 |
|
|
|
107 |
wakaba |
1.5 |
toUL: function () { |
108 |
|
|
var ol = document.createElement ('ul'); |
109 |
wakaba |
1.6 |
ol.className = 'swe-links'; |
110 |
wakaba |
1.5 |
this.entries.forEach (function (entry) { |
111 |
|
|
ol.appendChild (entry.toLI ()); |
112 |
|
|
}); |
113 |
|
|
return ol; |
114 |
|
|
} // toUL |
115 |
wakaba |
1.4 |
}); // Neighbors |
116 |
wakaba |
1.1 |
|
117 |
wakaba |
1.5 |
SW.Neighbors.Entry = new SAMI.Class (function (v, id) { |
118 |
|
|
this.docId = v[0]; |
119 |
|
|
this.docName = v[1]; |
120 |
|
|
this.sourceDocId = id; |
121 |
|
|
}, { |
122 |
|
|
toLI: function () { |
123 |
|
|
var self = this; |
124 |
|
|
var doc = SW.CurrentDocument.getInstance (); |
125 |
|
|
|
126 |
|
|
var li = document.createElement ('li'); |
127 |
wakaba |
1.6 |
li.innerHTML = '<a href="">xxx</a> <button type=button class=swe-unrelated>X</button>'; |
128 |
wakaba |
1.5 |
var a = li.firstChild; |
129 |
|
|
a.firstChild.data = this.docName; |
130 |
|
|
a.href = doc.constructURL ('n', this.docName, this.docId); |
131 |
|
|
|
132 |
|
|
// XXX We don't use the real |ping| attribute for now since there |
133 |
|
|
// is no reliable way to know whether the browser does or does not |
134 |
|
|
// send ping and therefore we have to send the ping using a custom |
135 |
|
|
// script code anyway. |
136 |
|
|
|
137 |
|
|
// Use |GET| method instead of |POST| method to not require Basic auth. |
138 |
|
|
a.onclick = function () { |
139 |
|
|
var pingURL = doc.constructURL |
140 |
|
|
('i', self.sourceDocId, null, 'related-' + self.docId); |
141 |
|
|
new SAMI.XHR (pingURL).get (); |
142 |
|
|
|
143 |
|
|
var url = a.href; |
144 |
|
|
setTimeout (function () { |
145 |
|
|
location.href = url; |
146 |
|
|
}, 500); |
147 |
|
|
|
148 |
|
|
return false; |
149 |
|
|
}; // a.onclick |
150 |
|
|
|
151 |
|
|
var button = li.lastChild; |
152 |
|
|
button.onclick = function () { |
153 |
|
|
var pingURL = doc.constructURL |
154 |
|
|
('i', self.sourceDocId, null, 'unrelated-' + self.docId); |
155 |
|
|
new SAMI.XHR (pingURL).get (); |
156 |
|
|
li.parentNode.removeChild (li); |
157 |
|
|
}; // button.onclick |
158 |
|
|
|
159 |
|
|
return li; |
160 |
|
|
} // toLI |
161 |
|
|
}); // Neighbors.Entry |
162 |
|
|
|
163 |
wakaba |
1.2 |
SW.PageContents = new SAMI.Class (function () { |
164 |
|
|
this.footer = document.getElementsByTagName ('footer')[0]; |
165 |
|
|
}, { |
166 |
|
|
insertSection: function (sectionId, content) { |
167 |
|
|
var sectionName = { |
168 |
wakaba |
1.4 |
'search-results': 'Related pages', |
169 |
|
|
'neighbors': 'Nearby' |
170 |
wakaba |
1.2 |
}[sectionId] || sectionId; |
171 |
|
|
var section = document.createElement ('section'); |
172 |
|
|
section.id = sectionId; |
173 |
|
|
var h = document.createElement ('h2'); |
174 |
|
|
h.innerHTML = 'xxx'; |
175 |
|
|
h.firstChild.data = sectionName; |
176 |
|
|
section.appendChild (h); |
177 |
|
|
section.appendChild (content); |
178 |
|
|
document.body.insertBefore (section, this.footer); |
179 |
|
|
} // insertSection |
180 |
|
|
}); // PageContents |
181 |
|
|
|
182 |
|
|
SW.PageContents.getInstance = function () { |
183 |
|
|
if (!this._instance) { |
184 |
|
|
this._instance = new SW.PageContents; |
185 |
|
|
} |
186 |
|
|
return this._instance; |
187 |
|
|
}; // getInstance |
188 |
|
|
|
189 |
wakaba |
1.3 |
var CanvasInstructions = new SAMI.Class(function (canvas) { |
190 |
|
|
this.canvas = canvas; |
191 |
|
|
}, { |
192 |
|
|
clear: function () { |
193 |
|
|
this.canvas.width = this.canvas.width; |
194 |
|
|
}, // clear |
195 |
|
|
|
196 |
|
|
processText: function (text) { |
197 |
|
|
text = text.replace (/^<!--/, '').replace (/-->$/, ''); |
198 |
|
|
this.processLines (text.split(/\x0D\x0A?|\x0A/)); |
199 |
|
|
}, // processText |
200 |
|
|
|
201 |
|
|
processLines: function (lines) { |
202 |
|
|
var ctx = this.canvas.getContext ('2d'); |
203 |
|
|
for (var i = 0; i < lines.length; i++) { |
204 |
|
|
var ev = lines[i].split (/,/); |
205 |
|
|
if (ev[0] == 'lineTo') { |
206 |
|
|
var x = parseFloat (ev[1]); |
207 |
|
|
var y = parseFloat (ev[2]); |
208 |
|
|
ctx.lineTo (x, y); |
209 |
|
|
ctx.stroke (); |
210 |
|
|
// ctx.closePath (); |
211 |
|
|
// ctx.beginPath (); |
212 |
|
|
// ctx.moveTo (x, y); |
213 |
|
|
} else if (ev[0] == 'moveTo') { |
214 |
|
|
var x = parseFloat (ev[1]); |
215 |
|
|
var y = parseFloat (ev[2]); |
216 |
|
|
ctx.moveTo (x, y); |
217 |
|
|
} else if (ev[0] == 'strokeStyle' || ev[0] == 'lineWidth') { |
218 |
|
|
ctx.closePath (); |
219 |
|
|
ctx.beginPath (); |
220 |
|
|
ctx[ev[0]] = ev[1]; |
221 |
|
|
} |
222 |
|
|
} |
223 |
|
|
} // processLines |
224 |
|
|
}); // CanvasInstructions |
225 |
|
|
|
226 |
|
|
SW.Drawings = {}; |
227 |
|
|
SAMI.Class.addClassMethods (SW.Drawings, { |
228 |
|
|
drawCanvases: function () { |
229 |
|
|
var canvases = SAMI.Node.getElementsByClassName (document.body, 'swe-canvas-instructions'); |
230 |
|
|
canvases.forEach(function (canvas) { |
231 |
|
|
var script = canvas.firstChild; |
232 |
|
|
if (!script) return; |
233 |
|
|
if (script.nodeName.toLowerCase () != 'script') return; |
234 |
|
|
if (script.type != 'image/x-canvas-instructions+text') return; |
235 |
|
|
var text = SAMI.Element.getTextContent (script); |
236 |
|
|
new CanvasInstructions (canvas).processText (text); |
237 |
|
|
}); |
238 |
|
|
} // drawCanvases |
239 |
|
|
}); // SW.Drawings |
240 |
|
|
|
241 |
wakaba |
1.1 |
SW.init = function () { |
242 |
|
|
var doc = SW.CurrentDocument.getInstance (); |
243 |
|
|
if (doc.area == 'n') { |
244 |
|
|
var searchURL = doc.constructURL (null, null, null, 'search'); |
245 |
|
|
|
246 |
|
|
new SAMI.XHR (searchURL, function () { |
247 |
|
|
var sr = new SW.SearchResult (this.getText ()); |
248 |
|
|
if (sr.entries.list.length) { |
249 |
|
|
var ol = sr.toOL (); |
250 |
wakaba |
1.2 |
SW.PageContents.getInstance ().insertSection ('search-results', ol); |
251 |
wakaba |
1.1 |
} |
252 |
|
|
}).get (); |
253 |
wakaba |
1.4 |
|
254 |
|
|
if (location.href.match(/-temp/)) { // XXX |
255 |
|
|
|
256 |
|
|
var id = SW.CurrentDocument.getDocumentId (); |
257 |
|
|
if (id) { |
258 |
|
|
var neighborsURL = doc.constructURL ('i', id, null, 'neighbors'); |
259 |
|
|
new SAMI.XHR (neighborsURL, function () { |
260 |
wakaba |
1.5 |
var sr = new SW.Neighbors (id, this.getText ()); |
261 |
wakaba |
1.4 |
if (sr.entries.list.length) { |
262 |
wakaba |
1.5 |
var ol = sr.toUL (); |
263 |
wakaba |
1.4 |
SW.PageContents.getInstance ().insertSection ('neighbors', ol); |
264 |
|
|
} |
265 |
|
|
}).get (); |
266 |
|
|
} |
267 |
|
|
|
268 |
|
|
} |
269 |
wakaba |
1.3 |
|
270 |
|
|
SW.Drawings.drawCanvases (); |
271 |
wakaba |
1.1 |
} |
272 |
|
|
|
273 |
|
|
}; // init |