Introduced ESLint for code style checks

This commit is contained in:
squidfunk
2016-09-30 13:29:45 +02:00
parent 02915210f4
commit 46f45bb8a9
31 changed files with 1245 additions and 536 deletions

View File

@@ -20,8 +20,6 @@
* IN THE SOFTWARE.
*/
'use strict';
/* ----------------------------------------------------------------------------
* Navigation expander
* ------------------------------------------------------------------------- */
@@ -35,59 +33,59 @@ class Expander {
* @param {(string|HTMLElement)} el - Selector or HTML element
*/
constructor(el) {
this.el_ = (typeof el === 'string') ? document.querySelector(el) : el;
this.el_ = (typeof el === "string")
? document.querySelector(el)
: el
/* Event listener */
this.handler_ = ev => {
this.update(ev);
};
};
this.update(ev)
}
}
/**
* Update state of expandable element
*
* @param {Event} ev - Event
* @return {void}
*/
update(ev) {
console.log("foo");
};
update() {}
/**
* Reset state of expandable element
*
* @return {void}
*/
reset() {
// this.el_.classList.remove('md-js__sidebar--locked');
// this.el_.style.height = '';
//
// /* Reset parameters */
// this.height_ = 0;
// this.locked_ = false;
};
reset() {}
/**
* Register listener for all relevant events
*
* @return {void}
*/
listen() {
['click'].forEach(name => {
window.addEventListener(name, this.handler_, false);
});
};
["click"].forEach(name => {
window.addEventListener(name, this.handler_, false)
})
}
/**
* Unregister listener for all relevant events
*
* @return {void}
*/
unlisten() {
['click'].forEach(name => {
window.removeEventListener(name, this.handler_, false);
});
["click"].forEach(name => {
window.removeEventListener(name, this.handler_, false)
})
/* Perform reset */
this.reset();
};
this.reset()
}
}
/* ----------------------------------------------------------------------------
* Exports
* ------------------------------------------------------------------------- */
export default Expander;
export default Expander

View File

