Repository navigation
Expand file tree
/
Copy pathcustom_dialog.js
More file actions
158 lines (128 loc) · 4.85 KB
/
Copy pathcustom_dialog.js
File metadata and controls
158 lines (128 loc) · 4.85 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* exported CustomDialog */
//XXX: Waiting for the window.showModalDialog support in B2G
'use strict';
var CustomDialog = (function() {
var screen = null;
var dialog = null;
var header = null;
var message = null;
var yes = null;
var no = null;
return {
hide: function dialog_hide() {
if (screen === null) {
return;
}
document.body.removeChild(screen);
screen = null;
dialog = null;
header = null;
message = null;
yes = null;
no = null;
},
/**
* Method that shows the dialog
* @param {String} title the title of the dialog. null or empty for
* no title. or you can give a object with more options
* like {icon: path or empty string, title: String}.
* @param {String} msg message for the dialog. give a object like the
* title to enable more options:
* {icon: path or empty string, message: String}.
* @param {Object} cancel {title, callback} object when confirm.
* @param {Object} confirm {title, callback} object when cancel.
*/
show: function dialog_show(title, msg, cancel, confirm) {
if (screen === null) {
screen = document.createElement('form');
screen.setAttribute('role', 'dialog');
screen.setAttribute('data-type', 'confirm');
screen.id = 'dialog-screen';
dialog = document.createElement('section');
screen.appendChild(dialog);
// Create a reusable function to decorate elements with all
// possible options, instead of scattering similar code about
// everywhere.
//
// It's also possible to be extended with more usable decorating
// options and elements.
//
// 'title'|'message' -> Object|String -> dialog -> the element
// -> the decorated element
var decorateWithOptions = function cd_decorateWithOptions(type, options,
elm, dialog) {
if ('string' === typeof options) {
elm.textContent = options;
return elm;
}
var text = options[type];
var icon = options.icon;
elm.textContent = text;
if (icon && '' !== icon) {
var iconImg = new Image();
iconImg.src = icon;
iconImg.classList.add('custom-dialog-' + type + '-icon');
// Icons usually insert as the first element.
elm.insertBefore(iconImg, elm.firstChild);
}
// More decorating options goes here.
return elm;
};
header = document.createElement('h1');
header.id = 'dialog-title';
if (title && title !== '') {
header = decorateWithOptions('title', title, header, dialog);
}
dialog.appendChild(header);
message = document.createElement('p');
message.id = 'dialog-message';
message = decorateWithOptions('message', msg, message, dialog);
dialog.appendChild(message);
var menu = document.createElement('menu');
menu.dataset.items = 1;
no = document.createElement('button');
// The default type of the button element is "Submit",
// and form submit in system app would make system app reload.
no.type = 'button';
var noText = document.createTextNode(cancel.title);
no.appendChild(noText);
no.id = 'dialog-no';
no.addEventListener('click', clickHandler);
menu.appendChild(no);
if (confirm) {
menu.dataset.items = 2;
yes = document.createElement('button');
// The default type of button element is "Submit",
// and form submit in system app would make system app reload.
yes.type = 'button';
var yesText = document.createTextNode(confirm.title);
yes.appendChild(yesText);
yes.id = 'dialog-yes';
//confirm can be with class "danger" or "recommend"
//the default is "danger"
yes.className = confirm.recommend ? 'recommend' : 'danger';
yes.addEventListener('click', clickHandler);
menu.appendChild(yes);
}
else { // 1 button, should be full.
no.classList.add('full');
}
screen.appendChild(menu);
document.body.appendChild(screen);
}
// Make the screen visible
screen.classList.add('visible');
// This is the event listener function for the buttons
function clickHandler(evt) {
// Hide the dialog
screen.classList.remove('visible');
// Call the appropriate callback, if it is defined
if (evt.target === yes && confirm.callback) {
confirm.callback();
} else if (evt.target === no && cancel.callback) {
cancel.callback();
}
}
}
};
}());