|
|
@ -0,0 +1,142 @@ |
|
|
|
var Hammer = require('../../../module/hammer'); |
|
|
|
var Item = require('./Item'); |
|
|
|
var ItemRange = require('./ItemRange'); |
|
|
|
|
|
|
|
/** |
|
|
|
* @constructor ItemBackground |
|
|
|
* @extends Item |
|
|
|
* @param {Object} data Object containing parameters start, end |
|
|
|
* content, className. |
|
|
|
* @param {{toScreen: function, toTime: function}} conversion |
|
|
|
* Conversion functions from time to screen and vice versa |
|
|
|
* @param {Object} [options] Configuration options |
|
|
|
* // TODO: describe options
|
|
|
|
*/ |
|
|
|
// TODO: implement support for the ItemBackground just having a start, then being displayed as a sort of an annotation
|
|
|
|
function ItemBackground (data, conversion, options) { |
|
|
|
this.props = { |
|
|
|
content: { |
|
|
|
width: 0 |
|
|
|
} |
|
|
|
}; |
|
|
|
this.overflow = false; // if contents can overflow (css styling), this flag is set to true
|
|
|
|
|
|
|
|
// validate data
|
|
|
|
if (data) { |
|
|
|
if (data.start == undefined) { |
|
|
|
throw new Error('Property "start" missing in item ' + data.id); |
|
|
|
} |
|
|
|
if (data.end == undefined) { |
|
|
|
throw new Error('Property "end" missing in item ' + data.id); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
Item.call(this, data, conversion, options); |
|
|
|
} |
|
|
|
|
|
|
|
ItemBackground.prototype = new Item (null, null, null); |
|
|
|
|
|
|
|
ItemBackground.prototype.baseClassName = 'item background'; |
|
|
|
|
|
|
|
/** |
|
|
|
* Check whether this item is visible inside given range |
|
|
|
* @returns {{start: Number, end: Number}} range with a timestamp for start and end |
|
|
|
* @returns {boolean} True if visible |
|
|
|
*/ |
|
|
|
ItemBackground.prototype.isVisible = function(range) { |
|
|
|
// determine visibility
|
|
|
|
return (this.data.start < range.end) && (this.data.end > range.start); |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* Repaint the item |
|
|
|
*/ |
|
|
|
ItemBackground.prototype.redraw = function() { |
|
|
|
var dom = this.dom; |
|
|
|
if (!dom) { |
|
|
|
// create DOM
|
|
|
|
this.dom = {}; |
|
|
|
dom = this.dom; |
|
|
|
|
|
|
|
// background box
|
|
|
|
dom.box = document.createElement('div'); |
|
|
|
// className is updated in redraw()
|
|
|
|
|
|
|
|
// contents box
|
|
|
|
dom.content = document.createElement('div'); |
|
|
|
dom.content.className = 'content'; |
|
|
|
dom.box.appendChild(dom.content); |
|
|
|
|
|
|
|
// attach this item as attribute
|
|
|
|
dom.box['timeline-item'] = this; |
|
|
|
|
|
|
|
this.dirty = true; |
|
|
|
} |
|
|
|
|
|
|
|
// append DOM to parent DOM
|
|
|
|
if (!this.parent) { |
|
|
|
throw new Error('Cannot redraw item: no parent attached'); |
|
|
|
} |
|
|
|
if (!dom.box.parentNode) { |
|
|
|
var background = this.parent.dom.background; |
|
|
|
if (!background) { |
|
|
|
throw new Error('Cannot redraw time axis: parent has no background container element'); |
|
|
|
} |
|
|
|
background.appendChild(dom.box); |
|
|
|
} |
|
|
|
this.displayed = true; |
|
|
|
|
|
|
|
// Update DOM when item is marked dirty. An item is marked dirty when:
|
|
|
|
// - the item is not yet rendered
|
|
|
|
// - the item's data is changed
|
|
|
|
// - the item is selected/deselected
|
|
|
|
if (this.dirty) { |
|
|
|
this._updateContents(this.dom.content); |
|
|
|
this._updateTitle(this.dom.content); |
|
|
|
this._updateDataAttributes(this.dom.content); |
|
|
|
|
|
|
|
// update class
|
|
|
|
var className = (this.data.className ? (' ' + this.data.className) : '') + |
|
|
|
(this.selected ? ' selected' : ''); |
|
|
|
dom.box.className = this.baseClassName + className; |
|
|
|
|
|
|
|
// determine from css whether this box has overflow
|
|
|
|
this.overflow = window.getComputedStyle(dom.content).overflow !== 'hidden'; |
|
|
|
|
|
|
|
// recalculate size
|
|
|
|
this.props.content.width = this.dom.content.offsetWidth; |
|
|
|
this.height = 0; // set height zero, so this item will be ignored when stacking items
|
|
|
|
|
|
|
|
this.dirty = false; |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* Show the item in the DOM (when not already visible). The items DOM will |
|
|
|
* be created when needed. |
|
|
|
*/ |
|
|
|
ItemBackground.prototype.show = ItemRange.prototype.show; |
|
|
|
|
|
|
|
/** |
|
|
|
* Hide the item from the DOM (when visible) |
|
|
|
* @return {Boolean} changed |
|
|
|
*/ |
|
|
|
ItemBackground.prototype.hide = ItemRange.prototype.hide; |
|
|
|
|
|
|
|
/** |
|
|
|
* Reposition the item horizontally |
|
|
|
* @Override |
|
|
|
*/ |
|
|
|
ItemBackground.prototype.repositionX = ItemRange.prototype.repositionX; |
|
|
|
|
|
|
|
/** |
|
|
|
* Reposition the item vertically |
|
|
|
* @Override |
|
|
|
*/ |
|
|
|
ItemBackground.prototype.repositionY = function() { |
|
|
|
var onTop = this.options.orientation === 'top'; |
|
|
|
this.dom.content.style.top = onTop ? '' : '0'; |
|
|
|
this.dom.content.style.bottom = onTop ? '0' : ''; |
|
|
|
}; |
|
|
|
|
|
|
|
module.exports = ItemBackground; |