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 | ab4d3ca | 2015-04-17 01:48:43 +0000 | [diff] [blame] | 5 | // 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 Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 8 | define(['session', 'util'], function (sessionClass) { |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 9 | "use strict"; |
| 10 | |
| 11 | // Localization values |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 12 | var loc = KorAP.Locale; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 13 | loc.CLOSE = loc.CLOSE || 'Close'; |
| 14 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 15 | return { |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 16 | /** |
| 17 | * Create new tutorial object. |
| 18 | * Accepts an element to bind the tutorial window to. |
| 19 | */ |
| 20 | create : function (obj) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 21 | if (!obj) |
| 22 | return null; |
| 23 | return Object.create(this)._init(obj); |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 24 | }, |
| 25 | |
| 26 | // Initialize Tutorial object |
| 27 | _init : function (obj) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 28 | |
| 29 | this._session = sessionClass.create(); |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 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'); |
Nils Diewald | ab4d3ca | 2015-04-17 01:48:43 +0000 | [diff] [blame] | 56 | this._iframe.setAttribute( |
| 57 | 'src', |
| 58 | (this.getPage() || this.start) + '?embedded=true' |
| 59 | ); |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 60 | |
| 61 | var ul = document.createElement('ul'); |
| 62 | ul.classList.add('action', 'right'); |
| 63 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 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 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 83 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 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 | }, |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 132 | }; |
| 133 | }); |