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 |
|
|
this.entries.forEach (function (entry) { |
73 |
|
|
ol.appendChild (entry.toLI ()); |
74 |
|
|
}); |
75 |
|
|
return ol; |
76 |
|
|
} // toOL |
77 |
|
|
}); // SearchResult |
78 |
|
|
|
79 |
|
|
SW.SearchResult.Entry = new SAMI.Class (function (v) { |
80 |
wakaba |
1.4 |
if (v.length == 2) { |
81 |
|
|
this.score = 0; |
82 |
|
|
this.docId = v[0]; |
83 |
|
|
this.docName = v[1]; |
84 |
|
|
} else { |
85 |
|
|
this.score = v[0]; |
86 |
|
|
this.docId = v[1]; |
87 |
|
|
this.docName = v[2]; |
88 |
|
|
} |
89 |
wakaba |
1.1 |
}, { |
90 |
|
|
toLI: function () { |
91 |
|
|
var li = document.createElement ('li'); |
92 |
|
|
li.innerHTML = '<a href="">xxx</a>'; |
93 |
|
|
li.firstChild.firstChild.data = this.docName; |
94 |
|
|
li.firstChild.href = SW.CurrentDocument.getInstance ().constructURL |
95 |
|
|
('n', this.docName, this.docId); |
96 |
|
|
return li; |
97 |
|
|
} // toLI |
98 |
wakaba |
1.4 |
}); // SearchResult.Entry |
99 |
|
|
|
100 |
|
|
SW.Neighbors = new SAMI.Class (function (source) { |
101 |
|
|
this.parse (source); |
102 |
|
|
}, { |
103 |
|
|
parse: function (source) { |
104 |
|
|
this.entries = new SAMI.List (source.split (/\x0D?\x0A/)).map (function (v) { |
105 |
|
|
if (v == '') return; |
106 |
|
|
return new SW.SearchResult.Entry (v.split (/\t/, 2)); |
107 |
|
|
}).grep (function (v) { return v }); |
108 |
|
|
}, // parse |
109 |
|
|
|
110 |
|
|
toOL: SW.SearchResult.prototype.toOL |
111 |
|
|
}); // Neighbors |
112 |
wakaba |
1.1 |
|
113 |
wakaba |
1.2 |
SW.PageContents = new SAMI.Class (function () { |
114 |
|
|
this.footer = document.getElementsByTagName ('footer')[0]; |
115 |
|
|
}, { |
116 |
|
|
insertSection: function (sectionId, content) { |
117 |
|
|
var sectionName = { |
118 |
wakaba |
1.4 |
'search-results': 'Related pages', |
119 |
|
|
'neighbors': 'Nearby' |
120 |
wakaba |
1.2 |
}[sectionId] || sectionId; |
121 |
|
|
var section = document.createElement ('section'); |
122 |
|
|
section.id = sectionId; |
123 |
|
|
var h = document.createElement ('h2'); |
124 |
|
|
h.innerHTML = 'xxx'; |
125 |
|
|
h.firstChild.data = sectionName; |
126 |
|
|
section.appendChild (h); |
127 |
|
|
section.appendChild (content); |
128 |
|
|
document.body.insertBefore (section, this.footer); |
129 |
|
|
} // insertSection |
130 |
|
|
}); // PageContents |
131 |
|
|
|
132 |
|
|
SW.PageContents.getInstance = function () { |
133 |
|
|
if (!this._instance) { |
134 |
|
|
this._instance = new SW.PageContents; |
135 |
|
|
} |
136 |
|
|
return this._instance; |
137 |
|
|
}; // getInstance |
138 |
|
|
|
139 |
wakaba |
1.3 |
var CanvasInstructions = new SAMI.Class(function (canvas) { |
140 |
|
|
this.canvas = canvas; |
141 |
|
|
}, { |
142 |
|
|
clear: function () { |
143 |
|
|
this.canvas.width = this.canvas.width; |
144 |
|
|
}, // clear |
145 |
|
|
|
146 |
|
|
processText: function (text) { |
147 |
|
|
text = text.replace (/^<!--/, '').replace (/-->$/, ''); |
148 |
|
|
this.processLines (text.split(/\x0D\x0A?|\x0A/)); |
149 |
|
|
}, // processText |
150 |
|
|
|
151 |
|
|
processLines: function (lines) { |
152 |
|
|
var ctx = this.canvas.getContext ('2d'); |
153 |
|
|
for (var i = 0; i < lines.length; i++) { |
154 |
|
|
var ev = lines[i].split (/,/); |
155 |
|
|
if (ev[0] == 'lineTo') { |
156 |
|
|
var x = parseFloat (ev[1]); |
157 |
|
|
var y = parseFloat (ev[2]); |
158 |
|
|
ctx.lineTo (x, y); |
159 |
|
|
ctx.stroke (); |
160 |
|
|
// ctx.closePath (); |
161 |
|
|
// ctx.beginPath (); |
162 |
|
|
// ctx.moveTo (x, y); |
163 |
|
|
} else if (ev[0] == 'moveTo') { |
164 |
|
|
var x = parseFloat (ev[1]); |
165 |
|
|
var y = parseFloat (ev[2]); |
166 |
|
|
ctx.moveTo (x, y); |
167 |
|
|
} else if (ev[0] == 'strokeStyle' || ev[0] == 'lineWidth') { |
168 |
|
|
ctx.closePath (); |
169 |
|
|
ctx.beginPath (); |
170 |
|
|
ctx[ev[0]] = ev[1]; |
171 |
|
|
} |
172 |
|
|
} |
173 |
|
|
} // processLines |
174 |
|
|
}); // CanvasInstructions |
175 |
|
|
|
176 |
|
|
SW.Drawings = {}; |
177 |
|
|
SAMI.Class.addClassMethods (SW.Drawings, { |
178 |
|
|
drawCanvases: function () { |
179 |
|
|
var canvases = SAMI.Node.getElementsByClassName (document.body, 'swe-canvas-instructions'); |
180 |
|
|
canvases.forEach(function (canvas) { |
181 |
|
|
var script = canvas.firstChild; |
182 |
|
|
if (!script) return; |
183 |
|
|
if (script.nodeName.toLowerCase () != 'script') return; |
184 |
|
|
if (script.type != 'image/x-canvas-instructions+text') return; |
185 |
|
|
var text = SAMI.Element.getTextContent (script); |
186 |
|
|
new CanvasInstructions (canvas).processText (text); |
187 |
|
|
}); |
188 |
|
|
} // drawCanvases |
189 |
|
|
}); // SW.Drawings |
190 |
|
|
|
191 |
wakaba |
1.1 |
SW.init = function () { |
192 |
|
|
var doc = SW.CurrentDocument.getInstance (); |
193 |
|
|
if (doc.area == 'n') { |
194 |
|
|
var searchURL = doc.constructURL (null, null, null, 'search'); |
195 |
|
|
|
196 |
|
|
new SAMI.XHR (searchURL, function () { |
197 |
|
|
var sr = new SW.SearchResult (this.getText ()); |
198 |
|
|
if (sr.entries.list.length) { |
199 |
|
|
var ol = sr.toOL (); |
200 |
wakaba |
1.2 |
SW.PageContents.getInstance ().insertSection ('search-results', ol); |
201 |
wakaba |
1.1 |
} |
202 |
|
|
}).get (); |
203 |
wakaba |
1.4 |
|
204 |
|
|
if (location.href.match(/-temp/)) { // XXX |
205 |
|
|
|
206 |
|
|
var id = SW.CurrentDocument.getDocumentId (); |
207 |
|
|
if (id) { |
208 |
|
|
var neighborsURL = doc.constructURL ('i', id, null, 'neighbors'); |
209 |
|
|
new SAMI.XHR (neighborsURL, function () { |
210 |
|
|
var sr = new SW.Neighbors (this.getText ()); |
211 |
|
|
if (sr.entries.list.length) { |
212 |
|
|
var ol = sr.toOL (); |
213 |
|
|
SW.PageContents.getInstance ().insertSection ('neighbors', ol); |
214 |
|
|
} |
215 |
|
|
}).get (); |
216 |
|
|
} |
217 |
|
|
|
218 |
|
|
} |
219 |
wakaba |
1.3 |
|
220 |
|
|
SW.Drawings.drawCanvases (); |
221 |
wakaba |
1.1 |
} |
222 |
|
|
|
223 |
|
|
}; // init |