/[suikacvs]/webroot/swe/scripts/sw-sami.js
Suika

Contents of /webroot/swe/scripts/sw-sami.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.9 - (hide annotations) (download) (as text)
Wed Sep 23 14:43:15 2009 UTC (15 years, 2 months ago) by wakaba
Branch: MAIN
CVS Tags: HEAD
Changes since 1.8: +8 -0 lines
File MIME type: application/javascript
adjust algorithm parameters

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 wakaba 1.8 var self = this;
87    
88 wakaba 1.1 var li = document.createElement ('li');
89     li.innerHTML = '<a href="">xxx</a>';
90 wakaba 1.8
91     var a = li.firstChild;
92     a.firstChild.data = this.docName;
93     a.href = SW.CurrentDocument.getInstance ().constructURL
94 wakaba 1.1 ('n', this.docName, this.docId);
95 wakaba 1.8
96     // XXX We don't use the real |ping| attribute for now since there
97     // is no reliable way to know whether the browser does or does not
98     // send ping and therefore we have to send the ping using a custom
99     // script code anyway.
100    
101     // Use |GET| method instead of |POST| method to not require Basic auth.
102     a.onclick = function () {
103     var pingURL = SW.CurrentDocument.getInstance ().constructURL
104     ('i', SW.CurrentDocument.getDocumentId (), null,
105     'related-' + self.docId);
106     new SAMI.XHR (pingURL, function () {
107     location.href = a.href;
108     }, function () {
109     location.href = a.href;
110     }).get ();
111    
112 wakaba 1.9 setTimeout(function () {
113     location.href = a.href;
114     }, 1000);
115    
116 wakaba 1.8 return false;
117     }; // a.onclick
118    
119 wakaba 1.1 return li;
120     } // toLI
121 wakaba 1.4 }); // SearchResult.Entry
122    
123 wakaba 1.5 SW.Neighbors = new SAMI.Class (function (id, source) {
124     this.documentId = id;
125 wakaba 1.4 this.parse (source);
126     }, {
127     parse: function (source) {
128 wakaba 1.5 var id = this.documentId;
129 wakaba 1.4 this.entries = new SAMI.List (source.split (/\x0D?\x0A/)).map (function (v) {
130     if (v == '') return;
131 wakaba 1.5 return new SW.Neighbors.Entry (v.split (/\t/, 2), id);
132 wakaba 1.4 }).grep (function (v) { return v });
133     }, // parse
134    
135 wakaba 1.5 toUL: function () {
136     var ol = document.createElement ('ul');
137 wakaba 1.6 ol.className = 'swe-links';
138 wakaba 1.5 this.entries.forEach (function (entry) {
139     ol.appendChild (entry.toLI ());
140     });
141     return ol;
142     } // toUL
143 wakaba 1.4 }); // Neighbors
144 wakaba 1.1
145 wakaba 1.5 SW.Neighbors.Entry = new SAMI.Class (function (v, id) {
146     this.docId = v[0];
147     this.docName = v[1];
148     this.sourceDocId = id;
149     }, {
150     toLI: function () {
151     var self = this;
152     var doc = SW.CurrentDocument.getInstance ();
153    
154     var li = document.createElement ('li');
155 wakaba 1.6 li.innerHTML = '<a href="">xxx</a> <button type=button class=swe-unrelated>X</button>';
156 wakaba 1.5 var a = li.firstChild;
157     a.firstChild.data = this.docName;
158     a.href = doc.constructURL ('n', this.docName, this.docId);
159    
160     // XXX We don't use the real |ping| attribute for now since there
161     // is no reliable way to know whether the browser does or does not
162     // send ping and therefore we have to send the ping using a custom
163     // script code anyway.
164    
165     // Use |GET| method instead of |POST| method to not require Basic auth.
166     a.onclick = function () {
167     var pingURL = doc.constructURL
168     ('i', self.sourceDocId, null, 'related-' + self.docId);
169 wakaba 1.8 new SAMI.XHR (pingURL, function () {
170     location.href = a.href;
171     }, function () {
172     location.href = a.href;
173     }).get ();
174 wakaba 1.9
175     setTimeout(function () {
176     location.href = a.href;
177     }, 1000);
178 wakaba 1.5
179     return false;
180     }; // a.onclick
181    
182     var button = li.lastChild;
183     button.onclick = function () {
184     var pingURL = doc.constructURL
185     ('i', self.sourceDocId, null, 'unrelated-' + self.docId);
186     new SAMI.XHR (pingURL).get ();
187     li.parentNode.removeChild (li);
188     }; // button.onclick
189    
190     return li;
191     } // toLI
192     }); // Neighbors.Entry
193    
194 wakaba 1.2 SW.PageContents = new SAMI.Class (function () {
195     this.footer = document.getElementsByTagName ('footer')[0];
196     }, {
197     insertSection: function (sectionId, content) {
198     var sectionName = {
199 wakaba 1.4 'search-results': 'Related pages',
200     'neighbors': 'Nearby'
201 wakaba 1.2 }[sectionId] || sectionId;
202     var section = document.createElement ('section');
203     section.id = sectionId;
204     var h = document.createElement ('h2');
205     h.innerHTML = 'xxx';
206     h.firstChild.data = sectionName;
207     section.appendChild (h);
208     section.appendChild (content);
209     document.body.insertBefore (section, this.footer);
210     } // insertSection
211     }); // PageContents
212    
213     SW.PageContents.getInstance = function () {
214     if (!this._instance) {
215     this._instance = new SW.PageContents;
216     }
217     return this._instance;
218     }; // getInstance
219    
220 wakaba 1.3 var CanvasInstructions = new SAMI.Class(function (canvas) {
221     this.canvas = canvas;
222     }, {
223     clear: function () {
224     this.canvas.width = this.canvas.width;
225     }, // clear
226    
227     processText: function (text) {
228     text = text.replace (/^<!--/, '').replace (/-->$/, '');
229     this.processLines (text.split(/\x0D\x0A?|\x0A/));
230     }, // processText
231    
232     processLines: function (lines) {
233     var ctx = this.canvas.getContext ('2d');
234     for (var i = 0; i < lines.length; i++) {
235     var ev = lines[i].split (/,/);
236     if (ev[0] == 'lineTo') {
237     var x = parseFloat (ev[1]);
238     var y = parseFloat (ev[2]);
239     ctx.lineTo (x, y);
240     ctx.stroke ();
241     // ctx.closePath ();
242     // ctx.beginPath ();
243     // ctx.moveTo (x, y);
244     } else if (ev[0] == 'moveTo') {
245     var x = parseFloat (ev[1]);
246     var y = parseFloat (ev[2]);
247     ctx.moveTo (x, y);
248     } else if (ev[0] == 'strokeStyle' || ev[0] == 'lineWidth') {
249     ctx.closePath ();
250     ctx.beginPath ();
251     ctx[ev[0]] = ev[1];
252     }
253     }
254     } // processLines
255     }); // CanvasInstructions
256    
257     SW.Drawings = {};
258     SAMI.Class.addClassMethods (SW.Drawings, {
259     drawCanvases: function () {
260     var canvases = SAMI.Node.getElementsByClassName (document.body, 'swe-canvas-instructions');
261     canvases.forEach(function (canvas) {
262     var script = canvas.firstChild;
263     if (!script) return;
264     if (script.nodeName.toLowerCase () != 'script') return;
265     if (script.type != 'image/x-canvas-instructions+text') return;
266     var text = SAMI.Element.getTextContent (script);
267     new CanvasInstructions (canvas).processText (text);
268     });
269     } // drawCanvases
270     }); // SW.Drawings
271    
272 wakaba 1.1 SW.init = function () {
273     var doc = SW.CurrentDocument.getInstance ();
274     if (doc.area == 'n') {
275     var searchURL = doc.constructURL (null, null, null, 'search');
276    
277     new SAMI.XHR (searchURL, function () {
278     var sr = new SW.SearchResult (this.getText ());
279     if (sr.entries.list.length) {
280     var ol = sr.toOL ();
281 wakaba 1.2 SW.PageContents.getInstance ().insertSection ('search-results', ol);
282 wakaba 1.1 }
283     }).get ();
284 wakaba 1.4
285     var id = SW.CurrentDocument.getDocumentId ();
286     if (id) {
287     var neighborsURL = doc.constructURL ('i', id, null, 'neighbors');
288     new SAMI.XHR (neighborsURL, function () {
289 wakaba 1.5 var sr = new SW.Neighbors (id, this.getText ());
290 wakaba 1.4 if (sr.entries.list.length) {
291 wakaba 1.5 var ol = sr.toUL ();
292 wakaba 1.4 SW.PageContents.getInstance ().insertSection ('neighbors', ol);
293     }
294     }).get ();
295     }
296 wakaba 1.3
297     SW.Drawings.drawCanvases ();
298 wakaba 1.1 }
299    
300     }; // init

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24