From 0362d3b05db2e22e1e1759fe1c00693f013b2131 Mon Sep 17 00:00:00 2001 From: John McLear Date: Sun, 20 Dec 2020 07:15:58 +0000 Subject: [PATCH] lint: pad prefix files (#4577) * lint: pad_connectionstatus * lint: pad_utils * lint: pad_userlist.js -- still WIP * shift underscore not to be in require but to be used from window * lint: pad_modals * pad_impexp.js * lint: more errors done * lint: auto reconn * lint: pad_editor * lint: finish auto reconn * lint: imp exp rework * lint: import * lint: pad.js nearly done but pizza here... * lint: clientVars global query * put clientVars in window * Revert incorrect lint fixes * Properly fix guard-for-in lint errors * Properly fix no-unused-vars error regarding `gritter` * Refine lint fixes Co-authored-by: Richard Hansen --- src/static/js/pad.js | 480 ++++++++++++----------- src/static/js/pad_automatic_reconnect.js | 50 +-- src/static/js/pad_connectionstatus.js | 24 +- src/static/js/pad_cookie.js | 2 + src/static/js/pad_editbar.js | 1 + src/static/js/pad_editor.js | 47 +-- src/static/js/pad_impexp.js | 84 ++-- src/static/js/pad_modals.js | 14 +- src/static/js/pad_savedrevs.js | 11 +- src/static/js/pad_userlist.js | 254 ++++++------ src/static/js/pad_utils.js | 4 +- 11 files changed, 512 insertions(+), 459 deletions(-) diff --git a/src/static/js/pad.js b/src/static/js/pad.js index 424c45241..c19bd58ea 100644 --- a/src/static/js/pad.js +++ b/src/static/js/pad.js @@ -1,3 +1,5 @@ +'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. @@ -20,8 +22,6 @@ * limitations under the License. */ -/* global $, window */ - let socket; // These jQuery things should create local references, but for now `require()` @@ -43,24 +43,13 @@ const padsavedrevs = require('./pad_savedrevs'); const paduserlist = require('./pad_userlist').paduserlist; const padutils = require('./pad_utils').padutils; const colorutils = require('./colorutils').colorutils; -var randomString = require('./pad_utils').randomString; -const gritter = require('./gritter').gritter; +const randomString = require('./pad_utils').randomString; +require('./gritter'); // Mutates the jQuery object to make $.gritter available. const hooks = require('./pluginfw/hooks'); let receivedClientVars = false; -function randomString() { - const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; - const string_length = 20; - let randomstring = ''; - for (let i = 0; i < string_length; i++) { - const rnum = Math.floor(Math.random() * chars.length); - randomstring += chars.substring(rnum, rnum + 1); - } - return `t.${randomstring}`; -} - // This array represents all GET-parameters which can be used to change a setting. // name: the parameter-name, eg `?noColors=true` => `noColors` // checkVal: the callback is only executed when @@ -68,26 +57,101 @@ function randomString() { // * the parameter was supplied and checkVal is null // callback: the function to call when all above succeeds, `val` is the value supplied by the user const getParameters = [ - {name: 'noColors', checkVal: 'true', callback(val) { settings.noColors = true; $('#clearAuthorship').hide(); }}, - {name: 'showControls', checkVal: 'true', callback(val) { $('#editbar').css('display', 'flex'); }}, - {name: 'showChat', checkVal: null, callback(val) { if (val === 'false') { settings.hideChat = true; chat.hide(); $('#chaticon').hide(); } }}, - {name: 'showLineNumbers', checkVal: 'false', callback(val) { settings.LineNumbersDisabled = true; }}, - {name: 'useMonospaceFont', checkVal: 'true', callback(val) { settings.useMonospaceFontGlobal = true; }}, - // If the username is set as a parameter we should set a global value that we can call once we have initiated the pad. - {name: 'userName', checkVal: null, callback(val) { settings.globalUserName = decodeURIComponent(val); clientVars.userName = decodeURIComponent(val); }}, - // If the userColor is set as a parameter, set a global value to use once we have initiated the pad. - {name: 'userColor', checkVal: null, callback(val) { settings.globalUserColor = decodeURIComponent(val); clientVars.userColor = decodeURIComponent(val); }}, - {name: 'rtl', checkVal: 'true', callback(val) { settings.rtlIsTrue = true; }}, - {name: 'alwaysShowChat', checkVal: 'true', callback(val) { if (!settings.hideChat) chat.stickToScreen(); }}, - {name: 'chatAndUsers', checkVal: 'true', callback(val) { chat.chatAndUsers(); }}, - {name: 'lang', checkVal: null, callback(val) { window.html10n.localize([val, 'en']); Cookies.set('language', val); }}, + { + name: 'noColors', + checkVal: 'true', + callback: (val) => { + settings.noColors = true; + $('#clearAuthorship').hide(); + }, + }, + { + name: 'showControls', + checkVal: 'true', + callback: (val) => { + $('#editbar').css('display', 'flex'); + }, + }, + { + name: 'showChat', + checkVal: null, + callback: (val) => { + if (val === 'false') { + settings.hideChat = true; + chat.hide(); + $('#chaticon').hide(); + } + }, + }, + { + name: 'showLineNumbers', + checkVal: 'false', + callback: (val) => { + settings.LineNumbersDisabled = true; + }, + }, + { + name: 'useMonospaceFont', + checkVal: 'true', + callback: (val) => { + settings.useMonospaceFontGlobal = true; + }, + }, + // If the username is set as a parameter we should set a global value that we can call once we + // have initiated the pad. + { + name: 'userName', + checkVal: null, + callback: (val) => { + settings.globalUserName = decodeURIComponent(val); + clientVars.userName = decodeURIComponent(val); + }, + }, + // If the userColor is set as a parameter, set a global value to use once we have initiated the + // pad. + { + name: 'userColor', + checkVal: null, + callback: (val) => { + settings.globalUserColor = decodeURIComponent(val); + clientVars.userColor = decodeURIComponent(val); + }, + }, + { + name: 'rtl', + checkVal: 'true', + callback: (val) => { + settings.rtlIsTrue = true; + }, + }, + { + name: 'alwaysShowChat', + checkVal: 'true', + callback: (val) => { + if (!settings.hideChat) chat.stickToScreen(); + }, + }, + { + name: 'chatAndUsers', + checkVal: 'true', + callback: (val) => { + chat.chatAndUsers(); + }, + }, + { + name: 'lang', + checkVal: null, + callback: (val) => { + window.html10n.localize([val, 'en']); + Cookies.set('language', val); + }, + }, ]; -function getParams() { +const getParams = () => { // Tries server enforced options first.. - for (var i = 0; i < getParameters.length; i++) { - var setting = getParameters[i]; - var value = clientVars.padOptions[setting.name]; + for (const setting of getParameters) { + const value = clientVars.padOptions[setting.name]; if (value.toString() === setting.checkVal) { setting.callback(value); } @@ -96,19 +160,18 @@ function getParams() { // Then URL applied stuff const params = getUrlVars(); - for (var i = 0; i < getParameters.length; i++) { - var setting = getParameters[i]; - var value = params[setting.name]; + for (const setting of getParameters) { + const value = params[setting.name]; - if (value && (value == setting.checkVal || setting.checkVal == null)) { + if (value && (value === setting.checkVal || setting.checkVal == null)) { setting.callback(value); } } -} +}; -function getUrlVars() { - const vars = []; let - hash; +const getUrlVars = () => { + const vars = []; + let hash; const hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&'); for (let i = 0; i < hashes.length; i++) { hash = hashes[i].split('='); @@ -116,12 +179,13 @@ function getUrlVars() { vars[hash[0]] = hash[1]; } return vars; -} +}; -function sendClientReady(isReconnect, messageType) { +const sendClientReady = (isReconnect, messageType) => { messageType = typeof messageType !== 'undefined' ? messageType : 'CLIENT_READY'; let padId = document.location.pathname.substring(document.location.pathname.lastIndexOf('/') + 1); - padId = decodeURIComponent(padId); // unescape neccesary due to Safari and Opera interpretation of spaces + // unescape neccesary due to Safari and Opera interpretation of spaces + padId = decodeURIComponent(padId); if (!isReconnect) { const titleArray = document.title.split('|'); @@ -151,12 +215,12 @@ function sendClientReady(isReconnect, messageType) { } socket.json.send(msg); -} +}; -function handshake() { +const handshake = () => { const loc = document.location; // get the correct port - const port = loc.port == '' ? (loc.protocol == 'https:' ? 443 : 80) : loc.port; + const port = loc.port === '' ? (loc.protocol === 'https:' ? 443 : 80) : loc.port; // create the url const url = `${loc.protocol}//${loc.hostname}:${port}/`; // find out in which subfolder we are @@ -211,12 +275,10 @@ function handshake() { throw new Error(`socket.io connection error: ${JSON.stringify(error)}`); }); - let initalized = false; - socket.on('message', (obj) => { // the access was not granted, give the user a message if (obj.accessStatus) { - if (obj.accessStatus == 'deny') { + if (obj.accessStatus === 'deny') { $('#loading').hide(); $('#permissionDenied').show(); @@ -226,18 +288,15 @@ function handshake() { $('#editorloadingbox').show(); } } - } - - // if we haven't recieved the clientVars yet, then this message should it be - else if (!receivedClientVars && obj.type == 'CLIENT_VARS') { + } else if (!receivedClientVars && obj.type === 'CLIENT_VARS') { + // if we haven't recieved the clientVars yet, then this message should it be receivedClientVars = true; // set some client vars - clientVars = obj.data; + window.clientVars = obj.data; // initalize the pad pad._afterHandshake(); - initalized = true; if (clientVars.readonly) { chat.hide(); @@ -255,63 +314,61 @@ function handshake() { }); // If the LineNumbersDisabled value is set to true then we need to hide the Line Numbers - if (settings.LineNumbersDisabled == true) { + if (settings.LineNumbersDisabled === true) { pad.changeViewOption('showLineNumbers', false); } - // If the noColors value is set to true then we need to hide the background colors on the ace spans - if (settings.noColors == true) { + // If the noColors value is set to true then we need to + // hide the background colors on the ace spans + if (settings.noColors === true) { pad.changeViewOption('noColors', true); } - if (settings.rtlIsTrue == true) { + if (settings.rtlIsTrue === true) { pad.changeViewOption('rtlIsTrue', true); } // If the Monospacefont value is set to true then change it to monospace. - if (settings.useMonospaceFontGlobal == true) { + if (settings.useMonospaceFontGlobal === true) { pad.changeViewOption('padFontFamily', 'monospace'); } - // if the globalUserName value is set we need to tell the server and the client about the new authorname + // if the globalUserName value is set we need to tell the server and + // the client about the new authorname if (settings.globalUserName !== false) { pad.notifyChangeName(settings.globalUserName); // Notifies the server pad.myUserInfo.name = settings.globalUserName; $('#myusernameedit').val(settings.globalUserName); // Updates the current users UI } if (settings.globalUserColor !== false && colorutils.isCssHex(settings.globalUserColor)) { - // Add a 'globalUserColor' property to myUserInfo, so collabClient knows we have a query parameter. + // Add a 'globalUserColor' property to myUserInfo, + // so collabClient knows we have a query parameter. pad.myUserInfo.globalUserColor = settings.globalUserColor; pad.notifyChangeColor(settings.globalUserColor); // Updates pad.myUserInfo.colorId paduserlist.setMyUserInfo(pad.myUserInfo); } - } - // This handles every Message after the clientVars - else { - // this message advices the client to disconnect - if (obj.disconnect) { - padconnectionstatus.disconnected(obj.disconnect); - socket.disconnect(); + } else if (obj.disconnect) { + padconnectionstatus.disconnected(obj.disconnect); + socket.disconnect(); - // block user from making any change to the pad - padeditor.disable(); - padeditbar.disable(); - padimpexp.disable(); + // block user from making any change to the pad + padeditor.disable(); + padeditbar.disable(); + padimpexp.disable(); - return; - } else { - pad.collabClient.handleMessageFromServer(obj); - } + return; + } else { + pad.collabClient.handleMessageFromServer(obj); } }); // Bind the colorpicker - const fb = $('#colorpicker').farbtastic({callback: '#mycolorpickerpreview', width: 220}); + $('#colorpicker').farbtastic({callback: '#mycolorpickerpreview', width: 220}); // Bind the read only button $('#readonlyinput').on('click', () => { padeditbar.setEmbedLinks(); }); -} +}; -var pad = { +const pad = { // don't access these directly from outside this file, except // for debugging collabClient: null, @@ -322,44 +379,28 @@ var pad = { padOptions: {}, // these don't require init; clientVars should all go through here - getPadId() { - return clientVars.padId; - }, - getClientIp() { - return clientVars.clientIp; - }, - getColorPalette() { - return clientVars.colorPalette; - }, - getIsDebugEnabled() { - return clientVars.debugEnabled; - }, - getPrivilege(name) { - return clientVars.accountPrivs[name]; - }, - getUserId() { - return pad.myUserInfo.userId; - }, - getUserName() { - return pad.myUserInfo.name; - }, - userList() { - return paduserlist.users(); - }, - switchToPad(padId) { - let newHref = new RegExp(/.*\/p\/[^\/]+/).exec(document.location.pathname) || clientVars.padId; + getPadId: () => clientVars.padId, + getClientIp: () => clientVars.clientIp, + getColorPalette: () => clientVars.colorPalette, + getIsDebugEnabled: () => clientVars.debugEnabled, + getPrivilege: (name) => clientVars.accountPrivs[name], + getUserId: () => pad.myUserInfo.userId, + getUserName: () => pad.myUserInfo.name, + userList: () => paduserlist.users(), + switchToPad: (padId) => { + let newHref = new RegExp(/.*\/p\/[^/]+/).exec(document.location.pathname) || clientVars.padId; newHref = newHref[0]; const options = clientVars.padOptions; if (typeof options !== 'undefined' && options != null) { - var option_str = []; + const optionArr = []; $.each(options, (k, v) => { const str = `${k}=${v}`; - option_str.push(str); + optionArr.push(str); }); - var option_str = option_str.join('&'); + const optionStr = optionArr.join('&'); - newHref = `${newHref}?${option_str}`; + newHref = `${newHref}?${optionStr}`; } // destroy old pad from DOM @@ -373,21 +414,21 @@ var pad = { window.history.pushState('', '', newHref); receivedClientVars = false; sendClientReady(false, 'SWITCH_TO_PAD'); - } else // fallback - { + } else { + // fallback window.location.href = newHref; } }, - sendClientMessage(msg) { + sendClientMessage: (msg) => { pad.collabClient.sendClientMessage(msg); }, - init() { + init: () => { padutils.setupGlobalExceptionHandler(); $(document).ready(() => { // start the custom js - if (typeof customStart === 'function') customStart(); + if (typeof customStart === 'function') customStart(); // eslint-disable-line no-undef handshake(); // To use etherpad you have to allow cookies. @@ -406,7 +447,6 @@ var pad = { pad.initTime = +(new Date()); pad.padOptions = clientVars.initialOptions; - // order of inits is important here: pad.myUserInfo = { userId: clientVars.userId, name: clientVars.userName, @@ -414,18 +454,60 @@ var pad = { colorId: clientVars.userColor, }; + const postAceInit = () => { + padeditbar.init(); + setTimeout(() => { + padeditor.ace.focus(); + }, 0); + // if we have a cookie for always showing chat then show it + if (padcookie.getPref('chatAlwaysVisible')) { + chat.stickToScreen(true); // stick it to the screen + $('#options-stickychat').prop('checked', true); // set the checkbox to on + } + // if we have a cookie for always showing chat then show it + if (padcookie.getPref('chatAndUsers')) { + chat.chatAndUsers(true); // stick it to the screen + $('#options-chatandusers').prop('checked', true); // set the checkbox to on + } + if (padcookie.getPref('showAuthorshipColors') === false) { + pad.changeViewOption('showAuthorColors', false); + } + if (padcookie.getPref('showLineNumbers') === false) { + pad.changeViewOption('showLineNumbers', false); + } + if (padcookie.getPref('rtlIsTrue') === true) { + pad.changeViewOption('rtlIsTrue', true); + } + pad.changeViewOption('padFontFamily', padcookie.getPref('padFontFamily')); + $('#viewfontmenu').val(padcookie.getPref('padFontFamily')).niceSelect('update'); + + // Prevent sticky chat or chat and users to be checked for mobiles + const checkChatAndUsersVisibility = (x) => { + if (x.matches) { // If media query matches + $('#options-chatandusers:checked').click(); + $('#options-stickychat:checked').click(); + } + }; + const mobileMatch = window.matchMedia('(max-width: 800px)'); + mobileMatch.addListener(checkChatAndUsersVisibility); // check if window resized + setTimeout(() => { checkChatAndUsersVisibility(mobileMatch); }, 0); // check now after load + + $('#editorcontainer').addClass('initialized'); + + hooks.aCallAll('postAceInit', {ace: padeditor.ace, pad}); + }; + + // order of inits is important here: padimpexp.init(this); padsavedrevs.init(this); - padeditor.init(postAceInit, pad.padOptions.view || {}, this); - paduserlist.init(pad.myUserInfo, this); padconnectionstatus.init(); padmodals.init(this); - pad.collabClient = getCollabClient(padeditor.ace, clientVars.collab_client_vars, pad.myUserInfo, { - colorPalette: pad.getColorPalette(), - }, pad); + pad.collabClient = getCollabClient( + padeditor.ace, clientVars.collab_client_vars, pad.myUserInfo, + {colorPalette: pad.getColorPalette()}, pad); pad.collabClient.setOnUserJoin(pad.handleUserJoin); pad.collabClient.setOnUpdateUserInfo(pad.handleUserUpdate); pad.collabClient.setOnUserLeave(pad.handleUserLeave); @@ -434,68 +516,27 @@ var pad = { pad.collabClient.setOnInternalAction(pad.handleCollabAction); // load initial chat-messages - if (clientVars.chatHead != -1) { + if (clientVars.chatHead !== -1) { const chatHead = clientVars.chatHead; const start = Math.max(chatHead - 100, 0); pad.collabClient.sendMessage({type: 'GET_CHAT_MESSAGES', start, end: chatHead}); - } else // there are no messages - { + } else { + // there are no messages $('#chatloadmessagesbutton').css('display', 'none'); } - - function postAceInit() { - padeditbar.init(); - setTimeout(() => { - padeditor.ace.focus(); - }, 0); - if (padcookie.getPref('chatAlwaysVisible')) { // if we have a cookie for always showing chat then show it - chat.stickToScreen(true); // stick it to the screen - $('#options-stickychat').prop('checked', true); // set the checkbox to on - } - if (padcookie.getPref('chatAndUsers')) { // if we have a cookie for always showing chat then show it - chat.chatAndUsers(true); // stick it to the screen - $('#options-chatandusers').prop('checked', true); // set the checkbox to on - } - if (padcookie.getPref('showAuthorshipColors') == false) { - pad.changeViewOption('showAuthorColors', false); - } - if (padcookie.getPref('showLineNumbers') == false) { - pad.changeViewOption('showLineNumbers', false); - } - if (padcookie.getPref('rtlIsTrue') == true) { - pad.changeViewOption('rtlIsTrue', true); - } - pad.changeViewOption('padFontFamily', padcookie.getPref('padFontFamily')); - $('#viewfontmenu').val(padcookie.getPref('padFontFamily')).niceSelect('update'); - - // Prevent sticky chat or chat and users to be checked for mobiles - function checkChatAndUsersVisibility(x) { - if (x.matches) { // If media query matches - $('#options-chatandusers:checked').click(); - $('#options-stickychat:checked').click(); - } - } - const mobileMatch = window.matchMedia('(max-width: 800px)'); - mobileMatch.addListener(checkChatAndUsersVisibility); // check if window resized - setTimeout(() => { checkChatAndUsersVisibility(mobileMatch); }, 0); // check now after load - - $('#editorcontainer').addClass('initialized'); - - hooks.aCallAll('postAceInit', {ace: padeditor.ace, pad}); - } }, - dispose() { + dispose: () => { padeditor.dispose(); }, - notifyChangeName(newName) { + notifyChangeName: (newName) => { pad.myUserInfo.name = newName; pad.collabClient.updateUserInfo(pad.myUserInfo); }, - notifyChangeColor(newColorId) { + notifyChangeColor: (newColorId) => { pad.myUserInfo.colorId = newColorId; pad.collabClient.updateUserInfo(pad.myUserInfo); }, - changePadOption(key, value) { + changePadOption: (key, value) => { const options = {}; options[key] = value; pad.handleOptionsChange(options); @@ -506,32 +547,30 @@ var pad = { changedBy: pad.myUserInfo.name || 'unnamed', }); }, - changeViewOption(key, value) { + changeViewOption: (key, value) => { const options = { view: {}, }; options.view[key] = value; pad.handleOptionsChange(options); }, - handleOptionsChange(opts) { + handleOptionsChange: (opts) => { // opts object is a full set of options or just // some options to change if (opts.view) { if (!pad.padOptions.view) { pad.padOptions.view = {}; } - for (const k in opts.view) { - pad.padOptions.view[k] = opts.view[k]; - padcookie.setPref(k, opts.view[k]); + for (const [k, v] of Object.entries(opts.view)) { + pad.padOptions.view[k] = v; + padcookie.setPref(k, v); } padeditor.setViewOptions(pad.padOptions.view); } }, - getPadOptions() { - // caller shouldn't mutate the object - return pad.padOptions; - }, - suggestUserName(userId, name) { + // caller shouldn't mutate the object + getPadOptions: () => pad.padOptions, + suggestUserName: (userId, name) => { pad.collabClient.sendClientMessage( { type: 'suggestUserName', @@ -539,31 +578,31 @@ var pad = { newName: name, }); }, - handleUserJoin(userInfo) { + handleUserJoin: (userInfo) => { paduserlist.userJoinOrUpdate(userInfo); }, - handleUserUpdate(userInfo) { + handleUserUpdate: (userInfo) => { paduserlist.userJoinOrUpdate(userInfo); }, - handleUserLeave(userInfo) { + handleUserLeave: (userInfo) => { paduserlist.userLeave(userInfo); }, - handleClientMessage(msg) { - if (msg.type == 'suggestUserName') { - if (msg.unnamedId == pad.myUserInfo.userId && msg.newName && !pad.myUserInfo.name) { + handleClientMessage: (msg) => { + if (msg.type === 'suggestUserName') { + if (msg.unnamedId === pad.myUserInfo.userId && msg.newName && !pad.myUserInfo.name) { pad.notifyChangeName(msg.newName); paduserlist.setMyUserInfo(pad.myUserInfo); } - } else if (msg.type == 'newRevisionList') { + } else if (msg.type === 'newRevisionList') { padsavedrevs.newRevisionList(msg.revisionList); - } else if (msg.type == 'revisionLabel') { + } else if (msg.type === 'revisionLabel') { padsavedrevs.newRevisionList(msg.revisionList); - } else if (msg.type == 'padoptions') { + } else if (msg.type === 'padoptions') { const opts = msg.options; pad.handleOptionsChange(opts); } }, - dmesg(m) { + dmesg: (m) => { if (pad.getIsDebugEnabled()) { const djs = $('#djs').get(0); const wasAtBottom = (djs.scrollTop - (djs.scrollHeight - $(djs).height()) >= -20); @@ -573,31 +612,30 @@ var pad = { } } }, - handleChannelStateChange(newState, message) { + handleChannelStateChange: (newState, message) => { const oldFullyConnected = !!padconnectionstatus.isFullyConnected(); - const wasConnecting = (padconnectionstatus.getStatus().what == 'connecting'); - if (newState == 'CONNECTED') { + const wasConnecting = (padconnectionstatus.getStatus().what === 'connecting'); + if (newState === 'CONNECTED') { padeditor.enable(); padeditbar.enable(); padimpexp.enable(); padconnectionstatus.connected(); - } else if (newState == 'RECONNECTING') { + } else if (newState === 'RECONNECTING') { padeditor.disable(); padeditbar.disable(); padimpexp.disable(); padconnectionstatus.reconnecting(); - } else if (newState == 'DISCONNECTED') { + } else if (newState === 'DISCONNECTED') { pad.diagnosticInfo.disconnectedMessage = message; pad.diagnosticInfo.padId = pad.getPadId(); pad.diagnosticInfo.socket = {}; // we filter non objects from the socket object and put them in the diagnosticInfo // this ensures we have no cyclic data - this allows us to stringify the data - for (const i in socket.socket) { - const value = socket.socket[i]; + for (const [i, value] of Object.entries(socket.socket || {})) { const type = typeof value; - if (type == 'string' || type == 'number') { + if (type === 'string' || type === 'number') { pad.diagnosticInfo.socket[i] = value; } } @@ -613,11 +651,11 @@ var pad = { padconnectionstatus.disconnected(message); } const newFullyConnected = !!padconnectionstatus.isFullyConnected(); - if (newFullyConnected != oldFullyConnected) { + if (newFullyConnected !== oldFullyConnected) { pad.handleIsFullyConnected(newFullyConnected, wasConnecting); } }, - handleIsFullyConnected(isConnected, isInitialConnect) { + handleIsFullyConnected: (isConnected, isInitialConnect) => { pad.determineChatVisibility(isConnected && !isInitialConnect); pad.determineChatAndUsersVisibility(isConnected && !isInitialConnect); pad.determineAuthorshipColorsVisibility(); @@ -625,7 +663,7 @@ var pad = { padeditbar.toggleDropDown('none'); }, 1000); }, - determineChatVisibility(asNowConnectedFeedback) { + determineChatVisibility: (asNowConnectedFeedback) => { const chatVisCookie = padcookie.getPref('chatAlwaysVisible'); if (chatVisCookie) { // if the cookie is set for chat always visible chat.stickToScreen(true); // stick it to the screen @@ -634,7 +672,7 @@ var pad = { $('#options-stickychat').prop('checked', false); // set the checkbox for off } }, - determineChatAndUsersVisibility(asNowConnectedFeedback) { + determineChatAndUsersVisibility: (asNowConnectedFeedback) => { const chatAUVisCookie = padcookie.getPref('chatAndUsersVisible'); if (chatAUVisCookie) { // if the cookie is set for chat always visible chat.chatAndUsers(true); // stick it to the screen @@ -643,7 +681,7 @@ var pad = { $('#options-chatandusers').prop('checked', false); // set the checkbox for off } }, - determineAuthorshipColorsVisibility() { + determineAuthorshipColorsVisibility: () => { const authColCookie = padcookie.getPref('showAuthorshipColors'); if (authColCookie) { pad.changeViewOption('showAuthorColors', true); @@ -652,14 +690,14 @@ var pad = { $('#options-colorscheck').prop('checked', false); } }, - handleCollabAction(action) { - if (action == 'commitPerformed') { + handleCollabAction: (action) => { + if (action === 'commitPerformed') { padeditbar.setSyncStatus('syncing'); - } else if (action == 'newlyIdle') { + } else if (action === 'newlyIdle') { padeditbar.setSyncStatus('done'); } }, - asyncSendDiagnosticInfo() { + asyncSendDiagnosticInfo: () => { window.setTimeout(() => { $.ajax( { @@ -668,32 +706,29 @@ var pad = { data: { diagnosticInfo: JSON.stringify(pad.diagnosticInfo), }, - success() {}, - error() {}, + success: () => {}, + error: () => {}, }); }, 0); }, - forceReconnect() { + forceReconnect: () => { $('form#reconnectform input.padId').val(pad.getPadId()); pad.diagnosticInfo.collabDiagnosticInfo = pad.collabClient.getDiagnosticInfo(); $('form#reconnectform input.diagnosticInfo').val(JSON.stringify(pad.diagnosticInfo)); - $('form#reconnectform input.missedChanges').val(JSON.stringify(pad.collabClient.getMissedChanges())); + $('form#reconnectform input.missedChanges') + .val(JSON.stringify(pad.collabClient.getMissedChanges())); $('form#reconnectform').submit(); }, // this is called from code put into a frame from the server: - handleImportExportFrameCall(callName, varargs) { + handleImportExportFrameCall: (callName, varargs) => { padimpexp.handleFrameCall.call(padimpexp, callName, Array.prototype.slice.call(arguments, 1)); }, - callWhenNotCommitting(f) { + callWhenNotCommitting: (f) => { pad.collabClient.callWhenNotCommitting(f); }, - getCollabRevisionNumber() { - return pad.collabClient.getCurrentRevisionNumber(); - }, - isFullyConnected() { - return padconnectionstatus.isFullyConnected(); - }, - addHistoricalAuthors(data) { + getCollabRevisionNumber: () => pad.collabClient.getCurrentRevisionNumber(), + isFullyConnected: () => padconnectionstatus.isFullyConnected(), + addHistoricalAuthors: (data) => { if (!pad.collabClient) { window.setTimeout(() => { pad.addHistoricalAuthors(data); @@ -704,11 +739,9 @@ var pad = { }, }; -function init() { - return pad.init(); -} +const init = () => pad.init(); -var settings = { +const settings = { LineNumbersDisabled: false, noColors: false, useMonospaceFontGlobal: false, @@ -718,6 +751,7 @@ var settings = { }; pad.settings = settings; + exports.baseURL = ''; exports.settings = settings; exports.randomString = randomString; diff --git a/src/static/js/pad_automatic_reconnect.js b/src/static/js/pad_automatic_reconnect.js index 8edd09b38..db803e896 100644 --- a/src/static/js/pad_automatic_reconnect.js +++ b/src/static/js/pad_automatic_reconnect.js @@ -1,4 +1,6 @@ -exports.showCountDownTimerToReconnectOnModal = function ($modal, pad) { +'use strict'; + +exports.showCountDownTimerToReconnectOnModal = ($modal, pad) => { if (clientVars.automaticReconnectionTimeout && $modal.is('.with_reconnect_timer')) { createCountDownElementsIfNecessary($modal); @@ -13,7 +15,7 @@ exports.showCountDownTimerToReconnectOnModal = function ($modal, pad) { } }; -var createCountDownElementsIfNecessary = function ($modal) { +const createCountDownElementsIfNecessary = ($modal) => { const elementsDoNotExist = $modal.find('#cancelreconnect').length === 0; if (elementsDoNotExist) { const $defaultMessage = $modal.find('#defaulttext'); @@ -45,12 +47,13 @@ var createCountDownElementsIfNecessary = function ($modal) { } }; -var localize = function ($element) { +const localize = ($element) => { html10n.translateElement(html10n.translations, $element.get(0)); }; -var createTimerForModal = function ($modal, pad) { - const timeUntilReconnection = clientVars.automaticReconnectionTimeout * reconnectionTries.nextTry(); +const createTimerForModal = ($modal, pad) => { + const timeUntilReconnection = + clientVars.automaticReconnectionTimeout * reconnectionTries.nextTry(); const timer = new CountDownTimer(timeUntilReconnection); timer.onTick((minutes, seconds) => { @@ -68,23 +71,23 @@ var createTimerForModal = function ($modal, pad) { return timer; }; -var disableAutomaticReconnection = function ($modal) { +const disableAutomaticReconnection = ($modal) => { toggleAutomaticReconnectionOption($modal, true); }; -var enableAutomaticReconnection = function ($modal) { +const enableAutomaticReconnection = ($modal) => { toggleAutomaticReconnectionOption($modal, false); }; -var toggleAutomaticReconnectionOption = function ($modal, disableAutomaticReconnect) { +const toggleAutomaticReconnectionOption = ($modal, disableAutomaticReconnect) => { $modal.find('#cancelreconnect, .reconnecttimer').toggleClass('hidden', disableAutomaticReconnect); $modal.find('#defaulttext').toggleClass('hidden', !disableAutomaticReconnect); }; -var waitUntilClientCanConnectToServerAndThen = function (callback, pad) { +const waitUntilClientCanConnectToServerAndThen = (callback, pad) => { whenConnectionIsRestablishedWithServer(callback, pad); pad.socket.connect(); }; -var whenConnectionIsRestablishedWithServer = function (callback, pad) { +const whenConnectionIsRestablishedWithServer = (callback, pad) => { // only add listener for the first try, don't need to add another listener // on every unsuccessful try if (reconnectionTries.counter === 1) { @@ -92,11 +95,11 @@ var whenConnectionIsRestablishedWithServer = function (callback, pad) { } }; -var forceReconnection = function ($modal) { +const forceReconnection = ($modal) => { $modal.find('#forcereconnect').click(); }; -var updateCountDownTimerMessage = function ($modal, minutes, seconds) { +const updateCountDownTimerMessage = ($modal, minutes, seconds) => { minutes = minutes < 10 ? `0${minutes}` : minutes; seconds = seconds < 10 ? `0${seconds}` : seconds; @@ -105,7 +108,7 @@ var updateCountDownTimerMessage = function ($modal, minutes, seconds) { // store number of tries to reconnect to server, in order to increase time to wait // until next try -var reconnectionTries = { +const reconnectionTries = { counter: 0, nextTry() { @@ -119,8 +122,9 @@ var reconnectionTries = { // Timer based on http://stackoverflow.com/a/20618517. // duration: how many **seconds** until the timer ends -// granularity (optional): how many **milliseconds** between each 'tick' of timer. Default: 1000ms (1s) -var CountDownTimer = function (duration, granularity) { +// granularity (optional): how many **milliseconds** +// between each 'tick' of timer. Default: 1000ms (1s) +const CountDownTimer = function (duration, granularity) { this.duration = duration; this.granularity = granularity || 1000; this.running = false; @@ -137,8 +141,7 @@ CountDownTimer.prototype.start = function () { const start = Date.now(); const that = this; let diff; - - (function timer() { + const timer = () => { diff = that.duration - Math.floor((Date.now() - start) / 1000); if (diff > 0) { @@ -149,7 +152,8 @@ CountDownTimer.prototype.start = function () { that.tick(0); that.expire(); } - }()); + }; + timer(); }; CountDownTimer.prototype.tick = function (diff) { @@ -184,9 +188,7 @@ CountDownTimer.prototype.cancel = function () { return this; }; -CountDownTimer.parse = function (seconds) { - return { - minutes: (seconds / 60) | 0, - seconds: (seconds % 60) | 0, - }; -}; +CountDownTimer.parse = (seconds) => ({ + minutes: (seconds / 60) | 0, + seconds: (seconds % 60) | 0, +}); diff --git a/src/static/js/pad_connectionstatus.js b/src/static/js/pad_connectionstatus.js index 54d7dc760..1282d488c 100644 --- a/src/static/js/pad_connectionstatus.js +++ b/src/static/js/pad_connectionstatus.js @@ -1,3 +1,5 @@ +'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. @@ -22,25 +24,25 @@ const padmodals = require('./pad_modals').padmodals; -const padconnectionstatus = (function () { +const padconnectionstatus = (() => { let status = { what: 'connecting', }; const self = { - init() { + init: () => { $('button#forcereconnect').click(() => { window.location.reload(); }); }, - connected() { + connected: () => { status = { what: 'connected', }; padmodals.showModal('connected'); padmodals.hideOverlay(); }, - reconnecting() { + reconnecting: () => { status = { what: 'reconnecting', }; @@ -48,8 +50,8 @@ const padconnectionstatus = (function () { padmodals.showModal('reconnecting'); padmodals.showOverlay(); }, - disconnected(msg) { - if (status.what == 'disconnected') return; + disconnected: (msg) => { + if (status.what === 'disconnected') return; status = { what: 'disconnected', @@ -81,14 +83,10 @@ const padconnectionstatus = (function () { padmodals.showModal(k); padmodals.showOverlay(); }, - isFullyConnected() { - return status.what == 'connected'; - }, - getStatus() { - return status; - }, + isFullyConnected: () => status.what === 'connected', + getStatus: () => status, }; return self; -}()); +})(); exports.padconnectionstatus = padconnectionstatus; diff --git a/src/static/js/pad_cookie.js b/src/static/js/pad_cookie.js index f3dedf03c..620d41735 100644 --- a/src/static/js/pad_cookie.js +++ b/src/static/js/pad_cookie.js @@ -1,3 +1,5 @@ +'use strict'; + /** * Copyright 2009 Google Inc. * diff --git a/src/static/js/pad_editbar.js b/src/static/js/pad_editbar.js index dae96b192..788d5ecd0 100644 --- a/src/static/js/pad_editbar.js +++ b/src/static/js/pad_editbar.js @@ -1,4 +1,5 @@ '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. diff --git a/src/static/js/pad_editor.js b/src/static/js/pad_editor.js index 912c50f64..b7b94f720 100644 --- a/src/static/js/pad_editor.js +++ b/src/static/js/pad_editor.js @@ -1,3 +1,5 @@ +'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. @@ -24,26 +26,26 @@ const Cookies = require('./pad_utils').Cookies; const padcookie = require('./pad_cookie').padcookie; const padutils = require('./pad_utils').padutils; -const padeditor = (function () { +const padeditor = (() => { let Ace2Editor = undefined; let pad = undefined; let settings = undefined; - var self = { + const self = { ace: null, // this is accessed directly from other files viewZoom: 100, - init(readyFunc, initialViewOptions, _pad) { + init: (readyFunc, initialViewOptions, _pad) => { Ace2Editor = require('./ace').Ace2Editor; pad = _pad; settings = pad.settings; - function aceReady() { + const aceReady = () => { $('#editorloadingbox').hide(); if (readyFunc) { readyFunc(); } - } + }; self.ace = new Ace2Editor(); self.ace.init('editorcontainer', '', aceReady); @@ -57,7 +59,7 @@ const padeditor = (function () { // view bar $('#viewbarcontents').show(); }, - initViewOptions() { + initViewOptions: () => { // Line numbers padutils.bindCheckboxChange($('#options-linenoscheck'), () => { pad.changeViewOption('showLineNumbers', padutils.getCheckbox($('#options-linenoscheck'))); @@ -74,8 +76,8 @@ const padeditor = (function () { pad.changeViewOption('rtlIsTrue', padutils.getCheckbox($('#options-rtlcheck'))); }); html10n.bind('localized', () => { - pad.changeViewOption('rtlIsTrue', ('rtl' == html10n.getDirection())); - padutils.setCheckbox($('#options-rtlcheck'), ('rtl' == html10n.getDirection())); + pad.changeViewOption('rtlIsTrue', ('rtl' === html10n.getDirection())); + padutils.setCheckbox($('#options-rtlcheck'), ('rtl' === html10n.getDirection())); }); // font family change @@ -87,9 +89,10 @@ const padeditor = (function () { html10n.bind('localized', () => { $('#languagemenu').val(html10n.getLanguage()); // translate the value of 'unnamed' and 'Enter your name' textboxes in the userlist - // this does not interfere with html10n's normal value-setting because html10n just ingores s - // also, a value which has been set by the user will be not overwritten since a user-edited - // does *not* have the editempty-class + // this does not interfere with html10n's normal value-setting because + // html10n just ingores s + // also, a value which has been set by the user will be not overwritten + // since a user-edited does *not* have the editempty-class $('input[data-l10n-id]').each((key, input) => { input = $(input); if (input.hasClass('editempty')) { @@ -106,17 +109,17 @@ const padeditor = (function () { } }); }, - setViewOptions(newOptions) { - function getOption(key, defaultValue) { + setViewOptions: (newOptions) => { + const getOption = (key, defaultValue) => { const value = String(newOptions[key]); - if (value == 'true') return true; - if (value == 'false') return false; + if (value === 'true') return true; + if (value === 'false') return false; return defaultValue; - } + }; let v; - v = getOption('rtlIsTrue', ('rtl' == html10n.getDirection())); + v = getOption('rtlIsTrue', ('rtl' === html10n.getDirection())); self.ace.setProperty('rtlIsTrue', v); padutils.setCheckbox($('#options-rtlcheck'), v); @@ -137,29 +140,29 @@ const padeditor = (function () { self.ace.setProperty('textface', newOptions.padFontFamily || ''); }, - dispose() { + dispose: () => { if (self.ace) { self.ace.destroy(); self.ace = null; } }, - enable() { + enable: () => { if (self.ace) { self.ace.setEditable(true); } }, - disable() { + disable: () => { if (self.ace) { self.ace.setProperty('grayedOut', true); self.ace.setEditable(false); } }, - restoreRevisionText(dataFromServer) { + restoreRevisionText: (dataFromServer) => { pad.addHistoricalAuthors(dataFromServer.historicalAuthorData); self.ace.importAText(dataFromServer.atext, dataFromServer.apool, true); }, }; return self; -}()); +})(); exports.padeditor = padeditor; diff --git a/src/static/js/pad_impexp.js b/src/static/js/pad_impexp.js index 9c5dd076e..a44c69d8a 100644 --- a/src/static/js/pad_impexp.js +++ b/src/static/js/pad_impexp.js @@ -1,3 +1,5 @@ +'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. @@ -20,24 +22,27 @@ * limitations under the License. */ -const padimpexp = (function () { +const padimpexp = (() => { // /// import let currentImportTimer = null; - function addImportFrames() { + const addImportFrames = () => { $('#import .importframe').remove(); - const iframe = $(''); + const iframe = $('