Skip to content

Commit 6d944ec

Browse files
committed
added syntax highlighting and enhanced formatting
1 parent 70e6652 commit 6d944ec

File tree

1 file changed

+58
-48
lines changed

1 file changed

+58
-48
lines changed

README.md

+58-48
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,51 @@
33
## The difference ##
44

55
There are various types of dialog windows, generally they are divided into Modal dialogs and Modeless dialogs.
6+
67
The difference between those is that the modal dialog requires an input from the user, it cannot be hidden and it doesn't allow to focus the opener window till the dialog is closed.
8+
79
The modeless dialog instead allows to focus the opener window cause it doesn't require the response from the user, so it can be hidden too.
810

11+
912
## The problem ##
1013

1114
What are the behaviors of the modal dialogs and modeless dialogs in JavaScript? The modal dialogs block the execution of the script till the user gives the input, and the modeless dialogs don't exist in the pure JavaScript.
15+
1216
So, if you need to show a modal dialog to the user and in the meantime do some other code in JavaScript you can't by using the default dialogs offered by JavaScript (alert(), prompt() and confirm()), because they block the execution of any code.
1317

18+
1419
## The solution ##
1520

1621
Using JQuery in you project will make it easier to find a nice plugin which does this task (also better than this library). However, if you're not planning to use JQuery, it seems meaningless to import that huge framework to only integrate a plugin: at this point this library is the perfect solution if you only need the personalized dialogs and nothing else.
22+
1723
So I solved the problem making a function named `dialog()` which provides an asynchronous modal dialog: *modal*, because the user cannot hide it or focus the opener window, *asynchronous*, because it allows the execution of code not related with the dialog.
1824

25+
1926
## How it works ##
2027

21-
How to use the main function is very easy. First of all you have to include the script and the style (which you can modify) of the AMD in the `<head>` of your page:
28+
Using the main function is very easy. First of all you have to include the script and the style (which you can modify) of the AMD in the `<head>` of your page:
2229

23-
```
30+
```html
2431
<link rel="stylesheet" type="text/css" href="dialog/style.css">
2532
<script type="text/javascript" src="dialog/script.js"></script>
2633
```
2734

35+
2836
### The `dialogSettings` object ###
2937

3038
The JavaScript code integrates 3 objects, the first it's a global object which includes the default values and behaviors of the function that shows the dialog: `dialogSettings`, which has the following properties:
39+
3140
- `defType`: default to *"alert"*, specify the default type of the dialog. Its possible values are *"alert"*, *"prompt"* and *"confirm"*.
3241
- `defTitle`: default to *"Message"*, specify the default title of the dialog.
3342
- `defContent`: default to *"<i>Missing text</i>"*, specify the default content of the dialog.
3443
- `okText`: default to *"OK"*, specify the text of the ok button.
3544
- `continueText`: default to *"Continue"*, specify the text of the continue button.
3645
- `cancelText`: default to *"Cancel"*, specify the text of the cancel button.
3746

47+
3848
Changing this properties may be useful to localize the dialog box. For example, adding this code to the personal html page will show a confirm dialog with content in Italian, if the function `dialog()` is called without parameters:
3949

40-
```
41-
<script type="text/javascript">
50+
```javascript
4251
dialogSettings = {
4352
defType: "confirm",
4453
defTitle: "Conferma",
@@ -47,14 +56,15 @@ Changing this properties may be useful to localize the dialog box. For example,
4756
continueText: "Continua",
4857
cancelText: "Annulla"
4958
};
50-
</script>
5159

52-
<button onclick="dialog()">Try it</button>
60+
// Show the default dialog
61+
dialog();
5362
```
5463

5564
### The `dialog()` function ###
5665

5766
The second object is the main function that will show the dialog box: `dialog()`.
67+
5868
To specify the content, the type, the tile, etc... of the dialog you have to pass an object as first parameter to the function which has the following properties:
5969

