-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQs.o.js
68 lines (61 loc) · 1.53 KB
/
Qs.o.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Other
/**
* Clone the first (and only) child of a template element.
*
* @param {string} id the id of the template element
* @return {Element} the new element
*/
function cloneTemplate (id) {
return document.getElementById(id).content.children[0].cloneNode(true)
}
/**
* Create an element from html text.
*
* @param {string} text the html text
* @return {Element} the new element
*/
function elementFromHtml (text) {
var tmp = document.createElement('div')
tmp.innerHTML = text
return tmp.children[0]
}
/**
* An event callback.
*
* @callback callbackEnter
* @param {Event} e the keydown event
*/
/**
* Adds a listener to an element for the keydown event of the enter character.
*
* @param {Element} ele the element
* @param {callbackEnter} f the callback
*/
function onEnter (ele, f) {
ele.addEventListener('keydown', function (e) {
if (e.keyCode === 13) {
f(e)
e.preventDefault()
}
})
}
/**
* An event callback.
*
* @callback callbackScrollNearEnd
* @param {number} offset offset of the limit
*/
/**
* Adds a listener for scrolling near the end of the page.
* Near is defined by a number of pixels from the end of the page.
*
* @param {number} limit the number of pixels
* @param {callbackScrollNearEnd} f the callback
*/
function onScrollNearEnd (limit, f) {
window.onscroll = function () {
if (window.innerHeight + window.scrollY >= document.documentElement.scrollHeight - limit) {
f(window.innerHeight + window.scrollY - document.documentElement.scrollHeight + limit)
}
}
}