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 |
wakaba |
1.2 |
SW.PageContents = new SAMI.Class (function () { |
85 |
|
|
this.footer = document.getElementsByTagName ('footer')[0]; |
86 |
|
|
}, { |
87 |
|
|
insertSection: function (sectionId, content) { |
88 |
|
|
var sectionName = { |
89 |
|
|
'search-results': 'Related pages' |
90 |
|
|
}[sectionId] || sectionId; |
91 |
|
|
var section = document.createElement ('section'); |
92 |
|
|
section.id = sectionId; |
93 |
|
|
var h = document.createElement ('h2'); |
94 |
|
|
h.innerHTML = 'xxx'; |
95 |
|
|
h.firstChild.data = sectionName; |
96 |
|
|
section.appendChild (h); |
97 |
|
|
section.appendChild (content); |
98 |
|
|
document.body.insertBefore (section, this.footer); |
99 |
|
|
} // insertSection |
100 |
|
|
}); // PageContents |
101 |
|
|
|
102 |
|
|
SW.PageContents.getInstance = function () { |
103 |
|
|
if (!this._instance) { |
104 |
|
|
this._instance = new SW.PageContents; |
105 |
|
|
} |
106 |
|
|
return this._instance; |
107 |
|
|
}; // getInstance |
108 |
|
|
|
109 |
wakaba |
1.1 |
SW.init = function () { |
110 |
|
|
var doc = SW.CurrentDocument.getInstance (); |
111 |
|
|
if (doc.area == 'n') { |
112 |
|
|
var searchURL = doc.constructURL (null, null, null, 'search'); |
113 |
|
|
|
114 |
|
|
new SAMI.XHR (searchURL, function () { |
115 |
|
|
var sr = new SW.SearchResult (this.getText ()); |
116 |
|
|
if (sr.entries.list.length) { |
117 |
|
|
var ol = sr.toOL (); |
118 |
wakaba |
1.2 |
SW.PageContents.getInstance ().insertSection ('search-results', ol); |
119 |
wakaba |
1.1 |
} |
120 |
|
|
}).get (); |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
}; // init |