6070
- `type`: specify the type of the dialog. Its possible values are *"alert"*, *"prompt"* and *"confirm"*.
@@ -67,16 +77,15 @@ To specify the content, the type, the tile, etc... of the dialog you have to pas
6777

6878
This is an example of an AMD:
6979

70-
```
71-
<script type="text/javascript">
72-
var parameters = {
73-
type: "prompt",
74-
title: "Example",
75-
content: "What's your name?",
76-
}
77-
</script>
78-
79-
<button onclick="dialog(parameters)">Try it</button>
80+
```javascript
81+
var parameters = {
82+
type: "prompt",
83+
title: "Example",
84+
content: "What's your name?",
85+
}
86+
87+
// Show the dialog
88+
dialog(parameters);
8089
```
8190

8291

@@ -93,37 +102,37 @@ In fact, cause this kind of dialog boxes are asynchronous they allow the executi
93102

94103
This is the previous example, but with some callback functions:
95104

105+
```javascript
106+
var parameters = {
107+
type: "prompt",
108+
title: "Example",
109+
content: "What's your name?",
110+
callBack: "check(returnObj)",
111+
id: "first",
112+
vars: {foo: "bar"}
113+
}
114+
115+
function check(obj) {
116+
if (obj.id=="first" && obj.boolean)
117+
dialog({
118+
type: "confirm",
119+
content: "Do you want your name printed on a message?",
120+
vars: {foo: obj.vars.foo, name: obj.value!=""?obj.value:"Unnamed"},
121+
callBack: "check(returnObj)",
122+
id: "second"
123+
});
124+
else if (obj.id=="second" && obj.boolean)
125+
dialog({
126+
//NB: default type is alert, so we don't need to specify it
127+
title: "Print out",
128+
content: "Hello "+obj.vars.name+"<br>Foo: "+obj.vars.foo
129+
});
130+
}
131+
132+
// Start the chain
133+
dialog(parameters);
96134
```
97-
<script type="text/javascript">
98-
var parameters = {
99-
type: "prompt",
100-
title: "Example",
101-
content: "What's your name?",
102-
callBack: "check(returnObj)",
103-
id: "first",
104-
vars: {foo: "bar"}
105-
}
106-
107-
function check(obj) {
108-
if (obj.id=="first" && obj.boolean)
109-
dialog({
110-
type: "confirm",
111-
content: "Do you want your name printed on a message?",
112-
vars: {foo: obj.vars.foo, name: obj.value!=""?obj.value:"Unnamed"},
113-
callBack: "check(returnObj)",
114-
id: "second"
115-
});
116-
else if (obj.id=="second" && obj.boolean)
117-
dialog({
118-
//NB: default type is alert, so we don't need to specify it
119-
title: "Print out",
120-
content: "Hello "+obj.vars.name+"<br>Foo: "+obj.vars.foo
121-
});
122-
}
123-
</script>
124-
125-
<button onclick="dialog(parameters)">Try it</button>
126-
```
135+
127136

128137
## Live demo ##
129138

@@ -143,16 +152,17 @@ You can also see the changelog of the various releases with the date of when the
143152

144153
You can see the changelog [here][2]</a>.
145154

155+
146156
## License and sharing ##
147157

148-
Version: 1.0
149-
Made by: Matteo Bernardini, [@mttbernardini][4].
158+
Made by: Matteo Bernardini, [@mttbernardini][4].
150159
This project is shared with license CC, by-nc-sa:
151160

152161
- **Attribution**: You are free to distribute the codes, download it and use it, but you must declare who's the owner.
153162
- **No Commercial**: You aren't allowed to use the codes for commercial or lucrative purposes.
154163
- **Share Alike**: you are free to edit the codes as you like, but please leave the commented part of the codes which includes the description, the version, the owner, and the license. If you modify the code you must re-share it with the same license.
155164

165+
156166
To see the integral declaration of the Creative Commons license [see here][3].
157167

158168

0 commit comments

Comments
 (0)