linting: pad_editbar.js

This commit is contained in:
John McLear 2020-12-16 12:12:25 +00:00 committed by GitHub
parent 6f309ac20a
commit 92e36b82b5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,3 +1,4 @@
'use strict';
/** /**
* This code is mostly from the old Etherpad. Please help us to comment this code. * 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. * This helps other people to understand this code better and helps them to improve it.
@ -25,8 +26,8 @@ const hooks = require('./pluginfw/hooks');
const padutils = require('./pad_utils').padutils; const padutils = require('./pad_utils').padutils;
const padeditor = require('./pad_editor').padeditor; const padeditor = require('./pad_editor').padeditor;
const padsavedrevs = require('./pad_savedrevs'); const padsavedrevs = require('./pad_savedrevs');
const _ = require('ep_etherpad-lite/static/js/underscore'); const _ = require('underscore');
require('ep_etherpad-lite/static/js/vendors/nice-select'); require('./vendors/nice-select');
const ToolbarItem = function (element) { const ToolbarItem = function (element) {
this.$el = element; this.$el = element;
@ -54,11 +55,11 @@ ToolbarItem.prototype.getType = function () {
}; };
ToolbarItem.prototype.isSelect = function () { ToolbarItem.prototype.isSelect = function () {
return this.getType() == 'select'; return this.getType() === 'select';
}; };
ToolbarItem.prototype.isButton = function () { ToolbarItem.prototype.isButton = function () {
return this.getType() == 'button'; return this.getType() === 'button';
}; };
ToolbarItem.prototype.bind = function (callback) { ToolbarItem.prototype.bind = function (callback) {
@ -78,8 +79,8 @@ ToolbarItem.prototype.bind = function (callback) {
}; };
var padeditbar = (function () { const padeditbar = (function () {
const syncAnimation = (function () { const syncAnimationFn = () => {
const SYNCING = -100; const SYNCING = -100;
const DONE = 100; const DONE = 100;
let state = DONE; let state = DONE;
@ -89,7 +90,7 @@ var padeditbar = (function () {
const T_FADE = 1.0; const T_FADE = 1.0;
const T_GONE = 1.5; const T_GONE = 1.5;
const animator = padutils.makeAnimationScheduler(() => { const animator = padutils.makeAnimationScheduler(() => {
if (state == SYNCING || state == DONE) { if (state === SYNCING || state === DONE) {
return false; return false;
} else if (state >= T_GONE) { } else if (state >= T_GONE) {
state = DONE; state = DONE;
@ -112,19 +113,20 @@ var padeditbar = (function () {
} }
}, step * 1000); }, step * 1000);
return { return {
syncing() { syncing: () => {
state = SYNCING; state = SYNCING;
$('#syncstatussyncing').css('display', 'block'); $('#syncstatussyncing').css('display', 'block');
$('#syncstatusdone').css('display', 'none'); $('#syncstatusdone').css('display', 'none');
}, },
done() { done: () => {
state = T_START; state = T_START;
animator.scheduleAnimation(); animator.scheduleAnimation();
}, },
}; };
}()); };
const syncAnimation = syncAnimationFn();
var self = { const self = {
init() { init() {
const self = this; const self = this;
self.dropdowns = []; self.dropdowns = [];
@ -170,13 +172,11 @@ var padeditbar = (function () {
$('#editbar').toggleClass('editor-scrolled', $(this).scrollTop() > 2); $('#editbar').toggleClass('editor-scrolled', $(this).scrollTop() > 2);
}); });
}, },
isEnabled() { isEnabled: () => true,
return true; disable: () => {
},
disable() {
$('#editbar').addClass('disabledtoolbar').removeClass('enabledtoolbar'); $('#editbar').addClass('disabledtoolbar').removeClass('enabledtoolbar');
}, },
enable() { enable: () => {
$('#editbar').addClass('enabledtoolbar').removeClass('disabledtoolbar'); $('#editbar').addClass('enabledtoolbar').removeClass('disabledtoolbar');
}, },
commands: {}, commands: {},
@ -204,7 +204,7 @@ var padeditbar = (function () {
} }
if (padeditor.ace) padeditor.ace.focus(); if (padeditor.ace) padeditor.ace.focus();
}, },
toggleDropDown(moduleName, cb) { toggleDropDown: (moduleName, cb) => {
// do nothing if users are sticked // do nothing if users are sticked
if (moduleName === 'users' && $('#users').hasClass('stickyUsers')) { if (moduleName === 'users' && $('#users').hasClass('stickyUsers')) {
return; return;
@ -214,15 +214,15 @@ var padeditbar = (function () {
$('.toolbar-popup').removeClass('popup-show'); $('.toolbar-popup').removeClass('popup-show');
// hide all modules and remove highlighting of all buttons // hide all modules and remove highlighting of all buttons
if (moduleName == 'none') { if (moduleName === 'none') {
const returned = false; const returned = false;
for (var i = 0; i < self.dropdowns.length; i++) { for (let i = 0; i < self.dropdowns.length; i++) {
var thisModuleName = self.dropdowns[i]; const thisModuleName = self.dropdowns[i];
// skip the userlist // skip the userlist
if (thisModuleName == 'users') continue; if (thisModuleName === 'users') continue;
var module = $(`#${thisModuleName}`); const module = $(`#${thisModuleName}`);
// skip any "force reconnect" message // skip any "force reconnect" message
const isAForceReconnectMessage = module.find('button#forcereconnect:visible').length > 0; const isAForceReconnectMessage = module.find('button#forcereconnect:visible').length > 0;
@ -237,14 +237,14 @@ var padeditbar = (function () {
} else { } else {
// hide all modules that are not selected and remove highlighting // hide all modules that are not selected and remove highlighting
// respectively add highlighting to the corresponding button // respectively add highlighting to the corresponding button
for (var i = 0; i < self.dropdowns.length; i++) { for (let i = 0; i < self.dropdowns.length; i++) {
var thisModuleName = self.dropdowns[i]; const thisModuleName = self.dropdowns[i];
var module = $(`#${thisModuleName}`); const module = $(`#${thisModuleName}`);
if (module.hasClass('popup-show')) { if (module.hasClass('popup-show')) {
$(`li[data-key=${thisModuleName}] > a`).removeClass('selected'); $(`li[data-key=${thisModuleName}] > a`).removeClass('selected');
module.removeClass('popup-show'); module.removeClass('popup-show');
} else if (thisModuleName == moduleName) { } else if (thisModuleName === moduleName) {
$(`li[data-key=${thisModuleName}] > a`).addClass('selected'); $(`li[data-key=${thisModuleName}] > a`).addClass('selected');
module.addClass('popup-show'); module.addClass('popup-show');
if (cb) { if (cb) {
@ -254,35 +254,42 @@ var padeditbar = (function () {
} }
} }
}, },
setSyncStatus(status) { setSyncStatus: (status) => {
if (status == 'syncing') { if (status === 'syncing') {
syncAnimation.syncing(); syncAnimation.syncing();
} else if (status == 'done') { } else if (status === 'done') {
syncAnimation.done(); syncAnimation.done();
} }
}, },
setEmbedLinks() { setEmbedLinks: () => {
const padUrl = window.location.href.split('?')[0]; const padUrl = window.location.href.split('?')[0];
const params = '?showControls=true&showChat=true&showLineNumbers=true&useMonospaceFont=false';
const props = 'width="100%" height="600" frameborder="0"';
if ($('#readonlyinput').is(':checked')) { if ($('#readonlyinput').is(':checked')) {
const urlParts = padUrl.split('/'); const urlParts = padUrl.split('/');
urlParts.pop(); urlParts.pop();
const readonlyLink = `${urlParts.join('/')}/${clientVars.readOnlyId}`; const readonlyLink = `${urlParts.join('/')}/${clientVars.readOnlyId}`;
$('#embedinput').val(`<iframe name="embed_readonly" src="${readonlyLink}?showControls=true&showChat=true&showLineNumbers=true&useMonospaceFont=false" width="100%" height="600" frameborder="0"></iframe>`); $('#embedinput')
.val(`<iframe name="embed_readonly" src="${readonlyLink}${params}" ${props}></iframe>`);
$('#linkinput').val(readonlyLink); $('#linkinput').val(readonlyLink);
} else { } else {
$('#embedinput').val(`<iframe name="embed_readwrite" src="${padUrl}?showControls=true&showChat=true&showLineNumbers=true&useMonospaceFont=false" width="100%" height="600" frameborder="0"></iframe>`); $('#embedinput')
.val(`<iframe name="embed_readwrite" src="${padUrl}${params}" ${props}></iframe>`);
$('#linkinput').val(padUrl); $('#linkinput').val(padUrl);
} }
}, },
checkAllIconsAreDisplayedInToolbar() { checkAllIconsAreDisplayedInToolbar: () => {
// reset style // reset style
$('.toolbar').removeClass('cropped'); $('.toolbar').removeClass('cropped');
$('body').removeClass('mobile-layout'); $('body').removeClass('mobile-layout');
const menu_left = $('.toolbar .menu_left')[0]; const menu_left = $('.toolbar .menu_left')[0];
const menuRightWidth = 280; // this is approximate, we cannot measure it because on mobileLayour it takes the full width on the bottom of the page // this is approximate, we cannot measure it because on mobile
if (menu_left && menu_left.scrollWidth > $('.toolbar').width() - menuRightWidth || $('.toolbar').width() < 1000) { // Layout it takes the full width on the bottom of the page
const menuRightWidth = 280;
if (menu_left && menu_left.scrollWidth > $('.toolbar').width() - menuRightWidth ||
$('.toolbar').width() < 1000) {
$('body').addClass('mobile-layout'); $('body').addClass('mobile-layout');
} }
if (menu_left && menu_left.scrollWidth > $('.toolbar').width()) { if (menu_left && menu_left.scrollWidth > $('.toolbar').width()) {
@ -293,7 +300,7 @@ var padeditbar = (function () {
let editbarPosition = 0; let editbarPosition = 0;
function bodyKeyEvent(evt) { const bodyKeyEvent = (evt) => {
// If the event is Alt F9 or Escape & we're already in the editbar menu // If the event is Alt F9 or Escape & we're already in the editbar menu
// Send the users focus back to the pad // Send the users focus back to the pad
if ((evt.keyCode === 120 && evt.altKey) || evt.keyCode === 27) { if ((evt.keyCode === 120 && evt.altKey) || evt.keyCode === 27) {
@ -317,7 +324,8 @@ var padeditbar = (function () {
} }
} else { } else {
// Focus on the editbar :) // Focus on the editbar :)
const firstEditbarElement = parent.parent.$('#editbar').children('ul').first().children().first().children().first().children().first(); const firstEditbarElement = parent.parent.$('#editbar button').first();
$(this).blur(); $(this).blur();
firstEditbarElement.focus(); firstEditbarElement.focus();
evt.preventDefault(); evt.preventDefault();
@ -353,13 +361,13 @@ var padeditbar = (function () {
$(focusItems[editbarPosition]).focus(); $(focusItems[editbarPosition]).focus();
} }
} }
} };
function aceAttributeCommand(cmd, ace) { const aceAttributeCommand = (cmd, ace) => {
ace.ace_toggleAttributeOnSelection(cmd); ace.ace_toggleAttributeOnSelection(cmd);
} };
function registerDefaultCommands(toolbar) { const registerDefaultCommands = (toolbar) => {
toolbar.registerDropdownCommand('showusers', 'users'); toolbar.registerDropdownCommand('showusers', 'users');
toolbar.registerDropdownCommand('settings'); toolbar.registerDropdownCommand('settings');
toolbar.registerDropdownCommand('connectivity'); toolbar.registerDropdownCommand('connectivity');
@ -440,12 +448,13 @@ var padeditbar = (function () {
toolbar.registerAceCommand('clearauthorship', (cmd, ace) => { toolbar.registerAceCommand('clearauthorship', (cmd, ace) => {
// If we have the whole document selected IE control A has been hit // If we have the whole document selected IE control A has been hit
const rep = ace.ace_getRep(); const rep = ace.ace_getRep();
let doPrompt = false;
const lastChar = rep.lines.atIndex(rep.lines.length() - 1).width - 1; const lastChar = rep.lines.atIndex(rep.lines.length() - 1).width - 1;
const lastLineIndex = rep.lines.length() - 1; const lastLineIndex = rep.lines.length() - 1;
if (rep.selStart[0] === 0 && rep.selStart[1] === 0) { if (rep.selStart[0] === 0 && rep.selStart[1] === 0) {
// nesting intentionally here to make things readable // nesting intentionally here to make things readable
if (rep.selEnd[0] === lastLineIndex && rep.selEnd[1] === lastChar) { if (rep.selEnd[0] === lastLineIndex && rep.selEnd[1] === lastChar) {
var doPrompt = true; doPrompt = true;
} }
} }
/* /*
@ -470,13 +479,16 @@ var padeditbar = (function () {
}); });
toolbar.registerCommand('timeslider_returnToPad', (cmd) => { toolbar.registerCommand('timeslider_returnToPad', (cmd) => {
if (document.referrer.length > 0 && document.referrer.substring(document.referrer.lastIndexOf('/') - 1, document.referrer.lastIndexOf('/')) === 'p') { if (document.referrer.length > 0 &&
document.referrer.substring(document.referrer.lastIndexOf('/') - 1,
document.referrer.lastIndexOf('/')) === 'p') {
document.location = document.referrer; document.location = document.referrer;
} else { } else {
document.location = document.location.href.substring(0, document.location.href.lastIndexOf('/')); document.location = document.location.href
.substring(0, document.location.href.lastIndexOf('/'));
} }
}); });
} };
return self; return self;
}()); }());