-
Notifications
You must be signed in to change notification settings - Fork 1
/
dialog.js
162 lines (132 loc) · 6.01 KB
/
dialog.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
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
158
159
160
161
162
/*
* dialog.js is a utility which can replace the default JavaScript dialog windows for webpages (alert(), prompt(), confirm())
*
* Version: 3.0
* Copyright (c) 2015 Matteo Bernardini
* Licensed under the MIT License (refer to the LICENSE file for further information).
*
* Check this project on GitHub:
* https://github.com/mttbernardin/dialog
*
*/
/**
* The library provides 3 types of objects.
*
* ## A global Object `dialogSettings`, used to modify the default behavior of the dialog boxes ##
*
* __object_key__ __type__ __default_value__ __comment__
* defType string "alert" Default type
* defTitle string "Message" Default title
* defContent string "Missing text" Default message of the dialog
* okText string "OK" Label for the <ok> button
* continueText string "Continue" Label for the <continue> button
* cancelText string "Cancel" Label for the <cancel> button
*
*
* ## The main Function `dialog(opt)`, returns a Promise and takes the following options as properties of the Object `opt` ##
*
* __object_key__ __type__ __comment__
* type string Type of the dialog window. Possible values are ['alert', 'prompt', 'confirm']
* title string Text which will be displayed as the title of the dialog window
* content string Message of the dialog window
* placeholder string If the type is 'prompt', this will define a placeholder text for the input box
* id string Identifier which can be referenced afterwards in the resolved object (see below)
* data Object Object of convenience that will be referenced in the resolved object (see below)
*
*
* ## The resolved Object of the Promise ##
*
* __object_key__ __type__ __comment__
* id string Identifier passed when calling the `dialog()` function, defaults to `undefined`
* data Object Reference of the object passed with the `dialog()` function, defaults to `undefined`
* action boolean `true` if the user has pressed <ok> or <continue>, `false` for <cancel>
* value string If the type of the requested dialog window was 'prompt' it will contain
* the value inserted in the input box, otherwise it will be `undefined`
*
*/
"use strict";
var dialogSettings = new Object();
function dialog(params) {
params = params || new Object();
dialogSettings = dialogSettings || new Object();
// == Default Values == //
dialogSettings.defTitle = dialogSettings.defTitle || "Message";
dialogSettings.defType = dialogSettings.defType || "alert";
dialogSettings.defContent = dialogSettings.defContent || "Missing text";
dialogSettings.okText = dialogSettings.okText || "OK";
dialogSettings.continueText = dialogSettings.continueText || "Continue";
dialogSettings.cancelText = dialogSettings.cancelText || "Cancel";
// == Switch between default values or passed values == //
params.title = params.title || dialogSettings.defTitle;
params.type = params.type || dialogSettings.defType;
params.content = params.content || dialogSettings.defContent;
// == Creating elements == //
var parts = ["window", "wrapper", "title", "body", "message", "prompt", "actions", "ok-btn", "cancel-btn"],
elms = {};
for (var i = 0, partName; partName = parts[i]; i++) {
if (partName.indexOf("btn") !== -1) {
elms[partName] = document.createElement("button");
elms[partName].setAttribute("type", "button");
} else {
elms[partName] = document.createElement("div");
}
elms[partName].className = "dialog-" + partName;
}
var prompt = document.createElement("input");
prompt.setAttribute("type", "text");
prompt.className = "dialog-prompt-input";
// == Arranging elements == //
elms["window"].appendChild(elms["wrapper"]);
elms["wrapper"].appendChild(elms["title"]);
elms["wrapper"].appendChild(elms["body"]);
elms["body"].appendChild(elms["message"]);
elms["actions"].appendChild(elms["ok-btn"]);
switch (params.type) {
case "prompt":
elms["prompt"].appendChild(prompt);
elms["body"].appendChild(elms["prompt"]);
case "confirm": // or "prompt", notice no break here
elms["actions"].appendChild(elms["cancel-btn"]);
}
elms["body"].appendChild(elms["actions"]);
// == Assigning values == //
elms["title"].textContent = params.title;
elms["message"].textContent = params.content;
elms["ok-btn"].textContent = params.type === "confirm" ? dialogSettings.continueText : dialogSettings.okText;
elms["cancel-btn"].textContent = dialogSettings.cancelText;
prompt.placeholder = params.placeholder || "";
// == Appending to body & focus == //
var dialogWindow = elms["window"];
dialogWindow.setAttribute("data-dialog-type", params.type);
document.body.appendChild(dialogWindow);
if (params.type === "prompt")
prompt.focus();
// == Return promise == //
return new Promise(function(resolve, reject) {
// Will be triggered when closing the dialog
function action(e, userAction) {
e = e || window.event;
var target = e.target ? e.target : e.srcElement;
if (typeof userAction === "boolean" || target.tagName.toLowerCase() === "button") {
document.removeEventListener("keyup", useKeys, false);
document.body.removeChild(dialogWindow);
resolve({
id: params.id,
action: typeof userAction === "boolean" ? userAction : target.className.indexOf("ok") !== -1,
value: params.type === "prompt" ? prompt.value : void 0,
data: params.data
});
}
}
// Handler for keyboard events
function useKeys(e) {
e = e || window.event;
if (e.keyCode === 13) // ENTER key
action(null, true);
else if (e.keyCode === 27 && params.type !== "alert") // ESC key
action(null, false);
}
document.addEventListener("keyup", useKeys, false);
elms["actions"].addEventListener("click", action, false);
});
}