etherpad-lite/src/static/js/broadcast_slider.js

336 lines
11 KiB
JavaScript
Raw Normal View History

/**
* Copyright 2009 Google Inc.
2011-07-07 18:59:34 +01: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 18:59:34 +01:00
*
* http://www.apache.org/licenses/LICENSE-2.0
2011-07-07 18:59:34 +01: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-03-17 13:36:42 +01:00
var _ = require('./underscore');
var padmodals = require('./pad_modals').padmodals;
2013-12-05 16:41:50 +02:00
var sliderui = require('./sliderui');
2013-12-05 18:41:06 +02:00
require('./jquery.class');
$.Class("RevisionSlider",
{//statics
2013-12-16 15:32:08 +02:00
PLAYBACK_DELAY: 400,
},
{//instance
2013-12-13 11:59:17 +02:00
init: function (connection, root_element) {
this.connection = connection;
this.revision_number = this.connection.getCurrentRevision().revnum;
2013-12-16 15:32:08 +02:00
this.timestamp = 0;
this.is_playing = false;
// if there was a revision specified in the 'location.hash', jump to it.
if (window.location.hash.length > 1) {
var rev = Number(window.location.hash.substr(1));
if(!isNaN(rev))
this.revision_number = rev;
}
console.log("New RevisionSlider, head_revision = %d", this.revision_number);
// parse the various elements we need:
this.elements = {};
this.loadElements(root_element);
var _this = this;
2013-12-12 13:43:33 +02:00
this.slider = new SliderUI(this.elements.slider_bar,
options = {
2013-12-13 11:59:17 +02:00
value: this.revision_number,
max: this.connection.head_revision,
change: function () { _this.onChange.apply(_this, arguments); },
slide: function () { _this.onSlide.apply(_this, arguments); },
});
this.loadSavedRevisionHandles();
2013-12-13 11:59:17 +02:00
this.slider.render();
this._mouseInit();
this.goToRevision(this.revision_number);
},
onChange: function (value) {
console.log("in change handler:", value);
this.goToRevision(value);
},
onSlide: function (value) {
console.log("in slide handler:", value);
2013-12-16 14:17:10 +02:00
this.goToRevision(value);
},
loadElements: function (root_element) {
2013-12-12 13:43:33 +02:00
this.elements.root = root_element;
//this.elements['slider-handle'] = root_element.first("#ui-slider-handle");
2013-12-12 13:43:33 +02:00
this.elements.slider_bar = root_element.find("#ui-slider-bar");
this.elements.slider = root_element.find("#timeslider-slider");
this.elements.button_left = root_element.find("#leftstep");
this.elements.button_right = root_element.find("#rightstep");
this.elements.button_play = root_element.find("#playpause_button");
this.elements.timestamp = root_element.find("#timer");
this.elements.revision_label = root_element.find("#revision_label");
this.elements.revision_date = root_element.find("#revision_date");
this.elements.authors = root_element.first("#authorsList");
},
loadSavedRevisionHandles: function () {
2013-12-13 11:59:17 +02:00
for (var r in this.connection.savedRevisions) {
var rev = this.connection.savedRevisions[r];
2013-12-05 16:41:50 +02:00
this.slider.createHandle(rev.revNum, "star");
2013-12-12 13:43:33 +02:00
}
},
2013-12-16 15:32:08 +02:00
playpause: function () {
if (this.is_playing) {
this.is_playing = false;
return;
}
var revnum = this.revision_number;
if (revnum == this.connection.head_revision)
revnum = 0;
var _this = this;
var keepPlaying = function (current_revnum) {
if (current_revnum == _this.connection.head_revision)
_this.is_playing = false;
if (!_this.is_playing)
return;
setTimeout(function () {
_this.goToRevision(current_revnum + 1, keepPlaying);
}, RevisionSlider.PLAYBACK_DELAY);
};
this.is_playing = true;
this.goToRevision(revnum, keepPlaying);
},
/**
* Update the UI elements to the current revision
*/
render: function () {
this.elements.revision_label.html(html10n.get("timeslider.version", { "version": this.revision_number }));
this.slider.setValue(this.revision_number);
window.location.hash = "#" + this.revision_number;
this.setTimestamp(this.timestamp);
if (this.is_playing)
this.elements.button_play.find("div").addClass("pause");
else
this.elements.button_play.find("div").removeClass("pause");
if (this.revision_number == this.connection.head_revision)
this.elements.button_right.addClass("disabled");
else
this.elements.button_right.removeClass("disabled");
if (this.revision_number === 0)
this.elements.button_left.addClass("disabled");
else
this.elements.button_left.removeClass("disabled");
},
/**
* Go to a specific revision number. This will perform the actual
* transition to the revision and set the UI elements as required
* once the transition is done. The callback can be used to perform
* actions after the transition is complete and the UI has been
* updated.
* @param {number} revnum - The revision to transition to.
* @param {callback} atRevision_callback - The callback.
*/
goToRevision: function (revnum, atRevision_callback) {
if (revnum > this.connection.head_revision)
revnum = this.connection.latest_revision;
if (revnum < 0)
revnum = 0;
console.log("GO TO REVISION", revnum);
var _this = this;
this.connection.goToRevision(revnum, function (revision, timestamp) {
console.log("[revisionslider > goToRevision > callback]", revision, timestamp);
//update UI elements:
2013-12-16 15:32:08 +02:00
_this.revision_number = revision.revnum;
_this.timestamp = timestamp;
_this.render.call(_this);
//TODO: set the enabled/disabled for button-left and button-right
2013-12-16 15:32:08 +02:00
if (atRevision_callback)
atRevision_callback(revnum);
});
},
2013-12-16 15:32:08 +02:00
/**
* Set the timestamp and revision date displays
* @param {number} timestamp - The timestamp of the current revision.
*/
setTimestamp: function (timestamp) {
var zeropad = function (str, length) {
str = str + "";
while (str.length < length)
str = '0' + str;
return str;
};
var months = [
html10n.get("timeslider.month.january"),
html10n.get("timeslider.month.february"),
html10n.get("timeslider.month.march"),
html10n.get("timeslider.month.april"),
html10n.get("timeslider.month.may"),
html10n.get("timeslider.month.june"),
html10n.get("timeslider.month.july"),
html10n.get("timeslider.month.august"),
html10n.get("timeslider.month.september"),
html10n.get("timeslider.month.october"),
html10n.get("timeslider.month.november"),
html10n.get("timeslider.month.december")
];
var date = new Date(timestamp);
var timestamp_format = html10n.get("timeslider.dateformat",
{
"day": zeropad(date.getDate(), 2),
"month": zeropad(date.getMonth() + 1, 2),
"year": date.getFullYear(),
"hours": zeropad(date.getHours(), 2),
"minutes": zeropad(date.getMinutes(), 2),
"seconds": zeropad(date.getSeconds(), 2),
});
this.elements.timestamp.html(timestamp_format);
var revisionDate = html10n.get("timeslider.saved", {
"day": date.getDate(),
"month": months[date.getMonth()],
"year": date.getFullYear()
});
this.elements.revision_date.html(revisionDate);
},
_mouseInit: function () {
var _this = this;
2013-12-12 13:43:33 +02:00
this.elements.button_left.on("click", function (event) {
if (! $(this).hasClass("disabled"))
_this.goToRevision(_this.revision_number - 1);
});
2013-12-12 13:43:33 +02:00
this.elements.button_right.on("click", function (event) {
if (! $(this).hasClass("disabled"))
_this.goToRevision(_this.revision_number + 1);
});
2013-12-16 15:32:08 +02:00
this.elements.button_play.on("click", function (event) {
_this.playpause();
});
}
}
);
function init(connection, fireWhenAllScriptsAreLoaded)
{
var BroadcastSlider;
2011-07-07 18:59:34 +01:00
(function()
{ // wrap this code in its own namespace
tsui = new RevisionSlider(connection, $("#timeslider-top"));
var clientVars = connection.clientVars;
function disableSelection(element)
{
2011-07-07 18:59:34 +01:00
element.onselectstart = function()
{
return false;
};
element.unselectable = "on";
element.style.MozUserSelect = "none";
element.style.cursor = "default";
}
// just take over the whole slider screen with a reconnect message
function showReconnectUI()
{
2012-07-14 15:54:44 +02:00
padmodals.showModal("disconnected");
}
//TODO: figure out what the hell this is for
2012-03-24 20:35:45 +01:00
var fixPadHeight = _.throttle(function(){
var height = $('#timeslider-top').height();
$('#editorcontainerbox').css({marginTop: height});
}, 600);
function setAuthors(authors)
{
2012-03-24 20:35:45 +01:00
var authorsList = $("#authorsList");
authorsList.empty();
var numAnonymous = 0;
var numNamed = 0;
2012-03-25 15:48:31 +02:00
var colorsAnonymous = [];
2012-03-17 13:36:42 +01:00
_.each(authors, function(author)
{
2012-03-25 15:48:31 +02:00
var authorColor = clientVars.colorPalette[author.colorId] || author.colorId;
if (author.name)
{
2012-03-24 20:35:45 +01:00
if (numNamed !== 0) authorsList.append(', ');
2012-03-25 15:48:31 +02:00
$('<span />')
.text(author.name || "unnamed")
.css('background-color', authorColor)
.addClass('author')
.appendTo(authorsList);
numNamed++;
}
else
{
numAnonymous++;
2012-03-25 15:48:31 +02:00
if(authorColor) colorsAnonymous.push(authorColor);
}
});
if (numAnonymous > 0)
{
2013-07-23 12:43:30 +02:00
var anonymousAuthorString = html10n.get("timeslider.unnamedauthors", { num: numAnonymous });
2012-03-24 20:35:45 +01:00
if (numNamed !== 0){
authorsList.append(' + ' + anonymousAuthorString);
} else {
authorsList.append(anonymousAuthorString);
}
2012-03-25 15:48:31 +02:00
if(colorsAnonymous.length > 0){
authorsList.append(' (');
_.each(colorsAnonymous, function(color, i){
if( i > 0 ) authorsList.append(' ');
2012-04-15 19:18:29 +02:00
$('<span>&nbsp;</span>')
2012-03-25 15:48:31 +02:00
.css('background-color', color)
.addClass('author author-anonymous')
.appendTo(authorsList);
});
authorsList.append(')');
}
}
2013-12-12 13:43:33 +02:00
if (authors.length === 0)
{
2012-12-19 19:55:31 +01:00
authorsList.append(html10n.get("timeslider.toolbar.authorsList"));
}
2012-03-24 20:35:45 +01:00
fixPadHeight();
}
// assign event handlers to html UI elements after page load
//$(window).load(function ()
2011-07-07 18:59:34 +01:00
fireWhenAllScriptsAreLoaded.push(function()
{
disableSelection($("#playpause_button")[0]);
disableSelection($("#timeslider")[0]);
if (clientVars)
{
$("#timeslider").show();
}
});
})();
}
2013-12-13 11:59:17 +02:00
exports.init = init;