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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (show annotations) (download) (as text)
Sat Mar 14 01:01:55 2009 UTC (17 years, 5 months ago) by wakaba
Branch: MAIN
Changes since 1.3: +36 -15 lines
File MIME type: application/javascript
++ swe/scripts/ChangeLog	14 Mar 2009 01:01:18 -0000
2009-03-14  Wakaba  <wakaba@suika.fam.cx>

	* sw.js: Added a button to insert the current time.

1 window.onload = function () {
2 createToolbar ();
3 initializeHatenaStar();
4 enableHTML5Support ();
5 }; // window.onload
6
7 function getElementsByClassName (c) {
8 if (document.getElementsByClassName) {
9 return document.getElementsByClassName (c);
10 } else {
11 return [];
12 }
13 } // getElementsByClassName
14
15 function getAncestorElement (n, t) {
16 while (n != null) {
17 if (n.nodeName == t) {
18 return n;
19 } else {
20 n = n.parentNode;
21 }
22 }
23 return null;
24 } // getAncestorElement
25
26 function getGlobalDateAndTimeString (date) {
27 var r = '';
28 r = date.getUTCFullYear (); // JS does not support years 0001-0999
29 r += '-' + ('0' + (date.getUTCMonth () + 1)).slice (-2);
30 r += '-' + ('0' + date.getUTCDate ()).slice (-2);
31 r += 'T' + ('0' + date.getUTCHours ()).slice (-2);
32 r += ':' + ('0' + date.getUTCMinutes ()).slice (-2);
33 r += ':' + ('0' + date.getUTCSeconds ()).slice (-2);
34 r += '.' + (date.getUTCMilliseconds () + '00').slice (2);
35 r += 'Z';
36 return r;
37 } // getGlobalDateAndTimeString
38
39 function getNextAnchorNumber (s) {
40 var lastId = 0;
41 s.replace (/\[([0-9]+)\]/g, function (l, n) {
42 var v = parseInt (n);
43 if (v > lastId) {
44 lastId = v;
45 }
46 });
47 return lastId + 1;
48 } // getNextAnchorNumber
49
50 function createToolbar () {
51 var containers = getElementsByClassName ('text-toolbar');
52 for (var i = 0; i < containers.length; i++) {
53 var container = containers[i];
54
55 var form = getAncestorElement (container, 'FORM');
56 var ta = form.elements.text;
57 var insertText = function (added) {
58 var st = ta.scrollTop;
59 var ss = ta.selectionStart;
60 var se = (ss != ta.selectionEnd);
61 ta.value = ta.value.substring (0, ta.selectionStart)
62 + added + ta.value.substring (ta.selectionEnd);
63 if (se) {
64 ta.setSelectionRange (ss, ss + added.length);
65 } else {
66 ta.setSelectionRange (ss + added.length, ss + added.length);
67 }
68 ta.scrollTop = st;
69 ta.focus ();
70 }; // insertText
71
72 var addButton = function (labelHTML, onclick) {
73 var button = document.createElement ('button');
74 button.setAttribute ('type', 'button');
75 button.innerHTML = labelHTML;
76 button.onclick = onclick;
77 container.appendChild (button);
78 }; // addButton
79
80 addButton ('[#]', function () {
81 var added = '[' + getNextAnchorNumber (ta.value) + '] ';
82 insertText (added);
83 });
84
85 addButton ('Now', function () {
86 var added = '[TIME[' + getGlobalDateAndTimeString (new Date) + ']]';
87 insertText (added);
88 });
89 }
90 } // createToolbar
91
92 function initializeHatenaStar () {
93 if (!window.Hatena) {
94 window.Hatena = {};
95 }
96 if (!Hatena.Star) {
97 Hatena.Star = {};
98 }
99 if (!Hatena.Star.onLoadFunctions) {
100 Hatena.Star.onLoadFunctions = [];
101 }
102
103 Hatena.Star.onLoadFunctions.push (function () {
104 /*
105 if (Ten.DOM.loaded) {
106 Ten.DOM.loaded = false;
107 Ten.DOM.addObserver ();
108 }
109 */
110
111 Hatena.Star.SiteConfig = {
112 entryNodes: {
113 '' /* body */: {
114 uri: 'h1 a',
115 title: 'h1',
116 container: '.tools' /* .nav.tools */
117 }
118 }
119 };
120
121 /*
122 var realLoadNewEntries = Hatena.Star.EntryLoader.loadNewEntries;
123 Hatena.Star.EntryLoader.loadNewEntries = function (node) {
124 if (!node) {
125 node = document.documentElement;
126 }
127 realLoadNewEntries.apply (this, [node]);
128 };
129 */
130 setTimeout (function () {
131 Ten.DOM.dispatchEvent ('onload');
132 }, 1);
133 }); // Hatena.Star.onLoadFunctions.push'ed function
134
135 var hsScript = document.createElement ('script');
136 hsScript.defer = true;
137 hsScript.src = 'http://s.hatena.ne.jp/js/HatenaStar.js';
138 document.documentElement.lastChild.appendChild (hsScript);
139
140 /*
141 In SuikaWiki, a WikiName can be associated with multiple WikiPages,
142 while a WikiPage can be associated with multiple WikiNames. It would
143 be desired to associate Hatena Stars with WikiPages in theory.
144 However, WikiPages do not have permalinks at the moment.
145
146 See <http://suika.fam.cx/%7Ewakaba/wiki/sw/n/%E3%81%AF%E3%81%A6%E3%81%AA%E3%82%B9%E3%82%BF%E3%83%BC#anchor-2>.
147 */
148 } // initializeHatenaStar
149
150 function enableHTML5Support () {
151 window.TEROnLoad = function () {
152 new TER.Delta (document.body);
153 }; // window.TEROnLoad
154
155 var timeScript = document.createElement ('script');
156 timeScript.defer = true;
157 timeScript.charset = 'utf-8';
158 timeScript.src = 'http://suika.fam.cx/www/style/ui/time.js';
159 document.documentElement.lastChild.appendChild (timeScript);
160 } // enableHTML5Support
161
162 /* Hack for IE */
163 document.createElement ('time');

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24