/[suikacvs]/test/suikawebwww/www/js/jste/tutorial.js
Suika

Contents of /test/suikawebwww/www/js/jste/tutorial.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.23 - (hide annotations) (download) (as text)
Sun Feb 1 14:12:46 2009 UTC (17 years, 5 months ago) by wakaba
Branch: MAIN
Changes since 1.22: +100 -4 lines
File MIME type: application/javascript
Implemented cookie-based storage

1 wakaba 1.1
2     if (typeof (JSTE) === "undefined") var JSTE = {};
3    
4     JSTE.WATNS = 'http://suika.fam.cx/ns/wat';
5     JSTE.SpaceChars = /[\x09\x0A\x0C\x0D\x20]+/;
6    
7     JSTE.Class = function (constructor, prototype) {
8     return JSTE.Subclass (constructor, JSTE.EventTarget, prototype);
9     }; // Class
10    
11     JSTE.Class.addClassMethods = function (classObject, methods) {
12     new JSTE.Hash (methods).forEach (function (n, v) {
13     if (!classObject[n]) {
14     classObject[n] = v;
15     }
16     });
17     }; // addClassMethods
18    
19     JSTE.Subclass = function (constructor, superclass, prototype) {
20     constructor.prototype = new superclass;
21     for (var n in prototype) {
22     constructor.prototype[n] = prototype[n];
23     }
24     constructor.prototype.constructor = constructor;
25     constructor.prototype._super = superclass;
26     return constructor;
27     }; // Subclass
28    
29     JSTE.EventTarget = new JSTE.Subclass (function () {
30    
31     }, function () {}, {
32     addEventListener: function (eventType, handler, useCapture) {
33     if (useCapture) return;
34     if (!this.eventListeners) this.eventListeners = {};
35     if (!this.eventListeners[eventType]) {
36     this.eventListeners[eventType] = new JSTE.List;
37     }
38     this.eventListeners[eventType].push (handler);
39     }, // addEventListener
40     removeEventListener: function (eventType, handler, useCapture) {
41     if (useCapture) return;
42     if (!this.eventListeners) return;
43     if (!this.eventListeners[eventType]) return;
44     this.eventListeners[eventType].remove (handler);
45     }, // removeEventListener
46     dispatchEvent: function (e) {
47     if (!this.eventListeners) return;
48     var handlers = this.eventListeners[e.type];
49     if (!handlers) return;
50     e.currentTarget = this;
51     e.target = this;
52     var preventDefault;
53     handlers.forEach (function (handler) {
54     if (handler.apply (this, [e])) {
55     preventDefault = true;
56     }
57     });
58     return preventDefault || e.isDefaultPrevented ();
59     } // dispatchEvent
60     }); // EventTarget
61    
62     JSTE.Event = new JSTE.Class (function (eventType, canBubble, cancelable) {
63     this.type = eventType;
64     this.bubbles = canBubble;
65     this.cancelable = cancelable;
66     }, {
67     preventDefault: function () {
68     this.defaultPrevented = true;
69     }, // preventDefault
70     isDefaultPrevented: function () {
71     return this.defaultPrevented;
72     } // isDefaultPrevented
73     });
74    
75     JSTE.Observer = new JSTE.Class (function (eventType, target, onevent) {
76     this.eventType = eventType;
77 wakaba 1.10 this.target = target;
78 wakaba 1.1 if (target.addEventListener) {
79     this.code = onevent;
80     } else if (target.attachEvent) {
81     this.code = function () {
82     onevent (event);
83     };
84 wakaba 1.10 } else {
85     this.code = onevent;
86 wakaba 1.1 }
87 wakaba 1.10 this.disabled = true;
88     this.start ();
89 wakaba 1.1 }, {
90 wakaba 1.10 start: function () {
91     if (!this.disabled) return;
92     if (this.target.addEventListener) {
93     this.target.addEventListener (this.eventType, this.code, false);
94     this.disabled = false;
95     } else if (this.target.attachEvent) {
96     this.target.attachEvent ("on" + this.eventType, this.code);
97     this.disabled = false;
98     }
99     }, // start
100 wakaba 1.1 stop: function () {
101 wakaba 1.10 if (this.disabled) return;
102 wakaba 1.1 if (this.target.removeEventListener) {
103     this.target.removeEventListener (this.eventType, this.code, false);
104 wakaba 1.10 this.disabled = true;
105 wakaba 1.11 } else if (this.target.detachEvent) {
106 wakaba 1.1 this.target.detachEvent ("on" + this.eventType, this.code);
107 wakaba 1.10 this.disabled = true;
108 wakaba 1.1 }
109     } // stop
110     }); // Observer
111    
112 wakaba 1.5 new JSTE.Observer ('load', window, function () {
113     JSTE.windowLoaded = true;
114     });
115    
116 wakaba 1.10
117     JSTE.Hash = new JSTE.Class (function (hash) {
118     this.hash = hash || {};
119     }, {
120     forEach: function (code) {
121     for (var n in this.hash) {
122     var r = code (n, this.hash[n]);
123     if (r && r.stop) break;
124     }
125     }, // forEach
126     clone: function (code) {
127     var newHash = {};
128     this.forEach (function (n, v) {
129     newHash[n] = v;
130     });
131     return new this.constructor (newHash);
132     }, // clone
133    
134     getNamedItem: function (n) {
135     return this.hash[n];
136     }, // getNamedItem
137     setNamedItem: function (n, v) {
138     return this.hash[n] = v;
139 wakaba 1.22 }, // setNamedItem
140    
141 wakaba 1.23 getNames: function () {
142     var r = new JSTE.List;
143     for (var n in this.hash) {
144     r.push (n);
145     }
146     return r;
147     }, // getNames
148    
149 wakaba 1.22 getByNames: function (names) {
150     var self = this;
151     return names.forEach (function (name) {
152     var value = self.getNamedItem (name);
153     if (value != null) {
154     return new JSTE.List.Return (value);
155     } else {
156     return null;
157     }
158     });
159     } // getByNames
160     }); // Hash
161 wakaba 1.10
162    
163 wakaba 1.1 JSTE.List = new JSTE.Class (function (arrayLike) {
164     this.list = arrayLike || [];
165     }, {
166     getLast: function () {
167     if (this.list.length) {
168     return this.list[this.list.length - 1];
169     } else {
170     return null;
171     }
172     }, // getLast
173    
174     forEach: function (code) {
175     var length = this.list.length;
176     for (var i = 0; i < length; i++) {
177     var r = code (this.list[i]);
178     if (r && r.stop) return r.returnValue;
179     }
180     return null;
181     }, // forEach
182 wakaba 1.23 map: function (code) {
183     var newList = new this.constructor;
184     var length = this.list.length;
185     for (var i = 0; i < length; i++) {
186     newList.push (code (this.list[i]));
187     }
188     return newList;
189     }, // map
190     mapToHash: function (code) {
191     var newHash = new JSTE.Hash;
192     var length = this.list.length;
193     for (var i = 0; i < length; i++) {
194     var nv = code (this.list[i]);
195     newHash.setNamedItem (nv[0], nv[1]);
196     }
197     return newHash;
198     }, // mapToHash
199 wakaba 1.4
200     numberToInteger: function () {
201     var newList = [];
202     this.forEach (function (item) {
203     if (typeof item === "number") {
204     newList.push (Math.floor (item));
205     } else {
206     newList.push (item);
207     }
208     });
209     return new this.constructor (newList);
210     }, // numberToInteger
211 wakaba 1.23
212 wakaba 1.1 clone: function () {
213     var newList = [];
214     this.forEach (function (item) {
215     newList.push (item);
216     });
217     return new this.constructor (newList);
218     }, // clone
219    
220     grep: function (code) {
221     var newList = [];
222     this.forEach (function (item) {
223     if (code (item)) {
224     newList.push (item);
225     }
226     });
227     return new this.constructor (newList);
228     }, // grep
229     onlyNonNull: function () {
230     return this.grep (function (item) {
231     return item != null; /* Intentionally "!=" */
232     });
233     }, // onlyNonNull
234 wakaba 1.11
235     uniq: function (eq) {
236     if (!eq) eq = function (i1, i2) { return i1 === i2 };
237     var prevItems = [];
238     return this.grep (function (item) {
239     for (var i = 0; i < prevItems.length; i++) {
240     if (eq (item, prevItems[i])) {
241     return false;
242     }
243     }
244     prevItems.push (item);
245     return true;
246     });
247     }, // uniq
248 wakaba 1.1
249     getFirstMatch: function (code) {
250     return this.forEach (function (item) {
251     if (code (item)) {
252     return new JSTE.List.Return (item);
253     }
254     });
255     }, // getFirstMatch
256    
257     switchByElementType: function () {
258     var cases = new JSTE.List (arguments);
259     this.forEach (function (n) {
260     cases.forEach (function (c) {
261     if (c.namespaceURI == n.namespaceURI) {
262     return new JSTE.List.Return (c.execute (n));
263     }
264     });
265     });
266     }, // switchByElementType
267    
268 wakaba 1.10 // destructive
269 wakaba 1.1 push: function (item) {
270     this.list.push (item);
271     }, // push
272 wakaba 1.10
273     // destructive
274 wakaba 1.1 pushCloneOfLast: function () {
275     this.list.push (this.getLast ().clone ());
276     }, // pushCloneOfLast
277 wakaba 1.10
278     // destructive
279 wakaba 1.1 append: function (list) {
280     var self = this;
281     list.forEach (function (n) {
282     self.list.push (n);
283     });
284 wakaba 1.10 return this;
285 wakaba 1.1 }, // append
286    
287 wakaba 1.10 // destructive
288 wakaba 1.1 pop: function () {
289     return this.list.pop ();
290     }, // pop
291 wakaba 1.10
292     // destructive
293 wakaba 1.1 remove: function (removedItem) {
294     var length = this.list.length;
295     for (var i = length - 1; i >= 0; --i) {
296     var item = this.list[i];
297     if (item == removedItem) { // Intentionally "=="
298     this.list.splice (i, 1);
299     }
300     }
301     }, // remove
302 wakaba 1.10
303     // destructive
304 wakaba 1.1 clear: function () {
305     this.list.splice (0, this.list.length);
306     } // clear
307    
308     }); // List
309    
310 wakaba 1.10 JSTE.Class.addClassMethods (JSTE.List, {
311     spaceSeparated: function (v) {
312     return new JSTE.List ((v || '').split (JSTE.SpaceChars)).grep (function (v) {
313     return v.length;
314     });
315     }, // spaceSeparated
316    
317     getCommonItems: function (l1, l2, cb, eq) {
318 wakaba 1.11 if (!eq) eq = function (i1, i2) { return i1 === i2 };
319 wakaba 1.10
320     var common = new JSTE.List;
321    
322     l1 = l1.grep (function (i1) {
323     var hasI1;
324     l2 = l2.grep (function (i2) {
325     if (eq (i1, i2)) {
326     common.push (i1);
327     hasI1 = true;
328     return false;
329     } else {
330     return true;
331     }
332     });
333     return !hasI1;
334     });
335    
336     cb (common, l1, l2);
337     } // getCommonItems
338     }); // List class methods
339    
340 wakaba 1.1 JSTE.List.Return = new JSTE.Class (function (rv) {
341     this.stop = true;
342     this.returnValue = rv;
343     }, {
344    
345     }); // Return
346    
347     JSTE.List.SwitchByLocalName = new JSTE.Class (function (ns, cases, ow) {
348     this.namespaceURI = ns;
349     this.cases = cases;
350     this.otherwise = ow || function (n) { };
351     }, {
352     execute: function (n) {
353     for (var ln in this.cases) {
354     if (JSTE.Element.matchLocalName (n, ln)) {
355     return this.cases[ln] (n);
356     }
357     }
358     return this.otherwise (n);
359     }
360     });
361    
362    
363     if (!JSTE.Node) JSTE.Node = {};
364    
365     JSTE.Class.addClassMethods (JSTE.Node, {
366     querySelector: function (node, selectors) {
367     if (node.querySelector) {
368     var el;
369     try {
370     el = node.querySelector (selectors);
371     } catch (e) {
372     el = null;
373     }
374     return el;
375     } else if (window.uu && uu.css) {
376     if (selectors != "") {
377     /* NOTE: uu.css return all elements for "" or ",xxx" */
378     return uu.css (selectors, node)[0];
379     } else {
380     return null;
381     }
382     } else if (window.Ten && Ten.DOM && Ten.DOM.getElementsBySelector) {
383     return Ten.DOM.getElementsBySelector (selectors)[0];
384     } else {
385     return null;
386     }
387     }, // querySelector
388     querySelectorAll: function (node, selectors) {
389     if (node.querySelectorAll) {
390     var nl;
391     try {
392     nl = node.querySelectorAll (selectors);
393     } catch (e) {
394     nl = null;
395     }
396     return new JSTE.List (nl);
397     } else if (window.uu && uu.css) {
398     if (selectors != "") {
399 wakaba 1.11 /* NOTE: uu.css return all elements for "" or ",xxx". */
400 wakaba 1.1 return new JSTE.List (uu.css (selectors, node));
401     } else {
402     return new JSTE.List;
403     }
404     } else if (window.Ten && Ten.DOM && Ten.DOM.getElementsBySelector) {
405     return new JSTE.List (Ten.DOM.getElementsBySelector (selectors));
406     } else {
407     return new JSTE.List;
408     }
409     } // querySelectorAll
410     });
411    
412     if (!JSTE.Element) JSTE.Element = {};
413    
414     JSTE.Class.addClassMethods (JSTE.Element, {
415     getLocalName: function (el) {
416     var localName = el.localName;
417     if (!localName) {
418     localName = el.nodeName;
419     if (el.prefix) {
420     localName = localName.substring (el.prefix.length + 1);
421     }
422     }
423     return localName;
424     }, // getLocalName
425    
426     match: function (el, ns, ln) {
427     if (el.nodeType !== 1) return false;
428     if (el.namespaceURI !== ns) return false;
429     return JSTE.Element.matchLocalName (el, ln);
430     }, // match
431     matchLocalName: function (el, ln) {
432     var localName = JSTE.Element.getLocalName (el);
433     if (ln instanceof RegExp) {
434     if (!localName.match (ln)) return false;
435     } else {
436     if (localName !== ln) return false;
437     }
438     return true;
439     }, // matchLocalName
440    
441     getChildElement: function (el, ns, ln) {
442     return new JSTE.List (el.childNodes).getFirstMatch (function (item) {
443     return JSTE.Element.match (item, ns, ln);
444     });
445     }, // getChildElement
446     getChildElements: function (el, ns, ln) {
447     return new JSTE.List (el.childNodes).grep (function (item) {
448     return JSTE.Element.match (item, ns, ln);
449     });
450     }, // getChildElements
451 wakaba 1.17 getChildrenClassifiedByType: function (el) {
452     var r = new JSTE.ElementHash;
453     new JSTE.List (el.childNodes).forEach (function (n) {
454     if (n.nodeType == 1) {
455     r.getOrCreate (n.namespaceURI, JSTE.Element.getLocalName (n)).push (n);
456     } else {
457     r.getOrCreate (null, n.nodeType).push (n);
458     }
459     });
460     return r;
461     }, // getChildrenClassifiedByType
462 wakaba 1.18
463     isEmpty: function (el) {
464     // HTML5 definition of "empty"
465     return !new JSTE.List (el.childNodes).forEach (function (n) {
466     var nt = n.nodeType;
467     if (nt == 1) {
468     return new JSTE.List.Return (true /* not empty */);
469     } else if (nt == 3 || nt == 4) {
470     if (/[^\u0009\u000A\u000C\u000D\u0020]/.test (n.data)) {
471     return new JSTE.List.Return (true /* not empty */);
472     }
473     } else if (nt == 7 || nt == 8) { // comment/pi
474     // does not affect emptyness
475     } else {
476     // We don't support EntityReference.
477     return new JSTE.List.Return (true /* not empty */);
478     }
479     });
480     }, // isEmpty
481 wakaba 1.1
482     appendText: function (el, s) {
483     return el.appendChild (el.ownerDocument.createTextNode (s));
484     }, // appendText
485    
486     createTemplate: function (doc, node) {
487     var df = doc.createDocumentFragment ();
488     new JSTE.List (node.childNodes).forEach (function (n) {
489     if (n.nodeType == 1) {
490     var c = doc.createElement (JSTE.Element.getLocalName (n));
491 wakaba 1.15 new JSTE.List (n.attributes).forEach (function (n) {
492 wakaba 1.1 c.setAttribute (n.name, n.value);
493     });
494     c.appendChild (JSTE.Element.createTemplate (doc, n));
495     df.appendChild (c);
496     } else if (n.nodeType == 3 || n.nodeType == 4) {
497     df.appendChild (doc.createTextNode (n.data));
498     }
499     });
500     return df;
501     }, // createTemplate
502    
503     hasAttribute: function (el, localName) {
504     if (el.hasAttribute) {
505     return el.hasAttribute (localName);
506     } else {
507     return el.getAttribute (localName) != null;
508     }
509     }, // hasAttribute
510    
511     getClassNames: function (el) {
512     return new JSTE.List (el.className.split (JSTE.SpaceChars));
513     }, // getClassNames
514     addClassName: function (el, newClassName) {
515     el.className = el.className + ' ' + newClassName;
516     }, // deleteClassName
517     deleteClassName: function (el, oldClassName) {
518     var classNames = el.className.split (JSTE.SpaceChars);
519     var newClasses = [];
520     for (var n in classNames) {
521     if (classNames[n] != oldClassName) {
522     newClasses.push (classNames[n]);
523     }
524     }
525     el.className = newClasses.join (' ');
526     }, // deleteClassName
527     replaceClassName: function (el, oldClassName, newClassName) {
528     var classNames = el.className.split (JSTE.SpaceChars);
529     var newClasses = [newClassName];
530     for (var n in classNames) {
531     if (classNames[n] != oldClassName) {
532     newClasses.push (classNames[n]);
533     }
534     }
535     el.className = newClasses.join (' ');
536     }, // replaceClassName
537    
538     getIds: function (el) {
539     return new JSTE.List (el.id != "" ? [el.id] : []);
540 wakaba 1.5 }, // getIds
541    
542     /*
543     NR.js <http://suika.fam.cx/www/css/noderect/NodeRect.js> must be loaded
544     before the invocation.
545     */
546     scroll: function (elements) {
547     if (!JSTE.windowLoaded) {
548     new JSTE.Observer ('load', window, function () {
549     JSTE.Element.scroll (elements);
550     });
551     return;
552     }
553    
554     var top = Infinity;
555     var left = Infinity;
556     var topEl;
557     var leftEl;
558     elements.forEach (function (el) {
559     var rect = NR.Element.getRects (el, window).borderBox;
560     if (rect.top < top) {
561     top = rect.top;
562     topEl = el;
563     }
564     if (rect.left < left) {
565     left = rect.left;
566     leftEl = el;
567     }
568     });
569    
570     if (!leftEl && !topEl) {
571     return;
572     }
573    
574     var doc = (leftEl || topEl).ownerDocument;
575 wakaba 1.1
576 wakaba 1.5 var rect = NR.View.getViewportRects (window, doc).contentBox;
577     if (rect.top <= top && top <= rect.bottom) {
578     top = rect.top;
579     }
580     if (rect.left <= left && left <= rect.right) {
581     left = rect.left;
582     }
583    
584     /*
585     Set scroll* of both <html> and <body> elements, to support all of
586     four browsers and two (or three) rendering modes. This might result
587     in confusing the user if both <html> and <body> elements have their
588     'overflow' properties specified to 'scroll'.
589    
590     Note that this code does not do a good job if the |el| is within an
591     |overflow: scroll| element.
592     */
593     doc.body.scrollTop = top;
594     doc.body.scrollLeft = left;
595     doc.documentElement.scrollTop = top;
596     doc.documentElement.scrollLeft = left;
597     } // scroll
598 wakaba 1.18 }); // Element
599 wakaba 1.1
600 wakaba 1.17 JSTE.ElementHash = new JSTE.Class (function () {
601     this.items = [];
602     }, {
603     get: function (ns, ln) {
604     ns = ns || '';
605     if (this.items[ns]) {
606     return this.items[ns].getNamedItem (ln) || new JSTE.List;
607     } else {
608     return new JSTE.List;
609     }
610     }, // get
611     getOrCreate: function (ns, ln) {
612     ns = ns || '';
613     if (this.items[ns]) {
614     var l = this.items[ns].getNamedItem (ln);
615     if (!l) this.items[ns].setNamedItem (ln, l = new JSTE.List);
616     return l;
617     } else {
618     var l;
619     this.items[ns] = new JSTE.Hash;
620     this.items[ns].setNamedItem (ln, l = new JSTE.List);
621     return l;
622     }
623     } // getOrCreate
624     }); // ElementHash
625    
626 wakaba 1.9 JSTE.XHR = new JSTE.Class (function (url, onsuccess, onerror) {
627     try {
628     this._xhr = new XMLHttpRequest ();
629     } catch (e) {
630     try {
631     this._xhr = new ActiveXObject ('Msxml2.XMLHTTP');
632     } catch (e) {
633     try {
634     this._xhr = new ActiveXObject ('Microsoft.XMLHTTP');
635     } catch (e) {
636     try {
637     this._xhr = new ActiveXObject ('Msxml2.XMLHTTP.4.0');
638     } catch (e) {
639     this._xhr = null;
640     }
641     }
642     }
643     }
644    
645     this._url = url;
646     this._onsuccess = onsuccess || function () { };
647     this._onerror = onerror || function () { };
648     }, {
649     get: function () {
650     if (!this._xhr) return;
651    
652     var self = this;
653     this._xhr.open ('GET', this._url, true);
654     this._xhr.onreadystatechange = function () {
655     self._onreadystatechange ();
656     }; // onreadystatechange
657     this._xhr.send (null);
658     }, // get
659    
660     _onreadystatechange: function () {
661     if (this._xhr.readyState == 4) {
662     if (this.succeeded ()) {
663     this._onsuccess ();
664     } else {
665     this._onerror ();
666     }
667     }
668     }, // _onreadystatechange
669    
670     succeeded: function () {
671     return (this._xhr.status < 400);
672     }, // succeeded
673    
674     getDocument: function () {
675     return this._xhr.responseXML;
676     } // getDocument
677     }); // XHR
678    
679 wakaba 1.22 // An abstract class
680     JSTE.Storage = new JSTE.Class (function () {
681    
682     }, {
683 wakaba 1.23 get: function (name) {
684     throw "not implemented";
685     }, // get
686     set: function (name, value) {
687     throw "not implemented";
688     }, // set
689    
690     getNames: function () {
691     throw "not implemented";
692     } // getNames
693 wakaba 1.22 }); // Storage
694    
695     JSTE.Storage.PageLocal = new JSTE.Subclass (function () {
696    
697     }, JSTE.Storage, {
698     get: function (name) {
699     return this['value-' + name];
700     }, // get
701     set: function (name, value) {
702     this['value-' + name] = value;
703     }, // set
704    
705     getNames: function () {
706     var names = new JSTE.List;
707     for (var n in this) {
708     if (/^value-/.test (n)) {
709     names.push (n.substring (6));
710     }
711     }
712     return names;
713     } // getNames
714 wakaba 1.23 }); // PageLocal
715    
716     JSTE.Storage.Cookie = JSTE.Subclass (function () {
717     this.keyPrefix = '';
718     this.domain = null;
719     this.path = '/';
720     this.persistent = false;
721     this.expires = null; // or Date
722     }, JSTE.Storage, {
723     _parse: function () {
724     return new JSTE.List (document.cookie.split (/;/)).mapToHash (function (nv) {
725     nv = nv.replace (/^\s+/, '').replace (/\s+$/, '').split (/=/, 2);
726     nv[0] = decodeURIComponent (nv[0]);
727     nv[1] = decodeURIComponent (nv[1]);
728     return nv;
729     });
730     }, // _parse
731    
732     get: function (name) {
733     return this._parse ().getNamedItem (this.keyPrefix + name);
734     }, // get
735     set: function (name, value) {
736     name = this.keyPrefix + name;
737     var r = encodeURIComponent (name) + '=' + encodeURIComponent (value);
738     if (this.domain) {
739     r += '; domain=' + this.domain;
740     }
741     if (this.path) {
742     r += '; path=' + this.path;
743     }
744     if (this.persistent) {
745     r += '; expires=' + new Date (2030, 1-1, 1).toUTCString ();
746     } else if (this.expires) {
747     r += '; expires=' + this.expires.toUTCString ();
748     }
749     document.cookie = r;
750     }, // set
751     delete: function (name) {
752     var expires = this.expires;
753     var persistent = this.persistent;
754     this.expires = new Date (0);
755     this.persistent = false;
756     this.set (name, '');
757     this.expires = expires;
758     this.persistent = persistent;
759     }, // delete
760    
761     getNames: function () {
762     var self = this;
763     return this._parse ().getNames ().grep (function (name) {
764     return name.substring (0, self.keyPrefix.length) == self.keyPrefix;
765     }).map (function (name) {
766     return name.substring (self.keyPrefix.length);
767     });
768     } // getNames
769     }); // Cookie
770    
771     JSTE.Storage.Local = JSTE.Class (function () {
772     var self = new JSTE.Storage.Cookie;
773     self.keyPrefix = 'localStorage-';
774     self.persistent = true;
775     return self;
776     }); // Local
777 wakaba 1.22
778 wakaba 1.9
779 wakaba 1.1 /* Events: load, close, shown, hidden */
780 wakaba 1.18 JSTE.Message = new JSTE.Class (function (doc, template, commandTarget, availCommands) {
781 wakaba 1.1 if (!doc) return;
782     this._targetDocument = doc;
783     this._template = template || doc.createDocumentFragment ();
784 wakaba 1.8
785 wakaba 1.1 this._commandTarget = commandTarget;
786 wakaba 1.18 this._availCommands = availCommands || new JSTE.List;
787 wakaba 1.8
788 wakaba 1.1 this.hidden = true;
789     this.select = "";
790    
791     var e = new JSTE.Event ('load');
792     this.dispatchEvent (e);
793     }, {
794     render: function () {
795     var self = this;
796     var doc = this._targetDocument;
797    
798     var msgContainer = doc.createElement ('section');
799     msgContainer.appendChild (this._template);
800 wakaba 1.20
801     if (!this._availCommands.list.length) {
802 wakaba 1.8 this._availCommands.push ({name: 'back'});
803     this._availCommands.push ({name: 'next'});
804 wakaba 1.7 }
805 wakaba 1.20
806     this._availCommands = this._availCommands.grep(function (item) {
807     return self._commandTarget.canExecuteCommand (item.name, item.args);
808     });
809 wakaba 1.1
810 wakaba 1.8 this._outermostElement = this._render (msgContainer);
811 wakaba 1.1
812     this.show ();
813     }, // render
814     _render: function (msgContainer, buttonContainer) {
815     var doc = this._targetDocument;
816    
817     var container = doc.createElement ('article');
818    
819     container.appendChild (msgContainer);
820 wakaba 1.8
821     var buttonContainer = this.createCommandButtons ();
822 wakaba 1.1 container.appendChild (buttonContainer);
823 wakaba 1.8
824 wakaba 1.1 doc.documentElement.appendChild (container);
825    
826     return container;
827     }, // _render
828 wakaba 1.8 createCommandButtons: function () {
829     var self = this;
830 wakaba 1.18 var doc = this._targetDocument;
831     var buttonContainer = doc.createElement ('menu');
832 wakaba 1.8 this._availCommands.forEach (function (cmd) {
833 wakaba 1.18 var label = cmd.name;
834     if (cmd.labelTemplate) {
835     label = JSTE.Element.createTemplate (doc, cmd.labelTemplate);
836     }
837    
838 wakaba 1.8 var button = new JSTE.Message.Button
839 wakaba 1.18 (label, self._commandTarget, cmd.name, cmd.args);
840 wakaba 1.8 buttonContainer.appendChild (button.element);
841     });
842     return buttonContainer;
843     }, // createCommandButtons
844    
845 wakaba 1.1 remove: function () {
846     this.hide ();
847    
848     this._remove ();
849    
850     if (this._outermostElement && this._outermostElement.parentNode) {
851     this._outermostElement.parentNode.removeChild (this._outermostElement);
852     }
853    
854     var e = new JSTE.Event ("close");
855     this.dispatchEvent (e);
856     }, // remove
857     _remove: function () {
858    
859     }, // remove
860    
861     show: function () {
862     if (!this.hidden) return;
863     this.hidden = false;
864     if (this._outermostElement) {
865     JSTE.Element.replaceClassName
866     (this._outermostElement, "jste-hidden", "jste-shown");
867     }
868    
869     var e = new JSTE.Event ("shown");
870     this.dispatchEvent (e);
871     }, // show
872     hide: function () {
873     if (this.hidden) return;
874     this.hidden = true;
875     if (this._outermostElement) {
876     JSTE.Element.replaceClassName
877     (this._outermostElement, "jste-shown", "jste-hidden");
878     }
879    
880     var e = new JSTE.Event ("hidden");
881     this.dispatchEvent (e);
882     }, // hide
883    
884     setTimeout: function () {
885     /* TODO: ... */
886    
887     }
888    
889     }); // Message
890    
891 wakaba 1.7 /* TODO: button label text should refer message catalog */
892    
893 wakaba 1.1 JSTE.Message.Button =
894 wakaba 1.18 new JSTE.Class (function (label, commandTarget, commandName, commandArgs) {
895     this._label = label != null ? label : "";
896 wakaba 1.6
897 wakaba 1.1 if (commandTarget && commandTarget instanceof Function) {
898     this._command = commandTarget;
899 wakaba 1.6 this._classNames = new JSTE.List;
900 wakaba 1.1 } else if (commandTarget) {
901     this._command = function () {
902     return commandTarget.executeCommand.apply
903     (commandTarget, [commandName, commandArgs]);
904     };
905 wakaba 1.6 this._classNames = new JSTE.List (['jste-command-' + commandName]);
906 wakaba 1.1 } else {
907     this._command = function () { };
908 wakaba 1.6 this._classNames = new JSTE.List;
909 wakaba 1.1 }
910    
911 wakaba 1.6 this._createElement ();
912 wakaba 1.1 }, {
913 wakaba 1.6 _createElement: function () {
914     try {
915     this.element = document.createElement ('button');
916     this.element.setAttribute ('type', 'button');
917     } catch (e) {
918     this.element = document.createElement ('<button type=button>');
919     }
920 wakaba 1.18 if (this._label.nodeType) {
921     this.element.appendChild (this._label);
922     } else {
923     JSTE.Element.appendText (this.element, this._label);
924     }
925 wakaba 1.6 this.element.className = this._classNames.list.join (' ');
926 wakaba 1.1
927 wakaba 1.6 var self = this;
928     new JSTE.Observer ("click", this.element, function (e) {
929     self._command (e);
930     });
931     } // _createElement
932 wakaba 1.1 }); // Button
933    
934     JSTE.Course = new JSTE.Class (function (doc) {
935     this._targetDocument = doc;
936    
937 wakaba 1.22 this._entryPointsByStateName = new JSTE.Hash;
938 wakaba 1.23 this._entryPointsByStateName.setNamedItem ('done', 'special-none');
939 wakaba 1.22
940 wakaba 1.1 this._entryPointsByURL = {};
941     this._entryPointsById = {};
942     this._entryPointsByClassName = {};
943    
944     this._stepsState = new JSTE.List ([new JSTE.Hash]);
945     this._steps = new JSTE.Hash;
946    
947     var nullState = new JSTE.Step;
948     nullState.uid = "";
949     this._steps.setNamedItem (nullState.uid, nullState);
950     this._initialStepUid = nullState.uid;
951     }, {
952 wakaba 1.10 _processStepsContent: function (el, parentSteps) {
953 wakaba 1.1 var self = this;
954     new JSTE.List (el.childNodes).switchByElementType (
955     new JSTE.List.SwitchByLocalName (JSTE.WATNS, {
956 wakaba 1.10 steps: function (n) { self._processStepsElement (n, parentSteps) },
957     step: function (n) { self._processStepElement (n, parentSteps) },
958     jump: function (n) { self._processJumpElement (n, parentSteps) },
959 wakaba 1.14 entryPoint: function (n) { self._processEntryPointElement (n, parentSteps) }
960 wakaba 1.1 })
961     );
962     }, // _processStepsContent
963 wakaba 1.10 _processStepsElement: function (e, parentSteps) {
964     var steps = new JSTE.Steps ();
965     steps.parentSteps = parentSteps;
966 wakaba 1.1 this._stepsState.pushCloneOfLast ();
967     this._stepsState.getLast ().prevStep = null;
968 wakaba 1.10 this._processStepsContent (e, steps);
969 wakaba 1.1 this._stepsState.pop ();
970     }, // _processStepsElement
971    
972 wakaba 1.14 _processEntryPointElement: function (e, parentSteps) {
973 wakaba 1.22 if (JSTE.Element.hasAttribute (e, 'state')) {
974     this.setEntryPointByStateName
975     (e.getAttribute ('state'), e.getAttribute ('step'));
976     } else if (JSTE.Element.hasAttribute (e, 'url')) {
977 wakaba 1.1 this.setEntryPointByURL
978     (e.getAttribute ('url'), e.getAttribute ('step'));
979     } else if (JSTE.Element.hasAttribute (e, 'root-id')) {
980     this.setEntryPointById
981     (e.getAttribute ('root-id'), e.getAttribute ('step'));
982     } else if (JSTE.Element.hasAttribute (e, 'root-class')) {
983     this.setEntryPointByClassName
984     (e.getAttribute ('root-class'), e.getAttribute ('step'));
985     }
986 wakaba 1.14 }, // _processEntryPointElement
987 wakaba 1.22 setEntryPointByStateName: function (stateName, stepName) {
988     this._entryPointsByStateName.setNamedItem (stateName, stepName || '');
989     }, // setEntryPointByStateName
990 wakaba 1.1 setEntryPointByURL: function (url, stepName) {
991     this._entryPointsByURL[url] = stepName || '';
992     }, // setEntryPointByURL
993     setEntryPointById: function (id, stepName) {
994     this._entryPointsById[id] = stepName || '';
995     }, // setEntryPointById
996     setEntryPointByClassName: function (className, stepName) {
997     this._entryPointsByClassName[className] = stepName || '';
998     }, // setEntryPointByClassName
999 wakaba 1.22 findEntryPoint: function (doc, states) {
1000 wakaba 1.1 var self = this;
1001     var td = this._targetDocument;
1002     var stepName;
1003 wakaba 1.22
1004     if (states) {
1005     stepName = self._entryPointsByStateName.getByNames (states.getNames ());
1006     if (stepName) return stepName;
1007     }
1008 wakaba 1.1
1009     var url = doc.URL;
1010     if (url) {
1011     stepName = self._entryPointsByURL[url];
1012     if (stepName) return 'id-' + stepName;
1013     }
1014    
1015     var docEl = td.documentElement;
1016     if (docEl) {
1017     var docElId = JSTE.Element.getIds (docEl).forEach (function (i) {
1018     stepName = self._entryPointsById[i];
1019     if (stepName) return new JSTE.List.Return (stepName);
1020     });
1021     if (stepName) return 'id-' + stepName;
1022    
1023     stepName = JSTE.Element.getClassNames (docEl).forEach (function (c) {
1024     stepName = self._entryPointsByClassName[c];
1025     if (stepName) return new JSTE.List.Return (stepName);
1026     });
1027     if (stepName) return 'id-' + stepName;
1028     }
1029    
1030     var bodyEl = td.body;
1031     if (bodyEl) {
1032     var bodyElId = JSTE.Element.getIds (bodyEl).forEach (function (i) {
1033     stepName = self._entryPointsById[i];
1034     if (stepName) return new JSTE.List.Return (stepName);
1035     });
1036     if (stepName) return 'id-' + stepName;
1037    
1038     stepName = JSTE.Element.getClassNames (bodyEl).forEach (function (c) {
1039     stepName = self._entryPointsByClassName[c];
1040     if (stepName) return new JSTE.List.Return (stepName);
1041     });
1042     if (stepName) return 'id-' + stepName;
1043     }
1044    
1045     return this._initialStepUid;
1046     }, // findEntryPoint
1047    
1048 wakaba 1.10 _processStepElement: function (e, parentSteps) {
1049 wakaba 1.18 var self = this;
1050    
1051 wakaba 1.1 var step = new JSTE.Step (e.getAttribute ('id'));
1052 wakaba 1.10 step.parentSteps = parentSteps;
1053 wakaba 1.1 step.setPreviousStep (this._stepsState.getLast ().prevStep);
1054     step.select = e.getAttribute ('select') || "";
1055     step.nextEvents.append
1056 wakaba 1.10 (JSTE.List.spaceSeparated (e.getAttribute ('next-event')));
1057 wakaba 1.17
1058     var cs = JSTE.Element.getChildrenClassifiedByType (e);
1059    
1060     var msgEl = cs.get (JSTE.WATNS, 'message').list[0];
1061 wakaba 1.1 if (msgEl) {
1062     var msg = JSTE.Element.createTemplate (this._targetDocument, msgEl);
1063     step.setMessageTemplate (msg);
1064     }
1065 wakaba 1.17
1066     var nextEls = cs.get (JSTE.WATNS, 'next-step');
1067 wakaba 1.16 if (nextEls.list.length) {
1068 wakaba 1.1 nextEls.forEach (function (nextEl) {
1069     step.addNextStep
1070     (nextEl.getAttribute ('if'), nextEl.getAttribute ('step'));
1071     });
1072     this._stepsState.getLast ().prevStep = null;
1073     } else {
1074     this._stepsState.getLast ().prevStep = step;
1075     }
1076 wakaba 1.17
1077 wakaba 1.19 cs.get (JSTE.WATNS, 'command').forEach (function (bEl) {
1078 wakaba 1.18 var cmd = {
1079 wakaba 1.20 name: bEl.getAttribute ('type') || 'gotoStep'
1080 wakaba 1.18 };
1081     if (cmd.name == 'gotoStep') {
1082     cmd.args = ['id-' + bEl.getAttribute ('step')];
1083     }
1084     if (!JSTE.Element.isEmpty (bEl)) {
1085     cmd.labelTemplate = JSTE.Element.createTemplate (self._targetDocument, bEl);
1086     }
1087     step.availCommands.push (cmd);
1088 wakaba 1.22 }); // wat:command
1089 wakaba 1.18
1090 wakaba 1.22 cs.get (JSTE.WATNS, 'save-state').forEach (function (bEl) {
1091     var ss = new JSTE.SaveState
1092     (bEl.getAttribute ('name'), bEl.getAttribute ('value'));
1093     step.saveStates.push (ss);
1094     }); // wat:save-state
1095 wakaba 1.13
1096     var evs = JSTE.List.spaceSeparated (e.getAttribute ('entry-event'));
1097     if (evs.list.length) {
1098     var jump = new JSTE.Jump (step.select, evs, step.uid);
1099     if (parentSteps) parentSteps._jumps.push (jump);
1100     }
1101 wakaba 1.1
1102     this._steps.setNamedItem (step.uid, step);
1103     if (!this._initialStepUid) {
1104     this._initialStepUid = step.uid;
1105     }
1106     }, // _processStepElement
1107    
1108 wakaba 1.10 _processJumpElement: function (e, parentSteps) {
1109     var target = e.getAttribute ('target') || '';
1110     var evs = JSTE.List.spaceSeparated (e.getAttribute ('event'));
1111     var stepName = e.getAttribute ('step') || '';
1112    
1113     var jump = new JSTE.Jump (target, evs, 'id-' + stepName);
1114     if (parentSteps) parentSteps._jumps.push (jump);
1115 wakaba 1.1 }, // _processJumpElement
1116    
1117     getStep: function (uid) {
1118     return this._steps.getNamedItem (uid);
1119     } // getStep
1120     }); // Course
1121    
1122 wakaba 1.9 JSTE.Class.addClassMethods (JSTE.Course, {
1123     createFromDocument: function (doc, targetDoc) {
1124     var course = new JSTE.Course (targetDoc);
1125     var docEl = doc.documentElement;
1126     if (!docEl) return course;
1127     if (!JSTE.Element.match (docEl, JSTE.WATNS, 'course')) return course;
1128 wakaba 1.10 course._processStepsContent (docEl, null);
1129 wakaba 1.9 return course;
1130     }, // createFromDocument
1131     createFromURL: function (url, targetDoc, onload, onerror) {
1132     new JSTE.XHR (url, function () {
1133     var course = JSTE.Course.createFromDocument
1134     (this.getDocument (), targetDoc);
1135     if (onload) onload (course);
1136     }, onerror).get ();
1137     } // creatFromURL
1138     }); // Course class methods
1139 wakaba 1.1
1140 wakaba 1.10 JSTE.Jump = new JSTE.Class (function (selectors, eventNames, stepUid) {
1141     this.selectors = selectors;
1142     this.eventNames = eventNames;
1143     this.stepUid = stepUid;
1144     // this.parentSteps
1145     }, {
1146     startObserver: function (doc, commandTarget) {
1147     var self = this;
1148     var observers = new JSTE.List;
1149    
1150     var onev = function () {
1151     commandTarget.gotoStep (self.stepUid);
1152     };
1153    
1154     JSTE.Node.querySelectorAll (doc, this.selectors).forEach
1155     (function (el) {
1156     self.eventNames.forEach (function (evName) {
1157     var ob = new JSTE.Observer (evName, el, onev);
1158     ob._stepUid = self.stepUid;
1159     observers.push (ob);
1160     });
1161     });
1162    
1163     return observers;
1164     } // startObserver
1165     }); // Jump
1166    
1167     JSTE.Steps = new JSTE.Class (function () {
1168     this._jumps = new JSTE.List;
1169     this._jumpHandlers = new JSTE.List;
1170     }, {
1171     setCurrentStepByUid: function (uid) {
1172     this._jumpHandlers.forEach (function (jh) {
1173     if (jh._stepUid != uid && jh.disabled) {
1174     jh.start ();
1175     } else if (jh._stepUid == uid && !jh.disabled) {
1176     jh.stop ();
1177     }
1178     });
1179     }, // setCurrentStepByUid
1180    
1181     installJumps: function (doc, commandTarget) {
1182     if (this._jumpHandlers.list.length) return;
1183     var self = this;
1184     this._jumps.forEach (function (j) {
1185     self._jumpHandlers.append (j.startObserver (doc, commandTarget));
1186     });
1187     }, // installJumps
1188    
1189     uninstallJumps: function () {
1190     this._jumpHandlers.forEach (function (jh) {
1191     jh.stop ();
1192     });
1193     this._jumpHandlers.clear ();
1194     } // uninstallJumps
1195     }); // Steps
1196    
1197 wakaba 1.1 JSTE.Step = new JSTE.Class (function (id) {
1198     if (id != null && id != '') {
1199     this.uid = 'id-' + id;
1200     } else {
1201     this.uid = 'rand-' + Math.random ();
1202     }
1203     this._nextSteps = new JSTE.List;
1204     this.nextEvents = new JSTE.List;
1205 wakaba 1.18 this.availCommands = new JSTE.List;
1206 wakaba 1.22 this.saveStates = new JSTE.List;
1207 wakaba 1.1 this.select = "";
1208     }, {
1209     setMessageTemplate: function (msg) {
1210     this._messageTemplate = msg;
1211     }, // setMessageTemplate
1212     hasMessage: function () {
1213     return this._messageTemplate ? true : false;
1214     }, // hasMessage
1215     createMessage: function (msg, doc, commandTarget) {
1216     var msg;
1217     if (this._messageTemplate) {
1218     var clone = JSTE.Element.createTemplate (doc, this._messageTemplate);
1219 wakaba 1.18 msg = new msg (doc, clone, commandTarget, this.availCommands.clone ());
1220 wakaba 1.1 } else {
1221     msg = new msg (doc, null, commandTarget);
1222     }
1223     msg.select = this.select;
1224     return msg;
1225     }, // createMessage
1226    
1227     addNextStep: function (condition, stepId) {
1228 wakaba 1.16 if (stepId != null) this._nextSteps.push ([condition, stepId]);
1229 wakaba 1.1 }, // addNextStep
1230     setPreviousStep: function (prevStep) {
1231     if (!prevStep) return;
1232     if (prevStep._defaultNextStepUid) return;
1233     prevStep._defaultNextStepUid = this.uid;
1234     }, // setPreviousStep
1235    
1236     getNextStepUid: function (doc) {
1237     var m = this._nextSteps.getFirstMatch (function (item) {
1238     var condition = item[0];
1239     if (condition) {
1240     return JSTE.Node.querySelector (doc, condition) != null;
1241     } else {
1242     return true;
1243     }
1244     });
1245     if (m) {
1246     return 'id-' + m[1];
1247     } else if (this._defaultNextStepUid) {
1248     return this._defaultNextStepUid;
1249     } else {
1250     return null;
1251     }
1252 wakaba 1.10 }, // getNextStepUid
1253    
1254     getAncestorStepsObjects: function () {
1255     var steps = new JSTE.List;
1256     var s = this.parentSteps;
1257     while (s != null) {
1258     steps.push (s);
1259     s = s.parentSteps;
1260     }
1261     return steps;
1262     } // getAncestorStepsObjects
1263 wakaba 1.1 }); // Step
1264    
1265 wakaba 1.22 JSTE.SaveState = new JSTE.Class (function (name, value) {
1266     this.name = name || '';
1267     this.value = value || '';
1268     }, {
1269     save: function (states) {
1270     states.set (this.name, this.value);
1271     } // save
1272     }); // SaveState
1273    
1274     /* Events: load, error, cssomready, close */
1275 wakaba 1.9 JSTE.Tutorial = new JSTE.Class (function (course, doc, args) {
1276 wakaba 1.1 this._course = course;
1277     this._targetDocument = doc;
1278     this._messageClass = JSTE.Message;
1279     if (args) {
1280     if (args.messageClass) this._messageClass = args.messageClass;
1281 wakaba 1.22 if (args.states) this._states = args.states;
1282 wakaba 1.1 }
1283 wakaba 1.22 if (!this._states) this._states = new JSTE.Storage.PageLocal;
1284 wakaba 1.1
1285     this._currentMessages = new JSTE.List;
1286     this._currentObservers = new JSTE.List;
1287     this._prevStepUids = new JSTE.List;
1288 wakaba 1.10 this._currentStepsObjects = new JSTE.List;
1289 wakaba 1.1
1290 wakaba 1.22 var stepUid = this._course.findEntryPoint (document, this._states);
1291 wakaba 1.1 this._currentStep = this._getStepOrError (stepUid);
1292     if (this._currentStep) {
1293     var e = new JSTE.Event ('load');
1294     this.dispatchEvent (e);
1295    
1296 wakaba 1.3 var self = this;
1297     new JSTE.Observer ('cssomready', this, function () {
1298     self._renderCurrentStep ();
1299     });
1300     this._dispatchCSSOMReadyEvent ();
1301 wakaba 1.1 return this;
1302     } else {
1303     return {};
1304     }
1305     }, {
1306     _getStepOrError: function (stepUid) {
1307     var step = this._course.getStep (stepUid);
1308     if (step) {
1309     return step;
1310 wakaba 1.22 } else if (stepUid == 'special-none') {
1311     return null;
1312 wakaba 1.1 } else {
1313     var e = new JSTE.Event ('error');
1314     e.errorMessage = 'Step not found';
1315     e.errorArguments = [this._currentStepUid];
1316     this.dispatchEvent (e);
1317     return null;
1318     }
1319     }, // _getStepOrError
1320    
1321     _renderCurrentStep: function () {
1322     var self = this;
1323     var step = this._currentStep;
1324 wakaba 1.22
1325     step.saveStates.forEach (function (ss) { ss.save (self._states) });
1326 wakaba 1.1
1327     /* Message */
1328     var msg = step.createMessage
1329     (this._messageClass, this._targetDocument, this);
1330     msg.render ();
1331     this._currentMessages.push (msg);
1332    
1333     /* Next-events */
1334     var selectedNodes = JSTE.Node.querySelectorAll
1335     (this._targetDocument, step.select);
1336     var handler = function () {
1337     self.executeCommand ("next");
1338     };
1339     selectedNodes.forEach (function (node) {
1340     step.nextEvents.forEach (function (eventType) {
1341     self._currentObservers.push
1342     (new JSTE.Observer (eventType, node, handler));
1343     });
1344     });
1345 wakaba 1.10
1346     JSTE.List.getCommonItems (this._currentStepsObjects,
1347     step.getAncestorStepsObjects (),
1348     function (common, onlyInOld, onlyInNew) {
1349     common.forEach (function (item) {
1350     item.setCurrentStepByUid (step.uid);
1351     });
1352     onlyInOld.forEach (function (item) {
1353     item.uninstallJumps ();
1354     });
1355     onlyInNew.forEach (function (item) {
1356     item.installJumps (self._targetDocument, self);
1357     });
1358     self._currentStepsObjects = common.append (onlyInNew);
1359     });
1360 wakaba 1.1 }, // _renderCurrentStep
1361     clearMessages: function () {
1362     this._currentMessages.forEach (function (msg) {
1363     msg.remove ();
1364     });
1365     this._currentMessages.clear ();
1366    
1367     this._currentObservers.forEach (function (ob) {
1368     ob.stop ();
1369     });
1370     this._currentObservers.clear ();
1371     }, // clearMessages
1372 wakaba 1.10 clearStepsHandlers: function () {
1373     this._currentStepsObjects.forEach (function (item) {
1374     item.uninstallJumps ();
1375     });
1376     this._currentStepsObjects.clear ();
1377     }, // clearStepsHandlers
1378 wakaba 1.1
1379     executeCommand: function (commandName, commandArgs) {
1380     if (this[commandName]) {
1381     return this[commandName].apply (this, commandArgs || []);
1382     } else {
1383     var e = new JSTE.Event ('error');
1384     e.errorMessage = 'Command not found';
1385     e.errorArguments = [commandName];
1386     return null;
1387     }
1388     }, // executeCommand
1389 wakaba 1.7 canExecuteCommand: function (commandName, commandArgs) {
1390     if (this[commandName]) {
1391     var can = this['can' + commandName.substring (0, 1).toUpperCase ()
1392     + commandName.substring (1)];
1393     if (can) {
1394     return can.apply (this, arguments);
1395     } else {
1396     return true;
1397     }
1398     } else {
1399     return false;
1400     }
1401     }, // canExecuteCommand
1402 wakaba 1.1
1403     back: function () {
1404     var prevStepUid = this._prevStepUids.pop ();
1405     var prevStep = this._getStepOrError (prevStepUid);
1406     if (prevStep) {
1407     this.clearMessages ();
1408     this._currentStep = prevStep;
1409     this._renderCurrentStep ();
1410     }
1411     }, // back
1412 wakaba 1.7 canBack: function () {
1413     return this._prevStepUids.list.length > 0;
1414     }, // canBack
1415 wakaba 1.1 next: function () {
1416     var nextStepUid = this._currentStep.getNextStepUid (this._targetDocument);
1417     var nextStep = this._getStepOrError (nextStepUid);
1418     if (nextStep) {
1419     this._prevStepUids.push (this._currentStep.uid);
1420     this.clearMessages ();
1421     this._currentStep = nextStep;
1422     this._renderCurrentStep ();
1423     }
1424 wakaba 1.3 }, // next
1425 wakaba 1.7 canNext: function () {
1426     return this._currentStep.getNextStepUid (this._targetDocument) != null;
1427     }, // canNext
1428 wakaba 1.10 gotoStep: function (uid) {
1429     var nextStep = this._getStepOrError (uid);
1430     if (nextStep) {
1431     this._prevStepUids.push (this._currentStep.uid);
1432     this.clearMessages ();
1433     this._currentStep = nextStep;
1434     this._renderCurrentStep ();
1435     }
1436     }, // gotoStep
1437 wakaba 1.20
1438     close: function () {
1439     this.clearMessages ();
1440 wakaba 1.22 var e = new JSTE.Event ('closed');
1441     this.dispatchEvent (e);
1442 wakaba 1.20 }, // close
1443 wakaba 1.3
1444     // <http://twitter.com/waka/status/1129513097>
1445     _dispatchCSSOMReadyEvent: function () {
1446     var self = this;
1447     var e = new JSTE.Event ('cssomready');
1448     if (window.opera && document.readyState != 'complete') {
1449     new JSTE.Observer ('readystatechange', document, function () {
1450     if (document.readyState == 'complete') {
1451     self.dispatchEvent (e);
1452     }
1453     });
1454     } else {
1455     this.dispatchEvent (e);
1456     }
1457     } // dispatchCSSOMReadyEvent
1458    
1459 wakaba 1.1 }); // Tutorial
1460 wakaba 1.9
1461     JSTE.Class.addClassMethods (JSTE.Tutorial, {
1462 wakaba 1.12 createFromURL: function (url, doc, args, onload) {
1463 wakaba 1.9 JSTE.Course.createFromURL (url, doc, function (course) {
1464 wakaba 1.12 var tutorial = new JSTE.Tutorial (course, doc, args);
1465     if (onload) onload (tutorial);
1466 wakaba 1.9 });
1467     } // createFromURL
1468     }); // Tutorial class methods
1469    
1470    
1471 wakaba 1.5
1472     if (JSTE.onLoadFunctions) {
1473     new JSTE.List (JSTE.onLoadFunctions).forEach (function (code) {
1474     code ();
1475     });
1476     }
1477    
1478     if (JSTE.isDynamicallyLoaded) {
1479     JSTE.windowLoaded = true;
1480     }
1481 wakaba 1.2
1482     /* ***** BEGIN LICENSE BLOCK *****
1483     * Copyright 2008-2009 Wakaba <[email protected]>. All rights reserved.
1484     *
1485     * This program is free software; you can redistribute it and/or
1486     * modify it under the same terms as Perl itself.
1487     *
1488     * Alternatively, the contents of this file may be used
1489     * under the following terms (the "MPL/GPL/LGPL"),
1490     * in which case the provisions of the MPL/GPL/LGPL are applicable instead
1491     * of those above. If you wish to allow use of your version of this file only
1492     * under the terms of the MPL/GPL/LGPL, and not to allow others to
1493     * use your version of this file under the terms of the Perl, indicate your
1494     * decision by deleting the provisions above and replace them with the notice
1495     * and other provisions required by the MPL/GPL/LGPL. If you do not delete
1496     * the provisions above, a recipient may use your version of this file under
1497     * the terms of any one of the Perl or the MPL/GPL/LGPL.
1498     *
1499     * "MPL/GPL/LGPL":
1500     *
1501     * Version: MPL 1.1/GPL 2.0/LGPL 2.1
1502     *
1503     * The contents of this file are subject to the Mozilla Public License Version
1504     * 1.1 (the "License"); you may not use this file except in compliance with
1505     * the License. You may obtain a copy of the License at
1506     * <http://www.mozilla.org/MPL/>
1507     *
1508     * Software distributed under the License is distributed on an "AS IS" basis,
1509     * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
1510     * for the specific language governing rights and limitations under the
1511     * License.
1512     *
1513     * The Original Code is JSTE code.
1514     *
1515     * The Initial Developer of the Original Code is Wakaba.
1516     * Portions created by the Initial Developer are Copyright (C) 2008
1517     * the Initial Developer. All Rights Reserved.
1518     *
1519     * Contributor(s):
1520     * Wakaba <[email protected]>
1521     *
1522     * Alternatively, the contents of this file may be used under the terms of
1523     * either the GNU General Public License Version 2 or later (the "GPL"), or
1524     * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
1525     * in which case the provisions of the GPL or the LGPL are applicable instead
1526     * of those above. If you wish to allow use of your version of this file only
1527     * under the terms of either the GPL or the LGPL, and not to allow others to
1528     * use your version of this file under the terms of the MPL, indicate your
1529     * decision by deleting the provisions above and replace them with the notice
1530     * and other provisions required by the LGPL or the GPL. If you do not delete
1531     * the provisions above, a recipient may use your version of this file under
1532     * the terms of any one of the MPL, the GPL or the LGPL.
1533     *
1534     * ***** END LICENSE BLOCK ***** */

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24