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

338 lines
9.4 KiB
JavaScript
Raw Normal View History

/**
2013-03-09 23:57:42 +01:00
* 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
*/
2011-03-26 14:10:41 +01:00
/**
* Copyright 2009 Google Inc.
2011-07-07 19:59:34 +02:00
*
2011-03-26 14:10:41 +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 19:59:34 +02:00
*
2011-03-26 14:10:41 +01:00
* http://www.apache.org/licenses/LICENSE-2.0
2011-07-07 19:59:34 +02:00
*
2011-03-26 14:10:41 +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.
*/
var hooks = require('./pluginfw/hooks');
2012-03-07 02:27:03 +01:00
var padutils = require('./pad_utils').padutils;
var padeditor = require('./pad_editor').padeditor;
var padsavedrevs = require('./pad_savedrevs');
var ToolbarItem = function (element) {
this.$el = element;
};
ToolbarItem.prototype.getCommand = function () {
return this.$el.attr("data-key");
};
ToolbarItem.prototype.getValue = function () {
if (this.isSelect()) {
return this.$el.find("select").val();
}
};
ToolbarItem.prototype.setValue = function (val) {
if (this.isSelect()) {
return this.$el.find("select").val(val);
}
};
ToolbarItem.prototype.getType = function () {
return this.$el.attr("data-type");
};
ToolbarItem.prototype.isSelect = function () {
return this.getType() == "select";
};
ToolbarItem.prototype.isButton = function () {
return this.getType() == "button";
};
ToolbarItem.prototype.bind = function (callback) {
var self = this;
if (self.isButton()) {
self.$el.click(function (event) {
callback(self.getCommand(), self);
event.preventDefault();
});
}
else if (self.isSelect()) {
self.$el.find("select").change(function () {
callback(self.getCommand(), self);
});
}
};
2011-07-07 19:59:34 +02:00
var padeditbar = (function()
{
2011-03-26 14:10:41 +01:00
2011-07-07 19:59:34 +02:00
var syncAnimation = (function()
{
2011-03-26 14:10:41 +01:00
var SYNCING = -100;
var DONE = 100;
var state = DONE;
var fps = 25;
2011-07-07 19:59:34 +02:00
var step = 1 / fps;
2011-03-26 14:10:41 +01:00
var T_START = -0.5;
var T_FADE = 1.0;
var T_GONE = 1.5;
2011-07-07 19:59:34 +02:00
var animator = padutils.makeAnimationScheduler(function()
{
if (state == SYNCING || state == DONE)
{
2011-03-26 14:10:41 +01:00
return false;
}
2011-07-07 19:59:34 +02:00
else if (state >= T_GONE)
{
2011-03-26 14:10:41 +01:00
state = DONE;
$("#syncstatussyncing").css('display', 'none');
$("#syncstatusdone").css('display', 'none');
return false;
}
2011-07-07 19:59:34 +02:00
else if (state < 0)
{
2011-03-26 14:10:41 +01:00
state += step;
2011-07-07 19:59:34 +02:00
if (state >= 0)
{
2011-03-26 14:10:41 +01:00
$("#syncstatussyncing").css('display', 'none');
$("#syncstatusdone").css('display', 'block').css('opacity', 1);
}
return true;
}
2011-07-07 19:59:34 +02:00
else
{
2011-03-26 14:10:41 +01:00
state += step;
2011-07-07 19:59:34 +02:00
if (state >= T_FADE)
{
2011-03-26 14:10:41 +01:00
$("#syncstatusdone").css('opacity', (T_GONE - state) / (T_GONE - T_FADE));
}
return true;
}
2011-07-07 19:59:34 +02:00
}, step * 1000);
2011-03-26 14:10:41 +01:00
return {
2011-07-07 19:59:34 +02:00
syncing: function()
{
2011-03-26 14:10:41 +01:00
state = SYNCING;
$("#syncstatussyncing").css('display', 'block');
$("#syncstatusdone").css('display', 'none');
},
2011-07-07 19:59:34 +02:00
done: function()
{
2011-03-26 14:10:41 +01:00
state = T_START;
animator.scheduleAnimation();
}
};
}());
var self = {
init: function() {
var self = this;
2011-03-26 14:10:41 +01:00
$("#editbar .editbarbutton").attr("unselectable", "on"); // for IE
$("#editbar").removeClass("disabledtoolbar").addClass("enabledtoolbar");
$("#editbar [data-key]").each(function () {
(new ToolbarItem($(this))).bind(function (command, item) {
self.triggerCommand(command, item);
});
});
registerDefaultCommands(self);
hooks.callAll("postToolbarInit", {
toolbar: self,
ace: padeditor.ace
});
2011-03-26 14:10:41 +01:00
},
2011-07-07 19:59:34 +02:00
isEnabled: function()
{
// return !$("#editbar").hasClass('disabledtoolbar');
return true;
2011-03-26 14:10:41 +01:00
},
2011-07-07 19:59:34 +02:00
disable: function()
{
2011-03-26 14:10:41 +01:00
$("#editbar").addClass('disabledtoolbar').removeClass("enabledtoolbar");
},
commands: {},
registerCommand: function (cmd, callback) {
this.commands[cmd] = callback;
return this;
},
registerDropdownCommand: function (cmd, dropdown) {
dropdown = dropdown || cmd;
this.registerCommand(cmd, function () {
self.toggleDropDown(dropdown);
});
},
registerAceCommand: function (cmd, callback) {
this.registerCommand(cmd, function (cmd, ace) {
ace.callWithAce(function (ace) {
callback(cmd, ace);
}, cmd, true);
});
},
triggerCommand: function (cmd, item) {
if (self.isEnabled() && this.commands[cmd]) {
this.commands[cmd](cmd, padeditor.ace, item);
2011-03-26 14:10:41 +01:00
}
if(padeditor.ace) padeditor.ace.focus();
2011-03-26 14:10:41 +01:00
},
2012-07-13 08:24:02 +02:00
toggleDropDown: function(moduleName, cb)
2011-07-08 16:19:38 +02:00
{
var modules = ["settings", "connectivity", "importexport", "embed", "users"];
2013-03-09 23:57:42 +01:00
// hide all modules and remove highlighting of all buttons
2011-07-08 16:19:38 +02:00
if(moduleName == "none")
{
var returned = false
2011-07-08 16:19:38 +02:00
for(var i=0;i<modules.length;i++)
{
2011-08-21 20:07:35 +02:00
//skip the userlist
if(modules[i] == "users")
continue;
2013-03-09 23:57:42 +01:00
2011-07-08 16:19:38 +02:00
var module = $("#" + modules[i]);
2013-03-09 23:57:42 +01:00
2011-07-08 16:19:38 +02:00
if(module.css('display') != "none")
{
$("#" + modules[i] + "link").removeClass("selected");
module.slideUp("fast", cb);
returned = true;
2011-07-08 16:19:38 +02:00
}
}
if(!returned && cb) return cb();
2011-07-08 16:19:38 +02:00
}
2013-03-09 23:57:42 +01:00
else
2011-07-08 16:19:38 +02:00
{
// hide all modules that are not selected and remove highlighting
// respectively add highlighting to the corresponding button
2011-07-08 16:19:38 +02:00
for(var i=0;i<modules.length;i++)
{
var module = $("#" + modules[i]);
2013-03-09 23:57:42 +01:00
2011-07-08 16:19:38 +02:00
if(module.css('display') != "none")
{
$("#" + modules[i] + "link").removeClass("selected");
2011-07-08 16:19:38 +02:00
module.slideUp("fast");
}
else if(modules[i]==moduleName)
{
$("#" + modules[i] + "link").addClass("selected");
module.slideDown("fast", cb);
2011-07-08 16:19:38 +02:00
}
}
}
},
2011-07-07 19:59:34 +02:00
setSyncStatus: function(status)
{
if (status == "syncing")
{
2011-03-26 14:10:41 +01:00
syncAnimation.syncing();
}
2011-07-07 19:59:34 +02:00
else if (status == "done")
{
2011-03-26 14:10:41 +01:00
syncAnimation.done();
}
2011-11-24 16:34:28 +01:00
},
setEmbedLinks: function()
{
if ($('#readonlyinput').is(':checked'))
{
var basePath = document.location.href.substring(0, document.location.href.indexOf("/p/"));
2012-04-23 16:20:55 +02:00
var readonlyLink = basePath + "/p/" + clientVars.readOnlyId;
$('#embedinput').val("<iframe name='embed_readonly' src='" + readonlyLink + "?showControls=true&showChat=true&showLineNumbers=true&useMonospaceFont=false' width=600 height=400></iframe>");
2011-11-24 16:34:28 +01:00
$('#linkinput').val(readonlyLink);
}
else
{
var padurl = window.location.href.split("?")[0];
$('#embedinput').val("<iframe name='embed_readwrite' src='" + padurl + "?showControls=true&showChat=true&showLineNumbers=true&useMonospaceFont=false' width=600 height=400></iframe>");
2011-11-24 16:34:28 +01:00
$('#linkinput').val(padurl);
}
2011-03-26 14:10:41 +01:00
}
};
function aceAttributeCommand(cmd, ace) {
ace.ace_toggleAttributeOnSelection(cmd);
}
function registerDefaultCommands(toolbar) {
toolbar.registerDropdownCommand("showusers", "users");
toolbar.registerDropdownCommand("settings");
toolbar.registerDropdownCommand("connectivity");
toolbar.registerDropdownCommand("import_export", "importexport");
toolbar.registerCommand("embed", function () {
toolbar.setEmbedLinks();
$('#linkinput').focus().select();
toolbar.toggleDropDown("embed");
});
toolbar.registerCommand("savedRevision", function () {
padsavedrevs.saveNow();
});
toolbar.registerCommand("showTimeSlider", function () {
document.location = document.location + "/timeslider";
});
toolbar.registerAceCommand("bold", aceAttributeCommand);
toolbar.registerAceCommand("italic", aceAttributeCommand);
toolbar.registerAceCommand("underline", aceAttributeCommand);
toolbar.registerAceCommand("strikethrough", aceAttributeCommand);
toolbar.registerAceCommand("undo", function (cmd, ace) {
ace.ace_doUndoRedo(cmd);
});
toolbar.registerAceCommand("redo", function (cmd) {
ace.ace_doUndoRedo(cmd);
});
toolbar.registerAceCommand("insertunorderedlist", function (cmd, ace) {
ace.ace_doInsertUnorderedList();
});
toolbar.registerAceCommand("insertorderedlist", function (cmd, ace) {
ace.ace_doInsertOrderedList();
});
toolbar.registerAceCommand("indent", function (cmd, ace) {
if (!ace.ace_doIndentOutdent(false)) {
ace.ace_doInsertUnorderedList();
}
});
toolbar.registerAceCommand("outdent", function (cmd, ace) {
ace.ace_doIndentOutdent(true);
});
toolbar.registerAceCommand("clearauthorship", function (cmd, ace) {
if ((!(ace.ace_getRep().selStart && ace.ace_getRep().selEnd)) || ace.ace_isCaret()) {
if (window.confirm(html10n.get("pad.editbar.clearcolors"))) {
ace.ace_performDocumentApplyAttributesToCharRange(0, ace.ace_getRep().alltext.length, [
['author', '']
]);
}
}
else {
ace.ace_setAttributeOnSelection('author', '');
}
});
}
2011-03-26 14:10:41 +01:00
return self;
2011-03-26 15:50:13 +01:00
}());
exports.padeditbar = padeditbar;