iwmlib/lib/card/card.js

1886 lines
66 KiB
JavaScript
Raw Normal View History

/** To avoid problems with relative URL paths, we use inline data URI to load svg icons. */
const closeIconDataURI = `data:image/svg+xml;utf8,
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid meet">
<line x1="20" y1="20" x2="80" y2="80" stroke="black" stroke-width="8" />
<line x1="80" y1="20" x2="20" y2="80" stroke="black" stroke-width="8" />
</svg>
`
const resizeIconDataURI = `data:image/svg+xml;utf8,
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid meet">
<line x1="20" y1="80" x2="80" y2="20" stroke="lightgray" stroke-width="8" />
<line x1="40" y1="80" x2="80" y2="40" stroke="lightgray" stroke-width="8" />
</svg>
`
const enableNearestNeighborTaps = false
/**
* A class that collects static methods to maintain the states and parts of
* EyeVisit like cards.
*
* The class is used as a namespace and should never called with new.
*/
export default class Card {
static setup(context, modules = []) {
2019-07-18 12:26:39 +02:00
console.log('Setup Card...', modules)
context.modules = []
modules.forEach(module => {
2019-07-30 16:56:29 +02:00
if (module.apply(context)) context.modules.push(module.constructor.name)
})
}
/**
*
*
* @static
* @param {*} event
* @memberof Card
*/
static close(event) {
let context = this.getContext(event.target)
if (context) {
if (context.onClose) {
context.onClose(event)
} else context.parentNode.removeChild(context)
2019-07-18 12:26:39 +02:00
} else console.error('Could not find context!', event.target)
}
/**
* Replaces a set of attributes using a provided function.
*
* @static
* @param {*} html
* @param {*} attribute
* @param {*} replaceFunc
* @memberof Card
*/
static _replaceAttributes(html, attribute, replaceFunc) {
let clickables = html.querySelectorAll(`[${attribute}]`)
clickables.forEach(element => {
let attributeVal = element.getAttribute(attribute)
element.removeAttribute(attribute)
replaceFunc.call(this, element, attributeVal)
})
}
/**
2019-07-18 12:26:39 +02:00
* Replaces the onClick callback of the element with an
* Interaction mapper event.
*
* @static
* @param {*} element
* @param {*} attributeVal
* @returns
* @memberof Card
*/
static _replaceCallback(element, attributeVal) {
2019-07-18 12:26:39 +02:00
if (element.tagName == 'A') {
element.addEventListener('click', event => {
event.preventDefault()
})
}
2019-07-18 12:26:39 +02:00
let callbackParts = attributeVal.split('(')
let funcPart = callbackParts[0].trim()
let trimmedArgs = callbackParts[1].trim()
//Remove the closing ')'
trimmedArgs = trimmedArgs.substring(0, trimmedArgs.length - 1)
2019-07-18 12:26:39 +02:00
let callParts = funcPart.split('.')
let argsStrings = trimmedArgs.split(',').filter(entry => {
return entry.trim() != ''
})
let callStack = window
do {
callStack = callStack[callParts.shift().trim()]
} while (callParts.length > 0)
let that = this
let interactionType = this.interactionType
//Remove the events on the circle.
// These are 'hardcoded' inside the convert.js.
2019-07-18 12:26:39 +02:00
if (element.tagName == 'circle') return false
2019-07-18 12:26:39 +02:00
InteractionMapper.on(interactionType, element, event => {
/**
* Replaces the strings from the listener with the cooresponding variables.
*/
let args = []
argsStrings.forEach(arg => {
arg = arg.trim()
2019-07-18 12:26:39 +02:00
if (arg == 'this') args.push(event.target)
else if (arg == 'event') args.push(event)
else {
const firstCharacter = arg[0]
2019-07-18 12:26:39 +02:00
if (firstCharacter == '"' || firstCharacter == "'") {
arg = arg.substring(1)
}
const lastCharacter = arg[arg.length - 1]
2019-07-18 12:26:39 +02:00
if (lastCharacter == '"' || lastCharacter == "'") {
arg = arg.substring(0, arg.length - 1)
}
args.push(arg)
}
})
event.stopPropagation()
2019-07-18 12:26:39 +02:00
if (callStack) callStack.call(that, ...args)
else {
2019-07-30 16:56:29 +02:00
console.error('Could not call callback function ' + attributeVal, ...args)
}
})
}
/**
* Transform the relative links to absolute ones.
*
* Currently covers:
* Tags: a,img, image, circle
* Attributes: xlink:href,href,src (first occurence only)
*
* @static
* @param {DomElement} element - The children of the element are inspected, if there are relative paths, that has to be adjusted to absolute ones.
* @returns
* @memberof Card
*/
static _adjustRelativeLinks(html) {
const that = this
/*
This RegEx finds all requested tags[1], and all requested attributes[3] and replaces the relative path [4] with the absolute one.
while all other attributes [2],[5] are preserved.
*/
2019-07-18 12:26:39 +02:00
return html.replace(
/<\s*(a|video|img|image|circle)\s(.*?)(xlink:href|href|src)\s*=\s*["'](\..*?)["']\s*(.*?)>/g,
function(data) {
let path = that._getRelativePath(arguments[4])
2019-07-30 16:56:29 +02:00
const tag = `<${arguments[1]} ${arguments[2]} ${arguments[3]}="${path}" ${arguments[5]}>`
2019-07-18 12:26:39 +02:00
/* if (that.debug) */ console.log('Adjusted: ', tag)
return tag
}
)
}
/**
* Concats the given path with the relative path specified in the Card (as static variable).
*/
static _getRelativePath(src) {
2019-07-18 12:26:39 +02:00
let path = this.relativePath != '' ? this.relativePath + '/' + src : src
return path
}
/**
* Loads the card text using an ajax request.
*
* @static
* @param {*} path
* @returns
* @memberof Card
*/
static loadHTML(path) {
return new Promise((resolve, reject) => {
let request = new XMLHttpRequest()
2019-07-18 12:26:39 +02:00
request.onreadystatechange = function() {
if (this.readyState == 4) {
if (this.status == 200 || Card._isLocal()) {
try {
resolve(this.responseText)
} catch (e) {
reject(e)
}
2019-07-18 12:26:39 +02:00
} else
reject(
`Request failed '${path}'. Returned status ${this.status} and ready state ${this.readyState}.`
)
}
}
2019-07-18 12:26:39 +02:00
request.open('GET', path, true)
request.send()
})
}
/**
* TODO: Maybe put this in a utility script.
*/
static _isLocal() {
2019-07-18 12:26:39 +02:00
return window.location.protocol == 'file:'
}
/**
* Returns true if the card belongs to an editable page / context.
*
* @static
* @returns
* @memberof Card
*/
static isEditable() {
return typeof mainController != 'undefined' && mainController.mode
}
/**
* Finds the closest element with the given css class name along the parent chain.
*
* @static
* @param {any} node - The node to start on
* @param {any} klass - The css class name
* @returns {element} - The DOM element with the given class name
* @memberof Card
*/
static closestWithClass(node, klass) {
if (node && node.classList) {
2019-07-18 12:26:39 +02:00
if (node.classList.contains(klass)) return node
return this.closestWithClass(node.parentNode, klass)
}
return null
}
/**
* Returns a computed style property. Similar to the jQuery.css method.
*
* @param {any} node
* @param {any} attr
* @returns {any}
* @memberof Card
*/
static css(node, attr) {
return window.getComputedStyle(node, null).getPropertyValue(attr)
}
/**
* Defines the standard drag behavior of cards. Copies the outer HTML of
* the card to the dataTransfer object of the drag event.
*
* Since dragover does not have the right to see the content we must specify
* additional type information to ensure that only accepted content
* can be dropped. Use the optional type parameter to specify custom types.
*
* The optional effectAllowed is set to 'all'. This is necessary to ensure
* that elements can be copied and moved. Unfortunately this
* adds a plus marker to the mouse that might be misleading. Use
* DataTransfer.dropEffect in dragover events to provide correct feedback instead.
*
* @static
* @param {any} event
* @param {string} [type="card"]
* @param {string} [effectAllowed="all"]
* @memberof Card
*/
2019-07-18 12:26:39 +02:00
static dragStart(event, type = 'card', effectAllowed = 'all') {
event.dataTransfer.effectAllowed = effectAllowed
let html = event.target.outerHTML
2019-07-18 12:26:39 +02:00
event.dataTransfer.setData('text/html', html)
// https://stackoverflow.com/questions/11065803/determine-what-is-being-dragged-from-dragenter-dragover-events
event.dataTransfer.setData('iwmbrowser/' + type, '')
}
/**
* Opens a popup by calling the Popup.open method.
*
* @static
* @param {DOM element} parent - The DOM node to add the popup
* @param {any} position - The x,y position of the popup
* @param {any} content - The content to show within the popup
* @param {any} callback - A callback that is called on closing the popup
* @memberof Card
*/
static _openPopup(context, src, position, content, options = {}) {
let maxWidth = null
2019-07-18 12:26:39 +02:00
if (this.debug) console.log('Card._openPopup', position)
//logging
if (src) {
2019-07-18 12:26:39 +02:00
let strparts = src.split('/')
let cardID = strparts[strparts.length - 2]
let cardName = strparts[strparts.length - 1]
//console.log('open popup:',cardID,cardName,context,content)
let msg = 'ShowPopup: ' + cardID + '/' + cardName
console.log('Logging:', msg)
Logging.log(msg)
}
let highlight = null
if (options.highlight) {
highlight = options.highlight
delete options.highlight
}
2019-07-18 12:26:39 +02:00
this._createPopup(context, position, content, options)
.then(popup => {
if (
//Test if meanwhile another popup was registered...
this._getPopup(context) ||
// Or if an highlight was loaded, if the highlight already was closed.
2019-07-30 16:56:29 +02:00
(highlight !== null && !this._isHighlightActive(context, highlight))
2019-07-18 12:26:39 +02:00
) {
//.. if so remove the create popup instantly.
popup.remove()
} else {
// Otherwise set the popup regularly.
2019-07-30 16:56:29 +02:00
let popupParagraphs = popup.element.querySelectorAll('.popupContent > *')
2019-07-18 12:26:39 +02:00
// Remove a design error of naming two adjacent elements popup.
// Todo: fix this properly and remove this code.
// let unnecessaryPopupElement = popup.element.querySelector(".popupContent > .popup")
// unnecessaryPopupElement.classList.remove("popup")
popupParagraphs.forEach(popupParagraph => {
popupParagraph.setAttribute('draggable', false)
popupParagraph.addEventListener('mousedown', event => {
event.preventDefault()
})
})
2019-07-18 12:26:39 +02:00
this._setPopup(context, popup, src)
}
})
.catch(e => console.error(e))
}
/**
* Closes a provided popup and unsets it on the context.
*
* @static
* @param {*} context
* @param {*} popup
* @memberof Card
*/
static closePopup(context, popup) {
if (popup) {
2019-07-18 12:26:39 +02:00
if (this.debug) console.log('Close Popup.', context, popup)
window.popup = popup
popup.close()
this._unsetPopup(context)
} else {
2019-07-18 12:26:39 +02:00
console.error('Requested to close popup, but popup was not found.')
}
}
/**
* Creates a new Popup instance.
*
* @static
* @param {DomElement} context - The element on which the Popup should be created.
* @param {Point} position - The position, where the popup will be shown.
* @param {object} content - The content of the popup as required by the Popup class.
* @param {object} [options={}] - Additional options for the popup (optional).
* @returns {Promise} - Returns a Promise, which is resolved, when the Popup is fully loaded.
* @memberof Card
*/
static _createPopup(context, position, content, options = {}) {
2019-07-30 16:56:29 +02:00
if (this.debug) console.log('Create Popup.', context, position, content, options)
2019-07-18 12:26:39 +02:00
let popup = new Popup(
Object.assign(
{
parent: context,
content
},
Object.assign(
{
noStyle: true,
// TODO: Remove offset when positioning according to element position
// is working.
posOffset: 10
},
options
)
)
)
// Placing the popup when it required loading,
// it resulted in flahing up at the default position.
// We manually prevent this here.
2019-07-18 12:26:39 +02:00
popup.element.style.display = 'none'
2019-07-18 12:26:39 +02:00
let promise = new Promise(resolve => {
if (popup.loaded) resolve(popup)
else {
popup.onload = () => {
resolve(popup)
}
}
})
2019-07-18 12:26:39 +02:00
promise.then(popup => {
popup.element.style.display = 'block'
popup.element.style.visibility = 'hidden'
popup.element.style.opacity = 0
popup.placeAt(position)
InteractionMapper.on(this.interactionType, popup.element, e => {
this._cleanup(context)
})
Object.assign(popup.element.style, {
zIndex: this.zIndices.popup
})
2019-07-17 10:04:16 +02:00
TweenLite.to(popup.element, this.animation.popup, {
autoAlpha: 1,
ease: Power2.easeIn
})
})
return promise
}
static _overlayCleanup(context, overlay) {
/**
* The cleanup functionality is now covered by the _cleanup function.
* It cleans up zoomables, popups and open image highlights.
*
* TEST if this intereferes with the editor.
*/
if (overlay) {
2019-07-17 10:04:16 +02:00
TweenLite.to(overlay, 0.2, {
2019-07-18 12:26:39 +02:00
autoAlpha: 0,
onComplete: () => {
popup.remove()
//this._cleanup(context)
//overlay.parentNode.removeChild(overlay)
}
})
}
}
/**
* Opens a popup for a glossary. Typically used as a onlick handler of a link.
*
* @static
* @param {any} event - The trigger event, typically a click event
* @returns {bool} false - Returns false to prevent default click action
* @memberof Card
*/
static loadPopup(event, context = null, node = null, local = null) {
let editable = Card.isEditable()
if (context == null) {
context = this.getContext(event.target)
}
if (node == null) {
node = event.target
}
if (local == null) {
2019-07-30 16:56:29 +02:00
let globalClick = event.center ? event.center : { x: event.x, y: event.y }
local = Points.fromPageToNode(context, globalClick)
}
2019-07-18 12:26:39 +02:00
if (this.debug) console.log('loadPopup', event)
// Prevents loading the link in the current tab.
// Prevents loading the link in the current tab.
2019-07-18 12:26:39 +02:00
if (event.type != 'Follow') event.preventDefault()
if (editable && event.type == 'click') {
return false
}
let overlay = document.createElement('div')
2019-07-18 12:26:39 +02:00
let src = node.getAttribute('href')
let parentArticle = node.closest('article')
const that = this
let xhr = new XMLHttpRequest()
xhr.open('get', src, true)
xhr.onreadystatechange = () => {
2019-07-18 12:26:39 +02:00
if (this.debug) console.log('Popup Source: ', src)
if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
if (editable) {
2019-07-18 12:26:39 +02:00
if (this.debug) console.log('Append overlay.', context)
overlay.classList.add('overlay')
2019-07-17 10:04:16 +02:00
TweenLite.set(overlay, { autoAlpha: 0 })
context.appendChild(overlay)
2019-07-17 10:04:16 +02:00
TweenLite.to(overlay, 0.5, { autoAlpha: 0.25 })
}
// Extract the body from the Popup site.
let parser = new DOMParser()
2019-07-30 16:56:29 +02:00
let popupPage = parser.parseFromString(xhr.responseText, 'text/html')
//Fix the relative path of loaded images in the popup.
2019-07-18 12:26:39 +02:00
popupPage.querySelectorAll('img').forEach(node => {
2019-07-30 16:56:29 +02:00
node.setAttribute('src', that._getRelativePath(node.getAttribute('src')))
})
let html = popupPage.body.innerHTML
/**
* We do not want to use the popup class again.
* This results in problems when styling the popup.
* (You could style the .unselectable.popup, but that
* wouldn't be clean).
*
* Therefore the selector was changed:
* '.popup' => '.popupHtml'
*
* Which describes the construct even better than the
* '.popup' class.
*/
let selector = Card.popupHtmlSelector
let content = { html, selector }
let isSame = Card._checkForActiveSource(context, src)
Card._cleanup(context)
if (!isSame) {
2019-07-30 16:56:29 +02:00
Card._activateCorrespondingHighlights(context, node, parentArticle)
let callback = (popup, callback) => {
2019-07-30 16:56:29 +02:00
if (this.debug) console.log('Close popup (Editable = ' + editable + ').', popup)
if (editable) {
let isDirty = mainController.askSaveNode()
if (isDirty)
mainController.saveNode(html.innerHTML, url => {
if (callback) {
callback()
}
saved(url)
this._overlayCleanup(context, overlay)
})
else {
if (callback) {
callback()
}
this._overlayCleanup(context, overlay)
}
mainController.popController()
2019-07-18 12:26:39 +02:00
} else {
/** This may be in conflice with the cleanup method. */
//this._overlayCleanup(context, overlay)
popup.remove()
}
}
Card._openPopup(context, src, local, content, {
closeCommand: callback
})
}
}
overlay.onclick = e => {
2019-07-18 12:26:39 +02:00
if (editable) e.preventDefault()
}
//console.log("onreadystatechange", editable)
if (editable) {
2019-07-30 16:56:29 +02:00
if (this.debug) console.log('pushController', src, popup.insertedNode)
mainController.pushController(popup.insertedNode, src)
}
}
xhr.send()
return false
}
/**
2019-07-18 12:26:39 +02:00
* When an highlight link is clicked, this method activates all
* corresponding highlights.
*
* @static
* @param {DomElement} context - The context of the element.
* @param {DomElement} node - The node that (may) contain a highlightId.
* @param {DomElement} parent - The parent element that may contain more highlightIds.
* @memberof Card
*/
static _activateCorrespondingHighlights(context, node, parent) {
let highlightId = node.getAttribute('data-highlight-id')
// console.log("Request Highlight: " + highlightId)
let correspondingHighlights = []
if (highlightId) {
let queryString = 'circle[data-highlight-id="' + highlightId + '"]'
if (parent) {
correspondingHighlights = parent.querySelectorAll(queryString)
}
if (correspondingHighlights.length > 0) {
for (let highlight of correspondingHighlights) {
2019-07-30 16:56:29 +02:00
if (highlight.parentNode && highlight.parentNode.nodeName.toLowerCase() == 'g') {
2019-07-18 12:26:39 +02:00
Highlight.openHighlight(highlight, {
animation: Card.highlightAnimation
})
this._addHighlight(context, highlight)
}
}
}
}
}
/**
2019-07-18 12:26:39 +02:00
* Tests if any open item already contains the requested Source.
*
* @static
* @param {DomElement} context - Dom context we are in.
* @param {string} src - Source as dataUrl.
* @returns {boolean} - True if source is already active, false otherwise.
* @memberof Card
*/
static _checkForActiveSource(context, src) {
let requestedSame = false
let activePopup = Card._getPopup(context)
let activeHighlights = Card._getHighlights(context)
if (activePopup) {
let activeSrc = Card._getPopupSource(activePopup)
if (activeSrc && src == activeSrc) {
requestedSame = true
}
}
if (!requestedSame) {
for (let highlight of activeHighlights) {
let activeSrc = Card._getHighlightSource(highlight)
if (activeSrc == src) {
requestedSame = true
break
}
}
}
return requestedSame
}
/**
* Opens a popup for a highlight. Typically used as a onlick handler of a link.
*
* @static
* @param {any} event - The trigger event, typically a click event
* @returns {bool} false - Returns false to prevent default click action
* @memberof Card
*/
static loadHighlightPopup(event) {
2019-07-18 12:26:39 +02:00
if (this.debug) console.log('Load Highlight Popup: ', event)
let node
if (event.firstTarget) {
node = event.firstTarget
} else {
node = event.target
}
let context = this.getContext(node)
event.stopPropagation()
/**
2019-07-18 12:26:39 +02:00
* This node is the documents body, as events wont work
* on svg elements properly. We need a workaround for that.
*/
let src = node.getAttribute('xlink:href')
let isSame = this._checkForActiveSource(context, src)
this._cleanup(context)
if (!isSame) {
this._addHighlight(context, node)
Highlight.openHighlight(node, {
animation: Card.highlightAnimation,
onExpanded: () => {
// We assume it's always a circle. This may break, when other svg shapes are used.
2019-07-18 12:26:39 +02:00
let x = node.getAttribute('cx')
let y = node.getAttribute('cy')
let position = { x, y }
2019-07-18 12:26:39 +02:00
let radius = parseFloat(node.getAttribute('r'))
/*
As the popup is appended directly to the card. We have to
transform the location from the subcard-/svg-space to the
card space.
*/
2019-07-18 12:26:39 +02:00
let svgRoot = node.closest('svg')
let svgPoint = svgRoot.createSVGPoint()
svgPoint.x = position.x
svgPoint.y = position.y - radius
let matrix = node.getCTM()
let point = svgPoint.matrixTransform(matrix)
2019-07-30 16:56:29 +02:00
let global = Points.fromNodeToPage(node.closest('div'), point)
let local = Points.fromPageToNode(context, global)
let overlay = document.createElement('div')
//TODO: Check if this is necessary for the editor.
// overlay.onclick = e =>{}
// This is not really efficient, as
// we could load the data while the circle is animating.
// but for simplicity it's just done here for now.
// TODO: Adjust to load while animating (Problem: Unload when cancelled).
console.log(src)
this._loadPopupContent(src)
.then(content => {
this._openPopup(context, src, local, content, {
highlight: node,
closeCommand: (popup, callback) => {
this._overlayCleanup(context, overlay)
popup.remove()
}
})
})
2019-07-18 12:26:39 +02:00
.catch(err => {
console.error(err)
})
}
})
}
}
/**
2019-07-18 12:26:39 +02:00
* Loads the popup from a provided source.
*
* @static
* @private
* @param {string} source - Url to a popup file.
* @returns {Promise} - Returns a promise, that's resolved when the data is loaded.
* @memberof Card
*/
static _loadPopupContent(source) {
return new Promise((resolve, reject) => {
let xhr = new XMLHttpRequest()
xhr.open('get', source, true)
xhr.onreadystatechange = () => {
if (xhr.readyState == 4) {
2019-07-18 12:26:39 +02:00
if (xhr.status == 200 || xhr.status == 0) {
2019-07-30 16:56:29 +02:00
let html = this.postProcessResponseText(xhr.responseText)
let selector = Card.popupHtmlSelector
let content = { html: html.body.innerHTML, selector }
resolve(content)
} else {
2019-07-30 16:56:29 +02:00
reject(` Popup request failed (Code: ${xhr.status}): Could not load resource: ${src}`)
}
}
}
xhr.send()
})
}
static openZoomable(event) {
let node = event.target
//console.log("Open zoomable: ", node, node.classList)
let editable = Card.isEditable()
if (editable) {
2019-07-30 16:56:29 +02:00
if (node.classList.contains('zoomable-icon')) this._openZoomable(event)
return
}
this._openZoomable(event)
}
/**
* Retrieve a Rectangle object from the a given zoomable.
*
* @static
* @param {DomElement} zoomable - The zoomable, the rectangle should be calculated for.
* @param {DomElement} wrapper - Specifies the coordinate system of the generated rectangle.
* @returns {object} - Returns an object of the rectangle in the wrapper's coordinate system in the form of {x ,y ,width , height}.
* @memberof Card
*/
static zoomableCurrentGeometry(zoomable, wrapper) {
/*
I don't think it's wise, that the zoomable calculation relies on
some icon that may or may not be present. When the same calculation can be
done using the bounding box of the desired element.
- SO
*/
//The div is cloned and animated, therefore we need it's style!
2019-07-18 12:26:39 +02:00
let actuallyZoomedItem = zoomable.querySelector('div')
let zoomableStyle = window.getComputedStyle(actuallyZoomedItem)
2019-07-18 12:26:39 +02:00
let svgElement = zoomable.querySelector('svg')
let videoElement = zoomable.querySelector('video')
let curStyle = null
if (svgElement) {
curStyle = window.getComputedStyle(svgElement)
}
if (videoElement) {
curStyle = window.getComputedStyle(videoElement)
}
let imageWidth = parseFloat(curStyle.width)
let imageHeight = parseFloat(curStyle.height)
let globalBottomRight = {
x: imageWidth,
y: imageHeight
}
globalBottomRight = Points.fromNodeToPage(zoomable, globalBottomRight)
let globalFigurePos = Points.fromNodeToPage(zoomable, { x: 0, y: 0 })
let localFigurePos = Points.fromPageToNode(wrapper, globalFigurePos)
2019-07-30 16:56:29 +02:00
let relativeBottomRight = Points.fromPageToNode(zoomable, globalBottomRight)
2019-07-18 12:26:39 +02:00
let width =
relativeBottomRight.x +
parseFloat(zoomableStyle.borderLeftWidth) +
parseFloat(zoomableStyle.borderRightWidth) /*+ zoomIconRight*/
let height =
relativeBottomRight.y +
parseFloat(zoomableStyle.borderTopWidth) +
parseFloat(zoomableStyle.borderBottomWidth) /*+ zoomIconRight*/
return { x: localFigurePos.x, y: localFigurePos.y, width, height }
}
/**
2019-07-18 12:26:39 +02:00
* Opens a zoomable object, which can be a figure containing an image or video or an image group
*
* @static
* @param {any} event - The trigger event, typically a click event
* @memberof Card
*/
static _openZoomable(event, editable = false) {
event.stopPropagation()
let node = event.target
let wrapper = this.getContext(node)
let zoomable = node.closest('figure')
// load mainimg - if none exists, there is nothing to open
let img = zoomable.querySelector('.mainimg')
if (img == null) {
return
}
//close other open elements before opening the zoomable:
//let removeDelay = this.removeActiveZoomable(wrapper, true) ? 0 : 0
this._cleanup(wrapper)
// Clone the zoomable and create a clone that is zoomed instead.
let current = this.zoomableCurrentGeometry(zoomable, wrapper)
let zoomedFig = zoomable.cloneNode(true)
2019-07-18 12:26:39 +02:00
let caption = zoomedFig.querySelector('figcaption.cap')
caption.parentNode.removeChild(caption)
this._toggleNextIcon(zoomedFig)
this._setZoomable(wrapper, zoomable, zoomedFig)
if (!editable) {
/**
* I don't remember why we need yet another wrapper for the zoomable.
* This should be investigated.
* -SO
*/
let zoomContainer = document.createElement('div')
2019-07-18 12:26:39 +02:00
zoomContainer.classList.add('zoomable-wrapper')
Object.assign(zoomContainer.style, {
2019-07-18 12:26:39 +02:00
position: 'absolute',
top: 0,
left: 0,
zIndex: 200
})
wrapper.appendChild(zoomContainer)
//Note: Remaned ZoomedFigure to zoomed-figure
zoomedFig.className = 'zoomed-figure'
zoomedFig.style.zIndex = this.zIndices.zoomedFigure
let zoomedG = zoomedFig.querySelector('g')
2019-07-18 12:26:39 +02:00
if (zoomedG && !editable) zoomedG.parentNode.removeChild(zoomedG)
zoomContainer.appendChild(zoomedFig)
let zoomedImg = zoomedFig.querySelector('.mainimg')
zoomedImg.removeAttribute('onclick')
//set the values of corresponding zoomable element (needed when the zoomable is closed indirectly via removeActiveZoomable)
zoomedFig.zoomable = zoomable
zoomedFig.zoomableGeometry = current
//play video
2019-07-18 12:26:39 +02:00
let videoElement = zoomedFig.querySelector('video')
if (videoElement) {
videoElement.play()
}
//logging
2019-07-18 12:26:39 +02:00
let imgElt = zoomedFig.querySelector('image')
let videoElt = zoomedFig.querySelector('video')
//console.log('open zoomable:',imgElt,videoElt)
if (imgElt) {
let imgSrc = imgElt.getAttribute('xlink:href')
2019-07-18 12:26:39 +02:00
let strparts = imgSrc.split('/')
let cardID = strparts[strparts.length - 2]
let cardName = strparts[strparts.length - 1]
let msg = 'ShowImage: ' + cardID + '/' + cardName
console.log('Logging:', msg)
Logging.log(msg)
}
if (videoElt) {
let videoSrc = videoElt.getAttribute('src')
2019-07-18 12:26:39 +02:00
let strparts = videoSrc.split('/')
let cardID = strparts[strparts.length - 2]
let cardName = strparts[strparts.length - 1]
let msg = 'ShowVideo: ' + cardID + '/' + cardName
console.log('Logging:', msg)
Logging.log(msg)
}
// (At least in this style) We don't want the user to 'hit' the icon
// It's just an indicator that an action is possible. The click must be
// captured from the whole subcard.
2019-07-18 12:26:39 +02:00
InteractionMapper.on(this.interactionType, zoomedFig, event => {
this._cleanup(wrapper)
})
let zoomedFigStyle = window.getComputedStyle(zoomedFig)
2019-07-30 16:56:29 +02:00
let borderX = parseFloat(zoomedFigStyle.borderLeftWidth) + parseFloat(zoomedFigStyle.borderRightWidth)
let borderY = parseFloat(zoomedFigStyle.borderBottomWidth) + parseFloat(zoomedFigStyle.borderTopWidth)
const scaleFactor = 2
const transformOrigin = 'bottom right'
2019-07-17 10:04:16 +02:00
TweenLite.set(zoomedFig, {
x: current.x,
y: current.y,
width: current.width + borderX,
height: current.height + borderY,
transformOrigin
})
2019-07-17 10:04:16 +02:00
TweenLite.set(zoomable, { opacity: 0 })
2019-07-18 12:26:39 +02:00
let icon = zoomedFig.querySelector('.icon')
2019-07-17 10:04:16 +02:00
TweenLite.set(icon, {
transformOrigin
})
2019-07-18 12:26:39 +02:00
zoomedFig.style.transformOrigin =
'calc(100% - ' +
parseFloat(zoomedFigStyle.borderRightWidth) +
'px) calc(100% - ' +
parseFloat(zoomedFigStyle.borderBottomWidth) +
'px)'
let tl = new TimelineLite()
2019-07-18 12:26:39 +02:00
let zoomCaption = zoomedFig.querySelector('.zoomcap')
2019-07-18 12:26:39 +02:00
tl.to(
zoomedFig,
Card.animation.zoomable,
{
ease: Power2.easeIn,
css: {
scaleX: scaleFactor,
scaleY: scaleFactor
}
},
0
)
.set(zoomCaption, {
css: {
display: 'block',
opacity: 0,
x: -parseFloat(zoomedFigStyle.borderLeftWidth),
width: current.width + borderX
}
})
.to(zoomCaption, this.animation.fade, {
autoAlpha: 1
})
2019-07-30 16:56:29 +02:00
} else this._openZoomableEditorBehaviour(wrapper, img, zoomable, zoomedFig, current)
}
/**
2019-07-18 12:26:39 +02:00
* Selects and transforms the zoomicon from a zoomicon to a closeicon
* or the other way around.
*
* @static
* @param {DomElement} parent - Parent to be searched for an zoomicon.
* @memberof Card
*/
static _toggleNextIcon(parent) {
let zoomIcon = Card._findNextIcon(parent)
2019-07-18 12:26:39 +02:00
const closeClass = 'close'
const zoomClass = 'zoom'
if (zoomIcon.classList.contains(closeClass)) {
zoomIcon.classList.remove(closeClass)
zoomIcon.classList.add(zoomClass)
} else if (zoomIcon.classList.contains(zoomClass)) {
zoomIcon.classList.remove(zoomClass)
zoomIcon.classList.add(closeClass)
} else {
2019-07-18 12:26:39 +02:00
console.error(
`Error Toggleing Zoomicon: It did neither contain a class named ${closeClass} or ${zoomClass}.`
)
}
}
static _findNextIcon(parent) {
return parent.querySelector('.icon')
}
2019-07-30 16:56:29 +02:00
static _openZoomableEditorBehaviour(wrapper, img, zoomable, zoomedFig, current) {
let zoomContainer = document.createElement('div')
let zoomIcon = zoomable.querySelector('.zoom-icon')
zoomContainer.style.position = 'relative'
zoomContainer.style.zIndex = this.zIndices.zoomable
wrapper.appendChild(zoomContainer)
let zoomParent = zoomable.parentNode
zoomParent.appendChild(zoomedFig)
zoomedFig.style.opacity = 0.5
zoomContainer.appendChild(zoomable)
2019-07-18 12:26:39 +02:00
TweenLite.set(zoomable, {
x: current.x,
y: current.y,
width: current.width,
height: current.height
})
let editor = mainController.topController().ensureEditor(img)
let savedDisplay = zoomIcon.style.display
let iconClone = zoomIcon.cloneNode(true)
zoomIcon.style.display = 'none'
let savedOnClick = zoomIcon.getAttribute('onclick')
zoomIcon.removeAttribute('onclick')
iconClone.src = iconClone.src.replace('info.svg', 'close.svg')
iconClone.style.zIndex = 1000
iconClone.classList.add('zoomed-icon')
iconClone.onclick = event => {
zoomIcon.style.display = savedDisplay
iconClone.remove()
zoomIcon.setAttribute('onclick', savedOnClick)
zoomedFig.remove()
zoomContainer.remove()
zoomParent.appendChild(zoomable)
2019-07-17 10:04:16 +02:00
TweenLite.set(zoomable, { x: 0, y: 0 })
zoomable.onmousedown = null
zoomable.onmousemove = null
zoomable.onmouseup = null
zoomable.onmousewheel = null
}
wrapper.appendChild(iconClone)
2019-07-17 10:04:16 +02:00
TweenLite.set(iconClone, { x: current.iconPos.x, y: current.iconPos.y })
zoomable.onmousedown = event => {
if (this.debug) console.log('mousedown', event.target)
event.preventDefault()
zoomable.dragging = true
zoomable.dragStartPos = { x: event.pageX, y: event.pageY }
}
zoomable.onmousemove = event => {
if (this.debug) console.log('onmousemove', event.target)
if (zoomable.dragging) {
event.preventDefault()
let dx = event.pageX - zoomable.dragStartPos.x
let dy = event.pageY - zoomable.dragStartPos.y
2019-07-18 12:26:39 +02:00
TweenLite.set([zoomable, iconClone], {
x: '+=' + dx,
y: '+=' + dy
})
zoomable.dragStartPos = { x: event.pageX, y: event.pageY }
if (editor) {
editor.showControls()
}
}
}
zoomable.onmouseup = event => {
zoomable.dragging = false
}
let startZoom = 1
zoomable.onmousewheel = event => {
event.preventDefault()
let direction = event.detail < 0 || event.wheelDelta > 0
const zoomFactor = 1.1
let zoom = direction ? zoomFactor : 1 / zoomFactor
startZoom *= zoom
2019-07-17 10:04:16 +02:00
TweenLite.set(zoomable, { scale: startZoom })
if (editor) {
editor.showControls()
}
}
return
}
/**
* Closes a zoomable object with animation
*
* @static
* @param {any} wrapper - the wrapper containing the index card
* @param {any} div - the figure containing the relevant elements
* @param {any} zoomable - the zoomable element, from which the zoomed figure originates
* @param {any} rect - the target rect for the tween (typically the top left width height of the zoomable)
* @memberof Card
*/
static closeZoomable(context, zoomable, zoomedFig) {
2019-07-30 16:56:29 +02:00
if (this.debug) console.log('Close Zoomable', context, zoomable, zoomedFig)
if (zoomable) {
this._unsetZoomable(context)
2019-07-18 12:26:39 +02:00
let caption = zoomable.querySelector('figcaption.cap')
zoomable.removeChild(caption)
zoomable.appendChild(caption)
2019-07-18 12:26:39 +02:00
let zoomedCaption = zoomedFig.querySelector('figcaption.zoomcap')
2019-07-17 10:04:16 +02:00
TweenLite.to(zoomedCaption, this.animation.fade, {
2019-07-18 12:26:39 +02:00
autoAlpha: 0
})
2019-07-17 10:04:16 +02:00
TweenLite.to(zoomedFig, this.animation.zoomable, {
css: {
scaleX: 1,
scaleY: 1
},
onComplete: () => {
2019-07-17 10:04:16 +02:00
TweenLite.set(zoomable, {
opacity: 1
})
let div = zoomedFig.parentNode
let videoElement = div.querySelector('video')
2019-07-18 12:26:39 +02:00
if (videoElement) videoElement.pause()
div.parentNode.removeChild(div)
}
})
}
}
/**
* Starts and controls the expansion animation of an index card.
* The assumed card structure is as follows:
* <div class="card">
* <div class="card-content content">
* <h1 class="has-text-centered" contenteditable="true">...</h1>
* <div class="wrapper">
* <div data-innerhtml="preview" class="preview">...</div>
* <img class="zoom-icon cardicon" onclick="Card.openIndexCard(event, './article3.html')" src="../_theme/icons/info.svg">
* </div>
* </div>
* </div>
* The basic idea is that the card is cloned. In the animation the clone si expanded while the original is faded out.
*
* @param {*} card - The card to expand
* @param {string} html - The original HTML of the card
* @param {*} tagName - The tagname of the element that is used as exanded element
* @param {*} src - The src of the expanded element
* @param {*} callback - A callback that is called when the expanded element is closed
*/
2019-07-30 16:56:29 +02:00
static expandIndexCard(card, html, tagName = 'article', src = null, callback = null) {
let editable = Card.isEditable()
let context = this.getContext(card)
let indexbox = this.closestWithClass(card, 'mainview')
this._cleanup(context)
let angle = 0 // UO: out of context, to be computed from the scatter
let clone = card.cloneNode(true)
/**
* We have to reorder the clone, as it still contains the
* preview text image. And the new html is
* inserted before everything else.
*/
2019-07-18 12:26:39 +02:00
let cloneWrapper = clone.querySelector('.wrapper')
const article = html.body.querySelector('article')
2019-07-18 12:26:39 +02:00
let subcardContent = clone.querySelector('.subcard-content')
subcardContent.appendChild(article)
cloneWrapper.parentNode.removeChild(cloneWrapper)
/* Removes the 'default' cleanup on the card */
2019-07-18 12:26:39 +02:00
clone.removeAttribute('onclick')
InteractionMapper.on(this.interactionType, clone, event => {
this._cleanup(context)
})
let articleClone = clone.querySelector(tagName)
let globalPreviewRect = Card._getGlobalRect(card)
let globalIndexCardRect = Card._getGlobalRect(indexbox)
2019-07-30 16:56:29 +02:00
let localOrigin = Points.fromPageToNode(indexbox, Rect.getPosition(globalPreviewRect))
let scaleX = globalPreviewRect.width / globalIndexCardRect.width
let scaleY = globalPreviewRect.height / globalIndexCardRect.height
let padding = parseInt(this.css(indexbox, 'padding'))
let maxWidth = this.css(card, 'max-width')
2019-07-17 10:04:16 +02:00
TweenLite.set(clone, {
css: {
position: 'absolute',
width: globalIndexCardRect.width,
height: globalIndexCardRect.height,
maxWidth: '100%',
margin: 0,
zIndex: this.zIndices.article
}
})
2019-07-17 10:04:16 +02:00
TweenLite.set(articleClone, {
autoAlpha: 0
})
2019-07-17 10:04:16 +02:00
TweenLite.set(card, { css: { maxWidth: '100%' } })
TweenLite.set(clone, {
x: localOrigin.x - padding,
y: localOrigin.y - padding,
scaleX,
scaleY,
transformOrigin: '0% 0%',
2019-07-18 12:26:39 +02:00
rotation: angle
})
indexbox.prepend(clone)
2019-07-18 12:26:39 +02:00
let titlebar = clone.querySelector('.titlebar')
let title = titlebar.querySelector('h2')
let titlebarStyle = window.getComputedStyle(titlebar)
let start = {
height: parseInt(titlebarStyle.height)
}
if (this.dynamicHeight) {
2019-07-18 12:26:39 +02:00
let targetHeight = subcardContent.offsetHeight
console.log(targetHeight)
2019-07-18 12:26:39 +02:00
subcardContent.classList.add('dynamic-height')
/**
* Scale the content from 100% to it's target size.
*/
2019-07-17 10:04:16 +02:00
// TweenLite.set(subcardContent, {
// height: "100%"
// })
2019-07-17 10:04:16 +02:00
// TweenLite.to(subcardContent, Card.animation.articleTransition, {
// height: targetHeight + "px"
// })
}
//jquery hyphenate below
2019-07-18 12:26:39 +02:00
if (typeof $ != 'undefined') {
$('.column')
.not('.overview')
.children('p')
.hyphenate('de')
2019-07-16 10:04:25 +02:00
}
//logging
if (src) {
2019-07-18 12:26:39 +02:00
let strparts = src.split('/')
let cardID = strparts[strparts.length - 2]
let cardName = strparts[strparts.length - 1]
2019-07-18 12:26:39 +02:00
strparts = card.className.split(' ')
let cardType = strparts[1]
2019-07-30 16:56:29 +02:00
let msg = 'Card: ' + cardID + ': openTopic: ' + cardType + ', ' + cardName
console.log('Logging:', msg)
Logging.log(msg)
}
2019-07-30 16:56:29 +02:00
let desiredBorderBottomWidth = parseInt(window.getComputedStyle(titlebar).borderBottomWidth)
2019-07-17 10:04:16 +02:00
TweenLite.to(clone, Card.animation.articleTransition, {
x: -padding,
y: -padding,
ease: ExpoScaleEase.config(scaleX, 1),
scale: 1,
rotation: 0,
onComplete: () => {
2019-07-18 12:26:39 +02:00
card.classList.add('visited')
},
onUpdateParams: ['{self}'],
2019-07-18 12:26:39 +02:00
onUpdate: self => {
let transform = self.target._gsTransform
2019-07-17 10:04:16 +02:00
TweenLite.set(title, {
scale: 1 / transform.scaleX
})
2019-07-17 10:04:16 +02:00
TweenLite.set(titlebar, {
2019-07-18 12:26:39 +02:00
height: (start.height * 1) / transform.scaleY
})
// Retain the border at same visual thickness.
2019-07-30 16:56:29 +02:00
titlebar.style.borderBottomWidth = desiredBorderBottomWidth / transform.scaleY + 'px'
}
})
2019-07-17 10:04:16 +02:00
TweenLite.to([articleClone], this.animation.articleTransition / 2, {
delay: this.animation.articleTransition / 2,
autoAlpha: 1
})
if (editable) {
mainController.pushController(articleClone, src)
}
Card._disableCardCloseButton(context)
const closeAnimation = () => {
//logging
if (src) {
2019-07-18 12:26:39 +02:00
let strparts = src.split('/')
let cardID = strparts[strparts.length - 2]
let cardName = strparts[strparts.length - 1]
2019-07-18 12:26:39 +02:00
strparts = card.className.split(' ')
let cardType = strparts[1]
2019-07-30 16:56:29 +02:00
let msg = 'Card: ' + cardID + ': closeTopic: ' + cardType + ', ' + cardName
console.log('Logging:', msg)
Logging.log(msg)
}
Card._cleanup(context)
Card._unsetSubcard(context)
this._enableCardCloseButton(context)
2019-07-18 12:26:39 +02:00
let previewTitlebar = card.querySelector('.titlebar')
let titlebarStyle = window.getComputedStyle(previewTitlebar)
2019-07-18 12:26:39 +02:00
let titlebar = clone.querySelector('.titlebar')
2019-07-17 10:04:16 +02:00
TweenLite.to(titlebar, this.animation.articleTransition, {
height: parseInt(titlebarStyle.height)
})
2019-07-17 10:04:16 +02:00
TweenLite.to(articleClone, this.animation.articleTransition / 2, {
autoAlpha: 0
})
2019-07-18 12:26:39 +02:00
let title = titlebar.querySelector('h2')
let original = {
height: parseInt(titlebarStyle.height)
}
if (this.dynamicHeight) {
2019-07-17 10:04:16 +02:00
TweenLite.to(subcardContent, this.animation.articleTransition, {
2019-07-18 12:26:39 +02:00
height: '100%'
})
}
2019-07-17 10:04:16 +02:00
TweenLite.set(card, { autoAlpha: 1, css: { maxWidth } })
TweenLite.to(clone, this.animation.articleTransition, {
x: localOrigin.x - padding,
y: localOrigin.y - padding,
scaleX,
scaleY,
ease: ExpoScaleEase.config(1, scaleX),
rotation: angle,
onComplete: () => {
// article.remove()
2019-07-18 12:26:39 +02:00
TweenLite.to(clone, this.animation.fade, {
//delay: 0.2,
autoAlpha: 0,
onComplete: () => {
if (editable) {
mainController.popController()
}
clone.remove()
}
})
},
2019-07-18 12:26:39 +02:00
onUpdateParams: ['{self}'],
onUpdate: function(self) {
let transform = self.target._gsTransform
2019-07-17 10:04:16 +02:00
TweenLite.set(title, {
scale: 1 / transform.scaleX
})
2019-07-17 10:04:16 +02:00
TweenLite.set(titlebar, {
2019-07-18 12:26:39 +02:00
height: (original.height * 1) / transform.scaleY
})
// Retain the border at same visual thickness.
2019-07-30 16:56:29 +02:00
titlebar.style.borderBottomWidth = desiredBorderBottomWidth / transform.scaleY + 'px'
}
})
}
//TODO consider renaming it to something more intuitive.
let iconClone = clone.querySelector('.card-icon')
if (iconClone == null) {
2019-07-18 12:26:39 +02:00
iconClone = clone.querySelector('.cardicon')
2019-07-30 16:56:29 +02:00
console.warn("Legacy selector. Change it to 'card-icon' and find a more suitable name.")
}
2019-07-18 12:26:39 +02:00
if (iconClone.tagName == 'img') {
iconClone.src = iconClone.src.replace('info.svg', 'close.svg')
}
//console.log("ICON: ", iconClone)
2019-07-18 12:26:39 +02:00
iconClone.classList.remove('info')
2019-07-30 16:56:29 +02:00
iconClone.classList.add('close', 'view-button', 'transparent-background')
// We append the icon clone to the subcard-content.
// Then it's always on the content and not on the background
// (If there is one).
if (this.dynamicHeight) {
article.appendChild(iconClone)
}
if (enableNearestNeighborTaps) {
//look for nearby popups on tap
2019-07-18 12:26:39 +02:00
InteractionMapper.on('tap', indexbox, () => {
console.log('Tap handler called', editable)
if (!editable) {
this.findNearbyPopups(event, card)
}
})
}
// Use the 'tap' event for closing.
// Otherwise the subcard cannot be closed,
// when another subcard is touched.
2019-07-18 12:26:39 +02:00
InteractionMapper.on('tap', iconClone, () => {
if (editable) {
let isDirty = mainController.askSaveNode()
if (isDirty) {
mainController.saveNode(html.innerHTML, url => {
callback(url)
closeAnimation()
})
2019-07-18 12:26:39 +02:00
} else {
closeAnimation()
}
2019-07-18 12:26:39 +02:00
} else {
closeAnimation()
}
})
}
/**
* Opens the index card. Called by the zoom icon click handler.
* The assumed card structure is as follows:
* <div class="card">
* <div class="card-content content">
* <h1 class="has-text-centered" contenteditable="true">...</h1>
* <div class="wrapper">
* <div data-innerhtml="preview" class="preview">...</div>
* <img class="zoom-icon cardicon" onclick="Card.openIndexCard(event, './article3.html')" src="../_theme/icons/info.svg">
* </div>
* </div>
* </div>
* Loads the DOM element from a file source and adds it before the card-content to ensure that the article content
* can be positioned below the heading.
*
* <div class="card">
* <article> INSERTED CONTENT </article>
* <div class="card-content content">
* ...
* </div>
* </div>
*
* @static
* @param {any} event - The trigger event, typically a click event
* @param {string} src - The file source, typically a relative url
* @memberof Card
*/
static openIndexCard(event, src) {
//console.log("openIndexCard", src)
/*
2019-07-18 12:26:39 +02:00
* Called by the expandIndexCard(...)
*/
let target = event.target
2019-07-18 12:26:39 +02:00
const saveCallback = url => {
let handler = `Card.openIndexCard(event, '${url}')`
2019-07-18 12:26:39 +02:00
console.log('File has changed', target, handler)
//TODO If this is required, it should be accessing the interaction type.
2019-07-18 12:26:39 +02:00
target.setAttribute('onclick', handler)
}
let context = this.getContext(target)
let subcard = this._getSubcard(context)
//console.log("openIndexCard", { context, subcard })
//Dont proceeed if a subcard is active
2019-07-18 12:26:39 +02:00
if (subcard != null) return
// In edit mode we only accept icon clicks
let editable = Card.isEditable()
if (editable && !target.classList.contains('icon')) {
return
}
let card = this.closestWithClass(target, 'subcard')
this._setSubcard(context, card)
const relativeSource = this._getRelativePath(src)
let xhr = new XMLHttpRequest()
xhr.open('get', relativeSource, true)
xhr.onreadystatechange = () => {
if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
let html = xhr.responseText
let parsedHTML = this.postProcessResponseText(html)
// TODO: What is this good for?
// let article = parsedHTML.querySelector('article')
// card.insertAdjacentElement('afterbegin', article)
2019-07-17 10:04:16 +02:00
// TweenLite.set(article, { autoAlpha: 0 })
2019-07-30 16:56:29 +02:00
Card.expandIndexCard(card, parsedHTML, 'article', relativeSource, saveCallback)
}
}
xhr.onerror = () => {
alert('Unable to load ' + src)
}
xhr.send()
}
static _selectCardCloseButton(context) {
2019-07-18 12:26:39 +02:00
return context.querySelector('.mainview > .button.close')
}
static _enableCardCloseButton(context) {
//console.log("ENABLE")
let btn = this._selectCardCloseButton(context)
//console.log(btn)
2019-07-18 12:26:39 +02:00
btn.classList.remove('disabled')
}
static _disableCardCloseButton(context) {
let btn = this._selectCardCloseButton(context)
2019-07-18 12:26:39 +02:00
btn.classList.add('disabled')
}
/**
* Provides the nearest info-card of the provided item.
*
* @static
* @param {DomElement} child - A dom element for which the context shall be retrieved.
* @returns {DomElement} - The containing context / info-card.
* @memberof Card
*/
static getContext(child) {
2019-07-18 12:26:39 +02:00
let dom = child.closest('.info-card')
if (!dom) {
2019-07-18 12:26:39 +02:00
dom = child.querySelector('.wrapper')
2019-07-30 16:56:29 +02:00
console.warn("Change the 'wrapper' class to 'info-card' it's more suitable.")
}
return dom // (dom == null) ? document.body : dom
}
/**
* The cleanup method closes all active Popups, Zoomables and Highlights.
*
* @static
* @param {DomElement} context - The dom element to inspect.
* @memberof Card
*/
static _cleanup(context) {
let [zoomable, zoomedFig] = this._getZoomable(context)
if (zoomable && zoomedFig) {
this.closeZoomable(context, zoomable, zoomedFig)
}
let popup = this._getPopup(context)
if (popup) {
this.closePopup(context, popup)
}
let highlights = this._getHighlights(context)
highlights.forEach(highlight => {
2019-07-18 12:26:39 +02:00
Highlight.closeHighlight(highlight, {
animation: Card.highlightAnimation
})
})
this._unsetHighlights(context)
}
/**
* Retrieves an Rectangle for an element in the local space of a provided context.
*
* Note: This works also for rotated DomElements unlike the Element.getBoundingClientRectangle method.
*
* @static
* @param {DomElement} context - The element that defines the space, we want the rectangle to be in.
* @param {DomElement} element - The element, we want to get the rectangle for.
* @returns {DomRect} - Returns a DomRect representing the element outline in the space of context.
* @memberof Card
*/
static _getContextRect(context, element) {
let global = this._getGlobalRect(element)
2019-07-18 12:26:39 +02:00
let localPosition = Points.fromPageToNode(context, {
x: global.x,
y: global.y
})
return DOMRectReadOnly.fromRect({
x: localPosition.x,
y: localPosition.y,
width: global.width,
height: global.height
})
}
/**
* Gets a rectangle in global space for a provided element.
*
* Note: This works also for rotated DomElements unlike the Element.getBoundingClientRectangle method.
*
* @static
* @param {DomElement} element
* @returns {DomRect} - Returns a rectangle that specifies the location in global space.
* @memberof Card
*/
static _getGlobalRect(element) {
let cardStyle = window.getComputedStyle(element)
let cardWidth = parseInt(cardStyle.width)
let cardHeight = parseInt(cardStyle.height)
let globalPosition = Points.fromNodeToPage(element, { x: 0, y: 0 })
return DOMRectReadOnly.fromRect({
x: globalPosition.x,
y: globalPosition.y,
width: cardWidth,
height: cardHeight
})
}
/**
2019-07-18 12:26:39 +02:00
* Adjusts all links
*
* @static
* @param {*} htmlString
* @returns
* @memberof Card
*/
static postProcessResponseText(htmlString) {
let editable = this.isEditable()
htmlString = this._adjustRelativeLinks(htmlString)
//console.log(htmlString)
let parser = new DOMParser()
2019-07-18 12:26:39 +02:00
let html = parser.parseFromString(htmlString, 'text/html')
if (!editable) {
2019-07-18 12:26:39 +02:00
this._replaceAttributes(html, 'onclick', this._replaceCallback)
}
2019-07-18 12:26:39 +02:00
let zoomableWrappers = html.querySelectorAll('.svg-wrapper')
zoomableWrappers.forEach(wrapper => {
2019-07-18 12:26:39 +02:00
let svg = wrapper.querySelector('svg')
Object.assign(wrapper.style, {
2019-07-18 12:26:39 +02:00
width: svg.getAttribute('width') + 'px',
height: svg.getAttribute('height') + 'px'
})
})
2019-07-18 12:26:39 +02:00
let zoomableVideoWrappers = html.querySelectorAll('.video-wrapper')
zoomableVideoWrappers.forEach(wrapper => {
2019-07-18 12:26:39 +02:00
let video = wrapper.querySelector('video')
Object.assign(wrapper.style, {
2019-07-18 12:26:39 +02:00
width: video.getAttribute('width') + 'px',
height: video.getAttribute('height') + 'px'
})
})
return html
}
/**
* There occured a problem with the tap event, as it was canceled by the event on the svg element
* therefore we restructured the interaction, that only the svg listens for events and if the target is
* a circle (more are not supported yet) the target is adjusted using the .firstTarget of the event,
* that is provided by the Hammer.propergate plugin.
*/
static openPopupOrZoomable(event) {
let target = this._preferFirstTarget(event)
2019-07-18 12:26:39 +02:00
if (target.tagName == 'circle') {
Card.loadHighlightPopup(event)
event.stopPropagation()
} else {
Card.openZoomable(event)
}
}
static pointRectDist(p, r) {
var cx = Math.max(Math.min(p.x, r.x + r.width), r.x)
var cy = Math.max(Math.min(p.y, r.y + r.height), r.y)
return Math.sqrt((p.x - cx) * (p.x - cx) + (p.y - cy) * (p.y - cy))
}
static findNearbyPopups(event, card) {
if (event.target) {
//let column = event.target.closest(".column")
let indexbox = this.closestWithClass(card, 'mainview')
2019-07-18 12:26:39 +02:00
if (indexbox != null) {
// column != null ||
let links = Array.from(indexbox.getElementsByTagName('a'))
2019-07-30 16:56:29 +02:00
let globalClick = event.center ? event.center : { x: event.x, y: event.y }
let localClick = Points.fromPageToNode(indexbox, globalClick)
let linkRects = links.map(link => {
let rect = link.getBoundingClientRect()
let topLeft = Points.fromPageToNode(indexbox, rect)
2019-07-18 12:26:39 +02:00
let center = Points.fromPageToNode(indexbox, {
x: rect.x + rect.width / 2,
y: rect.y + rect.height / 2
})
return {
x: topLeft.x,
y: topLeft.y,
width: rect.width,
height: rect.height,
center,
link
}
})
let distances = []
linkRects.forEach(rect => {
let distance = Card.pointRectDist(localClick, rect)
if (distance == 0.0) {
// Distance == 0.0 indicates an inside relation. Since these
// are already handled by the links themselves we put these cases
// to the end of the sorted list below
distance = 100000.0
}
distances.push(parseInt(distance))
})
let closestLinkIndex = distances.indexOf(Math.min(...distances))
let closestLink = links[closestLinkIndex]
2019-07-30 16:56:29 +02:00
console.log('finding closest links', closestLink, closestLink.getClientRects())
2019-07-30 16:56:29 +02:00
if (distances[closestLinkIndex] < 44 && closestLink.getAttribute('href')) {
// Adapt context and local position
let context = this.getContext(closestLink)
let local = Points.fromPageToNode(context, globalClick)
let rect = linkRects[closestLinkIndex]
local.y = rect.center.y
2019-07-16 10:04:25 +02:00
Card.loadPopup(event, context, closestLink, local)
}
}
}
}
/**
* Evaluates an event, if it has a first target. If so it returns the first target,
* otherwise it returns the target.
*/
static _preferFirstTarget(event) {
2019-07-18 12:26:39 +02:00
return event.firstTarget ? event.firstTarget : event.target
}
/**
* Getter, Setter and 'Unsetter' for the properties hooked onto the context element.
*/
static _getPopup(context) {
return context.popup
}
static _setPopup(context, popup, source) {
this._setPopupSource(popup, source)
context.popup = popup
2019-07-18 12:26:39 +02:00
if (typeof $ != 'undefined') {
2019-07-16 10:04:25 +02:00
//jquery hyphenate below
console.log('hyphenated popup:', $('span').hyphenate('de'))
}
}
static _unsetPopup(context) {
context.popup = null
}
static _setPopupSource(popup, source) {
2019-07-18 12:26:39 +02:00
popup.element.setAttribute('data-src', source)
}
static _getPopupSource(popup) {
2019-07-18 12:26:39 +02:00
return popup.element.getAttribute('data-src')
}
static _unsetZoomable(context) {
context.zoomable = null
}
static _setZoomable(context, zoomable, zoomedFig) {
context.zoomable = [zoomable, zoomedFig]
}
static _getZoomable(context) {
2019-07-18 12:26:39 +02:00
return context.zoomable ? context.zoomable : []
}
/**
2019-07-18 12:26:39 +02:00
* Helper function to determine if a proided element is still
* an active highlight.
*/
static _isHighlightActive(context, element) {
let highlights = this._getHighlights(context)
2019-07-18 12:26:39 +02:00
return highlights.indexOf(element) != -1
}
static _getHighlights(context) {
2019-07-18 12:26:39 +02:00
return context.highlights ? context.highlights : []
}
static _addHighlight(context, highlight) {
if (context.highlights == null) this._unsetHighlight(context)
context.highlights.push(highlight)
}
static _unsetHighlights(context) {
context.highlights = []
}
static _getHighlightSource(highlight) {
2019-07-18 12:26:39 +02:00
return highlight.getAttribute('xlink:href')
}
static _getActiveSubcard(context) {
2019-07-18 12:26:39 +02:00
return context.querySelector('.mainview > .subcard')
}
static _setSubcard(context, subcard) {
//console.trace("_setSubcard", context)
context.subcard = subcard
}
static _getSubcard(context) {
return context.subcard
}
static _unsetSubcard(context) {
context.subcard = null
}
static incrementZIndex(context) {
if (!context.zIndex) context.zIndex = 0
context.zIndex++
}
static getZIndex(context) {
2019-07-18 12:26:39 +02:00
return context.zIndex || 0
}
/**
* This getter and setter pair solves the problem,
* when we assign 'relativePath' to a child class, the assign is routed
* to this base class. And we ensure, that we always set the same
* private variable.
*
* Not doing this lead to some cases, when Card-/ScatterCard.func
* was called and depending on context their were different values
* inside the relativePath variable.
*/
static set relativePath(value) {
Card._relativePath = value
}
static get relativePath() {
return Card._relativePath
}
}
Card.debug = true
2019-07-18 12:26:39 +02:00
Card._relativePath = ''
Card.scatterContainer = null
2019-07-18 12:26:39 +02:00
Card.interactionType = 'tap'
Card.popupHtmlSelector = '.popupHtml'
Card.dynamicHeight = false
Card.popupYOffset = -15
Card.zIndices = {
article: 10,
popup: 100,
zoomable: 101,
zoomedFigure: 102
}
Card.animation = {
articleTransition: 0.6,
fade: 0.2,
popup: 0.1,
highlight: 0.4,
zoomable: 0.5
}