@@ -20,8 +20,6 @@
* IN THE SOFTWARE.
*/
'use strict';
/* ----------------------------------------------------------------------------
* Sidebar scroll-spy
* ------------------------------------------------------------------------- */
@@ -35,87 +33,96 @@ class ScrollSpy {
* @param {(string|HTMLCollection)} el - Selector or HTML elements
*/
constructor(el) {
this.el_ = (typeof el === 'string') ? document.querySelectorAll(el) : el;
this.el_ = (typeof el === "string")
? document.querySelectorAll(el)
: el
/* Initialize index for currently active element */
this.index_ = 0;
this.offset_ = window.pageYOffset;
this.index_ = 0
this.offset_ = window.pageYOffset
/* Event listener */
this.handler_ = ev => {
this.update(ev);
};
this.update(ev)
}
}
/**
* Update state of sidebar and hash
*
* @param {Event} ev - Event
* @param {Event} ev - Event (omitted)
* @return {void}
*/
update(ev) {
update() {
/* Scroll direction is down */
if (this.offset_ <= window.pageYOffset) {
for (let i = this.index_ + 1; i < this.el_.length; i++) {
let anchor = document.querySelector(this.el_[i].hash); // TODO: improve performance by caching
const anchor = document.querySelector(this.el_[i].hash) // TODO: improve performance by caching
if (anchor.offsetTop <= window.pageYOffset) {
if (i > 0)
this.el_[i - 1].classList.add('md-nav__link--marked');
this.index_ = i;
this.el_[i - 1].classList.add("md-nav__link--marked")
this.index_ = i
} else {
break;
break
}
}
/* Scroll direction is up */
} else {
for (let i = this.index_; i >= 0; i--) {
let anchor = document.querySelector(this.el_[i].hash);
const anchor = document.querySelector(this.el_[i].hash)
if (anchor.offsetTop > window.pageYOffset) {
if (i > 0)
this.el_[i - 1].classList.remove('md-nav__link--marked');
this.el_[i - 1].classList.remove("md-nav__link--marked")
} else {
this.index_ = i;
break;
this.index_ = i
break
}
}
}
/* Remember current offset for next cycle */
this.offset_ = window.pageYOffset;
this.offset_ = window.pageYOffset
}
/**
* Reset state of sidebar
*
* @return {void}
*/
reset() {
[].forEach.call(this.el_, el => {
el.classList.remove('md-nav__link--marked');
});
el.classList.remove("md-nav__link--marked")
})
}
/**
* Register listener for all relevant events
*
* @return {void}
*/
listen() {
['scroll', 'resize', 'orientationchange'].forEach(name => {
window.addEventListener(name, this.handler_, false);
});
["scroll", "resize", "orientationchange"].forEach(name => {
window.addEventListener(name, this.handler_, false)
})
/* Initial update */
this.update();
this.update()
}
/**
* Unregister listener for all relevant events
*
* @return {void}
*/
unlisten() {
['scroll', 'resize', 'orientationchange'].forEach(name => {
window.removeEventListener(name, this.handler_, false);
});
["scroll", "resize", "orientationchange"].forEach(name => {
window.removeEventListener(name, this.handler_, false)
})
/* Perform reset */
this.reset();
this.reset()
}
}
@@ -123,4 +130,4 @@ class ScrollSpy {
* Exports
* ------------------------------------------------------------------------- */
export default ScrollSpy;
export default ScrollSpy

View File

@@ -0,0 +1,98 @@
/*
* Copyright (c) 2016 Martin Donath <martin.donath@squidfunk.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
"use strict"
/* ----------------------------------------------------------------------------
* Search
* ------------------------------------------------------------------------- */
class Search {
/**
* Constructor
*
* @constructor
* @param {(string|HTMLElement)} el - Selector or HTML element
*/
constructor(el) {
this.el_ = (typeof el === "string") ? document.querySelector(el) : el
/* Event listener */
this.handler_ = ev => {
this.update(ev)
}
}
/**
* Update state and height of sidebar
*
* @param {Event} ev - Event
* @return {void}
*/
update() {
}
/**
* Reset state and height of sidebar
*
* @return {void}
*/
reset() {
}
/**
* Register listener for all relevant events
*
* @return {void}
*/
listen() {
// ['scroll', 'resize', 'orientationchange'].forEach(name => {
// window.addEventListener(name, this.handler_, false);
// });
/* Initial update */
this.update()
}
/**
* Unregister listener for all relevant events
*
* @return {void}
*/
unlisten() {
// ['scroll', 'resize', 'orientationchange'].forEach(name => {
// window.removeEventListener(name, this.handler_, false);
// });
/* Perform reset */
this.reset()
}
}
/* ----------------------------------------------------------------------------
* Exports
* ------------------------------------------------------------------------- */
export default Search

View File

@@ -20,8 +20,6 @@
* IN THE SOFTWARE.
*/
'use strict';
/* ----------------------------------------------------------------------------
* Sidebar sticky-scroll handler
* ------------------------------------------------------------------------- */
@@ -35,101 +33,110 @@ class Sidebar {
* @param {(string|HTMLElement)} el - Selector or HTML element
*/
constructor(el) {
this.el_ = (typeof el === 'string') ? document.querySelector(el) : el;
this.el_ = (typeof el === "string")
? document.querySelector(el)
: el
/* Grab inner and outer container */
this.inner_ = this.el_.parentNode;
this.outer_ = this.el_.parentNode.parentNode;
this.inner_ = this.el_.parentNode
this.outer_ = this.el_.parentNode.parentNode
/* Initialize parameters */
this.height_ = 0;
this.locked_ = false;
this.height_ = 0
this.locked_ = false
/* Event listener */
this.handler_ = ev => {
this.update(ev);
};
};
this.update(ev)
}
}
/**
* Update state and height of sidebar
*
* @param {Event} ev - Event
* @return {void}
*/
update(ev) {
let bounds = this.inner_.getBoundingClientRect();
let parent = this.outer_.offsetTop;
update() {
const bounds = this.inner_.getBoundingClientRect()
const parent = this.outer_.offsetTop
/* Determine top and bottom offsets */
let top = bounds.top + window.pageYOffset,
bottom = bounds.bottom + window.pageYOffset;
const top = bounds.top + window.pageYOffset,
bottom = bounds.bottom + window.pageYOffset
/* Determine current y-offset at top and bottom of window */
let upper = window.pageYOffset,
lower = window.pageYOffset + window.innerHeight;
const upper = window.pageYOffset,
lower = window.pageYOffset + window.innerHeight
/* Calculate new bounds */
let offset = top - upper;
let height = window.innerHeight - Math.max(lower - bottom, 0)
- Math.max(offset, parent);
const offset = top - upper
const height = window.innerHeight - Math.max(lower - bottom, 0)
- Math.max(offset, parent)
/* If height changed, update element */
if (height != this.height_)
this.el_.style.height = (this.height_ = height) + 'px';
if (height !== this.height_)
this.el_.style.height = `${this.height_ = height}px`
/* Sidebar should be locked, as we're below parent offset */
if (offset < parent) {
if (!this.locked_) {
this.el_.classList.add('md-js__sidebar--locked');
this.locked_ = true;
this.el_.classList.add("md-js__sidebar--locked")
this.locked_ = true
}
/* Sidebar should be unlocked, if locked */
} else if (this.locked_) {
this.el_.classList.remove('md-js__sidebar--locked');
this.locked_ = false;
this.el_.classList.remove("md-js__sidebar--locked")
this.locked_ = false
}
};
}
/**
* Reset state and height of sidebar
*
* @return {void}
*/
reset() {
this.el_.classList.remove('md-js__sidebar--locked');
this.el_.style.height = '';
this.el_.classList.remove("md-js__sidebar--locked")
this.el_.style.height = ""
/* Reset parameters */
this.height_ = 0;
this.locked_ = false;
};
this.height_ = 0
this.locked_ = false
}
/**
* Register listener for all relevant events
*
* @return {void}
*/
listen() {
['scroll', 'resize', 'orientationchange'].forEach(name => {
window.addEventListener(name, this.handler_, false);
});
["scroll", "resize", "orientationchange"].forEach(name => {
window.addEventListener(name, this.handler_, false)
})
/* Initial update */
this.update();
};
this.update()
}
/**
* Unregister listener for all relevant events
*
* @return {void}
*/
unlisten() {
['scroll', 'resize', 'orientationchange'].forEach(name => {
window.removeEventListener(name, this.handler_, false);
});
["scroll", "resize", "orientationchange"].forEach(name => {
window.removeEventListener(name, this.handler_, false)
})
/* Perform reset */
this.reset();
};
this.reset()
}
}
/* ----------------------------------------------------------------------------
* Exports
* ------------------------------------------------------------------------- */
export default Sidebar;
export default Sidebar