blob: dae5fbfa8e174647f869b975eab82ec59944cabf [file] [log] [blame]
Nils Diewald58141332015-04-07 16:18:45 +00001/**
2 * Open and close a tutorial page.
3 * The current page is stored and retrieved in a session cookie.
4 */
Nils Diewald58141332015-04-07 16:18:45 +00005// Todo: add query mechanism!
6
Nils Diewald0e6992a2015-04-14 20:13:52 +00007define(['session', 'util'], function (sessionClass) {
Nils Diewald58141332015-04-07 16:18:45 +00008 "use strict";
9
10 // Localization values
Nils Diewald0e6992a2015-04-14 20:13:52 +000011 var loc = KorAP.Locale;
Nils Diewald58141332015-04-07 16:18:45 +000012 loc.CLOSE = loc.CLOSE || 'Close';
13
Nils Diewald0e6992a2015-04-14 20:13:52 +000014 return {
Nils Diewald58141332015-04-07 16:18:45 +000015 /**
16 * Create new tutorial object.
17 * Accepts an element to bind the tutorial window to.
18 */
19 create : function (obj) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000020 if (!obj)
21 return null;
22 return Object.create(this)._init(obj);
Nils Diewald58141332015-04-07 16:18:45 +000023 },
24
25 // Initialize Tutorial object
26 _init : function (obj) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000027
28 this._session = sessionClass.create();
Nils Diewald58141332015-04-07 16:18:45 +000029 this._show = obj;
30 this.start = obj.getAttribute('href');
31 obj.removeAttribute('href');
32 var that = this;
33 obj.onclick = function () {
34 that.show();
35 };
36
37 // Injects a tutorial div to the body
38 var div = document.createElement('div');
39 div.setAttribute('id', 'tutorial');
40 div.style.display = 'none';
41 document.getElementsByTagName('body')[0].appendChild(div);
42 this._iframe = null;
43
44 this._element = div;
45 return this;
46 },
47
48 show : function () {
49 var element = this._element;
50 if (element.style.display === 'block')
51 return;
52
53 if (this._iframe === null) {
54 this._iframe = document.createElement('iframe');
55 this._iframe.setAttribute('src', this.getPage() || this.start);
56
57 var ul = document.createElement('ul');
58 ul.classList.add('action', 'right');
59
Nils Diewald58141332015-04-07 16:18:45 +000060 // Add close button
61 var close = document.createElement('li');
62 close.appendChild(document.createElement('span'))
63 .appendChild(document.createTextNode(loc.CLOSE));
64 close.classList.add('close');
65 close.setAttribute('title', loc.CLOSE);
66 close.onclick = function () {
67 element.style.display = 'none';
68 };
69
70 // Add open in new window button
71 // Add scroll to top button
72 /*
73 var info = document.createElement('li');
74 info.appendChild(document.createElement('span'))
75 .appendChild(document.createTextNode(loc.SHOWINFO));
76 info.classList.add('info');
77 info.setAttribute('title', loc.SHOWINFO);
78 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000079
Nils Diewald58141332015-04-07 16:18:45 +000080 ul.appendChild(close);
81
82 element.appendChild(ul);
83 element.appendChild(this._iframe);
84 };
85
86 element.style.display = 'block';
87 },
88
89 /**
90 * Close tutorial window.
91 */
92 hide : function () {
93 this._element.display.style = 'none';
94 },
95
96 /**
97 * Set a page to be the current tutorial page.
98 * Expects either a string or an element.
99 */
100 setPage : function (obj) {
101 var page = obj;
102 if (typeof page != 'string') {
103 page = window.location.pathname + window.location.search;
104 for (i = 1; i < 5; i++) {
105 if (obj.nodeName === 'SECTION') {
106 if (obj.hasAttribute('id'))
107 page += '#' + obj.getAttribute('id');
108 break;
109 }
110 else if (obj.nodeName === 'PRE' && obj.hasAttribute('id')) {
111 page += '#' + obj.getAttribute('id');
112 break;
113 }
114 else {
115 obj = obj.parentNode;
116 };
117 };
118 };
119 this._session.set('tutpage', page);
120 },
121
122 /**
123 * Get the current tutorial URL
124 */
125 getPage : function () {
126 this._session.get('tutpage');
127 },
Nils Diewald0e6992a2015-04-14 20:13:52 +0000128 };
129});