blob: 03cf9036eea9cf964b81d11d2f7fd085973b78df [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 */
5// Requires session.js
6var KorAP = KorAP || {};
7
8// Todo: add query mechanism!
9
10(function (KorAP) {
11 "use strict";
12
13 // Localization values
14 var loc = (KorAP.Locale = KorAP.Locale || {} );
15 loc.CLOSE = loc.CLOSE || 'Close';
16
17 KorAP.Tutorial = {
18
19 /**
20 * Create new tutorial object.
21 * Accepts an element to bind the tutorial window to.
22 */
23 create : function (obj) {
24 return Object.create(KorAP.Tutorial)._init(obj);
25 },
26
27 // Initialize Tutorial object
28 _init : function (obj) {
29 this._session = KorAP.Session.create();
30 this._show = obj;
31 this.start = obj.getAttribute('href');
32 obj.removeAttribute('href');
33 var that = this;
34 obj.onclick = function () {
35 that.show();
36 };
37
38 // Injects a tutorial div to the body
39 var div = document.createElement('div');
40 div.setAttribute('id', 'tutorial');
41 div.style.display = 'none';
42 document.getElementsByTagName('body')[0].appendChild(div);
43 this._iframe = null;
44
45 this._element = div;
46 return this;
47 },
48
49 show : function () {
50 var element = this._element;
51 if (element.style.display === 'block')
52 return;
53
54 if (this._iframe === null) {
55 this._iframe = document.createElement('iframe');
56 this._iframe.setAttribute('src', this.getPage() || this.start);
57
58 var ul = document.createElement('ul');
59 ul.classList.add('action', 'right');
60
61 // Use localization
62 var loc = KorAP.Locale;
63
64 // Add close button
65 var close = document.createElement('li');
66 close.appendChild(document.createElement('span'))
67 .appendChild(document.createTextNode(loc.CLOSE));
68 close.classList.add('close');
69 close.setAttribute('title', loc.CLOSE);
70 close.onclick = function () {
71 element.style.display = 'none';
72 };
73
74 // Add open in new window button
75 // Add scroll to top button
76 /*
77 var info = document.createElement('li');
78 info.appendChild(document.createElement('span'))
79 .appendChild(document.createTextNode(loc.SHOWINFO));
80 info.classList.add('info');
81 info.setAttribute('title', loc.SHOWINFO);
82 */
83
84 ul.appendChild(close);
85
86 element.appendChild(ul);
87 element.appendChild(this._iframe);
88 };
89
90 element.style.display = 'block';
91 },
92
93 /**
94 * Close tutorial window.
95 */
96 hide : function () {
97 this._element.display.style = 'none';
98 },
99
100 /**
101 * Set a page to be the current tutorial page.
102 * Expects either a string or an element.
103 */
104 setPage : function (obj) {
105 var page = obj;
106 if (typeof page != 'string') {
107 page = window.location.pathname + window.location.search;
108 for (i = 1; i < 5; i++) {
109 if (obj.nodeName === 'SECTION') {
110 if (obj.hasAttribute('id'))
111 page += '#' + obj.getAttribute('id');
112 break;
113 }
114 else if (obj.nodeName === 'PRE' && obj.hasAttribute('id')) {
115 page += '#' + obj.getAttribute('id');
116 break;
117 }
118 else {
119 obj = obj.parentNode;
120 };
121 };
122 };
123 this._session.set('tutpage', page);
124 },
125
126 /**
127 * Get the current tutorial URL
128 */
129 getPage : function () {
130 this._session.get('tutpage');
131 },
132 }
133}(this.KorAP));