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 | */ |
| 22 | create : function (src, id) { |
| 23 | return Object.create(this)._init(src, id); |
| 24 | }, |
| 25 | |
Akron | e8e2c95 | 2018-07-04 13:43:12 +0200 | [diff] [blame] | 26 | // Initialize widget |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 27 | _init : function (src, id) { |
| 28 | this.src = src; |
| 29 | this.id = id; |
| 30 | return this; |
| 31 | }, |
| 32 | |
| 33 | /** |
| 34 | * The element of the widget |
| 35 | */ |
| 36 | element : function () { |
| 37 | |
| 38 | if (this._element) |
| 39 | return this._element; |
| 40 | |
Akron | 8d646d7 | 2018-07-08 13:45:53 +0200 | [diff] [blame^] | 41 | var div = document.createElement('div'); |
| 42 | div.classList.add('widget'); |
| 43 | |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 44 | // Spawn new iframe |
Akron | 8d646d7 | 2018-07-08 13:45:53 +0200 | [diff] [blame^] | 45 | var i = div.addE('iframe'); |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 46 | i.setAttribute('allowTransparency',"true"); |
| 47 | i.setAttribute('frameborder', 0); |
| 48 | i.setAttribute('sandbox','allow-scripts'); |
Akron | e8e2c95 | 2018-07-04 13:43:12 +0200 | [diff] [blame] | 49 | i.style.height = '0px'; |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 50 | i.setAttribute('name', this.id); |
| 51 | i.setAttribute('src', this.src); |
Akron | 8d646d7 | 2018-07-08 13:45:53 +0200 | [diff] [blame^] | 52 | this._iframe = i; |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 53 | |
Akron | 8d646d7 | 2018-07-08 13:45:53 +0200 | [diff] [blame^] | 54 | var ul = div.addE('ul'); |
| 55 | ul.classList.add('action','right'); |
| 56 | |
| 57 | // Add close button |
| 58 | var close = ul.addE('li'); |
| 59 | close.addE('span').addT(loc.CLOSE); |
| 60 | close.classList.add('close'); |
| 61 | close.setAttribute('title', loc.CLOSE); |
| 62 | |
| 63 | // Close match |
| 64 | close.addEventListener('click', function (e) { |
| 65 | e.halt(); |
| 66 | this.shutdown() |
| 67 | }.bind(this)); |
| 68 | |
| 69 | this._element = div; |
| 70 | |
| 71 | return div; |
| 72 | }, |
| 73 | |
| 74 | // Return iframe of widget |
| 75 | iframe : function () { |
| 76 | if (this._iframe) |
| 77 | return this._iframe; |
| 78 | this.element(); |
| 79 | return this._iframe; |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 80 | }, |
| 81 | |
| 82 | // Resize iframe |
| 83 | resize : function (data) { |
Akron | 8d646d7 | 2018-07-08 13:45:53 +0200 | [diff] [blame^] | 84 | this.iframe().style.height = data.height + 'px'; |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 85 | }, |
| 86 | |
| 87 | // Shutdown suspicious iframe |
| 88 | shutdown : function () { |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 89 | this._element.parentNode.removeChild(this._element); |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | }); |