|
| 1 | +(function (window, ufe, undefined) { |
| 2 | + 'use strict'; |
| 3 | + |
| 4 | + class MyCounterJavaScript { |
| 5 | + constructor(parentEl) { |
| 6 | + this.parentEl = parentEl; |
| 7 | + this.count = 0; |
| 8 | + |
| 9 | + this.initialize(); |
| 10 | + } |
| 11 | + |
| 12 | + initialize() { |
| 13 | + this.parentEl.innerHTML = ` |
| 14 | + <button id="dec">-</button> |
| 15 | + <span>${this.count}</span> |
| 16 | + <button id="inc">+</button> |
| 17 | + `; |
| 18 | + this.buttonInc = this.parentEl.querySelector('#inc'); |
| 19 | + this.buttonDec = this.parentEl.querySelector('#dec'); |
| 20 | + this.spanValue = this.parentEl.querySelector('span'); |
| 21 | + |
| 22 | + this.incCb = this.inc.bind(this); |
| 23 | + this.decCb = this.dec.bind(this); |
| 24 | + |
| 25 | + this.buttonInc.addEventListener('click', this.incCb, false); |
| 26 | + this.buttonDec.addEventListener('click', this.decCb, false); |
| 27 | + } |
| 28 | + |
| 29 | + dispose() { |
| 30 | + this.buttonInc.removeEventListener('click', this.incCb, false); |
| 31 | + this.buttonDec.removeEventListener('click', this.decCb, false); |
| 32 | + this.incCb = null; |
| 33 | + this.decCb = null; |
| 34 | + this.buttonInc = null; |
| 35 | + this.buttonDec = null; |
| 36 | + this.spanValue = null; |
| 37 | + this.parentEl.innerHTML = ''; |
| 38 | + this.parentEl = null; |
| 39 | + } |
| 40 | + |
| 41 | + inc() { |
| 42 | + this.count++; |
| 43 | + this.update(); |
| 44 | + } |
| 45 | + |
| 46 | + dec() { |
| 47 | + this.count--; |
| 48 | + this.update(); |
| 49 | + } |
| 50 | + |
| 51 | + update() { |
| 52 | + this.spanValue.innerText = this.count; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + class MyCounterJavaScriptWrapper extends MyCounterJavaScript { |
| 59 | + |
| 60 | + constructor(parentEl) { |
| 61 | + super(parentEl); |
| 62 | + } |
| 63 | + |
| 64 | + initialize() { |
| 65 | + super.initialize(); |
| 66 | + |
| 67 | + var manager = new ufe.HTMLElementCommunicationsManager( |
| 68 | + this.parentEl, |
| 69 | + ufe.CommunicationsEvent.CONTAINER_EVENT_TYPE, |
| 70 | + this.parentEl, |
| 71 | + ufe.CommunicationsEvent.CONTENT_EVENT_TYPE |
| 72 | + ); |
| 73 | + manager.initialize(); |
| 74 | + var contentMethods = new ufe.ContentCommunicationHandlerMethods(); |
| 75 | + contentMethods.dispose = () => this.disposeHandler(); |
| 76 | + this.communicationHandler = new ufe.InWindowContentCommunicationHandler(manager, contentMethods); |
| 77 | + |
| 78 | + // Simulate a delay in the initialization |
| 79 | + window.setTimeout(() => { |
| 80 | + this.communicationHandler.dispatchMounted(); // Signal to the parent that the component has finished mounting |
| 81 | + }, 1_000); |
| 82 | + } |
| 83 | + |
| 84 | + inc() { |
| 85 | + // Signal that we are processing stuff |
| 86 | + this.communicationHandler.dispatchBeforeUpdate(); |
| 87 | + super.inc(); |
| 88 | + // Simulate call to server or other long running operation |
| 89 | + setTimeout(() => { |
| 90 | + this.communicationHandler.dispatchUpdated(); |
| 91 | + }, 1_000); |
| 92 | + } |
| 93 | + |
| 94 | + dec() { |
| 95 | + // Signal that we are processing stuff |
| 96 | + this.communicationHandler.dispatchBeforeUpdate(); |
| 97 | + super.dec(); |
| 98 | + // Simulate call to server or other long running operation |
| 99 | + setTimeout(() => { |
| 100 | + this.communicationHandler.dispatchUpdated(); |
| 101 | + }, 1_000); |
| 102 | + } |
| 103 | + |
| 104 | + disposeHandler() { |
| 105 | + // Signal the dispose to the root component |
| 106 | + this.communicationHandler.dispatchBeforeDispose(); |
| 107 | + |
| 108 | + // Give the root component a chance to react |
| 109 | + setTimeout(() => { this.dispose(); }, 1_000) |
| 110 | + } |
| 111 | + |
| 112 | + dispose() { |
| 113 | + // Dispose any resources you might have allocated in the wrapper |
| 114 | + this.communicationHandler.dispatchDisposed(); |
| 115 | + this.communicationHandler.dispose(); |
| 116 | + this.communicationHandler = null; |
| 117 | + |
| 118 | + console.log('MyCounterJavaScriptWrapper Disposed. Count: ' + this.count); |
| 119 | + // Call the super's dispose method |
| 120 | + super.dispose(); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + window.demo_components = window.demo_components || {}; |
| 125 | + window.demo_components.MyCounterJavaScript = MyCounterJavaScript; |
| 126 | + window.demo_components.MyCounterJavaScriptWrapper = MyCounterJavaScriptWrapper; |
| 127 | + |
| 128 | +})(window, window.validide_uFrontEnds, void 0); |
0 commit comments