blob: cacbe2110c4c3fe6f744c065160adf966763d316 [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 Diewaldab4d3ca2015-04-17 01:48:43 +00005// TODO: Add query mechanism!
6// TODO: Highlight current section:
7// http://stackoverflow.com/questions/24887258/highlight-navigation-link-as-i-scroll-down-the-page
Nils Diewald0e6992a2015-04-14 20:13:52 +00008define(['session', 'util'], function (sessionClass) {
Nils Diewald58141332015-04-07 16:18:45 +00009 "use strict";
10
11 // Localization values
Nils Diewald0e6992a2015-04-14 20:13:52 +000012 var loc = KorAP.Locale;
Nils Diewald58141332015-04-07 16:18:45 +000013 loc.CLOSE = loc.CLOSE || 'Close';
14
Nils Diewald0e6992a2015-04-14 20:13:52 +000015 return {
Nils Diewald58141332015-04-07 16:18:45 +000016 /**
17 * Create new tutorial object.
18 * Accepts an element to bind the tutorial window to.
19 */
20 create : function (obj) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000021 if (!obj)
22 return null;
23 return Object.create(this)._init(obj);
Nils Diewald58141332015-04-07 16:18:45 +000024 },
25
26 // Initialize Tutorial object
27 _init : function (obj) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000028
29 this._session = sessionClass.create();
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000030
31 if (obj) {
32 this._show = obj;
33 this.start = obj.getAttribute('href');
34 obj.removeAttribute('href');
35 var that = this;
36 obj.onclick = function () {
37 that.show();
38 };
39
40 // Injects a tutorial div to the body
41 var div = document.createElement('div');
42 div.setAttribute('id', 'tutorial');
43 div.style.display = 'none';
44 document.getElementsByTagName('body')[0].appendChild(div);
45 this._iframe = null;
46 this._element = div;
47
48 // Some fields
49 this._ql = document.getElementById("ql-field");
50 this._q = document.getElementById("q-field")
51 this._cutoff = document.getElementById("q-cutoff-field");
Nils Diewald58141332015-04-07 16:18:45 +000052 };
53
Nils Diewald58141332015-04-07 16:18:45 +000054 return this;
55 },
56
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000057 useQuery : function (e) {
58 var q = e.getAttribute("data-query");
59 var ql = e.getAttribute("data-query-language");
60 var qc = e.getAttribute("data-query-cutoff");
61 if (qc !== 0 && qc !== "0" && qc !== "off" && qc !== null) {
62 this._cutoff.checked = true;
63 };
64
65 var qlf = this._ql.options;
66 for (var i in qlf) {
67 if (qlf[i].value == ql) {
68 qlf[i].selected = true;
69 };
70 };
71
72 this._q.value = q;
73 this.setPage(e);
74 this.hide();
75 },
76
77 initQueries : function (d) {
78 var qs = d.querySelectorAll('pre.query.tutorial');
79 var that = this;
80 for (var i = 0; i < qs.length; i++) {
81 qs[i].onclick = function (e) {
82 that.useQuery(this,e);
83 };
84 };
85 },
86
87
Nils Diewald58141332015-04-07 16:18:45 +000088 show : function () {
89 var element = this._element;
90 if (element.style.display === 'block')
91 return;
92
93 if (this._iframe === null) {
94 this._iframe = document.createElement('iframe');
Nils Diewaldab4d3ca2015-04-17 01:48:43 +000095 this._iframe.setAttribute(
96 'src',
97 (this.getPage() || this.start) + '?embedded=true'
98 );
Nils Diewald58141332015-04-07 16:18:45 +000099
100 var ul = document.createElement('ul');
101 ul.classList.add('action', 'right');
102
Nils Diewald58141332015-04-07 16:18:45 +0000103 // Add close button
104 var close = document.createElement('li');
105 close.appendChild(document.createElement('span'))
106 .appendChild(document.createTextNode(loc.CLOSE));
107 close.classList.add('close');
108 close.setAttribute('title', loc.CLOSE);
109 close.onclick = function () {
110 element.style.display = 'none';
111 };
112
113 // Add open in new window button
114 // Add scroll to top button
115 /*
116 var info = document.createElement('li');
117 info.appendChild(document.createElement('span'))
118 .appendChild(document.createTextNode(loc.SHOWINFO));
119 info.classList.add('info');
120 info.setAttribute('title', loc.SHOWINFO);
121 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000122
Nils Diewald58141332015-04-07 16:18:45 +0000123 ul.appendChild(close);
124
125 element.appendChild(ul);
126 element.appendChild(this._iframe);
127 };
128
129 element.style.display = 'block';
130 },
131
132 /**
133 * Close tutorial window.
134 */
135 hide : function () {
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000136 this._element.style.display = 'none';
Nils Diewald58141332015-04-07 16:18:45 +0000137 },
138
139 /**
140 * Set a page to be the current tutorial page.
141 * Expects either a string or an element.
142 */
143 setPage : function (obj) {
144 var page = obj;
145 if (typeof page != 'string') {
146 page = window.location.pathname + window.location.search;
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000147 for (var i = 1; i < 5; i++) {
Nils Diewald58141332015-04-07 16:18:45 +0000148 if (obj.nodeName === 'SECTION') {
149 if (obj.hasAttribute('id'))
150 page += '#' + obj.getAttribute('id');
151 break;
152 }
153 else if (obj.nodeName === 'PRE' && obj.hasAttribute('id')) {
154 page += '#' + obj.getAttribute('id');
155 break;
156 }
157 else {
158 obj = obj.parentNode;
159 };
160 };
161 };
162 this._session.set('tutpage', page);
163 },
164
165 /**
166 * Get the current tutorial URL
167 */
168 getPage : function () {
169 this._session.get('tutpage');
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000170 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000171 };
172});
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000173