pad_editbar: Use ES6 class syntax for readability

This commit is contained in:
Richard Hansen 2021-08-14 07:16:36 -04:00
parent 97ccf9e082
commit 5478d2ce60

View file

@ -30,52 +30,53 @@ const padsavedrevs = require('./pad_savedrevs');
const _ = require('underscore'); const _ = require('underscore');
require('./vendors/nice-select'); require('./vendors/nice-select');
const ToolbarItem = function (element) { class ToolbarItem {
this.$el = element; constructor(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) { getCommand() {
if (this.isSelect()) { return this.$el.attr('data-key');
return this.$el.find('select').val(val);
} }
};
getValue() {
ToolbarItem.prototype.getType = function () { if (this.isSelect()) {
return this.$el.attr('data-type'); return this.$el.find('select').val();
}; }
ToolbarItem.prototype.isSelect = function () {
return this.getType() === 'select';
};
ToolbarItem.prototype.isButton = function () {
return this.getType() === 'button';
};
ToolbarItem.prototype.bind = function (callback) {
if (this.isButton()) {
this.$el.click((event) => {
$(':focus').blur();
callback(this.getCommand(), this);
event.preventDefault();
});
} else if (this.isSelect()) {
this.$el.find('select').change(() => {
callback(this.getCommand(), this);
});
} }
};
setValue(val) {
if (this.isSelect()) {
return this.$el.find('select').val(val);
}
}
getType() {
return this.$el.attr('data-type');
}
isSelect() {
return this.getType() === 'select';
}
isButton() {
return this.getType() === 'button';
}
bind(callback) {
if (this.isButton()) {
this.$el.click((event) => {
$(':focus').blur();
callback(this.getCommand(), this);
event.preventDefault();
});
} else if (this.isSelect()) {
this.$el.find('select').change(() => {
callback(this.getCommand(), this);
});
}
}
}
const syncAnimation = (() => { const syncAnimation = (() => {
const SYNCING = -100; const SYNCING = -100;
@ -122,10 +123,12 @@ const syncAnimation = (() => {
}; };
})(); })();
exports.padeditbar = { exports.padeditbar = new class {
_editbarPosition: 0, constructor() {
commands: {}, this._editbarPosition = 0;
dropdowns: [], this.commands = {};
this.dropdowns = [];
}
init() { init() {
$('#editbar .editbarbutton').attr('unselectable', 'on'); // for IE $('#editbar .editbarbutton').attr('unselectable', 'on'); // for IE
@ -168,38 +171,38 @@ exports.padeditbar = {
$('iframe[name="ace_outer"]').contents().scroll((ev) => { $('iframe[name="ace_outer"]').contents().scroll((ev) => {
$('#editbar').toggleClass('editor-scrolled', $(ev.currentTarget).scrollTop() > 2); $('#editbar').toggleClass('editor-scrolled', $(ev.currentTarget).scrollTop() > 2);
}); });
}, }
isEnabled: () => true, isEnabled() { 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');
}, }
registerCommand(cmd, callback) { registerCommand(cmd, callback) {
this.commands[cmd] = callback; this.commands[cmd] = callback;
return this; return this;
}, }
registerDropdownCommand(cmd, dropdown) { registerDropdownCommand(cmd, dropdown) {
dropdown = dropdown || cmd; dropdown = dropdown || cmd;
this.dropdowns.push(dropdown); this.dropdowns.push(dropdown);
this.registerCommand(cmd, () => { this.registerCommand(cmd, () => {
this.toggleDropDown(dropdown); this.toggleDropDown(dropdown);
}); });
}, }
registerAceCommand(cmd, callback) { registerAceCommand(cmd, callback) {
this.registerCommand(cmd, (cmd, ace, item) => { this.registerCommand(cmd, (cmd, ace, item) => {
ace.callWithAce((ace) => { ace.callWithAce((ace) => {
callback(cmd, ace, item); callback(cmd, ace, item);
}, cmd, true); }, cmd, true);
}); });
}, }
triggerCommand(cmd, item) { triggerCommand(cmd, item) {
if (this.isEnabled() && this.commands[cmd]) { if (this.isEnabled() && this.commands[cmd]) {
this.commands[cmd](cmd, padeditor.ace, item); this.commands[cmd](cmd, padeditor.ace, item);
} }
if (padeditor.ace) padeditor.ace.focus(); if (padeditor.ace) padeditor.ace.focus();
}, }
// cb is deprecated (this function is synchronous so a callback is unnecessary). // cb is deprecated (this function is synchronous so a callback is unnecessary).
toggleDropDown(moduleName, cb = null) { toggleDropDown(moduleName, cb = null) {
@ -249,15 +252,15 @@ exports.padeditbar = {
} finally { } finally {
if (cb) Promise.resolve().then(() => cb(cbErr)); if (cb) Promise.resolve().then(() => cb(cbErr));
} }
}, }
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 params = '?showControls=true&showChat=true&showLineNumbers=true&useMonospaceFont=false';
const props = 'width="100%" height="600" frameborder="0"'; const props = 'width="100%" height="600" frameborder="0"';
@ -274,8 +277,8 @@ exports.padeditbar = {
.val(`<iframe name="embed_readwrite" src="${padUrl}${params}" ${props}></iframe>`); .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');
@ -291,7 +294,7 @@ exports.padeditbar = {
if (menu_left && menu_left.scrollWidth > $('.toolbar').width()) { if (menu_left && menu_left.scrollWidth > $('.toolbar').width()) {
$('.toolbar').addClass('cropped'); $('.toolbar').addClass('cropped');
} }
}, }
_bodyKeyEvent(evt) { _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
@ -352,7 +355,7 @@ exports.padeditbar = {
$(focusItems[this._editbarPosition]).focus(); $(focusItems[this._editbarPosition]).focus();
} }
} }
}, }
_registerDefaultCommands() { _registerDefaultCommands() {
this.registerDropdownCommand('showusers', 'users'); this.registerDropdownCommand('showusers', 'users');
@ -474,5 +477,5 @@ exports.padeditbar = {
.substring(0, document.location.href.lastIndexOf('/')); .substring(0, document.location.href.lastIndexOf('/'));
} }
}); });
}, }
}; }();