From 136d895d155f28c2e75b3af206549acaa2a354ed Mon Sep 17 00:00:00 2001 From: Philip Molares Date: Mon, 15 Feb 2021 09:42:51 +0100 Subject: Linter: Fix all lint errors Signed-off-by: Philip Molares --- public/js/index.js | 2171 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 1411 insertions(+), 760 deletions(-) (limited to 'public/js/index.js') diff --git a/public/js/index.js b/public/js/index.js index adc95461..03cae1b4 100644 --- a/public/js/index.js +++ b/public/js/index.js @@ -85,19 +85,49 @@ require('../css/site.css') require('highlight.js/styles/github-gist.css') -var defaultTextHeight = 20 -var viewportMargin = 20 -var defaultEditorMode = 'gfm' - -var idleTime = 300000 // 5 mins -var updateViewDebounce = 100 -var cursorMenuThrottle = 50 -var cursorActivityDebounce = 50 -var cursorAnimatePeriod = 100 -var supportContainers = ['success', 'info', 'warning', 'danger'] -var supportCodeModes = ['javascript', 'typescript', 'jsx', 'htmlmixed', 'htmlembedded', 'css', 'xml', 'clike', 'clojure', 'ruby', 'python', 'shell', 'php', 'sql', 'haskell', 'coffeescript', 'yaml', 'pug', 'lua', 'cmake', 'nginx', 'perl', 'sass', 'r', 'dockerfile', 'tiddlywiki', 'mediawiki', 'go', 'gherkin'].concat(hljs.listLanguages()) -var supportCharts = ['sequence', 'flow', 'graphviz', 'mermaid', 'abc'] -var supportHeaders = [ +let defaultTextHeight = 20 +let viewportMargin = 20 +const defaultEditorMode = 'gfm' + +const idleTime = 300000 // 5 mins +const updateViewDebounce = 100 +const cursorMenuThrottle = 50 +const cursorActivityDebounce = 50 +const cursorAnimatePeriod = 100 +const supportContainers = ['success', 'info', 'warning', 'danger'] +const supportCodeModes = [ + 'javascript', + 'typescript', + 'jsx', + 'htmlmixed', + 'htmlembedded', + 'css', + 'xml', + 'clike', + 'clojure', + 'ruby', + 'python', + 'shell', + 'php', + 'sql', + 'haskell', + 'coffeescript', + 'yaml', + 'pug', + 'lua', + 'cmake', + 'nginx', + 'perl', + 'sass', + 'r', + 'dockerfile', + 'tiddlywiki', + 'mediawiki', + 'go', + 'gherkin' +].concat(hljs.listLanguages()) +const supportCharts = ['sequence', 'flow', 'graphviz', 'mermaid', 'abc'] +const supportHeaders = [ { text: '# h1', search: '#' @@ -225,7 +255,7 @@ const supportExtraTags = [ text: '[random color tag]', search: '[]', command: function () { - var color = randomColor() + const color = randomColor() return '[color=' + color + ']' } } @@ -259,7 +289,7 @@ let visibleMD = false let visibleLG = false const isTouchDevice = 'ontouchstart' in document.documentElement let currentStatus = statusType.offline -let lastInfo = { +const lastInfo = { needRestore: false, cursor: null, scroll: null, @@ -285,14 +315,14 @@ let lastInfo = { let personalInfo = {} let onlineUsers = [] const fileTypes = { - 'pl': 'perl', - 'cgi': 'perl', - 'js': 'javascript', - 'php': 'php', - 'sh': 'bash', - 'rb': 'ruby', - 'html': 'html', - 'py': 'python' + pl: 'perl', + cgi: 'perl', + js: 'javascript', + php: 'php', + sh: 'bash', + rb: 'ruby', + html: 'html', + py: 'python' } // editor settings @@ -302,7 +332,7 @@ if (!textit) { } const editorInstance = new Editor() -var editor = editorInstance.init(textit) +const editor = editorInstance.init(textit) // FIXME: global referncing in jquery-textcomplete patch window.editor = editor @@ -313,7 +343,7 @@ defaultTextHeight = parseInt($('.CodeMirror').css('line-height')) const ui = getUIElements() // page actions -var opts = { +const opts = { lines: 11, // The number of lines to draw length: 20, // The length of each line width: 2, // The line thickness @@ -333,11 +363,11 @@ var opts = { } /* eslint-disable no-unused-vars */ -var spinner = new Spinner(opts).spin(ui.spinner[0]) +const spinner = new Spinner(opts).spin(ui.spinner[0]) /* eslint-enable no-unused-vars */ // idle -var idle = new Idle({ +const idle = new Idle({ onAway: function () { idle.isAway = true emitUserStatus() @@ -356,7 +386,7 @@ ui.area.codemirror.on('touchstart', function () { idle.onActive() }) -var haveUnreadChanges = false +let haveUnreadChanges = false function setHaveUnreadChanges (bool) { if (!window.loaded) return @@ -379,7 +409,9 @@ function updateTitleReminder () { function setRefreshModal (status) { $('#refreshModal').modal('show') $('#refreshModal').find('.modal-body > div').hide() - $('#refreshModal').find('.' + status).show() + $('#refreshModal') + .find('.' + status) + .show() } function setNeedRefresh () { @@ -395,9 +427,9 @@ setloginStateChangeEvent(function () { }) // visibility -var wasFocus = false +let wasFocus = false Visibility.change(function (e, state) { - var hidden = Visibility.hidden() + const hidden = Visibility.hidden() if (hidden) { if (editorHasFocus()) { wasFocus = true @@ -421,7 +453,7 @@ $(document).ready(function () { idle.checkAway() checkResponsive() // if in smaller screen, we don't need advanced scrollbar - var scrollbarStyle + let scrollbarStyle if (visibleXS) { scrollbarStyle = 'native' } else { @@ -434,7 +466,7 @@ $(document).ready(function () { checkEditorStyle() /* cache dom references */ - var $body = $('body') + const $body = $('body') /* we need this only on touch devices */ if (isTouchDevice) { @@ -463,7 +495,9 @@ $(document).ready(function () { $('[data-toggle="tooltip"]').tooltip() // shortcuts // allow on all tags - key.filter = function (e) { return true } + key.filter = function (e) { + return true + } key('ctrl+alt+e', function (e) { changeMode(modeType.edit) }) @@ -488,13 +522,18 @@ $(window).resize(function () { }) // when page unload $(window).on('unload', function () { -// updateHistoryInner(); + // updateHistoryInner(); }) $(window).on('error', function () { // setNeedRefresh(); }) -setupSyncAreas(ui.area.codemirrorScroll, ui.area.view, ui.area.markdown, editor) +setupSyncAreas( + ui.area.codemirrorScroll, + ui.area.view, + ui.area.markdown, + editor +) function autoSyncscroll () { if (editorHasFocus()) { @@ -504,8 +543,8 @@ function autoSyncscroll () { } } -var windowResizeDebounce = 200 -var windowResize = _.debounce(windowResizeInner, windowResizeDebounce) +const windowResizeDebounce = 200 +const windowResize = _.debounce(windowResizeInner, windowResizeDebounce) function windowResizeInner (callback) { checkLayout() @@ -520,7 +559,9 @@ function windowResizeInner (callback) { clearMap() autoSyncscroll() updateScrollspy() - if (callback && typeof callback === 'function') { callback() } + if (callback && typeof callback === 'function') { + callback() + } }, 1) } else { // force it load all docs at once to prevent scroll knob blink @@ -530,18 +571,22 @@ function windowResizeInner (callback) { autoSyncscroll() editor.setOption('viewportMargin', viewportMargin) // add or update user cursors - for (var i = 0; i < onlineUsers.length; i++) { - if (onlineUsers[i].id !== personalInfo.id) { buildCursor(onlineUsers[i]) } + for (let i = 0; i < onlineUsers.length; i++) { + if (onlineUsers[i].id !== personalInfo.id) { + buildCursor(onlineUsers[i]) + } } updateScrollspy() - if (callback && typeof callback === 'function') { callback() } + if (callback && typeof callback === 'function') { + callback() + } }, 1) } } } function checkLayout () { - var navbarHieght = $('.navbar').outerHeight() + const navbarHieght = $('.navbar').outerHeight() $('body').css('padding-top', navbarHieght + 'px') } @@ -557,22 +602,28 @@ function checkResponsive () { visibleLG = $('.visible-lg').is(':visible') if (visibleXS && appState.currentMode === modeType.both) { - if (editorHasFocus()) { changeMode(modeType.edit) } else { changeMode(modeType.view) } + if (editorHasFocus()) { + changeMode(modeType.edit) + } else { + changeMode(modeType.view) + } } emitUserStatus() } -var lastEditorWidth = 0 -var previousFocusOnEditor = null +let lastEditorWidth = 0 +let previousFocusOnEditor = null function checkEditorStyle () { - var desireHeight = editorInstance.statusBar ? (ui.area.edit.height() - editorInstance.statusBar.outerHeight()) : ui.area.edit.height() + let desireHeight = editorInstance.statusBar + ? ui.area.edit.height() - editorInstance.statusBar.outerHeight() + : ui.area.edit.height() if (editorInstance.toolBar) { desireHeight = desireHeight - editorInstance.toolBar.outerHeight() } // set editor height and min height based on scrollbar style and mode - var scrollbarStyle = editor.getOption('scrollbarStyle') + const scrollbarStyle = editor.getOption('scrollbarStyle') if (scrollbarStyle === 'overlay' || appState.currentMode === modeType.both) { ui.area.codemirrorScroll.css('height', desireHeight + 'px') ui.area.codemirrorScroll.css('min-height', '') @@ -590,9 +641,11 @@ function checkEditorStyle () { maxWidth: $(window).width() * 0.7, minWidth: $(window).width() * 0.2, create: function (e, ui) { - $(this).parent().on('resize', function (e) { - e.stopPropagation() - }) + $(this) + .parent() + .on('resize', function (e) { + e.stopPropagation() + }) }, start: function (e) { editor.setOption('viewportMargin', Infinity) @@ -622,23 +675,31 @@ function checkEditorStyle () { ui.area.resize.handle = $('.ui-resizable-handle') } if (!ui.area.resize.syncToggle.length) { - ui.area.resize.syncToggle = $('') - ui.area.resize.syncToggle.hover(function () { - previousFocusOnEditor = editorHasFocus() - }, function () { - previousFocusOnEditor = null - }) + ui.area.resize.syncToggle = $( + '' + ) + ui.area.resize.syncToggle.hover( + function () { + previousFocusOnEditor = editorHasFocus() + }, + function () { + previousFocusOnEditor = null + } + ) ui.area.resize.syncToggle.click(function () { appState.syncscroll = !appState.syncscroll checkSyncToggle() }) ui.area.resize.handle.append(ui.area.resize.syncToggle) ui.area.resize.syncToggle.hide() - ui.area.resize.handle.hover(function () { - ui.area.resize.syncToggle.stop(true, true).delay(200).fadeIn(100) - }, function () { - ui.area.resize.syncToggle.stop(true, true).delay(300).fadeOut(300) - }) + ui.area.resize.handle.hover( + function () { + ui.area.resize.syncToggle.stop(true, true).delay(200).fadeIn(100) + }, + function () { + ui.area.resize.syncToggle.stop(true, true).delay(300).fadeOut(300) + } + ) } } @@ -651,37 +712,53 @@ function checkSyncToggle () { window.preventSyncScrollToEdit = false syncScrollToEdit() } - ui.area.resize.syncToggle.find('i').removeClass('fa-unlink').addClass('fa-link') + ui.area.resize.syncToggle + .find('i') + .removeClass('fa-unlink') + .addClass('fa-link') } else { - ui.area.resize.syncToggle.find('i').removeClass('fa-link').addClass('fa-unlink') + ui.area.resize.syncToggle + .find('i') + .removeClass('fa-link') + .addClass('fa-unlink') } } -var checkEditorScrollbar = _.debounce(function () { +const checkEditorScrollbar = _.debounce(function () { editor.operation(checkEditorScrollbarInner) }, 50) function checkEditorScrollbarInner () { // workaround simple scroll bar knob // will get wrong position when editor height changed - var scrollInfo = editor.getScrollInfo() + const scrollInfo = editor.getScrollInfo() editor.scrollTo(null, scrollInfo.top - 1) editor.scrollTo(null, scrollInfo.top) } function checkTocStyle () { // toc right - var paddingRight = parseFloat(ui.area.markdown.css('padding-right')) - var right = ($(window).width() - (ui.area.markdown.offset().left + ui.area.markdown.outerWidth() - paddingRight)) + const paddingRight = parseFloat(ui.area.markdown.css('padding-right')) + const right = + $(window).width() - + (ui.area.markdown.offset().left + + ui.area.markdown.outerWidth() - + paddingRight) ui.toc.toc.css('right', right + 'px') // affix toc left - var newbool - var rightMargin = (ui.area.markdown.parent().outerWidth() - ui.area.markdown.outerWidth()) / 2 + let newbool + const rightMargin = + (ui.area.markdown.parent().outerWidth() - ui.area.markdown.outerWidth()) / + 2 // for ipad or wider device if (rightMargin >= 133) { newbool = true - var affixLeftMargin = (ui.toc.affix.outerWidth() - ui.toc.affix.width()) / 2 - var left = ui.area.markdown.offset().left + ui.area.markdown.outerWidth() - affixLeftMargin + const affixLeftMargin = + (ui.toc.affix.outerWidth() - ui.toc.affix.width()) / 2 + const left = + ui.area.markdown.offset().left + + ui.area.markdown.outerWidth() - + affixLeftMargin ui.toc.affix.css('left', left + 'px') ui.toc.affix.css('width', rightMargin + 'px') } else { @@ -708,12 +785,12 @@ function checkTocStyle () { function showStatus (type, num) { currentStatus = type - var shortStatus = ui.toolbar.shortStatus - var status = ui.toolbar.status - var label = $('') - var fa = $('') - var msg = '' - var shortMsg = '' + const shortStatus = ui.toolbar.shortStatus + const status = ui.toolbar.status + const label = $('') + const fa = $('') + let msg = '' + let shortMsg = '' shortStatus.html('') status.html('') @@ -738,7 +815,7 @@ function showStatus (type, num) { } label.append(fa) - var shortLabel = label.clone() + const shortLabel = label.clone() shortLabel.append(' ' + shortMsg) shortStatus.append(shortLabel) @@ -761,7 +838,7 @@ function toggleMode () { } } -var lastMode = null +let lastMode = null function changeMode (type) { // lock navbar to prevent it hide after changeMode @@ -771,8 +848,8 @@ function changeMode (type) { lastMode = appState.currentMode appState.currentMode = type } - var responsiveClass = 'col-lg-6 col-md-6 col-sm-6' - var scrollClass = 'ui-scrollable' + const responsiveClass = 'col-lg-6 col-md-6 col-sm-6' + const scrollClass = 'ui-scrollable' ui.area.codemirror.removeClass(scrollClass) ui.area.edit.removeClass(responsiveClass) ui.area.view.removeClass(scrollClass) @@ -798,11 +875,20 @@ function changeMode (type) { break } // save mode to url - if (history.replaceState && window.loaded) history.replaceState(null, '', serverurl + '/' + noteid + '?' + appState.currentMode.name) + if (history.replaceState && window.loaded) { + history.replaceState( + null, + '', + serverurl + '/' + noteid + '?' + appState.currentMode.name + ) + } if (appState.currentMode === modeType.view) { editor.getInputField().blur() } - if (appState.currentMode === modeType.edit || appState.currentMode === modeType.both) { + if ( + appState.currentMode === modeType.edit || + appState.currentMode === modeType.both + ) { // add and update status bar if (!editorInstance.statusBar) { editorInstance.addStatusBar() @@ -820,7 +906,10 @@ function changeMode (type) { $(document.body).css('background-color', 'white') updateView() } else { - $(document.body).css('background-color', ui.area.codemirror.css('background-color')) + $(document.body).css( + 'background-color', + ui.area.codemirror.css('background-color') + ) } // check resizable editor style if (appState.currentMode === modeType.both) { @@ -864,15 +953,18 @@ function changeMode (type) { ui.toolbar.both.removeClass('active') ui.toolbar.edit.removeClass('active') ui.toolbar.view.removeClass('active') - var modeIcon = ui.toolbar.mode.find('i') + const modeIcon = ui.toolbar.mode.find('i') modeIcon.removeClass('fa-pencil').removeClass('fa-eye') - if (ui.area.edit.is(':visible') && ui.area.view.is(':visible')) { // both + if (ui.area.edit.is(':visible') && ui.area.view.is(':visible')) { + // both ui.toolbar.both.addClass('active') modeIcon.addClass('fa-eye') - } else if (ui.area.edit.is(':visible')) { // edit + } else if (ui.area.edit.is(':visible')) { + // edit ui.toolbar.edit.addClass('active') modeIcon.addClass('fa-eye') - } else if (ui.area.view.is(':visible')) { // view + } else if (ui.area.view.is(':visible')) { + // view ui.toolbar.view.addClass('active') modeIcon.addClass('fa-pencil') } @@ -883,17 +975,27 @@ function lockNavbar () { $('.navbar').addClass('locked') } -var unlockNavbar = _.debounce(function () { +const unlockNavbar = _.debounce(function () { $('.navbar').removeClass('locked') }, 200) function showMessageModal (title, header, href, text, success) { - var modal = $('.message-modal') + const modal = $('.message-modal') modal.find('.modal-title').html(title) modal.find('.modal-body h5').html(header) - if (href) { modal.find('.modal-body a').attr('href', href).text(text) } else { modal.find('.modal-body a').removeAttr('href').text(text) } - modal.find('.modal-footer button').removeClass('btn-default btn-success btn-danger') - if (success) { modal.find('.modal-footer button').addClass('btn-success') } else { modal.find('.modal-footer button').addClass('btn-danger') } + if (href) { + modal.find('.modal-body a').attr('href', href).text(text) + } else { + modal.find('.modal-body a').removeAttr('href').text(text) + } + modal + .find('.modal-footer button') + .removeClass('btn-default btn-success btn-danger') + if (success) { + modal.find('.modal-footer button').addClass('btn-success') + } else { + modal.find('.modal-footer button').addClass('btn-danger') + } modal.modal('show') } @@ -923,9 +1025,9 @@ ui.toolbar.extra.slide.attr('href', noteurl + '/slide') ui.toolbar.download.markdown.click(function (e) { e.preventDefault() e.stopPropagation() - var filename = renderFilename(ui.area.markdown) + '.md' - var markdown = editor.getValue() - var blob = new Blob([markdown], { + const filename = renderFilename(ui.area.markdown) + '.md' + const markdown = editor.getValue() + const blob = new Blob([markdown], { type: 'text/markdown;charset=utf-8' }) saveAs(blob, filename, true) @@ -945,12 +1047,12 @@ ui.toolbar.download.rawhtml.click(function (e) { // export to dropbox ui.toolbar.export.dropbox.click(function (event) { event.preventDefault() - var filename = renderFilename(ui.area.markdown) + '.md' - var options = { + const filename = renderFilename(ui.area.markdown) + '.md' + const options = { files: [ { - 'url': noteurl + '/download', - 'filename': filename + url: noteurl + '/download', + filename: filename } ], error: function (errorMessage) { @@ -971,25 +1073,48 @@ ui.toolbar.export.snippet.click(function () { $('#snippetExportModalVersion').val(data.version) $('#snippetExportModalLoading').hide() $('#snippetExportModal').modal('toggle') - $('#snippetExportModalProjects').find('option').remove().end().append('') + $('#snippetExportModalProjects') + .find('option') + .remove() + .end() + .append( + '' + ) if (data.projects) { data.projects.sort(function (a, b) { - return (a.path_with_namespace < b.path_with_namespace) ? -1 : ((a.path_with_namespace > b.path_with_namespace) ? 1 : 0) + return a.path_with_namespace < b.path_with_namespace + ? -1 + : a.path_with_namespace > b.path_with_namespace + ? 1 + : 0 }) data.projects.forEach(function (project) { - if (!project.snippets_enabled || - (project.permissions.project_access === null && project.permissions.group_access === null) || - (project.permissions.project_access !== null && project.permissions.project_access.access_level < 20)) { + if ( + !project.snippets_enabled || + (project.permissions.project_access === null && + project.permissions.group_access === null) || + (project.permissions.project_access !== null && + project.permissions.project_access.access_level < 20) + ) { return } - $('') + $('#snippetImportModalProjects') + .find('option') + .remove() + .end() + .append( + '' + ) if (data.projects) { data.projects.sort(function (a, b) { - return (a.path_with_namespace < b.path_with_namespace) ? -1 : ((a.path_with_namespace > b.path_with_namespace) ? 1 : 0) + return a.path_with_namespace < b.path_with_namespace + ? -1 + : a.path_with_namespace > b.path_with_namespace + ? 1 + : 0 }) data.projects.forEach(function (project) { - if (!project.snippets_enabled || - (project.permissions.project_access === null && project.permissions.group_access === null) || - (project.permissions.project_access !== null && project.permissions.project_access.access_level < 20)) { + if ( + !project.snippets_enabled || + (project.permissions.project_access === null && + project.permissions.group_access === null) || + (project.permissions.project_access !== null && + project.permissions.project_access.access_level < 20) + ) { return } - $('') + $('#snippetImportModalSnippets') + .find('option') + .remove() + .end() + .append( + '' + ) data.forEach(function (snippet) { - $('