@checksub_team/peaks_timeline
Version:
JavaScript UI component for displaying audio waveforms
174 lines (150 loc) • 4.85 kB
JavaScript
/**
* @file
*
* Defines the {@link Line} class.
*
* @module line
*/
define([
'../utils'
], function(Utils) {
'use strict';
function validateLine(options, context) {
if (!Utils.isInteger(options.position) || options.position < 0) {
throw new TypeError('peaks.lines.' + context + ': position must be a non-negative integer');
}
if (Utils.isNullOrUndefined(options.indicatorType)) {
options.indicatorType = 'default';
}
else if (!Utils.isValidIndicatorType(options.indicatorType)) {
throw new TypeError('peaks.lines.' + context + ': indicatorType must be a valid indicator type');
}
if (Utils.isNullOrUndefined(options.indicatorText)) {
options.indicatorText = '';
}
else if (!Utils.isString(options.indicatorText)) {
throw new TypeError('peaks.lines.' + context + ': indicatorText must be a string');
}
if (Utils.isNullOrUndefined(options.indicatorSubText)) {
options.indicatorSubText = '';
}
else if (!Utils.isString(options.indicatorSubText)) {
throw new TypeError('peaks.lines.' + context + ': indicatorSubText must be a string');
}
if (Utils.isNullOrUndefined(options.locked)) {
options.locked = false;
}
else if (!Utils.isBoolean(options.locked)) {
throw new TypeError('peaks.lines.' + context + ': locked must be a boolean');
}
}
/**
* A line is a horizontal line that can be placed on the timeline.
* It can contain sources or segments.
*
* @class
* @alias Segment
* @param {Peaks} peaks The parent {@link Peaks} object.
* @param {String} id A unique identifier for the line.
* @param {Number} position Position of the line on the timeline.
* @param {String} indicatorType Type of the line indicator.
* @param {String} indicatorText Text to display above the line indicator.
*/
function Line(peaks, id, position, indicatorType, indicatorText, indicatorSubText, locked) {
var opts = {
position: position,
indicatorType: indicatorType,
indicatorText: indicatorText,
indicatorSubText: indicatorSubText,
locked: locked
};
validateLine(opts, 'add()');
this._peaks = peaks;
this._id = id;
this._position = opts.position;
this._indicatorType = opts.indicatorType;
this._indicatorText = opts.indicatorText;
this._indicatorSubText = opts.indicatorSubText;
this._locked = opts.locked;
}
Object.defineProperties(Line.prototype, {
id: {
enumerable: true,
get: function() {
return this._id;
}
},
position: {
enumerable: true,
get: function() {
return this._position;
},
set: function(pos) {
this._position = pos;
}
},
indicatorType: {
enumerable: true,
get: function() {
return this._indicatorType;
}
},
indicatorText: {
enumerable: true,
get: function() {
return this._indicatorText;
}
},
indicatorSubText: {
enumerable: true,
get: function() {
return this._indicatorSubText;
}
},
locked: {
enumerable: true,
get: function() {
return this._locked;
}
}
});
Line.prototype.update = function(options) {
var opts = {
position: this.position,
indicatorType: this.indicatorType,
indicatorText: this.indicatorText,
indicatorSubText: this.indicatorSubText,
locked: this.locked
};
Utils.extend(opts, options);
validateLine(opts, 'update()');
this._position = opts.position;
this._indicatorType = opts.indicatorType;
this._indicatorText = opts.indicatorText;
this._indicatorSubText = opts.indicatorSubText;
this._locked = opts.locked;
this._peaks.emit('model.line.update', this);
};
/**
* Returns a serializable object containing only the properties defined with Object.defineProperties.
* This includes all enumerable properties that can be safely serialized.
*
* @returns {Object} A plain object containing the serializable properties of the line.
*/
Line.prototype.toSerializable = function() {
var serializable = {};
// Add all the enumerable properties from the prototype
var proto = Object.getPrototypeOf(this);
var descriptors = Object.getOwnPropertyDescriptors(proto);
for (var prop in descriptors) {
if (Object.prototype.hasOwnProperty.call(descriptors, prop)) {
var descriptor = descriptors[prop];
if (descriptor.enumerable && descriptor.get && typeof descriptor.get === 'function') {
serializable[prop] = this[prop];
}
}
}
return serializable;
};
return Line;
});