-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
126 lines (108 loc) · 3.08 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
var fs = require('fs')
var glue = require('hyperglue')
var insertCss = require('insert-css')
var xtend = require('xtend')
var path = require('path')
var css = fs.readFileSync(path.join(__dirname, '/style.css'), 'utf-8')
var html = fs.readFileSync(path.join(__dirname, '/index.html'), 'utf-8')
module.exports = dialog
function dialog (opt) {
opt = opt || {}
opt = {
img: { src: opt.icon || '' },
'.ok': opt.ok || 'OK',
'.cancel': opt.cancel || 'Cancel',
'.url': opt.hostname || window.location.hostname
}
insertCss(opt.style || css)
return {
alert: render.bind(opt, 'alert'),
confirm: render.bind(opt, 'confirm'),
prompt: render.bind(opt, 'prompt'),
promptPassword: render.bind(opt, 'promptPassword'),
cancel: cancelOpenDialog
}
}
function render (type, title, defaultValue, cb) {
var inputPassword = type === 'promptPassword'
if (inputPassword) {
type = 'prompt'
}
if (typeof title === 'function') {
cb = title
defaultValue = ''
title = ''
} else if (typeof defaultValue === 'function') {
cb = defaultValue
defaultValue = ''
}
var opt = xtend(this)
opt['.type'] = { class: 'dialog-widget ' + type }
opt['.title'] = {
_html: (title || '').replace(/<[^>]+>/g).replace(/\n/, '<br>')
}
opt['input'] = { value: defaultValue || '' }
if (inputPassword) {
opt['input']['type'] = 'password'
}
var background = glue('<div class="dialog-widget background"></div>')
var el = glue(html, opt)
el.setAttribute('data-icon', !!opt.img.src)
cancelOpenDialog.fn = cancel
document.body.appendChild(background)
document.body.appendChild(el)
if (type === 'prompt') {
var input = el.querySelector('input')
input.focus()
if (defaultValue) input.setSelectionRange(0, defaultValue.length)
} else {
el.querySelector('.cancel').focus()
}
eventListeners('addEventListener')
if (!cb) {
cb = function noop () {}
return new Promise(function (resolve, reject) {
cb = resolve
})
}
function eventListeners (method) {
el.querySelector('.ok')[method]('click', ok)
el.querySelector('.cancel')[method]('click', cancel)
el.querySelector('form')[method]('submit', ok)
window[method]('keydown', keydown)
window[method]('focus', supress, true)
}
function supress (e) {
var node = e.target
while (node) {
if (node.classList && node.classList.contains('dialog-widget')) return
node = node.parentNode
}
setTimeout(function () {
e.target.blur()
})
}
function cancel () {
cb()
cleanup()
}
function keydown (e) {
if (e.keyCode === 27) cancelOpenDialog()
}
function ok (e) {
e.preventDefault()
// eslint-disable-next-line
if (type === 'confirm' || type === 'alert') cb(true)
if (type === 'prompt') cb(el.querySelector('input').value)
cleanup()
}
function cleanup () {
eventListeners('removeEventListener')
document.body.removeChild(el)
document.body.removeChild(background)
delete cancelOpenDialog.fn
}
}
function cancelOpenDialog () {
if (cancelOpenDialog.fn) cancelOpenDialog.fn()
}