oxjs/source/Ox.UI/js/Video/Ox.SmallVideoTimeline.js

240 lines
7.5 KiB
JavaScript
Raw Normal View History

2011-05-15 16:18:58 +00:00
Ox.SmallVideoTimeline = function(options, self) {
self = self || {};
var that = Ox.Element({}, self)
.defaults({
_offset: 0, // hack for cases where all these position: absolute elements have to go into a float: left
2011-05-15 16:18:58 +00:00
duration: 0,
editing: false,
2011-05-17 18:04:26 +00:00
find: '',
2011-05-15 16:18:58 +00:00
'in': 0,
out: 0,
2011-05-15 18:50:05 +00:00
paused: false,
showMilliseconds: 0,
2011-05-17 09:43:54 +00:00
timeline: '',
2011-05-15 18:50:05 +00:00
type: 'player',
width: 256
2011-05-15 16:18:58 +00:00
})
.options(options || {})
.addClass('OxSmallVideoTimeline')
2011-05-15 18:50:05 +00:00
.css(Ox.extend({
2011-05-15 16:18:58 +00:00
width: self.options.width + 'px'
2011-05-15 18:50:05 +00:00
}, self.options.type == 'player' ? {
background: 'rgb(0, 0, 0)',
borderRadius: '8px'
} : {}));
2011-05-15 16:18:58 +00:00
self.height = self.options.type == 'player' ? 16 : 24;
2011-05-15 18:50:05 +00:00
self.imageLeft = self.options.type == 'player' ? 8 : 4;
self.imageWidth = self.options.width -
(self.options.type == 'player' ? 16 : 8)
2011-05-15 16:18:58 +00:00
self.imageHeight = self.options.type == 'player' ? 16 : 18;
2011-05-15 18:50:05 +00:00
self.interfaceLeft = self.options.type == 'player' ? 0 : 4;
self.interfaceTop = self.options.type == 'player' ? 0 : 2;
self.interfaceWidth = self.options.type == 'player' ? self.options.width : self.imageWidth;
2011-05-15 16:18:58 +00:00
that.css({
height: self.height + 'px'
});
self.$image = getTimelineImage().appendTo(that);
2011-05-15 16:18:58 +00:00
2011-05-17 18:44:53 +00:00
self.$interface = Ox.Element({
tooltip: getTooltip
})
2011-05-15 18:50:05 +00:00
.addClass('OxInterface')
.css({
left: self.interfaceLeft + 'px',
top: self.interfaceTop + 'px',
width: self.interfaceWidth + 'px',
height: '20px',
})
.bind({
2011-05-17 18:44:53 +00:00
mousedown: mousedown
2011-05-15 18:50:05 +00:00
})
.bindEvent({
2011-09-17 11:49:29 +00:00
drag: function(data) {
mousedown(data);
},
2011-09-17 11:49:29 +00:00
dragend: function(data) {
self.triggered = false;
2011-09-17 11:49:29 +00:00
mousedown(data);
2011-05-15 18:50:05 +00:00
}
})
.appendTo(that);
2011-05-15 16:18:58 +00:00
2011-05-17 18:44:53 +00:00
self.$interface.$tooltip.css({
textAlign: 'center'
});
2011-05-15 18:50:05 +00:00
if (self.options.type == 'player') {
self.$positionMarker = $('<div>')
.addClass('OxMarkerPlay' + (self.options.paused ? ' OxPaused' : ''))
.append($('<div>').append($('<div>')))
2011-05-15 18:50:05 +00:00
.appendTo(that.$element);
} else {
self.$positionMarker = $('<img>')
.addClass('OxMarkerPosition')
2011-05-15 16:18:58 +00:00
.attr({
src: Ox.UI.getImageURL('markerPosition')
2011-05-15 16:18:58 +00:00
})
.appendTo(that.$element);
2011-05-15 18:50:05 +00:00
}
setPositionMarker();
2011-05-15 16:18:58 +00:00
2011-05-15 18:50:05 +00:00
if (self.options.type == 'editor') {
self.$pointMarker = {};
['in', 'out'].forEach(function(point) {
2011-09-02 00:32:23 +00:00
var titlecase = Ox.toTitleCase(point);
2011-05-15 18:50:05 +00:00
self.$pointMarker[point] = $('<img>')
.addClass('OxMarkerPoint' + titlecase)
2011-05-15 18:50:05 +00:00
.attr({
2011-09-02 00:32:23 +00:00
src: Ox.UI.getImageURL('marker' + titlecase)
2011-05-15 18:50:05 +00:00
})
.appendTo(that.$element);
setPointMarker(point);
});
}
2011-05-15 16:18:58 +00:00
2011-05-15 18:50:05 +00:00
function getPosition(e) {
var position =
(
(e.offsetX ? e.offsetX : e.clientX - $(e.target).offset().left) -
(self.options.type == 'player' ? 8 : 0)
) * self.options.duration / self.imageWidth;
return Ox.limit(position, 0, self.options.duration);
}
function getSubtitle(position) {
var subtitle = '';
Ox.forEach(self.options.subtitles, function(v) {
if (v['in'] <= position && v.out > position) {
subtitle = v;
return false;
}
});
return subtitle;
}
function getTimelineImage() {
return Ox.SmallVideoTimelineImage({
duration: self.options.duration,
editing: self.options.editing,
'in': self.options['in'],
out: self.options.out,
results: self.options.results,
subtitles: self.options.subtitles,
2011-05-17 09:43:54 +00:00
timeline: self.options.timeline,
width: self.imageWidth,
type: self.options.type
})
.css({
position: 'absolute',
left: self.imageLeft + 'px',
width: self.imageWidth + 'px'
});
}
2011-05-17 18:44:53 +00:00
function getTooltip(e) {
var position = getPosition(e),
subtitle = getSubtitle(position);
return subtitle ? '<span class=\'OxBright\'>' + Ox.highlight(
subtitle.text, self.options.find, 'OxHighlight'
).replace(/\n/g, '<br/>') + '</span><br/>' +
Ox.formatDuration(subtitle['in'], self.options.showMilliseconds) + ' - ' +
Ox.formatDuration(subtitle['out'], self.options.showMilliseconds) :
Ox.formatDuration(position, self.options.showMilliseconds);
}
2011-05-15 18:50:05 +00:00
function mousedown(e) {
if ($(e.target).is('.OxInterface')) {
self.options.position = getPosition(e);
setPositionMarker();
if (!self.triggered) {
that.triggerEvent('position', {
position: self.options.position
});
self.triggered = true;
setTimeout(function() {
self.triggered = false;
}, 250);
}
}
}
function setPointMarker(point) {
self.$pointMarker[point].css({
left: self.imageLeft + Math.round(
self.options[point] * self.imageWidth / self.options.duration
) + 'px'
});
}
function setPositionMarker() {
self.$positionMarker.css({
left: self.interfaceLeft + Math.round(
self.options.position * self.imageWidth / self.options.duration
) - (self.options.type == 'editor' ? 5 : 0) + self.options._offset + 'px'
2011-05-15 18:50:05 +00:00
});
}
function setWidth() {
self.imageWidth = self.options.width -
(self.options.type == 'player' ? 16 : 8);
self.interfaceWidth = self.options.type == 'player' ?
self.options.width : self.imageWidth;
2011-05-15 18:50:05 +00:00
that.css({
width: self.options.width + 'px'
});
self.$image.options({
width: self.imageWidth
}).css({
2011-05-15 18:50:05 +00:00
width: self.imageWidth + 'px'
});
self.$interface.css({
width: self.interfaceWidth + 'px'
});
setPositionMarker();
if (self.options.type == 'editor') {
setPointMarker('in');
setPointMarker('out');
}
}
self.setOption = function(key, value) {
if (key == 'duration') {
self.$image.options({
duration: value
});
2011-05-18 18:30:58 +00:00
} else if (key == 'in') {
self.$image.options({
'in': value
});
setPointMarker('in');
} else if (key == 'out') {
self.$image.options({
out: value
});
setPointMarker('out');
} else if (key == 'paused') {
self.$positionMarker[
self.options.paused ? 'addClass' : 'removeClass'
]('OxPaused');
} else if (key == 'position') {
setPositionMarker();
} else if (key == 'results') {
self.$image.options({
results: value
});
} else if (key == 'subtitles') {
self.$image.options({
subtitles: value
});
2011-05-15 18:50:05 +00:00
} else if (key == 'width') {
setWidth();
}
};
2011-05-15 16:18:58 +00:00
return that;
};