Commit d1908496 authored by Robert Knight's avatar Robert Knight

Use arrow functions to replace `self` with `this`

Remove the need to alias the `Adder` instance to `self` by using
arrow-functions.
parent 2e4e294b
...@@ -131,8 +131,7 @@ class Adder { ...@@ -131,8 +131,7 @@ class Adder {
* event handlers. * event handlers.
*/ */
constructor(container, options) { constructor(container, options) {
var self = this; this.element = createAdderDOM(container);
self.element = createAdderDOM(container);
// Set initial style // Set initial style
Object.assign(container.style, { Object.assign(container.style, {
...@@ -149,21 +148,16 @@ class Adder { ...@@ -149,21 +148,16 @@ class Adder {
// The adder is hidden using the `visibility` property rather than `display` // The adder is hidden using the `visibility` property rather than `display`
// so that we can compute its size in order to position it before display. // so that we can compute its size in order to position it before display.
self.element.style.visibility = 'hidden'; this.element.style.visibility = 'hidden';
var view = self.element.ownerDocument.defaultView; var view = this.element.ownerDocument.defaultView;
var enterTimeout; var enterTimeout;
self.element.querySelector(ANNOTATE_BTN_SELECTOR) var handleCommand = (event) => {
.addEventListener('click', handleCommand);
self.element.querySelector(HIGHLIGHT_BTN_SELECTOR)
.addEventListener('click', handleCommand);
function handleCommand(event) {
event.preventDefault(); event.preventDefault();
event.stopPropagation(); event.stopPropagation();
var isAnnotateCommand = this.classList.contains(ANNOTATE_BTN_CLASS); var isAnnotateCommand = event.target.classList.contains(ANNOTATE_BTN_CLASS);
if (isAnnotateCommand) { if (isAnnotateCommand) {
options.onAnnotate(); options.onAnnotate();
...@@ -171,22 +165,22 @@ class Adder { ...@@ -171,22 +165,22 @@ class Adder {
options.onHighlight(); options.onHighlight();
} }
self.hide(); this.hide();
} };
function width() { this.element.querySelector(ANNOTATE_BTN_SELECTOR)
return self.element.getBoundingClientRect().width; .addEventListener('click', handleCommand);
} this.element.querySelector(HIGHLIGHT_BTN_SELECTOR)
.addEventListener('click', handleCommand);
function height() { var width = () => this.element.getBoundingClientRect().width;
return self.element.getBoundingClientRect().height; var height = () => this.element.getBoundingClientRect().height;
}
/** Hide the adder */ /** Hide the adder */
this.hide = function () { this.hide = () => {
clearTimeout(enterTimeout); clearTimeout(enterTimeout);
self.element.className = classnames({'annotator-adder': true}); this.element.className = classnames({'annotator-adder': true});
self.element.style.visibility = 'hidden'; this.element.style.visibility = 'hidden';
}; };
/** /**
...@@ -200,7 +194,7 @@ class Adder { ...@@ -200,7 +194,7 @@ class Adder {
* edge of `targetRect`. * edge of `targetRect`.
* @return {Target} * @return {Target}
*/ */
this.target = function (targetRect, isSelectionBackwards) { this.target = (targetRect, isSelectionBackwards) => {
// Set the initial arrow direction based on whether the selection was made // Set the initial arrow direction based on whether the selection was made
// forwards/upwards or downwards/backwards. // forwards/upwards or downwards/backwards.
var arrowDirection; var arrowDirection;
...@@ -253,8 +247,8 @@ class Adder { ...@@ -253,8 +247,8 @@ class Adder {
* @param {number} left - Horizontal offset from left edge of viewport. * @param {number} left - Horizontal offset from left edge of viewport.
* @param {number} top - Vertical offset from top edge of viewport. * @param {number} top - Vertical offset from top edge of viewport.
*/ */
this.showAt = function (left, top, arrowDirection) { this.showAt = (left, top, arrowDirection) => {
self.element.className = classnames({ this.element.className = classnames({
'annotator-adder': true, 'annotator-adder': true,
'annotator-adder--arrow-down': arrowDirection === ARROW_POINTING_DOWN, 'annotator-adder--arrow-down': arrowDirection === ARROW_POINTING_DOWN,
'annotator-adder--arrow-up': arrowDirection === ARROW_POINTING_UP, 'annotator-adder--arrow-up': arrowDirection === ARROW_POINTING_UP,
...@@ -265,8 +259,8 @@ class Adder { ...@@ -265,8 +259,8 @@ class Adder {
// after use. So we need to make sure the button stays displayed // after use. So we need to make sure the button stays displayed
// the way it was originally displayed - without the inline styles // the way it was originally displayed - without the inline styles
// See: https://github.com/hypothesis/client/issues/137 // See: https://github.com/hypothesis/client/issues/137
self.element.querySelector(ANNOTATE_BTN_SELECTOR).style.display = ''; this.element.querySelector(ANNOTATE_BTN_SELECTOR).style.display = '';
self.element.querySelector(HIGHLIGHT_BTN_SELECTOR).style.display = ''; this.element.querySelector(HIGHLIGHT_BTN_SELECTOR).style.display = '';
// Translate the (left, top) viewport coordinates into positions relative to // Translate the (left, top) viewport coordinates into positions relative to
// the adder's nearest positioned ancestor (NPA). // the adder's nearest positioned ancestor (NPA).
...@@ -281,11 +275,11 @@ class Adder { ...@@ -281,11 +275,11 @@ class Adder {
top: toPx(top - parentRect.top), top: toPx(top - parentRect.top),
left: toPx(left - parentRect.left), left: toPx(left - parentRect.left),
}); });
self.element.style.visibility = 'visible'; this.element.style.visibility = 'visible';
clearTimeout(enterTimeout); clearTimeout(enterTimeout);
enterTimeout = setTimeout(function () { enterTimeout = setTimeout(() => {
self.element.className += ' is-active'; this.element.className += ' is-active';
}, 1); }, 1);
}; };
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment