iwmlib/lib/pixi/message.js

127 lines
4.0 KiB
JavaScript
Raw Permalink Normal View History

2019-03-21 09:57:27 +01:00
import Theme from './theme.js'
2019-07-18 12:26:39 +02:00
import { InteractivePopup } from './popup.js'
2019-03-21 09:57:27 +01:00
/**
* Class that represents a Message. A message pops up and disappears after a specific amount of time.
2019-07-18 12:26:39 +02:00
*
2019-03-21 09:57:27 +01:00
* @example
* // Create the PixiJS App
* const app = new PIXIApp({
* view: canvas,
* width: 900,
* height: 250
* }).setup().run()
2019-07-18 12:26:39 +02:00
*
2019-03-21 09:57:27 +01:00
* // Create a button
* let button = new Button({
* label: 'Click me',
* action: e => {
* const message = new Message({
* app: app,
* header: 'Header',
* content: 'Text.'
* })
* app.scene.addChild(message)
* }
* })
2019-07-18 12:26:39 +02:00
*
2019-03-21 09:57:27 +01:00
* // Add the button to the scene
* app.scene.addChild(button)
*
* @class
* @extends InteractivePopup
* @see {@link https://www.iwm-tuebingen.de/iwmbrowser/lib/pixi/message.html|DocTest}
*/
export default class Message extends InteractivePopup {
/**
* Creates an instance of a Message.
2019-07-18 12:26:39 +02:00
*
2019-03-21 09:57:27 +01:00
* @constructor
* @param {object} [opts] - An options object to specify to style and behaviour of the message.
* @param {PIXIApp} [opts.app=window.app] - The PIXIApp where this message belongs to.
* @param {boolean} [opts.closeButton=false] - Should a close button be displayed in the upper right corner?
* @param {number} [opts.minWidth=280] - The minimum width of the message box. Automatically expands with the content.
* @param {number} [opts.minHeight=100] - The minimum height of the message box. Automatically expands with the content.
* @param {number} [opts.margin=Theme.margin] - The outer spacing of the message box.
* @param {string} [opts.align=right] - The horizontal position of the message box relative to the app. Possible
* values are left, center, right.
* @param {string} [opts.verticalAlign=top] - The vertical position of the message box relative to the app. Possible
* values are top, middle, bottom.
* @param {number} [opts.duration=5] - The duration in seconds when the message box should disappear.
* @param {boolean} [opts.autoClose=true] - Should the message box be closed automatically?
* @param {number} [opts.closeDuration=Theme.fast] - The duration in seconds of the closing of the message box.
*/
constructor(opts = {}) {
const theme = Theme.fromString(opts.theme)
2019-07-18 12:26:39 +02:00
opts = Object.assign(
{},
{
app: window.app,
closeButton: false,
minWidth: 280,
minHeight: 100,
margin: theme.margin,
align: 'right', // left, center, right
verticalAlign: 'top', // top, middle, bottom
duration: 5,
autoClose: true,
closeDuration: theme.fast
},
opts
)
2019-03-21 09:57:27 +01:00
super(opts)
}
/**
* Relayouts the position of the message box.
2019-07-18 12:26:39 +02:00
*
2019-03-21 09:57:27 +01:00
* @return {Message} Returns the message box for chaining.
*/
layout() {
super.layout()
// horizontal
switch (this.opts.align) {
case 'left':
this.x = this.opts.margin
break
case 'center':
2019-07-18 12:26:39 +02:00
this.x = this.opts.app.size.width / 2 - this.width / 2
2019-03-21 09:57:27 +01:00
break
case 'right':
2019-07-30 16:56:29 +02:00
this.x = this.opts.app.size.width - this.opts.margin - this.width
2019-03-21 09:57:27 +01:00
break
}
// vertical
switch (this.opts.verticalAlign) {
case 'top':
this.y = this.opts.margin
break
case 'middle':
2019-07-18 12:26:39 +02:00
this.y = this.opts.app.size.height / 2 - this.height / 2
2019-03-21 09:57:27 +01:00
break
case 'bottom':
2019-07-30 16:56:29 +02:00
this.y = this.opts.app.size.height - this.opts.margin - this.height
2019-03-21 09:57:27 +01:00
break
}
}
/**
* Shows the message box.
2019-07-18 12:26:39 +02:00
*
2019-03-21 09:57:27 +01:00
* @private
*/
show() {
super.show()
if (this.opts.autoClose) {
window.setTimeout(() => {
this.hide()
}, this.opts.duration * 1000)
}
}
}