2011-12-04 16:33:56 +01:00
|
|
|
/**
|
2019-04-16 00:34:29 +02:00
|
|
|
* This code is mostly from the old Etherpad. Please help us to comment this code.
|
2011-12-04 16:33:56 +01:00
|
|
|
* This helps other people to understand this code better and helps them to improve it.
|
|
|
|
* TL;DR COMMENTS ON THIS FILE ARE HIGHLY APPRECIATED
|
|
|
|
*/
|
|
|
|
|
2011-06-20 12:44:04 +02:00
|
|
|
/**
|
|
|
|
* Copyright 2009 Google Inc.
|
2011-07-07 19:59:34 +02:00
|
|
|
*
|
2011-06-20 12:44:04 +02:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-07-07 19:59:34 +02:00
|
|
|
*
|
2011-06-20 12:44:04 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-07-07 19:59:34 +02:00
|
|
|
*
|
2011-06-20 12:44:04 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2012-01-27 06:20:26 +01:00
|
|
|
// These parameters were global, now they are injected. A reference to the
|
|
|
|
// Timeslider controller would probably be more appropriate.
|
2012-03-17 13:36:42 +01:00
|
|
|
var _ = require('./underscore');
|
2012-04-23 14:47:07 +02:00
|
|
|
var padmodals = require('./pad_modals').padmodals;
|
2020-06-02 11:25:43 +02:00
|
|
|
var colorutils = require('./colorutils').colorutils;
|
2012-03-04 22:10:36 +01:00
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function loadBroadcastSliderJS(fireWhenAllScriptsAreLoaded) {
|
2012-01-27 06:20:26 +01:00
|
|
|
var BroadcastSlider;
|
2011-06-20 12:44:04 +02:00
|
|
|
|
2014-12-27 22:31:23 +01:00
|
|
|
// Hack to ensure timeslider i18n values are in
|
|
|
|
$("[data-key='timeslider_returnToPad'] > a > span").html(html10n.get("timeslider.toolbar.returnbutton"));
|
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
(function() { // wrap this code in its own namespace
|
2011-06-20 12:44:04 +02:00
|
|
|
var sliderLength = 1000;
|
|
|
|
var sliderPos = 0;
|
|
|
|
var sliderActive = false;
|
|
|
|
var slidercallbacks = [];
|
|
|
|
var savedRevisions = [];
|
|
|
|
var sliderPlaying = false;
|
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
var _callSliderCallbacks = function(newval) {
|
2011-06-20 12:44:04 +02:00
|
|
|
sliderPos = newval;
|
|
|
|
for (var i = 0; i < slidercallbacks.length; i++)
|
|
|
|
{
|
|
|
|
slidercallbacks[i](newval);
|
|
|
|
}
|
2012-04-23 14:47:07 +02:00
|
|
|
}
|
2019-04-16 00:34:29 +02:00
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
var updateSliderElements = function() {
|
2011-06-20 12:44:04 +02:00
|
|
|
for (var i = 0; i < savedRevisions.length; i++)
|
|
|
|
{
|
|
|
|
var position = parseInt(savedRevisions[i].attr('pos'));
|
|
|
|
savedRevisions[i].css('left', (position * ($("#ui-slider-bar").width() - 2) / (sliderLength * 1.0)) - 1);
|
|
|
|
}
|
|
|
|
$("#ui-slider-handle").css('left', sliderPos * ($("#ui-slider-bar").width() - 2) / (sliderLength * 1.0));
|
2019-04-16 00:34:29 +02:00
|
|
|
}
|
2012-04-23 14:47:07 +02:00
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
var addSavedRevision = function(position, info) {
|
2011-06-20 12:44:04 +02:00
|
|
|
var newSavedRevision = $('<div></div>');
|
|
|
|
newSavedRevision.addClass("star");
|
|
|
|
|
|
|
|
newSavedRevision.attr('pos', position);
|
|
|
|
newSavedRevision.css('left', (position * ($("#ui-slider-bar").width() - 2) / (sliderLength * 1.0)) - 1);
|
2020-04-03 16:11:01 +02:00
|
|
|
$("#ui-slider-bar").append(newSavedRevision);
|
2020-11-21 19:37:57 +01:00
|
|
|
newSavedRevision.mouseup(function(evt) {
|
2011-06-20 12:44:04 +02:00
|
|
|
BroadcastSlider.setSliderPosition(position);
|
|
|
|
});
|
|
|
|
savedRevisions.push(newSavedRevision);
|
2012-04-23 14:47:07 +02:00
|
|
|
};
|
2011-06-20 12:44:04 +02:00
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
var removeSavedRevision = function(position) {
|
2011-06-20 12:44:04 +02:00
|
|
|
var element = $("div.star [pos=" + position + "]");
|
|
|
|
savedRevisions.remove(element);
|
|
|
|
element.remove();
|
|
|
|
return element;
|
2012-04-23 14:47:07 +02:00
|
|
|
};
|
2011-06-20 12:44:04 +02:00
|
|
|
|
|
|
|
/* Begin small 'API' */
|
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function onSlider(callback) {
|
2011-06-20 12:44:04 +02:00
|
|
|
slidercallbacks.push(callback);
|
|
|
|
}
|
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function getSliderPosition() {
|
2011-06-20 12:44:04 +02:00
|
|
|
return sliderPos;
|
|
|
|
}
|
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function setSliderPosition(newpos) {
|
2011-06-20 12:44:04 +02:00
|
|
|
newpos = Number(newpos);
|
|
|
|
if (newpos < 0 || newpos > sliderLength) return;
|
2013-02-07 00:35:58 +01:00
|
|
|
if(!newpos){
|
|
|
|
newpos = 0; // stops it from displaying NaN if newpos isn't set
|
|
|
|
}
|
2013-02-07 15:34:10 +01:00
|
|
|
window.location.hash = "#" + newpos;
|
2011-06-20 12:44:04 +02:00
|
|
|
$("#ui-slider-handle").css('left', newpos * ($("#ui-slider-bar").width() - 2) / (sliderLength * 1.0));
|
2020-11-21 19:37:57 +01:00
|
|
|
$("a.tlink").map(function() {
|
2011-06-20 12:44:04 +02:00
|
|
|
$(this).attr('href', $(this).attr('thref').replace("%revision%", newpos));
|
|
|
|
});
|
2013-02-07 15:34:10 +01:00
|
|
|
|
2012-12-19 19:55:31 +01:00
|
|
|
$("#revision_label").html(html10n.get("timeslider.version", { "version": newpos}));
|
2011-06-20 12:44:04 +02:00
|
|
|
|
2020-04-03 16:11:01 +02:00
|
|
|
$("#leftstar, #leftstep").toggleClass('disabled', newpos == 0);
|
|
|
|
$("#rightstar, #rightstep").toggleClass('disabled', newpos == sliderLength);
|
2011-06-20 12:44:04 +02:00
|
|
|
|
|
|
|
sliderPos = newpos;
|
|
|
|
_callSliderCallbacks(newpos);
|
|
|
|
}
|
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function getSliderLength() {
|
2011-06-20 12:44:04 +02:00
|
|
|
return sliderLength;
|
|
|
|
}
|
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function setSliderLength(newlength) {
|
2011-06-20 12:44:04 +02:00
|
|
|
sliderLength = newlength;
|
|
|
|
updateSliderElements();
|
|
|
|
}
|
|
|
|
|
|
|
|
// just take over the whole slider screen with a reconnect message
|
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function showReconnectUI() {
|
2012-07-14 15:54:44 +02:00
|
|
|
padmodals.showModal("disconnected");
|
2011-06-20 12:44:04 +02:00
|
|
|
}
|
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function setAuthors(authors) {
|
2012-03-24 20:35:45 +01:00
|
|
|
var authorsList = $("#authorsList");
|
|
|
|
authorsList.empty();
|
2011-06-20 12:44:04 +02:00
|
|
|
var numAnonymous = 0;
|
|
|
|
var numNamed = 0;
|
2012-03-25 15:48:31 +02:00
|
|
|
var colorsAnonymous = [];
|
2020-11-21 19:37:57 +01:00
|
|
|
_.each(authors, function(author) {
|
2014-12-27 22:25:24 +01:00
|
|
|
if(author)
|
2011-06-20 12:44:04 +02:00
|
|
|
{
|
2014-12-27 22:25:24 +01:00
|
|
|
var authorColor = clientVars.colorPalette[author.colorId] || author.colorId;
|
|
|
|
if (author.name)
|
|
|
|
{
|
|
|
|
if (numNamed !== 0) authorsList.append(', ');
|
2020-06-02 11:25:43 +02:00
|
|
|
var textColor = colorutils.textColorFromBackgroundColor(authorColor, clientVars.skinName)
|
2014-12-27 22:25:24 +01:00
|
|
|
$('<span />')
|
|
|
|
.text(author.name || "unnamed")
|
|
|
|
.css('background-color', authorColor)
|
2020-06-02 11:25:43 +02:00
|
|
|
.css('color', textColor)
|
2014-12-27 22:25:24 +01:00
|
|
|
.addClass('author')
|
|
|
|
.appendTo(authorsList);
|
|
|
|
|
|
|
|
numNamed++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
numAnonymous++;
|
|
|
|
if(authorColor) colorsAnonymous.push(authorColor);
|
|
|
|
}
|
2011-06-20 12:44:04 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
if (numAnonymous > 0)
|
|
|
|
{
|
2013-07-23 12:43:30 +02:00
|
|
|
var anonymousAuthorString = html10n.get("timeslider.unnamedauthors", { num: numAnonymous });
|
2019-04-16 00:34:29 +02:00
|
|
|
|
2012-03-24 20:35:45 +01:00
|
|
|
if (numNamed !== 0){
|
|
|
|
authorsList.append(' + ' + anonymousAuthorString);
|
|
|
|
} else {
|
|
|
|
authorsList.append(anonymousAuthorString);
|
|
|
|
}
|
2019-04-16 00:34:29 +02:00
|
|
|
|
2012-03-25 15:48:31 +02:00
|
|
|
if(colorsAnonymous.length > 0){
|
|
|
|
authorsList.append(' (');
|
|
|
|
_.each(colorsAnonymous, function(color, i){
|
2019-04-16 00:34:29 +02:00
|
|
|
if( i > 0 ) authorsList.append(' ');
|
2012-04-15 19:18:29 +02:00
|
|
|
$('<span> </span>')
|
2012-03-25 15:48:31 +02:00
|
|
|
.css('background-color', color)
|
|
|
|
.addClass('author author-anonymous')
|
|
|
|
.appendTo(authorsList);
|
|
|
|
});
|
|
|
|
authorsList.append(')');
|
|
|
|
}
|
2019-04-16 00:34:29 +02:00
|
|
|
|
2011-06-20 12:44:04 +02:00
|
|
|
}
|
|
|
|
if (authors.length == 0)
|
|
|
|
{
|
2012-12-19 19:55:31 +01:00
|
|
|
authorsList.append(html10n.get("timeslider.toolbar.authorsList"));
|
2011-06-20 12:44:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-27 06:20:26 +01:00
|
|
|
BroadcastSlider = {
|
2011-06-20 12:44:04 +02:00
|
|
|
onSlider: onSlider,
|
|
|
|
getSliderPosition: getSliderPosition,
|
|
|
|
setSliderPosition: setSliderPosition,
|
|
|
|
getSliderLength: getSliderLength,
|
|
|
|
setSliderLength: setSliderLength,
|
2020-11-21 19:37:57 +01:00
|
|
|
isSliderActive: function() {
|
2011-06-20 12:44:04 +02:00
|
|
|
return sliderActive;
|
|
|
|
},
|
|
|
|
playpause: playpause,
|
|
|
|
addSavedRevision: addSavedRevision,
|
|
|
|
showReconnectUI: showReconnectUI,
|
|
|
|
setAuthors: setAuthors
|
|
|
|
}
|
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function playButtonUpdater() {
|
2011-06-20 12:44:04 +02:00
|
|
|
if (sliderPlaying)
|
|
|
|
{
|
|
|
|
if (getSliderPosition() + 1 > sliderLength)
|
|
|
|
{
|
|
|
|
$("#playpause_button_icon").toggleClass('pause');
|
|
|
|
sliderPlaying = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setSliderPosition(getSliderPosition() + 1);
|
|
|
|
|
|
|
|
setTimeout(playButtonUpdater, 100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function playpause() {
|
2011-06-20 12:44:04 +02:00
|
|
|
$("#playpause_button_icon").toggleClass('pause');
|
|
|
|
|
|
|
|
if (!sliderPlaying)
|
|
|
|
{
|
|
|
|
if (getSliderPosition() == sliderLength) setSliderPosition(0);
|
|
|
|
sliderPlaying = true;
|
|
|
|
playButtonUpdater();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sliderPlaying = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// assign event handlers to html UI elements after page load
|
2020-11-21 19:37:57 +01:00
|
|
|
fireWhenAllScriptsAreLoaded.push(function() {
|
|
|
|
$(document).keyup(function(e) {
|
2012-04-15 19:25:23 +02:00
|
|
|
if (!e) var e = window.event;
|
2020-04-03 16:11:01 +02:00
|
|
|
var code = e.keyCode || e.which;
|
2012-04-15 19:25:23 +02:00
|
|
|
|
|
|
|
if (code == 37)
|
|
|
|
{ // left
|
2020-04-03 16:11:01 +02:00
|
|
|
if (e.shiftKey) {
|
|
|
|
$('#leftstar').click();
|
|
|
|
} else {
|
|
|
|
$('#leftstep').click();
|
2012-04-15 19:25:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (code == 39)
|
2020-04-03 16:11:01 +02:00
|
|
|
{ // right
|
|
|
|
if (e.shiftKey) {
|
|
|
|
$('#rightstar').click();
|
|
|
|
} else {
|
|
|
|
$('#rightstep').click();
|
2011-06-20 12:44:04 +02:00
|
|
|
}
|
2012-04-15 19:25:23 +02:00
|
|
|
}
|
2020-04-03 16:11:01 +02:00
|
|
|
else if (code == 32)
|
|
|
|
{ // spacebar
|
|
|
|
$("#playpause_button_icon").trigger('click');
|
|
|
|
}
|
2012-04-15 19:25:23 +02:00
|
|
|
});
|
2019-04-16 00:34:29 +02:00
|
|
|
|
2020-04-03 16:11:01 +02:00
|
|
|
// Resize
|
2020-11-21 19:37:57 +01:00
|
|
|
$(window).resize(function() {
|
2011-06-20 12:44:04 +02:00
|
|
|
updateSliderElements();
|
|
|
|
});
|
|
|
|
|
2020-04-03 16:11:01 +02:00
|
|
|
// Slider click
|
2020-11-21 19:37:57 +01:00
|
|
|
$("#ui-slider-bar").mousedown(function(evt) {
|
2011-06-20 12:44:04 +02:00
|
|
|
$("#ui-slider-handle").css('left', (evt.clientX - $("#ui-slider-bar").offset().left));
|
|
|
|
$("#ui-slider-handle").trigger(evt);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Slider dragging
|
2020-11-21 19:37:57 +01:00
|
|
|
$("#ui-slider-handle").mousedown(function(evt) {
|
2011-06-20 12:44:04 +02:00
|
|
|
this.startLoc = evt.clientX;
|
|
|
|
this.currentLoc = parseInt($(this).css('left'));
|
|
|
|
var self = this;
|
|
|
|
sliderActive = true;
|
2020-11-21 19:37:57 +01:00
|
|
|
$(document).mousemove(function(evt2) {
|
2011-06-20 12:44:04 +02:00
|
|
|
$(self).css('pointer', 'move')
|
|
|
|
var newloc = self.currentLoc + (evt2.clientX - self.startLoc);
|
|
|
|
if (newloc < 0) newloc = 0;
|
|
|
|
if (newloc > ($("#ui-slider-bar").width() - 2)) newloc = ($("#ui-slider-bar").width() - 2);
|
2012-12-19 19:55:31 +01:00
|
|
|
$("#revision_label").html(html10n.get("timeslider.version", { "version": Math.floor(newloc * sliderLength / ($("#ui-slider-bar").width() - 2))}));
|
2011-06-20 12:44:04 +02:00
|
|
|
$(self).css('left', newloc);
|
|
|
|
if (getSliderPosition() != Math.floor(newloc * sliderLength / ($("#ui-slider-bar").width() - 2))) _callSliderCallbacks(Math.floor(newloc * sliderLength / ($("#ui-slider-bar").width() - 2)))
|
|
|
|
});
|
2020-11-21 19:37:57 +01:00
|
|
|
$(document).mouseup(function(evt2) {
|
2011-06-20 12:44:04 +02:00
|
|
|
$(document).unbind('mousemove');
|
|
|
|
$(document).unbind('mouseup');
|
|
|
|
sliderActive = false;
|
|
|
|
var newloc = self.currentLoc + (evt2.clientX - self.startLoc);
|
|
|
|
if (newloc < 0) newloc = 0;
|
|
|
|
if (newloc > ($("#ui-slider-bar").width() - 2)) newloc = ($("#ui-slider-bar").width() - 2);
|
|
|
|
$(self).css('left', newloc);
|
|
|
|
// if(getSliderPosition() != Math.floor(newloc * sliderLength / ($("#ui-slider-bar").width()-2)))
|
|
|
|
setSliderPosition(Math.floor(newloc * sliderLength / ($("#ui-slider-bar").width() - 2)))
|
2013-01-24 17:48:40 +01:00
|
|
|
if(parseInt($(self).css('left')) < 2){
|
|
|
|
$(self).css('left', '2px');
|
|
|
|
}else{
|
|
|
|
self.currentLoc = parseInt($(self).css('left'));
|
|
|
|
}
|
2011-06-20 12:44:04 +02:00
|
|
|
});
|
|
|
|
})
|
|
|
|
|
|
|
|
// play/pause toggling
|
2020-11-21 19:37:57 +01:00
|
|
|
$("#playpause_button_icon").click(function(evt) {
|
2020-04-03 16:11:01 +02:00
|
|
|
BroadcastSlider.playpause();
|
2011-06-20 12:44:04 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// next/prev saved revision and changeset
|
2020-11-21 19:37:57 +01:00
|
|
|
$('.stepper').click(function(evt) {
|
2020-04-03 16:11:01 +02:00
|
|
|
switch ($(this).attr("id")) {
|
|
|
|
case "leftstep":
|
2011-06-20 12:44:04 +02:00
|
|
|
setSliderPosition(getSliderPosition() - 1);
|
2020-04-03 16:11:01 +02:00
|
|
|
break;
|
|
|
|
case "rightstep":
|
2011-06-20 12:44:04 +02:00
|
|
|
setSliderPosition(getSliderPosition() + 1);
|
2020-04-03 16:11:01 +02:00
|
|
|
break;
|
|
|
|
case "leftstar":
|
2011-06-20 12:44:04 +02:00
|
|
|
var nextStar = 0; // default to first revision in document
|
|
|
|
for (var i = 0; i < savedRevisions.length; i++)
|
|
|
|
{
|
|
|
|
var pos = parseInt(savedRevisions[i].attr('pos'));
|
|
|
|
if (pos < getSliderPosition() && nextStar < pos) nextStar = pos;
|
|
|
|
}
|
|
|
|
setSliderPosition(nextStar);
|
2020-04-03 16:11:01 +02:00
|
|
|
break;
|
|
|
|
case "rightstar":
|
2011-06-20 12:44:04 +02:00
|
|
|
var nextStar = sliderLength; // default to last revision in document
|
|
|
|
for (var i = 0; i < savedRevisions.length; i++)
|
|
|
|
{
|
|
|
|
var pos = parseInt(savedRevisions[i].attr('pos'));
|
|
|
|
if (pos > getSliderPosition() && nextStar > pos) nextStar = pos;
|
|
|
|
}
|
|
|
|
setSliderPosition(nextStar);
|
2020-04-03 16:11:01 +02:00
|
|
|
break;
|
|
|
|
}
|
2011-06-20 12:44:04 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if (clientVars)
|
|
|
|
{
|
2020-04-03 16:11:01 +02:00
|
|
|
$("#timeslider-wrapper").show();
|
2019-04-16 00:34:29 +02:00
|
|
|
|
2013-01-28 10:13:37 +01:00
|
|
|
var startPos = clientVars.collab_client_vars.rev;
|
|
|
|
if(window.location.hash.length > 1)
|
|
|
|
{
|
2013-12-05 08:41:29 +01:00
|
|
|
var hashRev = Number(window.location.hash.substr(1));
|
|
|
|
if(!isNaN(hashRev))
|
|
|
|
{
|
|
|
|
// this is necessary because of the socket.io-event which loads the changesets
|
2013-01-28 10:13:37 +01:00
|
|
|
setTimeout(function() { setSliderPosition(hashRev); }, 1);
|
|
|
|
}
|
|
|
|
}
|
2019-04-16 00:34:29 +02:00
|
|
|
|
2012-04-23 12:52:30 +02:00
|
|
|
setSliderLength(clientVars.collab_client_vars.rev);
|
|
|
|
setSliderPosition(clientVars.collab_client_vars.rev);
|
2019-04-16 00:34:29 +02:00
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
_.each(clientVars.savedRevisions, function(revision) {
|
2012-04-15 19:25:23 +02:00
|
|
|
addSavedRevision(revision.revNum, revision);
|
|
|
|
})
|
2019-04-16 00:34:29 +02:00
|
|
|
|
2011-06-20 12:44:04 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
})();
|
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
BroadcastSlider.onSlider(function(loc) {
|
2011-06-20 12:44:04 +02:00
|
|
|
$("#viewlatest").html(loc == BroadcastSlider.getSliderLength() ? "Viewing latest content" : "View latest content");
|
|
|
|
})
|
2012-01-27 06:20:26 +01:00
|
|
|
|
|
|
|
return BroadcastSlider;
|
2011-06-20 12:44:04 +02:00
|
|
|
}
|
2012-01-16 02:23:48 +01:00
|
|
|
|
|
|
|
exports.loadBroadcastSliderJS = loadBroadcastSliderJS;
|