/[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.1 - (hide annotations) (download) (as text)
Sun Jul 12 06:04:22 2009 UTC (15 years, 4 months ago) by wakaba
Branch: MAIN
File MIME type: application/javascript
++ swe/scripts/ChangeLog	12 Jul 2009 06:04:15 -0000
	* sw-sami.js: New script.  It loads search result and insert into
	the document.

	* sw.js: Loads sw-sami.js.

2009-07-12  Wakaba  <wakaba@suika.fam.cx>

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     SW.CurrentDocument.getInstance = function () {
44     if (!SW.CurrentDocument._instance) {
45     SW.CurrentDocument._instance = new SW.CurrentDocument;
46     }
47     return SW.CurrentDocument._instance;
48     }; // getInstance
49    
50     SW.SearchResult = new SAMI.Class (function (source) {
51     this.parse (source);
52     }, {
53     parse: function (source) {
54     this.entries = new SAMI.List (source.split (/\x0D?\x0A/)).map (function (v) {
55     if (v == '') return;
56     return new SW.SearchResult.Entry (v.split (/\t/, 3));
57     }).grep (function (v) { return v });
58     }, // parse
59    
60     toOL: function () {
61     var ol = document.createElement ('ol');
62     this.entries.forEach (function (entry) {
63     ol.appendChild (entry.toLI ());
64     });
65     return ol;
66     } // toOL
67     }); // SearchResult
68    
69     SW.SearchResult.Entry = new SAMI.Class (function (v) {
70     this.score = v[0];
71     this.docId = v[1];
72     this.docName = v[2];
73     }, {
74     toLI: function () {
75     var li = document.createElement ('li');
76     li.innerHTML = '<a href="">xxx</a>';
77     li.firstChild.firstChild.data = this.docName;
78     li.firstChild.href = SW.CurrentDocument.getInstance ().constructURL
79     ('n', this.docName, this.docId);
80     return li;
81     } // toLI
82     }); // SearchResult
83    
84     SW.init = function () {
85     var doc = SW.CurrentDocument.getInstance ();
86     if (doc.area == 'n') {
87     var searchURL = doc.constructURL (null, null, null, 'search');
88    
89     new SAMI.XHR (searchURL, function () {
90     var sr = new SW.SearchResult (this.getText ());
91     if (sr.entries.list.length) {
92     var ol = sr.toOL ();
93     document.body.appendChild (ol);
94     }
95     }).get ();
96     }
97    
98     }; // init

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24