| 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. | 
| Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 5 | * The widget component represents a single iframe and is | 
|  | 6 | * implemented as a view object. | 
| Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 7 | * | 
|  | 8 | * @author Nils Diewald | 
|  | 9 | */ | 
|  | 10 |  | 
| Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 11 | define(["view","util"], function (viewClass) { | 
| Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 12 | "use strict"; | 
|  | 13 |  | 
|  | 14 | return { | 
|  | 15 |  | 
|  | 16 | /** | 
|  | 17 | * Create new widget | 
|  | 18 | */ | 
| Akron | 7991b19 | 2018-07-09 17:28:43 +0200 | [diff] [blame] | 19 | create : function (name, src, id) { | 
| Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 20 | return Object.create(viewClass)._init(['widget']).upgradeTo(this)._init(name, src, id); | 
| Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 21 | }, | 
|  | 22 |  | 
| Akron | e8e2c95 | 2018-07-04 13:43:12 +0200 | [diff] [blame] | 23 | // Initialize widget | 
| Akron | 7991b19 | 2018-07-09 17:28:43 +0200 | [diff] [blame] | 24 | _init : function (name, src, id) { | 
| Akron | 56a11af | 2018-07-27 18:28:45 +0200 | [diff] [blame] | 25 | if (!name || !src || !id) | 
|  | 26 | throw Error("Widget not well defined"); | 
| Akron | 7991b19 | 2018-07-09 17:28:43 +0200 | [diff] [blame] | 27 | this.name = name; | 
| Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 28 | this.src = src; | 
|  | 29 | this.id = id; | 
|  | 30 | return this; | 
|  | 31 | }, | 
|  | 32 |  | 
|  | 33 | /** | 
| Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 34 | * The element of the widget as embedded in the view | 
| Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 35 | */ | 
| Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 36 | show : function () { | 
| Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 37 |  | 
| Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 38 | if (this._show) | 
|  | 39 | return this._show; | 
| Akron | 8d646d7 | 2018-07-08 13:45:53 +0200 | [diff] [blame] | 40 |  | 
| Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 41 | // Spawn new iframe | 
| Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 42 | var i = document.createElement('iframe'); | 
| Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 43 | i.setAttribute('allowTransparency',"true"); | 
|  | 44 | i.setAttribute('frameborder', 0); | 
|  | 45 | i.setAttribute('sandbox','allow-scripts'); | 
| Akron | e8e2c95 | 2018-07-04 13:43:12 +0200 | [diff] [blame] | 46 | i.style.height = '0px'; | 
| Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 47 | i.setAttribute('name', this.id); | 
|  | 48 | i.setAttribute('src', this.src); | 
| Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 49 |  | 
| Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 50 | // Per default there should at least be a button | 
|  | 51 | // for settings, if the plugin requires settings. | 
|  | 52 | // Otherwise a button indicating this is a plugin | 
|  | 53 | // is a nice idea as well. | 
| Akron | 8d646d7 | 2018-07-08 13:45:53 +0200 | [diff] [blame] | 54 |  | 
| Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 55 | this.actions.add( | 
|  | 56 | this.name, ['button-icon', 'plugin'], function (e) { | 
| Akron | 8d646d7 | 2018-07-08 13:45:53 +0200 | [diff] [blame] | 57 |  | 
| Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 58 | // Temporary | 
|  | 59 | window.alert("Basic information about this plugin"); | 
| Akron | 8d646d7 | 2018-07-08 13:45:53 +0200 | [diff] [blame] | 60 | }.bind(this)); | 
|  | 61 |  | 
| Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 62 | this._show = i; | 
|  | 63 | return i; | 
| Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 64 | }, | 
|  | 65 |  | 
|  | 66 | // Resize iframe | 
|  | 67 | resize : function (data) { | 
| Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 68 | this.show().style.height = data.height + 'px'; | 
| Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 69 | }, | 
|  | 70 |  | 
| Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 71 |  | 
|  | 72 | // On closing the widget view | 
|  | 73 | onClose : function () { | 
|  | 74 | if (this._mgr) { | 
|  | 75 | this._mgr._closeWidget(this._id); | 
|  | 76 | this._mgr = undefined; | 
|  | 77 | }; | 
| Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 78 | } | 
|  | 79 | } | 
|  | 80 | }); |