pad.libre-service.eu-etherpad/src/static/js/broadcast_slider.js

343 lines
11 KiB
JavaScript
Raw Normal View History

'use strict';
/**
* This code is mostly from the old Etherpad. Please help us to comment this code.
* This helps other people to understand this code better and helps them to improve it.
* TL;DR COMMENTS ON THIS FILE ARE HIGHLY APPRECIATED
*/
/**
* Copyright 2009 Google Inc.
2011-07-07 19:59:34 +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
*
* http://www.apache.org/licenses/LICENSE-2.0
2011-07-07 19:59:34 +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.
*/
2020-11-23 19:24:19 +01:00
// These parameters were global, now they are injected. A reference to the
// Timeslider controller would probably be more appropriate.
const _ = require('./underscore');
const padmodals = require('./pad_modals').padmodals;
const colorutils = require('./colorutils').colorutils;
const loadBroadcastSliderJS = (fireWhenAllScriptsAreLoaded) => {
2020-11-23 19:24:19 +01:00
let BroadcastSlider;
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-23 19:24:19 +01:00
(() => { // wrap this code in its own namespace
2020-11-23 19:24:19 +01:00
let sliderLength = 1000;
let sliderPos = 0;
let sliderActive = false;
const slidercallbacks = [];
const savedRevisions = [];
let sliderPlaying = false;
const _callSliderCallbacks = (newval) => {
2020-11-23 19:24:19 +01:00
sliderPos = newval;
for (let i = 0; i < slidercallbacks.length; i++) {
slidercallbacks[i](newval);
}
2020-11-23 19:24:19 +01:00
};
const updateSliderElements = () => {
2020-11-23 19:24:19 +01:00
for (let i = 0; i < savedRevisions.length; i++) {
const 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));
2020-11-23 19:24:19 +01:00
};
const addSavedRevision = (position, info) => {
2020-11-23 19:24:19 +01:00
const newSavedRevision = $('<div></div>');
newSavedRevision.addClass('star');
newSavedRevision.attr('pos', position);
newSavedRevision.css(
'left', (position * ($('#ui-slider-bar').width() - 2) / (sliderLength * 1.0)) - 1);
2020-11-23 19:24:19 +01:00
$('#ui-slider-bar').append(newSavedRevision);
newSavedRevision.mouseup((evt) => {
BroadcastSlider.setSliderPosition(position);
});
savedRevisions.push(newSavedRevision);
};
/* Begin small 'API' */
const onSlider = (callback) => {
slidercallbacks.push(callback);
};
const getSliderPosition = () => sliderPos;
const setSliderPosition = (newpos) => {
newpos = Number(newpos);
if (newpos < 0 || newpos > sliderLength) return;
2020-11-23 19:24:19 +01:00
if (!newpos) {
2013-02-07 00:35:58 +01:00
newpos = 0; // stops it from displaying NaN if newpos isn't set
}
2020-11-23 19:24:19 +01:00
window.location.hash = `#${newpos}`;
$('#ui-slider-handle').css(
'left', newpos * ($('#ui-slider-bar').width() - 2) / (sliderLength * 1.0));
2020-11-23 19:24:19 +01:00
$('a.tlink').map(function () {
$(this).attr('href', $(this).attr('thref').replace('%revision%', newpos));
});
2020-11-23 19:24:19 +01:00
$('#revision_label').html(html10n.get('timeslider.version', {version: newpos}));
$('#leftstar, #leftstep').toggleClass('disabled', newpos === 0);
$('#rightstar, #rightstep').toggleClass('disabled', newpos === sliderLength);
sliderPos = newpos;
_callSliderCallbacks(newpos);
};
const getSliderLength = () => sliderLength;
const setSliderLength = (newlength) => {
sliderLength = newlength;
updateSliderElements();
};
// just take over the whole slider screen with a reconnect message
const showReconnectUI = () => {
2020-11-23 19:24:19 +01:00
padmodals.showModal('disconnected');
};
const setAuthors = (authors) => {
2020-11-23 19:24:19 +01:00
const authorsList = $('#authorsList');
2012-03-24 20:35:45 +01:00
authorsList.empty();
2020-11-23 19:24:19 +01:00
let numAnonymous = 0;
let numNamed = 0;
const colorsAnonymous = [];
_.each(authors, (author) => {
if (author) {
const authorColor = clientVars.colorPalette[author.colorId] || author.colorId;
if (author.name) {
if (numNamed !== 0) authorsList.append(', ');
const textColor =
colorutils.textColorFromBackgroundColor(authorColor, clientVars.skinName);
$('<span />')
2020-11-23 19:24:19 +01:00
.text(author.name || 'unnamed')
.css('background-color', authorColor)
.css('color', textColor)
.addClass('author')
.appendTo(authorsList);
numNamed++;
2020-11-23 19:24:19 +01:00
} else {
numAnonymous++;
2020-11-23 19:24:19 +01:00
if (authorColor) colorsAnonymous.push(authorColor);
}
}
});
2020-11-23 19:24:19 +01:00
if (numAnonymous > 0) {
const anonymousAuthorString = html10n.get('timeslider.unnamedauthors', {num: numAnonymous});
2020-11-23 19:24:19 +01:00
if (numNamed !== 0) {
authorsList.append(` + ${anonymousAuthorString}`);
2012-03-24 20:35:45 +01:00
} else {
authorsList.append(anonymousAuthorString);
}
2020-11-23 19:24:19 +01:00
if (colorsAnonymous.length > 0) {
2012-03-25 15:48:31 +02:00
authorsList.append(' (');
2020-11-23 19:24:19 +01:00
_.each(colorsAnonymous, (color, i) => {
if (i > 0) authorsList.append(' ');
2012-04-15 19:18:29 +02:00
$('<span>&nbsp;</span>')
2020-11-23 19:24:19 +01:00
.css('background-color', color)
.addClass('author author-anonymous')
.appendTo(authorsList);
2012-03-25 15:48:31 +02:00
});
authorsList.append(')');
}
}
if (authors.length === 0) {
2020-11-23 19:24:19 +01:00
authorsList.append(html10n.get('timeslider.toolbar.authorsList'));
}
2020-11-23 19:24:19 +01:00
};
const playButtonUpdater = () => {
2020-11-23 19:24:19 +01:00
if (sliderPlaying) {
if (getSliderPosition() + 1 > sliderLength) {
$('#playpause_button_icon').toggleClass('pause');
sliderPlaying = false;
return;
}
setSliderPosition(getSliderPosition() + 1);
setTimeout(playButtonUpdater, 100);
}
};
const playpause = () => {
2020-11-23 19:24:19 +01:00
$('#playpause_button_icon').toggleClass('pause');
2020-11-23 19:24:19 +01:00
if (!sliderPlaying) {
if (getSliderPosition() === sliderLength) setSliderPosition(0);
sliderPlaying = true;
playButtonUpdater();
2020-11-23 19:24:19 +01:00
} else {
sliderPlaying = false;
}
};
BroadcastSlider = {
onSlider,
getSliderPosition,
setSliderPosition,
getSliderLength,
setSliderLength,
isSliderActive: () => sliderActive,
playpause,
addSavedRevision,
showReconnectUI,
setAuthors,
};
// assign event handlers to html UI elements after page load
2020-11-23 19:24:19 +01:00
fireWhenAllScriptsAreLoaded.push(() => {
$(document).keyup((e) => {
if (!e) e = window.event;
2020-11-23 19:24:19 +01:00
const code = e.keyCode || e.which;
if (code === 37) { // left
if (e.shiftKey) {
$('#leftstar').click();
} else {
$('#leftstep').click();
}
} else if (code === 39) { // right
if (e.shiftKey) {
$('#rightstar').click();
} else {
$('#rightstep').click();
}
} else if (code === 32) { // spacebar
2020-11-23 19:24:19 +01:00
$('#playpause_button_icon').trigger('click');
}
});
// Resize
2020-11-23 19:24:19 +01:00
$(window).resize(() => {
updateSliderElements();
});
// Slider click
2020-11-23 19:24:19 +01:00
$('#ui-slider-bar').mousedown((evt) => {
$('#ui-slider-handle').css('left', (evt.clientX - $('#ui-slider-bar').offset().left));
$('#ui-slider-handle').trigger(evt);
});
// Slider dragging
2020-11-23 19:24:19 +01:00
$('#ui-slider-handle').mousedown(function (evt) {
this.startLoc = evt.clientX;
this.currentLoc = parseInt($(this).css('left'));
sliderActive = true;
2020-11-23 19:24:19 +01:00
$(document).mousemove((evt2) => {
$(this).css('pointer', 'move');
let newloc = this.currentLoc + (evt2.clientX - this.startLoc);
if (newloc < 0) newloc = 0;
const maxPos = $('#ui-slider-bar').width() - 2;
if (newloc > maxPos) newloc = maxPos;
const version = Math.floor(newloc * sliderLength / maxPos);
$('#revision_label').html(html10n.get('timeslider.version', {version}));
$(this).css('left', newloc);
if (getSliderPosition() !== version) _callSliderCallbacks(version);
});
2020-11-23 19:24:19 +01:00
$(document).mouseup((evt2) => {
$(document).unbind('mousemove');
$(document).unbind('mouseup');
sliderActive = false;
let newloc = this.currentLoc + (evt2.clientX - this.startLoc);
if (newloc < 0) newloc = 0;
const maxPos = $('#ui-slider-bar').width() - 2;
if (newloc > maxPos) newloc = maxPos;
$(this).css('left', newloc);
setSliderPosition(Math.floor(newloc * sliderLength / maxPos));
if (parseInt($(this).css('left')) < 2) {
$(this).css('left', '2px');
2020-11-23 19:24:19 +01:00
} else {
this.currentLoc = parseInt($(this).css('left'));
2013-01-24 17:48:40 +01:00
}
});
2020-11-23 19:24:19 +01:00
});
// play/pause toggling
2020-11-23 19:24:19 +01:00
$('#playpause_button_icon').click((evt) => {
BroadcastSlider.playpause();
});
// next/prev saved revision and changeset
2020-11-23 19:24:19 +01:00
$('.stepper').click(function (evt) {
switch ($(this).attr('id')) {
case 'leftstep':
setSliderPosition(getSliderPosition() - 1);
break;
2020-11-23 19:24:19 +01:00
case 'rightstep':
setSliderPosition(getSliderPosition() + 1);
break;
case 'leftstar': {
let nextStar = 0; // default to first revision in document
for (let i = 0; i < savedRevisions.length; i++) {
const pos = parseInt(savedRevisions[i].attr('pos'));
if (pos < getSliderPosition() && nextStar < pos) nextStar = pos;
}
setSliderPosition(nextStar);
break;
}
case 'rightstar': {
let nextStar = sliderLength; // default to last revision in document
for (let i = 0; i < savedRevisions.length; i++) {
const pos = parseInt(savedRevisions[i].attr('pos'));
if (pos > getSliderPosition() && nextStar > pos) nextStar = pos;
}
setSliderPosition(nextStar);
break;
}
}
2020-11-23 19:24:19 +01:00
});
if (clientVars) {
$('#timeslider-wrapper').show();
if (window.location.hash.length > 1) {
const hashRev = Number(window.location.hash.substr(1));
if (!isNaN(hashRev)) {
// this is necessary because of the socket.io-event which loads the changesets
2020-11-23 19:24:19 +01:00
setTimeout(() => { setSliderPosition(hashRev); }, 1);
}
}
setSliderLength(clientVars.collab_client_vars.rev);
setSliderPosition(clientVars.collab_client_vars.rev);
2020-11-23 19:24:19 +01:00
_.each(clientVars.savedRevisions, (revision) => {
addSavedRevision(revision.revNum, revision);
2020-11-23 19:24:19 +01:00
});
}
});
})();
2020-11-23 19:24:19 +01:00
BroadcastSlider.onSlider((loc) => {
$('#viewlatest').html(
`${loc === BroadcastSlider.getSliderLength() ? 'Viewing' : 'View'} latest content`);
2020-11-23 19:24:19 +01:00
});
return BroadcastSlider;
};
exports.loadBroadcastSliderJS = loadBroadcastSliderJS;