Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 1 | /** |
| 2 | * The plugin system is based |
| 3 | * on registered widgets (iframes) from |
| 4 | * foreign services. |
| 5 | * The widget component represents a single iframe. |
| 6 | * |
| 7 | * @author Nils Diewald |
| 8 | */ |
| 9 | |
| 10 | define(["util"], function () { |
| 11 | "use strict"; |
| 12 | |
Akron | 8d646d7 | 2018-07-08 13:45:53 +0200 | [diff] [blame] | 13 | // Localization values |
| 14 | const loc = KorAP.Locale; |
| 15 | loc.CLOSE = loc.CLOSE || 'Close'; |
| 16 | |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 17 | return { |
| 18 | |
| 19 | /** |
| 20 | * Create new widget |
| 21 | */ |
Akron | 7991b19 | 2018-07-09 17:28:43 +0200 | [diff] [blame^] | 22 | create : function (name, src, id) { |
| 23 | return Object.create(this)._init(name, src, id); |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 24 | }, |
| 25 | |
Akron | e8e2c95 | 2018-07-04 13:43:12 +0200 | [diff] [blame] | 26 | // Initialize widget |
Akron | 7991b19 | 2018-07-09 17:28:43 +0200 | [diff] [blame^] | 27 | _init : function (name, src, id) { |
| 28 | this.name = name; |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 29 | this.src = src; |
| 30 | this.id = id; |
| 31 | return this; |
| 32 | }, |
| 33 | |
| 34 | /** |
| 35 | * The element of the widget |
| 36 | */ |
| 37 | element : function () { |
| 38 | |
| 39 | if (this._element) |
| 40 | return this._element; |
| 41 | |
Akron | 8d646d7 | 2018-07-08 13:45:53 +0200 | [diff] [blame] | 42 | var div = document.createElement('div'); |
| 43 | div.classList.add('widget'); |
| 44 | |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 45 | // Spawn new iframe |
Akron | 8d646d7 | 2018-07-08 13:45:53 +0200 | [diff] [blame] | 46 | var i = div.addE('iframe'); |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 47 | i.setAttribute('allowTransparency',"true"); |
| 48 | i.setAttribute('frameborder', 0); |
| 49 | i.setAttribute('sandbox','allow-scripts'); |
Akron | e8e2c95 | 2018-07-04 13:43:12 +0200 | [diff] [blame] | 50 | i.style.height = '0px'; |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 51 | i.setAttribute('name', this.id); |
| 52 | i.setAttribute('src', this.src); |
Akron | 8d646d7 | 2018-07-08 13:45:53 +0200 | [diff] [blame] | 53 | this._iframe = i; |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 54 | |
Akron | 8d646d7 | 2018-07-08 13:45:53 +0200 | [diff] [blame] | 55 | var ul = div.addE('ul'); |
| 56 | ul.classList.add('action','right'); |
| 57 | |
| 58 | // Add close button |
| 59 | var close = ul.addE('li'); |
| 60 | close.addE('span').addT(loc.CLOSE); |
| 61 | close.classList.add('close'); |
| 62 | close.setAttribute('title', loc.CLOSE); |
| 63 | |
Akron | 7991b19 | 2018-07-09 17:28:43 +0200 | [diff] [blame^] | 64 | // Add info button on plugin |
| 65 | var plugin = ul.addE('li'); |
| 66 | plugin.addE('span').addT(this.name); |
| 67 | plugin.classList.add('plugin'); |
| 68 | plugin.setAttribute('title', this.name); |
| 69 | |
Akron | 8d646d7 | 2018-07-08 13:45:53 +0200 | [diff] [blame] | 70 | // Close match |
| 71 | close.addEventListener('click', function (e) { |
| 72 | e.halt(); |
| 73 | this.shutdown() |
| 74 | }.bind(this)); |
| 75 | |
| 76 | this._element = div; |
| 77 | |
| 78 | return div; |
| 79 | }, |
| 80 | |
| 81 | // Return iframe of widget |
| 82 | iframe : function () { |
| 83 | if (this._iframe) |
| 84 | return this._iframe; |
| 85 | this.element(); |
| 86 | return this._iframe; |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 87 | }, |
| 88 | |
| 89 | // Resize iframe |
| 90 | resize : function (data) { |
Akron | 8d646d7 | 2018-07-08 13:45:53 +0200 | [diff] [blame] | 91 | this.iframe().style.height = data.height + 'px'; |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 92 | }, |
| 93 | |
| 94 | // Shutdown suspicious iframe |
| 95 | shutdown : function () { |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 96 | this._element.parentNode.removeChild(this._element); |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | }); |