blob: 4f00ee121a0373934172fe6d3b7bc151fff5384a [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 Diewald58141332015-04-07 16:18:45 +000030 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');
Nils Diewaldab4d3ca2015-04-17 01:48:43 +000056 this._iframe.setAttribute(
57 'src',
58 (this.getPage() || this.start) + '?embedded=true'
59 );
Nils Diewald58141332015-04-07 16:18:45 +000060
61 var ul = document.createElement('ul');
62 ul.classList.add('action', 'right');
63
Nils Diewald58141332015-04-07 16:18:45 +000064 // 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 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000083
Nils Diewald58141332015-04-07 16:18:45 +000084 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 },
Nils Diewald0e6992a2015-04-14 20:13:52 +0000132 };
133});