4 | Message - info
5 |
6 |
7 |
12 |
13 |
14 |
35 |
36 |
--------------------------------------------------------------------------------
/samples/03_confirm.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Message - info
5 |
6 |
7 |
12 |
13 |
14 |
35 |
36 |
--------------------------------------------------------------------------------
/license.txt:
--------------------------------------------------------------------------------
1 | Copyright (C) 2011 by dhtmlx.com
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy
4 | of this software and associated documentation files (the "Software"), to deal
5 | in the Software without restriction, including without limitation the rights
6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 | copies of the Software, and to permit persons to whom the Software is
8 | furnished to do so, subject to the following conditions:
9 |
10 | The above copyright notice and this permission notice shall be included in
11 | all copies or substantial portions of the Software.
12 |
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 | THE SOFTWARE.
--------------------------------------------------------------------------------
/samples/01_info.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Message - info
5 |
6 |
7 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
48 |
49 |
--------------------------------------------------------------------------------
/samples/05_modalbox_reuse.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Message - info
5 |
6 |
7 |
27 |
28 |
29 | Create once, use multiple times
30 |
31 |
32 |
33 |
';
96 | if (ok)
97 | inner += button(config.ok || "OK", true);
98 | if (cancel)
99 | inner += button(config.cancel || "Cancel", false);
100 | if (config.buttons){
101 | for (var i=0; i';
105 | box.innerHTML = inner;
106 |
107 | if (config.content){
108 | var node = config.content;
109 | if (typeof node == "string")
110 | node = document.getElementById(node);
111 | if (node.style.display == 'none')
112 | node.style.display = "";
113 | box.childNodes[config.title?1:0].appendChild(node);
114 | }
115 |
116 | box.onclick = function(e){
117 | e = e ||event;
118 | var source = e.target || e.srcElement;
119 | if (!source.className) source = source.parentNode;
120 | if (source.className == "dhtmlx_popup_button"){
121 | var result = source.getAttribute("result");
122 | result = (result == "true")||(result == "false"?false:result);
123 | callback(config, result);
124 | }
125 | };
126 | config.box = box;
127 | if (ok||cancel)
128 | _dhx_msg_cfg = config;
129 |
130 | return box;
131 | }
132 | function _createBox(config, ok, cancel){
133 | var box = config.tagName ? config : _boxStructure(config, ok, cancel);
134 |
135 | if (!config.hidden)
136 | modality(true);
137 | document.body.appendChild(box);
138 | var x = config.left||Math.abs(Math.floor(((window.innerWidth||document.documentElement.offsetWidth) - box.offsetWidth)/2));
139 | var y = config.top||Math.abs(Math.floor(((window.innerHeight||document.documentElement.offsetHeight) - box.offsetHeight)/2));
140 | if (config.position == "top")
141 | box.style.top = "-3px";
142 | else
143 | box.style.top = y+'px';
144 | box.style.left = x+'px';
145 | //necessary for IE only
146 | box.onkeydown = modal_key;
147 |
148 | box.focus();
149 | if (config.hidden)
150 | dhtmlx.modalbox.hide(box);
151 |
152 | return box;
153 | }
154 |
155 | function alertPopup(config){
156 | return _createBox(config, true, false);
157 | }
158 | function confirmPopup(config){
159 | return _createBox(config, true, true);
160 | }
161 | function boxPopup(config){
162 | return _createBox(config);
163 | }
164 | function box_params(text, type, callback){
165 | if (typeof text != "object"){
166 | if (typeof type == "function"){
167 | callback = type;
168 | type = "";
169 | }
170 | text = {text:text, type:type, callback:callback };
171 | }
172 | return text;
173 | }
174 | function params(text, type, expire, id){
175 | if (typeof text != "object")
176 | text = {text:text, type:type, expire:expire, id:id};
177 | text.id = text.id||t.uid();
178 | text.expire = text.expire||t.expire;
179 | return text;
180 | }
181 | dhtmlx.alert = function(){
182 | var text = box_params.apply(this, arguments);
183 | text.type = text.type || "confirm";
184 | return alertPopup(text);
185 | };
186 | dhtmlx.confirm = function(){
187 | var text = box_params.apply(this, arguments);
188 | text.type = text.type || "alert";
189 | return confirmPopup(text);
190 | };
191 | dhtmlx.modalbox = function(){
192 | var text = box_params.apply(this, arguments);
193 | text.type = text.type || "alert";
194 | return boxPopup(text);
195 | };
196 | dhtmlx.modalbox.hide = function(node){
197 | while (node && node.getAttribute && !node.getAttribute("dhxbox"))
198 | node = node.parentNode;
199 | if (node){
200 | node.parentNode.removeChild(node);
201 | modality(false);
202 | }
203 | };
204 | var t = dhtmlx.message = function(text, type, expire, id){
205 | text = params.apply(this, arguments);
206 | text.type = text.type||"info";
207 |
208 | var subtype = text.type.split("-")[0];
209 | switch (subtype){
210 | case "alert":
211 | return alertPopup(text);
212 | case "confirm":
213 | return confirmPopup(text);
214 | case "modalbox":
215 | return boxPopup(text);
216 | default:
217 | return info(text);
218 | break;
219 | }
220 | };
221 |
222 | t.seed = (new Date()).valueOf();
223 | t.uid = function(){return t.seed++;};
224 | t.expire = 4000;
225 | t.keyboard = true;
226 | t.position = "top";
227 | t.pull = {};
228 | t.timers = {};
229 |
230 | t.hideAll = function(){
231 | for (var key in t.pull)
232 | t.hide(key);
233 | };
234 | t.hide = function(id){
235 | var obj = t.pull[id];
236 | if (obj && obj.parentNode){
237 | window.setTimeout(function(){
238 | obj.parentNode.removeChild(obj);
239 | obj = null;
240 | },2000);
241 | obj.className+=" hidden";
242 |
243 | if(t.timers[id])
244 | window.clearTimeout(t.timers[id]);
245 | delete t.pull[id];
246 | }
247 | };
248 | })();
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | jsMessage
2 | =========
3 |
4 | Custom notifications, alerts, confirmations
5 |
6 | This library was extracted from code of [DHTMLX suite][dhtmlx].
7 |
8 | - Library can be used under terms of [MIT license][mit] (basically **free**).
9 | - Only **4kb** gzipped, without external dependencies.
10 | - Works in FF, Chrome, Safari (including iPhone), Opera, IE7+
11 |
12 | Live samples can be checked at [http://dhtmlx.github.com/message/][message]
13 |
14 | Supported message types
15 | -----------------------
16 |
17 | jsMessage offers 4 variations at your disposal:
18 |
19 | - alert
20 | - confirm
21 | - notification ( message )
22 | - modalbox
23 |
24 | How to use
25 | -----------
26 |
27 | The type (subtype) of the message window is specified through the parameter type. The default value is "message".
28 |
29 | ```javascript
30 | dhtmlx.message({
31 | type:"confirm-warning",
32 | text:"Are you sure you want to do it?"
33 | })
34 | ```
35 |
36 | or, you can use separate methods
37 |
38 | ```javascript
39 | dhtmlx.confirm({
40 | title:"Confirm",
41 | text:"Continue?"
42 | });
43 | //or
44 | dhtmlx.alert({
45 | title:"Alert",
46 | type:"alert-error",
47 | text:"You can do this"
48 | });
49 | //or
50 | dhtmlx.modalbox({
51 | title:"Settings",
52 | text:"Abstract popup"
53 | });
54 | ```
55 |
56 | Styling
57 | -------
58 | For any type of the message window you can define a custom style to achieve the desired look.
59 | Generally, the appropriate css class is specified through the parameter type: you define a css class and set the parameter to its name.
60 |
61 | ```html
62 |
69 |
72 | ```
73 |
74 | Options
75 | ---------
76 |
77 | ### Alert
78 |
79 | - title - (string) the text of the header (by default, 'Alert').
80 | - type - the subtype of the window or a custom css class. The default value for the window - 'alert'.
81 | - text - (string) the text of the window body.
82 | - ok - (string) the text of the 'Ok' button.
83 | - callback - (function) the function called on button click
84 | - position - for now support only one value "top", any other value will result in center align
85 | - width - width of the modal box ( samples "100px", "50%")
86 | - height - height of the modal box
87 |
88 | **Full form**
89 |
90 | ```javascript
91 | dhtmlx.message({
92 | title: "Close",
93 | type: "alert",
94 | text: "You can't close this window!",
95 | callback: function() {dhtmlx.alert("Test alert");}
96 | })
97 | //or
98 | dhtmlx.alert({
99 | text: "Download is completed.",
100 | callback: function() {dhtmlx.alert("Test alert");}
101 | })
102 | ```
103 |
104 | **Short form**
105 |
106 | ```javascript
107 | dhtmlx.alert("someText");
108 | ```
109 |
110 | Both alert and confirm blocks keyboard input while active. Pressing SPACE or ENTER will close message with positive result. Pressing ESC will close message with negative result. (you can use dhtmlx.message.keyboard = false; to disable this behavior)
111 |
112 | ### Confirm
113 |
114 | - title - (string) the text of the header (by default, 'Alert').
115 | - type - the subtype of the window or a custom css class
116 | - text - (string) the text of the window body.
117 | - ok - (string) the text of the 'Ok' button.
118 | - cancel - (string) the text of the 'Cancel' button.
119 | - callback - (function) the function called on button click. Receives 'true' or 'false' as the parameter (subject to the clicked button).
120 | - position - for now support only one value "top", any other value will result in center align
121 | - width - width of the modal box ( samples "100px", "50%")
122 | - height - height of the modal box
123 |
124 | **Full form**
125 |
126 | ```javascript
127 | dhtmlx.message({
128 | type:"confirm",
129 | text: "Continue?",
130 | callback: function() {dhtmlx.confirm("Test confirm");}
131 | });
132 | //or
133 | dhtmlx.confirm({
134 | title: "Close",
135 | type:"confirm-warning",
136 | text: "Are you sure you want to do it?",
137 | callback: function() {dhtmlx.confirm("Test confirm");}
138 | });
139 | ```
140 |
141 | **Short form**
142 |
143 | ```javascript
144 | dhtmlx.confirm("ConfirmText");
145 | ```
146 |
147 | ### ModalBox
148 |
149 | - title - (string) the text of the header (by default, 'Alert').
150 | - type - the subtype of the window or a custom css class
151 | - text - (string) the text of the window body.
152 | - ok - (string) the text of the 'Ok' button.
153 | - cancel - (string) the text of the 'Cancel' button.
154 | - callback - (function) the function called on button click. Receives 'true' or 'false' as the parameter (subject to the clicked button).
155 | - buttons - array of labels, each one will be converted to the button ( much like multi-button confirm ). Callback function will receive index of pressed button ( 0 - for first button, 1 - for second button and etc. )
156 | - position - for now support only one value "top", any other value will result in center align
157 | - width - width of the modal box ( samples "100px", "50%")
158 | - height - height of the modal box
159 | - content - can be used instead of text, defines html element (or its ID) which will be shown inside of a popup
160 |
161 | **Examples**
162 |
163 | ```javascript
164 | dhtmlx.modalbox({
165 | title:"Settings"
166 | text: " ... html code here... ",
167 | buttons:["Save", "Defaults", "Cancel"],
168 | callback:process_result
169 | });
170 | ```
171 |
172 | function returns the html container of the box which can be used for some actions
173 |
174 | ```javascript
175 | var box = dhtmlx.modalbox(...);
176 | box.getElementsByTagName("input")[0].focus();
177 | ...
178 | dhtmlx.modalbox.hide(box); //hide popup
179 | ```
180 |
181 | #### Closing modal box
182 |
183 | There are 3 way to close modal box
184 |
185 | - call dhtmlx.modalbox.hide(box) - where "box" is result of dhtmlx.modalbox command
186 | - call dhtmlx.modalbox.hide(node) - where node - any html node in the box (allows to create "close" links easily)
187 | - click on any button, which was defined through "buttons" property
188 |
189 | ```javascript
190 | var box = dhtmlx.modalbox({
191 | text:"Click to close"
192 | });
193 | ```
194 |
195 |
196 | #### Custom buttons
197 |
198 | You can place a custom button in the popup, which is styled exactly as default message buttons. To do so you need to place the next html snippet
199 |
200 | ```javascript
201 | var box = dhtmlx.modalbox({
202 | text:""
203 | });
204 | ```
205 |
206 | #### Content reusage
207 |
208 | Instead of setting html text you can place any html container on the page in the modalbox
209 |
210 | ```html
211 |