/[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.3 - (hide annotations) (download) (as text)
Mon Sep 14 03:57:06 2009 UTC (16 years, 10 months ago) by wakaba
Branch: MAIN
Changes since 1.2: +54 -0 lines
File MIME type: application/javascript
added support for displaying vector image in the Remote Canvas format

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.3 var CanvasInstructions = new SAMI.Class(function (canvas) {
110     this.canvas = canvas;
111     }, {
112     clear: function () {
113     this.canvas.width = this.canvas.width;
114     }, // clear
115    
116     processText: function (text) {
117     text = text.replace (/^<!--/, '').replace (/-->$/, '');
118     this.processLines (text.split(/\x0D\x0A?|\x0A/));
119     }, // processText
120    
121     processLines: function (lines) {
122     var ctx = this.canvas.getContext ('2d');
123     for (var i = 0; i < lines.length; i++) {
124     var ev = lines[i].split (/,/);
125     if (ev[0] == 'lineTo') {
126     var x = parseFloat (ev[1]);
127     var y = parseFloat (ev[2]);
128     ctx.lineTo (x, y);
129     ctx.stroke ();
130     // ctx.closePath ();
131     // ctx.beginPath ();
132     // ctx.moveTo (x, y);
133     } else if (ev[0] == 'moveTo') {
134     var x = parseFloat (ev[1]);
135     var y = parseFloat (ev[2]);
136     ctx.moveTo (x, y);
137     } else if (ev[0] == 'strokeStyle' || ev[0] == 'lineWidth') {
138     ctx.closePath ();
139     ctx.beginPath ();
140     ctx[ev[0]] = ev[1];
141     }
142     }
143     } // processLines
144     }); // CanvasInstructions
145    
146     SW.Drawings = {};
147     SAMI.Class.addClassMethods (SW.Drawings, {
148     drawCanvases: function () {
149     var canvases = SAMI.Node.getElementsByClassName (document.body, 'swe-canvas-instructions');
150     canvases.forEach(function (canvas) {
151     var script = canvas.firstChild;
152     if (!script) return;
153     if (script.nodeName.toLowerCase () != 'script') return;
154     if (script.type != 'image/x-canvas-instructions+text') return;
155     var text = SAMI.Element.getTextContent (script);
156     new CanvasInstructions (canvas).processText (text);
157     });
158     } // drawCanvases
159     }); // SW.Drawings
160    
161 wakaba 1.1 SW.init = function () {
162     var doc = SW.CurrentDocument.getInstance ();
163     if (doc.area == 'n') {
164     var searchURL = doc.constructURL (null, null, null, 'search');
165    
166     new SAMI.XHR (searchURL, function () {
167     var sr = new SW.SearchResult (this.getText ());
168     if (sr.entries.list.length) {
169     var ol = sr.toOL ();
170 wakaba 1.2 SW.PageContents.getInstance ().insertSection ('search-results', ol);
171 wakaba 1.1 }
172     }).get ();
173 wakaba 1.3
174     SW.Drawings.drawCanvases ();
175 wakaba 1.1 }
176    
177     }; // init

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24