Skip to content

Instantly share code, notes, and snippets.

@leylaso
Created March 28, 2012 04:18
Show Gist options
  • Save leylaso/2223573 to your computer and use it in GitHub Desktop.
Save leylaso/2223573 to your computer and use it in GitHub Desktop.
custom pad.js file for etherpad-lite allows setting author names as line numbers
/**
* @file
* This file is an example of ways to modify etherpad-lite behaviour from the
* static/custom/pad.js file generated by etherpad-lite on first run.
*/
var edBod = [];
var lastLine = 0;
/**
* This function is called in static/js/pad.js
*/
function customStart() {
//console.log('Starting custom pad.js for spectator mode hacks');
jQuery(document).ready( function() {
// Try and retrieve the first iframe, containing the outer body
ifr = jQuery('iframe');
if (ifr.length < 1) {
setTimeout('customStart()', 200);
//console.log('no outer frame, retrying');
return;
}
// Try and retrieve the inner iframe
ifr = jQuery(ChildAccessibleAce2Editor.registry[1].editor.getFrame().contentDocument.body).children('iframe');
if (ifr.length < 1) {
setTimeout('customStart()', 200);
//console.log('no inner frame, retrying');
return;
}
// Set the value of edBod for later use
edBod = ifr[0].contentDocument.body;
// Try and retrieve more than one div
edDivs = jQuery(edBod).children('div');
if (edDivs.length < 2) {
setTimeout('customStart()', 100);
//console.log('too few divs, retrying');
return;
}
// for this to work, we can't have people clearing the authorship
jQuery('#clearAuthorship').toggle();
setInterval("userNameLines()", 1000);
dumpSpecMode();
});
}
/**
* Stick author names into the pad line numbers where available
*/
function userNameLines() {
// Fetch the divs in the actual etherpad text
edDivs = jQuery(edBod).children('div');
// Fetch data about the various pad authors
edAuths = clientVars.collab_client_vars.historicalAuthorData;
// Fetch the side divs that contain the line numbers
sideDivs = jQuery(ChildAccessibleAce2Editor.registry[1].editor.getFrame().contentDocument.body).find('div#sidediv').find('td#sidedivinner').children();
// Iterate through the documents lines and get the authors
for (i=lastLine;i<edDivs.length;i++) {
sp = jQuery(edDivs[i]).find('span:first');
if (sp.length>0 && !jQuery(sideDivs[i]).hasClass('usernamed')) {
cl = jQuery(sp).attr('class');
if (cl.length > 0) {
// Assume the first class in the string is the author class
auth = cl.split(' ');
auth = convertClass2Author(auth[0]);
// Get the author object with the parsed id
auth = edAuths[auth];
// Inject the author name into the apropriate sideDiv
jQuery(sideDivs[i]).text(auth.name);
jQuery(sideDivs[i]).css('color', auth.colorId);
jQuery(sideDivs[i]).addClass('usernamed')
//console.log('set ' + auth.name + ' at ' + i);
lastLine = i;
}
}
}
}
/**
* Stolen from ace2inner.js, converts a class name to an author id
*/
function convertClass2Author(className) {
if (className.substring(0, 7) == "author-")
{
return className.substring(7).replace(/[a-y0-9]+|-|z.+?z/g, function(cc)
{
if (cc == '-') return '.';
else if (cc.charAt(0) == 'z')
{
return String.fromCharCode(Number(cc.slice(1, -1)));
}
else
{
return cc;
}
});
}
}
/**
* Insert a popup to dump specmode
*/
function dumpSpecMode() {
dumper = '<div id="specdump" class="popup" style="display: none; position: absolute; top: 55px; right: 20px;"><h1>HTML export of spectator mode</h1><textarea readonly="readonly" cols="50", rows="10"></textarea></div>';
button = '<li id="specdumplink" style="text-align: center"><a class="buttonicon" id="specdumplink" title="HTML export of spectator mode" style="background-image: none; color: #666; font-size: 16px;">&hearts;</a></li>';
jQuery('div#embed').after(dumper);
jQuery('ul#menu_right').prepend(button);
// Bind behaviour to the new menu button
jQuery('ul#menu_right li#specdumplink').click(function() {
specdump = jQuery('div#specdump')[0];
if (jQuery(specdump).hasClass('slid-down')) {
jQuery(specdump).slideUp();
jQuery(specdump).removeClass('slid-down');
}
else {
jQuery(specdump).slideDown();
jQuery(specdump).addClass('slid-down');
// Fetch and process the pad contents and insert it into the textarea
sideDivs = jQuery(ChildAccessibleAce2Editor.registry[1].editor.getFrame().contentDocument.body).find('div#sidediv').find('td#sidedivinner').children();
edDivs = jQuery(edBod).children('div');
output = "<table>\n";
for (i=0;i<edDivs.length;i++) {
output = output + " <tr><td class='" + jQuery(sideDivs[i]).attr('class') + "' style='" + jQuery(sideDivs[i]).attr('style') +"'>" + sideDivs[i].innerHTML + "</td><td class='" +jQuery(edDivs[i]).attr('class') + "'>" + edDivs[i].innerHTML + "</td></tr>\n";
}
output = output + "</table>";
jQuery(specdump).find('textarea').val(output);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment