-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (46 loc) · 1.22 KB
/
index.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
var classes = require('classes')
var events = require('events')
var body = document.body
function create(tag, className) {
var el = document.createElement(tag)
el.className = className
return el
}
function Overlay(el) {
if (!(this instanceof Overlay)) return new Overlay(el)
var root = this.el = create('div', 'overlay')
body.appendChild(root)
var close = create('a', 'overlay-close')
close.innerHTML = '×'
close.setAttribute('href', '#')
root.appendChild(close)
var container = create('div', 'overlay-container')
container.appendChild(el)
root.appendChild(container)
this.events = events(root, this)
this.events.bind('click a.overlay-close', 'hide')
this.docEvents = events(document, this)
this.docEvents.bind('keyup')
classes(body).add('overlay-active')
}
Overlay.prototype.onkeyup = function (e) {
if (e.which === 27) {
e.preventDefault()
this.hide()
}
}
Overlay.prototype.hide = function (e) {
if (this._removed) return
if (e) {
e.preventDefault()
}
this._removed = true
this.events.unbind()
this.docEvents.unbind()
classes(body).remove('overlay-active')
var el = this.el
setTimeout(function () {
el.parentNode.removeChild(el)
}, 200)
}
module.exports = Overlay