iwmlib/lib/card/scatter.js

163 lines
4.4 KiB
JavaScript
Raw Normal View History

2019-07-17 11:03:26 +02:00
import Card from './card.js'
/**
* Extends the card with scatter functionality.
*
* @class ScatterCard
*/
export default class ScatterCard extends Card {
/**
* TODO: Find a more suitable name.
* Adjusts the HTML to work in the new context.
*
* @static
* @param {*} domElement
* @param {*} htmlString
* @param {*} basePath
* @param {*} [opts={}]
* @memberof ScatterCard
*/
2019-07-18 12:26:39 +02:00
static setup(context, htmlString, { basePath = './', modules = [] } = {}) {
2019-08-14 17:46:12 +02:00
if (typeof context.scatter == 'undefined') {
console.error(
"You need to wrap the context inside a DOMScatter before executing the ScatterCard's setup function."
)
}
/**
* This is required for the callback functions to work properly
*/
window.ScatterCard = ScatterCard
2019-07-18 12:26:39 +02:00
context.classList.add('info-card')
this.relativePath = basePath
htmlString = this._adjustRelativeLinks(htmlString)
let parser = new DOMParser()
2019-07-18 12:26:39 +02:00
let html = parser.parseFromString(htmlString, 'text/html')
/**
* Conflicts with the FindTarget method of the Abstract scatter.
*/
this._replaceAttributes(context, html, 'onclick', this._replaceCallback)
2019-07-18 12:26:39 +02:00
let content = html.querySelector('.mainview')
context.appendChild(content)
super.setup(context, modules)
return context
}
/**
* Creates a scatter for the card and applies the card to it,
*
* @static
* @param {*} html
* @param {*} scatterContainer
* @param {string} [basePath=""]
* @param {*} [opts={}]
* @returns
* @memberof ScatterCard
*/
2019-07-30 16:56:29 +02:00
static createCardScatter(html, scatterContainer, { basePath = './', modules = [] } = {}) {
2019-07-18 12:26:39 +02:00
let element = document.createElement('div')
scatterContainer.element.appendChild(element)
new DOMScatter(element, scatterContainer, {
width: 1400,
2022-10-04 10:51:35 +02:00
height: 1200,
})
this.setup(element, html, {
basePath,
2022-10-04 10:51:35 +02:00
modules,
})
return element
}
/**
* Closes but NOT removes the scatter element.
*
* The remove must be called separately, it may be used in the close callback
* of the scatter.
*/
static close(context) {
2019-08-14 17:46:12 +02:00
if (typeof context.scatter != 'undefined') context.scatter.close()
else {
console.error('Expected a scatter element to close!', this)
}
2019-08-14 17:46:12 +02:00
// Card.close(context)
// if (context['scatter']) {
// console.error('CLOSED CARD')
// context.scatter.close()
// } else {
// console.error('Expected a scatter element to close!', this)
// }
}
/**
* Cleans up the card.
*
* @static
* @override
* @memberof ScatterCard
*/
static remove(context) {
if (context['scatter']) {
context.scatter = null
} else {
console.error('Expected a scatter element to remove!', this)
}
Card.remove(context)
}
2019-08-14 17:46:12 +02:00
/**
*Utility function to create a fully functional card scatter.
*
* @static
* @param {*} scatterContainer
* @param {*} path
* @param {string} [basePath="."]
* @param {*} opts
* @returns
* @memberof CardScatter
*/
2019-08-14 17:46:12 +02:00
static loadAndCreateScatterCard(scatterContainer, item, { basePath = '../', modules = [] } = {}) {
console.log(basePath)
return new Promise((resolve, reject) => {
2019-07-18 12:26:39 +02:00
let url = basePath + '/' + item + '/index.html'
console.log('Loading', url)
this.loadHTML(url)
2022-10-04 10:51:35 +02:00
.then((html) => {
2019-07-18 12:26:39 +02:00
console.log('Received', html)
2019-07-30 16:56:29 +02:00
let element = this.createCardScatter(html, scatterContainer, {
basePath,
2022-10-04 10:51:35 +02:00
modules,
2019-07-30 16:56:29 +02:00
})
resolve(element)
})
2022-10-04 10:51:35 +02:00
.catch((e) => reject(e))
})
}
static _setLanguage(context, language) {
context.language = language
}
static _getLanguage(context) {
return context.language
}
}
ScatterCard.selectedLanguage = 0
2019-07-18 12:26:39 +02:00
ScatterCard.languages = ['Deutsch', 'English']
ScatterCard.languageTags = {
2019-07-18 12:26:39 +02:00
Deutsch: 'de',
2022-10-04 10:51:35 +02:00
English: 'en',
}
ScatterCard.scatterContainer = null