import OlControl from 'ol/control/Control'
import { unByKey } from 'ol/Observable'
import OlExtElement from 'ol-ext/util/element'
/** On garde ce code ou le taff d'ouvrir une iframe est déjà fait.. */
// NON UTLISE
class ControlOpenIframe extends OlControl {
constructor (options) {
const className = options.className !== undefined ? options.className : ''
const classNames = (className || '') + 'kmap-connect-argis-tools' + (options.target ? '' : ' ol-unselectable')
const element = OlExtElement.create('DIV', {
className: classNames,
})
super({
element,
target: options.target,
})
if (!options.viewer) {
console.error('[ControlTriangulation] options.viewer non présent')
}
this.viewer = options.viewer
this.toolId = options.toolId || 'connect-arcgis-tool'
this.connectUrl = options?.options?.url || null
// #region build de l'ihm
this.buildIhm(element)
// lorsque ce controle est retiré de la carte, on arrête d'écouter les event
const listenerRemoveControl = this.viewer.Map.getControls().on('remove', (ev) => {
if (ev.element === this) {
// TODO remettre la barre horizontal dans son état d'origine
// this.viewer.horizontalToolbar.setVisible(true)
unByKey(listenerRemoveControl)
}
})
window.addEventListener(
'message',
(event) => {
console.log('test token : ', event)
// Do we trust the sender of this message? (might be
// different from what we originally opened, for example).
/* if (event.origin !== "http://example.com")
return; */
// event.source is popup
// event.data is "hi there yourself! the secret response is: rheeeeet!"
},
false
)
}
buildIhm (element) {
console.log(this.connectUrl)
const iframeDiv = OlExtElement.create('DIV', {
className: 'iframe-container',
})
element.appendChild(iframeDiv)
const ifrm = document.createElement('iframe')
// ifrm.setAttribute('allow', 'true')
ifrm.setAttribute('src', this.connectUrl)
ifrm.style.width = '100%'
ifrm.style.height = '100%'
iframeDiv.appendChild(ifrm)
}
}
export default ControlOpenIframe