blob: 2cea5f34b199443b9ecd010903f2c81a84a8a01e [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 */
Akron7716f012015-07-01 20:38:32 +020020 create : function (obj,session) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000021 if (!obj)
22 return null;
Akron7716f012015-07-01 20:38:32 +020023 return Object.create(this)._init(obj,session);
Nils Diewald58141332015-04-07 16:18:45 +000024 },
25
26 // Initialize Tutorial object
Akron7716f012015-07-01 20:38:32 +020027 _init : function (obj, session) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000028
Akron7716f012015-07-01 20:38:32 +020029 if (session === undefined) {
30 this._session = sessionClass.create();
31 }
32 else {
33 this._session = session;
34 };
35
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000036
37 if (obj) {
38 this._show = obj;
39 this.start = obj.getAttribute('href');
40 obj.removeAttribute('href');
41 var that = this;
42 obj.onclick = function () {
43 that.show();
44 };
45
46 // Injects a tutorial div to the body
47 var div = document.createElement('div');
48 div.setAttribute('id', 'tutorial');
49 div.style.display = 'none';
50 document.getElementsByTagName('body')[0].appendChild(div);
51 this._iframe = null;
52 this._element = div;
53
54 // Some fields
55 this._ql = document.getElementById("ql-field");
56 this._q = document.getElementById("q-field")
57 this._cutoff = document.getElementById("q-cutoff-field");
Nils Diewald58141332015-04-07 16:18:45 +000058 };
59
Nils Diewald58141332015-04-07 16:18:45 +000060 return this;
61 },
62
Nils Diewald7148c6f2015-05-04 15:07:53 +000063
64 /**
65 * Initialize a search with a defined query.
66 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000067 useQuery : function (e) {
68 var q = e.getAttribute("data-query");
69 var ql = e.getAttribute("data-query-language");
70 var qc = e.getAttribute("data-query-cutoff");
71 if (qc !== 0 && qc !== "0" && qc !== "off" && qc !== null) {
72 this._cutoff.checked = true;
73 };
74
75 var qlf = this._ql.options;
76 for (var i in qlf) {
Akron99713ef2017-06-28 18:19:28 +020077 if (qlf[i].value == ql) {
78 qlf[i].selected = true;
79 break;
80 };
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000081 };
82
83 this._q.value = q;
84 this.setPage(e);
85 this.hide();
86 },
87
Nils Diewald7148c6f2015-05-04 15:07:53 +000088 /**
89 * Decorate a page with query event handler.
90 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000091 initQueries : function (d) {
92 var qs = d.querySelectorAll('pre.query.tutorial');
93 var that = this;
94 for (var i = 0; i < qs.length; i++) {
95 qs[i].onclick = function (e) {
96 that.useQuery(this,e);
97 };
98 };
99 },
100
Nils Diewald61e6ff52015-05-07 17:26:50 +0000101 /**
102 * Decorate a page with documentation links
103 */
104 initDocLinks : function (d) {
105 var dl = d.getElementsByClassName('doc-link');
106 var that = this;
107 for (var i = 0; i < dl.length; i++) {
108 dl[i].onclick = function (e) {
109 that.setPage(this.getAttribute('href'));
110 return true;
111 };
112 };
113 },
114
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000115
Nils Diewald7148c6f2015-05-04 15:07:53 +0000116 /**
117 * Show the tutorial page embedded.
118 */
Nils Diewald58141332015-04-07 16:18:45 +0000119 show : function () {
120 var element = this._element;
121 if (element.style.display === 'block')
Akron99713ef2017-06-28 18:19:28 +0200122 return;
Nils Diewald58141332015-04-07 16:18:45 +0000123
124 if (this._iframe === null) {
Akron99713ef2017-06-28 18:19:28 +0200125 this._iframe = document.createElement('iframe');
126 this._iframe.setAttribute(
127 'src',
128 (this.getPage() || this.start) + '?embedded=true'
129 );
Nils Diewald58141332015-04-07 16:18:45 +0000130
Akron99713ef2017-06-28 18:19:28 +0200131 var ul = document.createElement('ul');
132 ul.classList.add('action', 'right');
Nils Diewald58141332015-04-07 16:18:45 +0000133
Akron99713ef2017-06-28 18:19:28 +0200134 // Add close button
135 var close = document.createElement('li');
136 close.appendChild(document.createElement('span'))
137 .appendChild(document.createTextNode(loc.CLOSE));
138 close.classList.add('close');
139 close.setAttribute('title', loc.CLOSE);
140 close.onclick = function () {
141 element.style.display = 'none';
142 };
Nils Diewald58141332015-04-07 16:18:45 +0000143
Akron99713ef2017-06-28 18:19:28 +0200144 // Add open in new window button
145 // Add scroll to top button
146 /*
147 var info = document.createElement('li');
148 info.appendChild(document.createElement('span'))
149 .appendChild(document.createTextNode(loc.SHOWINFO));
150 info.classList.add('info');
151 info.setAttribute('title', loc.SHOWINFO);
152 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000153
Akron99713ef2017-06-28 18:19:28 +0200154 ul.appendChild(close);
Nils Diewald58141332015-04-07 16:18:45 +0000155
Akron99713ef2017-06-28 18:19:28 +0200156 element.appendChild(ul);
157 element.appendChild(this._iframe);
Nils Diewald58141332015-04-07 16:18:45 +0000158 };
159
160 element.style.display = 'block';
161 },
162
163 /**
164 * Close tutorial window.
165 */
166 hide : function () {
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000167 this._element.style.display = 'none';
Nils Diewald58141332015-04-07 16:18:45 +0000168 },
169
170 /**
171 * Set a page to be the current tutorial page.
172 * Expects either a string or an element.
173 */
174 setPage : function (obj) {
175 var page = obj;
176 if (typeof page != 'string') {
Akron792f58b2015-07-08 18:59:36 +0200177 var l = this._iframe !== null ? window.frames[0].location : window.location;
178 page = l.pathname + l.search;
179
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000180 for (var i = 1; i < 5; i++) {
Nils Diewald58141332015-04-07 16:18:45 +0000181 if (obj.nodeName === 'SECTION') {
182 if (obj.hasAttribute('id'))
183 page += '#' + obj.getAttribute('id');
184 break;
185 }
186 else if (obj.nodeName === 'PRE' && obj.hasAttribute('id')) {
187 page += '#' + obj.getAttribute('id');
188 break;
189 }
190 else {
191 obj = obj.parentNode;
192 };
193 };
194 };
195 this._session.set('tutpage', page);
196 },
197
198 /**
199 * Get the current tutorial URL
200 */
201 getPage : function () {
Akron792f58b2015-07-08 18:59:36 +0200202 return this._session.get('tutpage');
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000203 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000204 };
205});
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000206