/[suikacvs]/webroot/www/js/jste/tutorial.js
Suika

Contents of /webroot/www/js/jste/tutorial.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.26 - (hide annotations) (download) (as text)
Tue Feb 3 04:43:10 2009 UTC (16 years, 6 months ago) by wakaba
Branch: MAIN
Changes since 1.25: +21 -14 lines
File MIME type: application/javascript
clear-state attribute is now supported by all command elements

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 wakaba 1.25 getJSON: function (name) {
687     var value = this.get (name);
688     if (value != null) {
689     return JSTE.JSON.parse (value); // XXX: try-catch?
690     } else {
691     return value;
692     }
693     }, // getJSON
694    
695 wakaba 1.23 set: function (name, value) {
696     throw "not implemented";
697     }, // set
698 wakaba 1.25 setJSON: function (name, obj) {
699     this.set (name, JSTE.JSON.stringify (obj));
700     }, // setJSON
701    
702     has: function (name) {
703     return this.get (name) !== undefined;
704     }, // has
705    
706     delete: function (name) {
707     throw "delete not implemented";
708     }, // delete
709    
710     flushGet: function (name) {
711     var v = this.get ('flush-' + name);
712     if (v !== undefined) {
713     this.delete ('flush-' + name);
714     }
715     return v;
716     }, // flushGet
717     flushSet: function (name, value) {
718     this.set ('flush-' + name, value);
719     }, // flushSet
720 wakaba 1.23
721     getNames: function () {
722     throw "not implemented";
723 wakaba 1.24 }, // getNames
724    
725     setPrefix: function (newPrefix) {
726     throw "not implemented";
727     } // setPrefix
728 wakaba 1.22 }); // Storage
729    
730     JSTE.Storage.PageLocal = new JSTE.Subclass (function () {
731 wakaba 1.24 this.keyPrefix = '';
732 wakaba 1.22 }, JSTE.Storage, {
733     get: function (name) {
734 wakaba 1.24 return this['value-' + this.keyPrefix + name];
735 wakaba 1.22 }, // get
736     set: function (name, value) {
737 wakaba 1.24 this['value-' + this.keyPrefix + name] = value;
738 wakaba 1.22 }, // set
739    
740     getNames: function () {
741     var names = new JSTE.List;
742     for (var n in this) {
743 wakaba 1.24 if (n.substring (0, 6 + this.keyPrefix.length) == 'value-' + this.keyPrefix) {
744     names.push (n.substring (6 + this.keyPrefix.length));
745 wakaba 1.22 }
746     }
747     return names;
748 wakaba 1.24 }, // getNames
749    
750     setPrefix: function (newPrefix) {
751     this.keyPrefix = newPrefix;
752     } // setPrefix
753 wakaba 1.23 }); // PageLocal
754    
755     JSTE.Storage.Cookie = JSTE.Subclass (function () {
756     this.keyPrefix = '';
757     this.domain = null;
758     this.path = '/';
759     this.persistent = false;
760     this.expires = null; // or Date
761     }, JSTE.Storage, {
762     _parse: function () {
763     return new JSTE.List (document.cookie.split (/;/)).mapToHash (function (nv) {
764     nv = nv.replace (/^\s+/, '').replace (/\s+$/, '').split (/=/, 2);
765     nv[0] = decodeURIComponent (nv[0]);
766     nv[1] = decodeURIComponent (nv[1]);
767     return nv;
768     });
769     }, // _parse
770    
771     get: function (name) {
772     return this._parse ().getNamedItem (this.keyPrefix + name);
773     }, // get
774     set: function (name, value) {
775     name = this.keyPrefix + name;
776     var r = encodeURIComponent (name) + '=' + encodeURIComponent (value);
777     if (this.domain) {
778     r += '; domain=' + this.domain;
779     }
780     if (this.path) {
781     r += '; path=' + this.path;
782     }
783     if (this.persistent) {
784     r += '; expires=' + new Date (2030, 1-1, 1).toUTCString ();
785     } else if (this.expires) {
786     r += '; expires=' + this.expires.toUTCString ();
787     }
788     document.cookie = r;
789     }, // set
790     delete: function (name) {
791     var expires = this.expires;
792     var persistent = this.persistent;
793     this.expires = new Date (0);
794     this.persistent = false;
795     this.set (name, '');
796     this.expires = expires;
797     this.persistent = persistent;
798     }, // delete
799    
800     getNames: function () {
801     var self = this;
802     return this._parse ().getNames ().grep (function (name) {
803     return name.substring (0, self.keyPrefix.length) == self.keyPrefix;
804     }).map (function (name) {
805     return name.substring (self.keyPrefix.length);
806     });
807 wakaba 1.24 }, // getNames
808    
809     setPrefix: function (newPrefix) {
810     this.keyPrefix = newPrefix;
811     } // setPrefix
812 wakaba 1.23 }); // Cookie
813    
814     JSTE.Storage.Local = JSTE.Class (function () {
815     var self = new JSTE.Storage.Cookie;
816     self.keyPrefix = 'localStorage-';
817     self.persistent = true;
818 wakaba 1.24 self.setPrefix = function (newPrefix) {
819     this.keyPrefix = 'localStorage-' + newPrefix;
820     }; // setPrefix
821 wakaba 1.23 return self;
822     }); // Local
823 wakaba 1.22
824 wakaba 1.25 JSTE.JSON = {};
825    
826     JSTE.Class.addClassMethods (JSTE.JSON, {
827     parse: function (value) {
828     if (self.JSON && JSON.parse) {
829     return JSON.parse (value); // json2.js or ES3.1
830     } else {
831     return eval ('(' + value + ')');
832     }
833     }, // parse
834    
835     stringify: function (obj) {
836     if (self.JSON && JSON.stringify) {
837     return JSON.stringify (obj); // json2.js or ES3.1
838     } else {
839     throw "JSTE.JSON.stringify not implemented";
840     }
841     } // serialize
842     }); // JSON class methods
843    
844    
845 wakaba 1.9
846 wakaba 1.1 /* Events: load, close, shown, hidden */
847 wakaba 1.18 JSTE.Message = new JSTE.Class (function (doc, template, commandTarget, availCommands) {
848 wakaba 1.1 if (!doc) return;
849     this._targetDocument = doc;
850     this._template = template || doc.createDocumentFragment ();
851 wakaba 1.8
852 wakaba 1.1 this._commandTarget = commandTarget;
853 wakaba 1.18 this._availCommands = availCommands || new JSTE.List;
854 wakaba 1.8
855 wakaba 1.1 this.hidden = true;
856     this.select = "";
857    
858     var e = new JSTE.Event ('load');
859     this.dispatchEvent (e);
860     }, {
861     render: function () {
862     var self = this;
863     var doc = this._targetDocument;
864    
865     var msgContainer = doc.createElement ('section');
866     msgContainer.appendChild (this._template);
867 wakaba 1.20
868     if (!this._availCommands.list.length) {
869 wakaba 1.8 this._availCommands.push ({name: 'back'});
870     this._availCommands.push ({name: 'next'});
871 wakaba 1.7 }
872 wakaba 1.20
873     this._availCommands = this._availCommands.grep(function (item) {
874     return self._commandTarget.canExecuteCommand (item.name, item.args);
875     });
876 wakaba 1.1
877 wakaba 1.8 this._outermostElement = this._render (msgContainer);
878 wakaba 1.1
879     this.show ();
880     }, // render
881     _render: function (msgContainer, buttonContainer) {
882     var doc = this._targetDocument;
883    
884     var container = doc.createElement ('article');
885    
886     container.appendChild (msgContainer);
887 wakaba 1.8
888     var buttonContainer = this.createCommandButtons ();
889 wakaba 1.1 container.appendChild (buttonContainer);
890 wakaba 1.8
891 wakaba 1.1 doc.documentElement.appendChild (container);
892    
893     return container;
894     }, // _render
895 wakaba 1.8 createCommandButtons: function () {
896     var self = this;
897 wakaba 1.18 var doc = this._targetDocument;
898     var buttonContainer = doc.createElement ('menu');
899 wakaba 1.8 this._availCommands.forEach (function (cmd) {
900 wakaba 1.18 var label = cmd.name;
901     if (cmd.labelTemplate) {
902     label = JSTE.Element.createTemplate (doc, cmd.labelTemplate);
903     }
904    
905 wakaba 1.8 var button = new JSTE.Message.Button
906 wakaba 1.26 (label, self._commandTarget, cmd.name, cmd.args, cmd.actions);
907 wakaba 1.8 buttonContainer.appendChild (button.element);
908     });
909     return buttonContainer;
910     }, // createCommandButtons
911    
912 wakaba 1.1 remove: function () {
913     this.hide ();
914    
915     this._remove ();
916    
917     if (this._outermostElement && this._outermostElement.parentNode) {
918     this._outermostElement.parentNode.removeChild (this._outermostElement);
919     }
920    
921     var e = new JSTE.Event ("close");
922     this.dispatchEvent (e);
923     }, // remove
924     _remove: function () {
925    
926     }, // remove
927    
928     show: function () {
929     if (!this.hidden) return;
930     this.hidden = false;
931     if (this._outermostElement) {
932     JSTE.Element.replaceClassName
933     (this._outermostElement, "jste-hidden", "jste-shown");
934     }
935    
936     var e = new JSTE.Event ("shown");
937     this.dispatchEvent (e);
938     }, // show
939     hide: function () {
940     if (this.hidden) return;
941     this.hidden = true;
942     if (this._outermostElement) {
943     JSTE.Element.replaceClassName
944     (this._outermostElement, "jste-shown", "jste-hidden");
945     }
946    
947     var e = new JSTE.Event ("hidden");
948     this.dispatchEvent (e);
949     }, // hide
950    
951     setTimeout: function () {
952     /* TODO: ... */
953    
954     }
955    
956     }); // Message
957    
958 wakaba 1.7 /* TODO: button label text should refer message catalog */
959    
960 wakaba 1.1 JSTE.Message.Button =
961 wakaba 1.26 new JSTE.Class (function (label, commandTarget, commandName, commandArgs, commandActions) {
962 wakaba 1.18 this._label = label != null ? label : "";
963 wakaba 1.6
964 wakaba 1.1 if (commandTarget && commandTarget instanceof Function) {
965     this._command = commandTarget;
966 wakaba 1.6 this._classNames = new JSTE.List;
967 wakaba 1.1 } else if (commandTarget) {
968     this._command = function () {
969     return commandTarget.executeCommand.apply
970 wakaba 1.26 (commandTarget, [commandName, commandArgs, commandActions]);
971 wakaba 1.1 };
972 wakaba 1.6 this._classNames = new JSTE.List (['jste-command-' + commandName]);
973 wakaba 1.1 } else {
974     this._command = function () { };
975 wakaba 1.6 this._classNames = new JSTE.List;
976 wakaba 1.1 }
977    
978 wakaba 1.6 this._createElement ();
979 wakaba 1.1 }, {
980 wakaba 1.6 _createElement: function () {
981     try {
982     this.element = document.createElement ('button');
983     this.element.setAttribute ('type', 'button');
984     } catch (e) {
985     this.element = document.createElement ('<button type=button>');
986     }
987 wakaba 1.18 if (this._label.nodeType) {
988     this.element.appendChild (this._label);
989     } else {
990     JSTE.Element.appendText (this.element, this._label);
991     }
992 wakaba 1.6 this.element.className = this._classNames.list.join (' ');
993 wakaba 1.1
994 wakaba 1.6 var self = this;
995     new JSTE.Observer ("click", this.element, function (e) {
996     self._command (e);
997     });
998     } // _createElement
999 wakaba 1.1 }); // Button
1000    
1001     JSTE.Course = new JSTE.Class (function (doc) {
1002     this._targetDocument = doc;
1003    
1004 wakaba 1.22 this._entryPointsByStateName = new JSTE.Hash;
1005 wakaba 1.23 this._entryPointsByStateName.setNamedItem ('done', 'special-none');
1006 wakaba 1.22
1007 wakaba 1.1 this._entryPointsByURL = {};
1008     this._entryPointsById = {};
1009     this._entryPointsByClassName = {};
1010    
1011     this._stepsState = new JSTE.List ([new JSTE.Hash]);
1012     this._steps = new JSTE.Hash;
1013    
1014     var nullState = new JSTE.Step;
1015     nullState.uid = "";
1016     this._steps.setNamedItem (nullState.uid, nullState);
1017     this._initialStepUid = nullState.uid;
1018     }, {
1019 wakaba 1.10 _processStepsContent: function (el, parentSteps) {
1020 wakaba 1.1 var self = this;
1021     new JSTE.List (el.childNodes).switchByElementType (
1022     new JSTE.List.SwitchByLocalName (JSTE.WATNS, {
1023 wakaba 1.10 steps: function (n) { self._processStepsElement (n, parentSteps) },
1024     step: function (n) { self._processStepElement (n, parentSteps) },
1025     jump: function (n) { self._processJumpElement (n, parentSteps) },
1026 wakaba 1.14 entryPoint: function (n) { self._processEntryPointElement (n, parentSteps) }
1027 wakaba 1.1 })
1028     );
1029     }, // _processStepsContent
1030 wakaba 1.10 _processStepsElement: function (e, parentSteps) {
1031     var steps = new JSTE.Steps ();
1032     steps.parentSteps = parentSteps;
1033 wakaba 1.1 this._stepsState.pushCloneOfLast ();
1034     this._stepsState.getLast ().prevStep = null;
1035 wakaba 1.10 this._processStepsContent (e, steps);
1036 wakaba 1.1 this._stepsState.pop ();
1037     }, // _processStepsElement
1038    
1039 wakaba 1.14 _processEntryPointElement: function (e, parentSteps) {
1040 wakaba 1.22 if (JSTE.Element.hasAttribute (e, 'state')) {
1041     this.setEntryPointByStateName
1042     (e.getAttribute ('state'), e.getAttribute ('step'));
1043     } else if (JSTE.Element.hasAttribute (e, 'url')) {
1044 wakaba 1.1 this.setEntryPointByURL
1045     (e.getAttribute ('url'), e.getAttribute ('step'));
1046     } else if (JSTE.Element.hasAttribute (e, 'root-id')) {
1047     this.setEntryPointById
1048     (e.getAttribute ('root-id'), e.getAttribute ('step'));
1049     } else if (JSTE.Element.hasAttribute (e, 'root-class')) {
1050     this.setEntryPointByClassName
1051     (e.getAttribute ('root-class'), e.getAttribute ('step'));
1052     }
1053 wakaba 1.14 }, // _processEntryPointElement
1054 wakaba 1.22 setEntryPointByStateName: function (stateName, stepName) {
1055     this._entryPointsByStateName.setNamedItem (stateName, stepName || '');
1056     }, // setEntryPointByStateName
1057 wakaba 1.1 setEntryPointByURL: function (url, stepName) {
1058 wakaba 1.24 // TODO: HTML5 URL->URI convertion
1059     this._entryPointsByURL[encodeURI (url)] = stepName || '';
1060 wakaba 1.1 }, // setEntryPointByURL
1061     setEntryPointById: function (id, stepName) {
1062     this._entryPointsById[id] = stepName || '';
1063     }, // setEntryPointById
1064     setEntryPointByClassName: function (className, stepName) {
1065     this._entryPointsByClassName[className] = stepName || '';
1066     }, // setEntryPointByClassName
1067 wakaba 1.22 findEntryPoint: function (doc, states) {
1068 wakaba 1.1 var self = this;
1069     var td = this._targetDocument;
1070     var stepName;
1071 wakaba 1.22
1072     if (states) {
1073     stepName = self._entryPointsByStateName.getByNames (states.getNames ());
1074     if (stepName) return stepName;
1075     }
1076 wakaba 1.1
1077     var url = doc.URL;
1078     if (url) {
1079     stepName = self._entryPointsByURL[url];
1080     if (stepName) return 'id-' + stepName;
1081     }
1082 wakaba 1.24 // TODO: multiple elements with same ID
1083     // TODO: interpage "back" button
1084     // TODO: prefetch
1085 wakaba 1.1
1086     var docEl = td.documentElement;
1087     if (docEl) {
1088     var docElId = JSTE.Element.getIds (docEl).forEach (function (i) {
1089     stepName = self._entryPointsById[i];
1090     if (stepName) return new JSTE.List.Return (stepName);
1091     });
1092     if (stepName) return 'id-' + stepName;
1093    
1094     stepName = JSTE.Element.getClassNames (docEl).forEach (function (c) {
1095     stepName = self._entryPointsByClassName[c];
1096     if (stepName) return new JSTE.List.Return (stepName);
1097     });
1098     if (stepName) return 'id-' + stepName;
1099     }
1100    
1101     var bodyEl = td.body;
1102     if (bodyEl) {
1103     var bodyElId = JSTE.Element.getIds (bodyEl).forEach (function (i) {
1104     stepName = self._entryPointsById[i];
1105     if (stepName) return new JSTE.List.Return (stepName);
1106     });
1107     if (stepName) return 'id-' + stepName;
1108    
1109     stepName = JSTE.Element.getClassNames (bodyEl).forEach (function (c) {
1110     stepName = self._entryPointsByClassName[c];
1111     if (stepName) return new JSTE.List.Return (stepName);
1112     });
1113     if (stepName) return 'id-' + stepName;
1114     }
1115    
1116     return this._initialStepUid;
1117     }, // findEntryPoint
1118    
1119 wakaba 1.10 _processStepElement: function (e, parentSteps) {
1120 wakaba 1.18 var self = this;
1121    
1122 wakaba 1.1 var step = new JSTE.Step (e.getAttribute ('id'));
1123 wakaba 1.10 step.parentSteps = parentSteps;
1124 wakaba 1.1 step.setPreviousStep (this._stepsState.getLast ().prevStep);
1125     step.select = e.getAttribute ('select') || "";
1126     step.nextEvents.append
1127 wakaba 1.10 (JSTE.List.spaceSeparated (e.getAttribute ('next-event')));
1128 wakaba 1.17
1129     var cs = JSTE.Element.getChildrenClassifiedByType (e);
1130    
1131     var msgEl = cs.get (JSTE.WATNS, 'message').list[0];
1132 wakaba 1.1 if (msgEl) {
1133     var msg = JSTE.Element.createTemplate (this._targetDocument, msgEl);
1134     step.setMessageTemplate (msg);
1135     }
1136 wakaba 1.17
1137     var nextEls = cs.get (JSTE.WATNS, 'next-step');
1138 wakaba 1.16 if (nextEls.list.length) {
1139 wakaba 1.1 nextEls.forEach (function (nextEl) {
1140     step.addNextStep
1141     (nextEl.getAttribute ('if'), nextEl.getAttribute ('step'));
1142     });
1143     this._stepsState.getLast ().prevStep = null;
1144     } else {
1145     this._stepsState.getLast ().prevStep = step;
1146     }
1147 wakaba 1.17
1148 wakaba 1.19 cs.get (JSTE.WATNS, 'command').forEach (function (bEl) {
1149 wakaba 1.18 var cmd = {
1150 wakaba 1.20 name: bEl.getAttribute ('type') || 'gotoStep'
1151 wakaba 1.18 };
1152     if (cmd.name == 'gotoStep') {
1153 wakaba 1.25 cmd.args = {stepUid: 'id-' + bEl.getAttribute ('step')};
1154 wakaba 1.24 } else if (cmd.name == 'url') {
1155 wakaba 1.26 cmd.args = {url: bEl.getAttribute ('href')};
1156 wakaba 1.18 }
1157 wakaba 1.26 cmd.actions = {
1158     clearStateNames: JSTE.List.spaceSeparated (bEl.getAttribute ('clear-state'))
1159     };
1160 wakaba 1.18 if (!JSTE.Element.isEmpty (bEl)) {
1161     cmd.labelTemplate = JSTE.Element.createTemplate (self._targetDocument, bEl);
1162     }
1163     step.availCommands.push (cmd);
1164 wakaba 1.22 }); // wat:command
1165 wakaba 1.18
1166 wakaba 1.22 cs.get (JSTE.WATNS, 'save-state').forEach (function (bEl) {
1167     var ss = new JSTE.SaveState
1168     (bEl.getAttribute ('name'), bEl.getAttribute ('value'));
1169     step.saveStates.push (ss);
1170     }); // wat:save-state
1171 wakaba 1.13
1172     var evs = JSTE.List.spaceSeparated (e.getAttribute ('entry-event'));
1173     if (evs.list.length) {
1174     var jump = new JSTE.Jump (step.select, evs, step.uid);
1175     if (parentSteps) parentSteps._jumps.push (jump);
1176     }
1177 wakaba 1.1
1178     this._steps.setNamedItem (step.uid, step);
1179     if (!this._initialStepUid) {
1180     this._initialStepUid = step.uid;
1181     }
1182     }, // _processStepElement
1183    
1184 wakaba 1.10 _processJumpElement: function (e, parentSteps) {
1185     var target = e.getAttribute ('target') || '';
1186     var evs = JSTE.List.spaceSeparated (e.getAttribute ('event'));
1187     var stepName = e.getAttribute ('step') || '';
1188    
1189     var jump = new JSTE.Jump (target, evs, 'id-' + stepName);
1190     if (parentSteps) parentSteps._jumps.push (jump);
1191 wakaba 1.1 }, // _processJumpElement
1192    
1193     getStep: function (uid) {
1194     return this._steps.getNamedItem (uid);
1195     } // getStep
1196     }); // Course
1197    
1198 wakaba 1.9 JSTE.Class.addClassMethods (JSTE.Course, {
1199     createFromDocument: function (doc, targetDoc) {
1200     var course = new JSTE.Course (targetDoc);
1201     var docEl = doc.documentElement;
1202     if (!docEl) return course;
1203     if (!JSTE.Element.match (docEl, JSTE.WATNS, 'course')) return course;
1204 wakaba 1.10 course._processStepsContent (docEl, null);
1205 wakaba 1.24 course.name = docEl.hasAttribute ('name') ? docEl.getAttribute ('name') + '-' : '';
1206 wakaba 1.9 return course;
1207     }, // createFromDocument
1208     createFromURL: function (url, targetDoc, onload, onerror) {
1209     new JSTE.XHR (url, function () {
1210     var course = JSTE.Course.createFromDocument
1211     (this.getDocument (), targetDoc);
1212     if (onload) onload (course);
1213     }, onerror).get ();
1214     } // creatFromURL
1215     }); // Course class methods
1216 wakaba 1.1
1217 wakaba 1.10 JSTE.Jump = new JSTE.Class (function (selectors, eventNames, stepUid) {
1218     this.selectors = selectors;
1219     this.eventNames = eventNames;
1220     this.stepUid = stepUid;
1221     // this.parentSteps
1222     }, {
1223     startObserver: function (doc, commandTarget) {
1224     var self = this;
1225     var observers = new JSTE.List;
1226    
1227     var onev = function () {
1228 wakaba 1.25 commandTarget.gotoStep ({stepUid: self.stepUid});
1229 wakaba 1.10 };
1230    
1231     JSTE.Node.querySelectorAll (doc, this.selectors).forEach
1232     (function (el) {
1233     self.eventNames.forEach (function (evName) {
1234     var ob = new JSTE.Observer (evName, el, onev);
1235     ob._stepUid = self.stepUid;
1236     observers.push (ob);
1237     });
1238     });
1239    
1240     return observers;
1241     } // startObserver
1242     }); // Jump
1243    
1244     JSTE.Steps = new JSTE.Class (function () {
1245     this._jumps = new JSTE.List;
1246     this._jumpHandlers = new JSTE.List;
1247     }, {
1248     setCurrentStepByUid: function (uid) {
1249     this._jumpHandlers.forEach (function (jh) {
1250     if (jh._stepUid != uid && jh.disabled) {
1251     jh.start ();
1252     } else if (jh._stepUid == uid && !jh.disabled) {
1253     jh.stop ();
1254     }
1255     });
1256     }, // setCurrentStepByUid
1257    
1258     installJumps: function (doc, commandTarget) {
1259     if (this._jumpHandlers.list.length) return;
1260     var self = this;
1261     this._jumps.forEach (function (j) {
1262     self._jumpHandlers.append (j.startObserver (doc, commandTarget));
1263     });
1264     }, // installJumps
1265    
1266     uninstallJumps: function () {
1267     this._jumpHandlers.forEach (function (jh) {
1268     jh.stop ();
1269     });
1270     this._jumpHandlers.clear ();
1271     } // uninstallJumps
1272     }); // Steps
1273    
1274 wakaba 1.1 JSTE.Step = new JSTE.Class (function (id) {
1275     if (id != null && id != '') {
1276     this.uid = 'id-' + id;
1277     } else {
1278     this.uid = 'rand-' + Math.random ();
1279     }
1280     this._nextSteps = new JSTE.List;
1281     this.nextEvents = new JSTE.List;
1282 wakaba 1.18 this.availCommands = new JSTE.List;
1283 wakaba 1.22 this.saveStates = new JSTE.List;
1284 wakaba 1.1 this.select = "";
1285     }, {
1286     setMessageTemplate: function (msg) {
1287     this._messageTemplate = msg;
1288     }, // setMessageTemplate
1289     hasMessage: function () {
1290     return this._messageTemplate ? true : false;
1291     }, // hasMessage
1292     createMessage: function (msg, doc, commandTarget) {
1293     var msg;
1294     if (this._messageTemplate) {
1295     var clone = JSTE.Element.createTemplate (doc, this._messageTemplate);
1296 wakaba 1.18 msg = new msg (doc, clone, commandTarget, this.availCommands.clone ());
1297 wakaba 1.1 } else {
1298     msg = new msg (doc, null, commandTarget);
1299     }
1300     msg.select = this.select;
1301     return msg;
1302     }, // createMessage
1303    
1304     addNextStep: function (condition, stepId) {
1305 wakaba 1.16 if (stepId != null) this._nextSteps.push ([condition, stepId]);
1306 wakaba 1.1 }, // addNextStep
1307     setPreviousStep: function (prevStep) {
1308     if (!prevStep) return;
1309     if (prevStep._defaultNextStepUid) return;
1310     prevStep._defaultNextStepUid = this.uid;
1311     }, // setPreviousStep
1312    
1313     getNextStepUid: function (doc) {
1314     var m = this._nextSteps.getFirstMatch (function (item) {
1315     var condition = item[0];
1316     if (condition) {
1317     return JSTE.Node.querySelector (doc, condition) != null;
1318     } else {
1319     return true;
1320     }
1321     });
1322     if (m) {
1323     return 'id-' + m[1];
1324     } else if (this._defaultNextStepUid) {
1325     return this._defaultNextStepUid;
1326     } else {
1327     return null;
1328     }
1329 wakaba 1.10 }, // getNextStepUid
1330    
1331     getAncestorStepsObjects: function () {
1332     var steps = new JSTE.List;
1333     var s = this.parentSteps;
1334     while (s != null) {
1335     steps.push (s);
1336     s = s.parentSteps;
1337     }
1338     return steps;
1339     } // getAncestorStepsObjects
1340 wakaba 1.1 }); // Step
1341    
1342 wakaba 1.22 JSTE.SaveState = new JSTE.Class (function (name, value) {
1343     this.name = name || '';
1344     this.value = value || '';
1345     }, {
1346 wakaba 1.25 save: function (tutorial) {
1347     var name = this.name;
1348     var value = this.value;
1349     if (name == 'back-state') return;
1350     tutorial._states.set (name, value);
1351 wakaba 1.22 } // save
1352     }); // SaveState
1353    
1354     /* Events: load, error, cssomready, close */
1355 wakaba 1.9 JSTE.Tutorial = new JSTE.Class (function (course, doc, args) {
1356 wakaba 1.1 this._course = course;
1357     this._targetDocument = doc;
1358     this._messageClass = JSTE.Message;
1359     if (args) {
1360     if (args.messageClass) this._messageClass = args.messageClass;
1361 wakaba 1.22 if (args.states) this._states = args.states;
1362 wakaba 1.1 }
1363 wakaba 1.22 if (!this._states) this._states = new JSTE.Storage.PageLocal;
1364 wakaba 1.24 this._states.setPrefix (course.name);
1365 wakaba 1.1
1366     this._currentMessages = new JSTE.List;
1367     this._currentObservers = new JSTE.List;
1368 wakaba 1.25 this._currentStepsObjects = new JSTE.List;
1369    
1370 wakaba 1.1 this._prevStepUids = new JSTE.List;
1371 wakaba 1.25 this._loadBackState ();
1372    
1373     var stepUid;
1374     if (this._prevStepUids.list.length) {
1375     stepUid = this._prevStepUids.pop ();
1376     } else {
1377     stepUid = this._course.findEntryPoint (document, this._states);
1378     }
1379    
1380 wakaba 1.1 this._currentStep = this._getStepOrError (stepUid);
1381     if (this._currentStep) {
1382     var e = new JSTE.Event ('load');
1383     this.dispatchEvent (e);
1384 wakaba 1.25
1385     this._saveBackState ();
1386    
1387 wakaba 1.3 var self = this;
1388     new JSTE.Observer ('cssomready', this, function () {
1389     self._renderCurrentStep ();
1390     });
1391     this._dispatchCSSOMReadyEvent ();
1392 wakaba 1.1 return this;
1393     } else {
1394     return {};
1395     }
1396     }, {
1397     _getStepOrError: function (stepUid) {
1398     var step = this._course.getStep (stepUid);
1399     if (step) {
1400     return step;
1401 wakaba 1.22 } else if (stepUid == 'special-none') {
1402     return null;
1403 wakaba 1.1 } else {
1404     var e = new JSTE.Event ('error');
1405     e.errorMessage = 'Step not found';
1406     e.errorArguments = [this._currentStepUid];
1407     this.dispatchEvent (e);
1408     return null;
1409     }
1410     }, // _getStepOrError
1411    
1412     _renderCurrentStep: function () {
1413     var self = this;
1414     var step = this._currentStep;
1415 wakaba 1.22
1416 wakaba 1.25 step.saveStates.forEach (function (ss) { ss.save (self) });
1417 wakaba 1.1
1418     /* Message */
1419     var msg = step.createMessage
1420     (this._messageClass, this._targetDocument, this);
1421     msg.render ();
1422     this._currentMessages.push (msg);
1423    
1424     /* Next-events */
1425     var selectedNodes = JSTE.Node.querySelectorAll
1426     (this._targetDocument, step.select);
1427     var handler = function () {
1428     self.executeCommand ("next");
1429     };
1430     selectedNodes.forEach (function (node) {
1431     step.nextEvents.forEach (function (eventType) {
1432     self._currentObservers.push
1433     (new JSTE.Observer (eventType, node, handler));
1434     });
1435     });
1436 wakaba 1.10
1437     JSTE.List.getCommonItems (this._currentStepsObjects,
1438     step.getAncestorStepsObjects (),
1439     function (common, onlyInOld, onlyInNew) {
1440     common.forEach (function (item) {
1441     item.setCurrentStepByUid (step.uid);
1442     });
1443     onlyInOld.forEach (function (item) {
1444     item.uninstallJumps ();
1445     });
1446     onlyInNew.forEach (function (item) {
1447     item.installJumps (self._targetDocument, self);
1448     });
1449     self._currentStepsObjects = common.append (onlyInNew);
1450     });
1451 wakaba 1.1 }, // _renderCurrentStep
1452     clearMessages: function () {
1453     this._currentMessages.forEach (function (msg) {
1454     msg.remove ();
1455     });
1456     this._currentMessages.clear ();
1457    
1458     this._currentObservers.forEach (function (ob) {
1459     ob.stop ();
1460     });
1461     this._currentObservers.clear ();
1462     }, // clearMessages
1463 wakaba 1.10 clearStepsHandlers: function () {
1464     this._currentStepsObjects.forEach (function (item) {
1465     item.uninstallJumps ();
1466     });
1467     this._currentStepsObjects.clear ();
1468     }, // clearStepsHandlers
1469 wakaba 1.1
1470 wakaba 1.26 executeCommand: function (commandName, commandArgs, commandActions) {
1471 wakaba 1.1 if (this[commandName]) {
1472 wakaba 1.26 // Common actions
1473     if (commandActions && commandActions.clearStateNames) {
1474     var self = this;
1475     commandActions.clearStateNames.forEach (function (stateName) {
1476     self._states.delete (stateName);
1477     });
1478     }
1479    
1480 wakaba 1.25 return this[commandName].apply (this, [commandArgs || {}]);
1481 wakaba 1.1 } else {
1482     var e = new JSTE.Event ('error');
1483     e.errorMessage = 'Command not found';
1484     e.errorArguments = [commandName];
1485     return null;
1486     }
1487     }, // executeCommand
1488 wakaba 1.7 canExecuteCommand: function (commandName, commandArgs) {
1489     if (this[commandName]) {
1490     var can = this['can' + commandName.substring (0, 1).toUpperCase ()
1491     + commandName.substring (1)];
1492     if (can) {
1493     return can.apply (this, arguments);
1494     } else {
1495     return true;
1496     }
1497     } else {
1498     return false;
1499     }
1500     }, // canExecuteCommand
1501 wakaba 1.1
1502     back: function () {
1503 wakaba 1.25 while (this._prevStepUids.list.length == 0 &&
1504     this._prevPages.list.length > 0) {
1505     var prevPage = this._prevPages.pop ();
1506     if (prevPage.url != location.href) {
1507     this._saveBackState (true);
1508 wakaba 1.26 if (document.referrer == prevPage.url) {
1509     history.back ();
1510     } else {
1511     location.href = prevPage.url;
1512     }
1513 wakaba 1.25 // TODO: maybe we should not return if locaton.href and prevPage.,url only differs their fragment ids?
1514     return;
1515     }
1516     this._prevStepUids = prevPage;
1517     }
1518    
1519 wakaba 1.1 var prevStepUid = this._prevStepUids.pop ();
1520     var prevStep = this._getStepOrError (prevStepUid);
1521     if (prevStep) {
1522     this.clearMessages ();
1523 wakaba 1.25 this._saveBackState ();
1524 wakaba 1.1 this._currentStep = prevStep;
1525     this._renderCurrentStep ();
1526     }
1527     }, // back
1528 wakaba 1.7 canBack: function () {
1529 wakaba 1.25 return this._prevStepUids.list.length > 0 || this._prevPages.list.length > 0;
1530 wakaba 1.7 }, // canBack
1531 wakaba 1.1 next: function () {
1532     var nextStepUid = this._currentStep.getNextStepUid (this._targetDocument);
1533     var nextStep = this._getStepOrError (nextStepUid);
1534     if (nextStep) {
1535     this._prevStepUids.push (this._currentStep.uid);
1536     this.clearMessages ();
1537 wakaba 1.25 this._saveBackState ();
1538 wakaba 1.1 this._currentStep = nextStep;
1539     this._renderCurrentStep ();
1540     }
1541 wakaba 1.3 }, // next
1542 wakaba 1.7 canNext: function () {
1543     return this._currentStep.getNextStepUid (this._targetDocument) != null;
1544     }, // canNext
1545 wakaba 1.25 gotoStep: function (args) {
1546     var nextStep = this._getStepOrError (args.stepUid);
1547 wakaba 1.10 if (nextStep) {
1548     this._prevStepUids.push (this._currentStep.uid);
1549 wakaba 1.25 this._saveBackState ();
1550 wakaba 1.10 this.clearMessages ();
1551     this._currentStep = nextStep;
1552     this._renderCurrentStep ();
1553     }
1554     }, // gotoStep
1555 wakaba 1.24
1556 wakaba 1.25 url: function (args) {
1557     location.href = args.url;
1558 wakaba 1.24 }, // url
1559 wakaba 1.20
1560     close: function () {
1561     this.clearMessages ();
1562 wakaba 1.22 var e = new JSTE.Event ('closed');
1563     this.dispatchEvent (e);
1564 wakaba 1.20 }, // close
1565 wakaba 1.25
1566     _loadBackState: function () {
1567     var self = this;
1568     this._prevPages = new JSTE.List;
1569     var bs = this._states.getJSON ('back-state');
1570     new JSTE.List (bs).forEach (function (b) {
1571     var i = new JSTE.List (b.stepUids);
1572     i.url = b.url;
1573     self._prevPages.push (i);
1574     });
1575     if ((this._prevPages.getLast () || {}).url == location.href) {
1576     this._prevStepUids = this._prevPages.pop ();
1577     }
1578     }, // loadBackState
1579     _saveBackState: function (ignoreCurrentPage) {
1580     var bs = [];
1581     this._prevPages.forEach (function (pp) {
1582     bs.push ({url: pp.url, stepUids: pp.list});
1583     });
1584     if (!ignoreCurrentPage) {
1585     var uids = this._prevStepUids.clone ();
1586     uids.push (this._currentStep.uid);
1587     // Add even if uids.list.length == 0.
1588     bs.push ({url: location.href, stepUids: uids.list});
1589     }
1590     this._states.setJSON ('back-state', bs);
1591     }, // _saveBackState
1592 wakaba 1.3
1593     // <http://twitter.com/waka/status/1129513097>
1594     _dispatchCSSOMReadyEvent: function () {
1595     var self = this;
1596     var e = new JSTE.Event ('cssomready');
1597     if (window.opera && document.readyState != 'complete') {
1598     new JSTE.Observer ('readystatechange', document, function () {
1599     if (document.readyState == 'complete') {
1600     self.dispatchEvent (e);
1601     }
1602     });
1603     } else {
1604     this.dispatchEvent (e);
1605     }
1606     } // dispatchCSSOMReadyEvent
1607    
1608 wakaba 1.1 }); // Tutorial
1609 wakaba 1.9
1610     JSTE.Class.addClassMethods (JSTE.Tutorial, {
1611 wakaba 1.12 createFromURL: function (url, doc, args, onload) {
1612 wakaba 1.9 JSTE.Course.createFromURL (url, doc, function (course) {
1613 wakaba 1.12 var tutorial = new JSTE.Tutorial (course, doc, args);
1614     if (onload) onload (tutorial);
1615 wakaba 1.9 });
1616     } // createFromURL
1617     }); // Tutorial class methods
1618    
1619    
1620 wakaba 1.5
1621     if (JSTE.onLoadFunctions) {
1622     new JSTE.List (JSTE.onLoadFunctions).forEach (function (code) {
1623     code ();
1624     });
1625     }
1626    
1627     if (JSTE.isDynamicallyLoaded) {
1628     JSTE.windowLoaded = true;
1629     }
1630 wakaba 1.2
1631     /* ***** BEGIN LICENSE BLOCK *****
1632     * Copyright 2008-2009 Wakaba <w@suika.fam.cx>. All rights reserved.
1633     *
1634     * This program is free software; you can redistribute it and/or
1635     * modify it under the same terms as Perl itself.
1636     *
1637     * Alternatively, the contents of this file may be used
1638     * under the following terms (the "MPL/GPL/LGPL"),
1639     * in which case the provisions of the MPL/GPL/LGPL are applicable instead
1640     * of those above. If you wish to allow use of your version of this file only
1641     * under the terms of the MPL/GPL/LGPL, and not to allow others to
1642     * use your version of this file under the terms of the Perl, indicate your
1643     * decision by deleting the provisions above and replace them with the notice
1644     * and other provisions required by the MPL/GPL/LGPL. If you do not delete
1645     * the provisions above, a recipient may use your version of this file under
1646     * the terms of any one of the Perl or the MPL/GPL/LGPL.
1647     *
1648     * "MPL/GPL/LGPL":
1649     *
1650     * Version: MPL 1.1/GPL 2.0/LGPL 2.1
1651     *
1652     * The contents of this file are subject to the Mozilla Public License Version
1653     * 1.1 (the "License"); you may not use this file except in compliance with
1654     * the License. You may obtain a copy of the License at
1655     * <http://www.mozilla.org/MPL/>
1656     *
1657     * Software distributed under the License is distributed on an "AS IS" basis,
1658     * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
1659     * for the specific language governing rights and limitations under the
1660     * License.
1661     *
1662     * The Original Code is JSTE code.
1663     *
1664     * The Initial Developer of the Original Code is Wakaba.
1665     * Portions created by the Initial Developer are Copyright (C) 2008
1666     * the Initial Developer. All Rights Reserved.
1667     *
1668     * Contributor(s):
1669     * Wakaba <w@suika.fam.cx>
1670     *
1671     * Alternatively, the contents of this file may be used under the terms of
1672     * either the GNU General Public License Version 2 or later (the "GPL"), or
1673     * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
1674     * in which case the provisions of the GPL or the LGPL are applicable instead
1675     * of those above. If you wish to allow use of your version of this file only
1676     * under the terms of either the GPL or the LGPL, and not to allow others to
1677     * use your version of this file under the terms of the MPL, indicate your
1678     * decision by deleting the provisions above and replace them with the notice
1679     * and other provisions required by the LGPL or the GPL. If you do not delete
1680     * the provisions above, a recipient may use your version of this file under
1681     * the terms of any one of the MPL, the GPL or the LGPL.
1682     *
1683     * ***** END LICENSE BLOCK ***** */

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24