-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
92 lines (87 loc) · 2 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
/*
* require dependencies
*/
var dialog = require('bootstrap-dialog'),
domify = require('domify'),
event = require('event'),
Emitter = require('emitter');
/*
* create new instance
*/
function YesNoDialog (title, text, save, cancel, lang) {
var self = this;
/*
* set default lang if not definied
*/
if (lang === undefined || lang === null)
lang = {};
if (lang.save === undefined)
lang.save = 'save';
if (lang.cancel === undefined)
lang.cancel = 'cancel';
/*
* set footer
*/
var saveEl = domify('<button class="btn btn-primary">' + lang.save + '</button>');
var cancelEl = domify('<button class="btn">' + lang.cancel + '</button>');
var footEl = domify('<span></span>');
footEl.appendChild(cancelEl);
footEl.appendChild(saveEl);
/*
* make new bootstrap-dialog
*/
var d = this.d = dialog(title, text, footEl)
.closable()
.overlay()
.show();
['close','hide','show'].forEach(function(event){
d.on(event, function () {
self.emit(event);
});
});
/*
* bind button events and implement standard behaviour
*/
event.bind(saveEl, 'click',
function () {
/*
* hide dialog
* call the save function
*/
d.hide();
if (!save)
return;
if (typeof save === 'function') {
save();
} else if (save['fn']) {
save['fn'].call(save['scope']);
}
}
);
event.bind(cancelEl, 'click',
function () {
/*
* hide dialog
call the cancel function
*/
d.hide();
if (!cancel)
return;
if (typeof cancel === 'function') {
cancel();
} else if (cancel['fn']) {
cancel['fn'].call(cancel['scope']);
}
}
);
};
YesNoDialog.prototype.show = function () {
this.d.show();
};
YesNoDialog.prototype.hide = function () {
this.d.hide();
};
Emitter(YesNoDialog.prototype);
module.exports = function (title, text, save, cancel, lang) {
return new YesNoDialog(title, text, save, cancel, lang);
};