-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
109 lines (98 loc) · 2.36 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* Notice
*
* A notice message at the top of a webpage.
*
*/
var classes = require('classes')
var events = require('event')
var query = require('query')
var tap = require('tap')
var hasTouch = 'ontouchend' in window
var zIndex = 999
function create(o) {
var el = document.createElement(o.tag || 'div')
el.className = o.className
el.innerHTML = o.html || ''
if (o.parent) o.parent.appendChild(el)
return el
}
var container
function Notice(msg, options) {
if (! (this instanceof Notice)) return new Notice(msg, options)
options = options || {}
if (!container) {
container = create({
className: 'notice-container',
parent: options.parent || document.body
})
}
if (options.type == 'success') options.duration = options.duration || 2000
var closable = options.hasOwnProperty('closable')? options.closable : true
var duration = options.duration
if (!closable && duration == null) duration = 2000
options.message = msg
var el = createElement(options, closable)
el.style.zIndex = -- zIndex
this.el = el
container.appendChild(this.el)
this.closeEl = query('.notice-close', el)
this._hideFn = this.hide.bind(this)
if (hasTouch) {
this._tap = tap(this.closeEl, this._hideFn)
} else {
events.bind(this.closeEl, 'click', this._hideFn)
}
if (duration) {
setTimeout(this.hide.bind(this), duration)
}
}
Notice.prototype.hide = function(e) {
if (e) {
e.preventDefault()
e.stopPropagation()
}
if (this._hide) return
this._hide = true
var self = this
if (this._tap) {
this._tap.unbind()
} else {
events.bind(this.closeEl, 'click', this._hideFn)
}
dismiss(this.el)
}
Notice.prototype.clear = function () {
var el = this.el
if (el && el.parentNode) {
el.parentNode.removeChild(el)
}
}
function createElement(options, closable) {
var className = 'notice-item' + (options.type
? ' notice-' + options.type
: '')
var item = create({className: className})
create({
className: 'notice-content',
html: options.message,
parent: item
})
if (closable) {
var close = create({
className : 'notice-close',
html: '×',
parent: item
})
}
return item
}
function dismiss(el) {
classes(el).add('notice-dismiss')
setTimeout(function() {
if (el && el.parentNode) {
el.parentNode.removeChild(el)
}
}, 200)
}
module.exports = Notice