Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Open and close a tutorial page. |
| 3 | * The current page is stored and retrieved in a session cookie. |
| 4 | */ |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 5 | // Todo: add query mechanism! |
| 6 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 7 | define(['session', 'util'], function (sessionClass) { |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 8 | "use strict"; |
| 9 | |
| 10 | // Localization values |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 11 | var loc = KorAP.Locale; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 12 | loc.CLOSE = loc.CLOSE || 'Close'; |
| 13 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 14 | return { |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 15 | /** |
| 16 | * Create new tutorial object. |
| 17 | * Accepts an element to bind the tutorial window to. |
| 18 | */ |
| 19 | create : function (obj) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 20 | if (!obj) |
| 21 | return null; |
| 22 | return Object.create(this)._init(obj); |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 23 | }, |
| 24 | |
| 25 | // Initialize Tutorial object |
| 26 | _init : function (obj) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 27 | |
| 28 | this._session = sessionClass.create(); |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 29 | 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 Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 60 | // 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 Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 79 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 80 | 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 Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 128 | }; |
| 129 | }); |