alertify-rails 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +27 -10
- data/alertify-rails.gemspec +2 -2
- data/lib/alertify-rails.rb +1 -6
- data/lib/alertify/rails.rb +9 -0
- data/lib/alertify/rails/engine.rb +9 -0
- data/lib/alertify/rails/railtie.rb +14 -0
- data/lib/alertify/rails/version.rb +8 -0
- data/vendor/assets/javascripts/alertify.js +459 -385
- data/vendor/assets/stylesheets/alertify.core.css.scss +95 -0
- data/vendor/assets/stylesheets/alertify.css.scss +5 -0
- data/vendor/assets/stylesheets/alertify.default.css.scss +74 -0
- metadata +11 -7
- data/lib/alertify/engine.rb +0 -5
- data/lib/alertify/railtie.rb +0 -12
- data/lib/alertify/version.rb +0 -7
- data/vendor/assets/stylesheets/alertify.css +0 -155
data/README.md
CHANGED
@@ -1,24 +1,38 @@
|
|
1
|
-
#
|
1
|
+
# alertify-rails
|
2
2
|
|
3
|
-
|
3
|
+
This gem provides [alertify.js](http://fabien-d.github.com/alertify.js) (v0.2.12) for Rails.
|
4
4
|
|
5
|
-
## Installation
|
6
5
|
|
7
|
-
|
6
|
+
## Installation
|
8
7
|
|
9
|
-
|
8
|
+
In your Gemfile:
|
10
9
|
|
11
|
-
|
10
|
+
```ruby
|
11
|
+
gem 'alertify-rails'
|
12
|
+
```
|
12
13
|
|
13
|
-
|
14
|
+
or system wide:
|
14
15
|
|
15
|
-
|
16
|
+
```console
|
17
|
+
$ gem install alertify-rails
|
18
|
+
```
|
16
19
|
|
17
|
-
$ gem install alertify-rails
|
18
20
|
|
19
21
|
## Usage
|
20
22
|
|
21
|
-
|
23
|
+
The alertify files will be added to the asset pipeline and available for you to use. Add the following line to `app/assets/javascripts/application.js`
|
24
|
+
|
25
|
+
```javascript
|
26
|
+
//= require alertify
|
27
|
+
```
|
28
|
+
|
29
|
+
In order to get the CSS, add the following line to `app/assets/stylesheets/application.css.scss`
|
30
|
+
|
31
|
+
```css
|
32
|
+
/*
|
33
|
+
*= require alertify
|
34
|
+
*/
|
35
|
+
```
|
22
36
|
|
23
37
|
## Contributing
|
24
38
|
|
@@ -27,3 +41,6 @@ TODO: Write usage instructions here
|
|
27
41
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
42
|
4. Push to the branch (`git push origin my-new-feature`)
|
29
43
|
5. Create new Pull Request
|
44
|
+
|
45
|
+
Copyright © 2012 Rudolf Schmidt, released under the MIT license
|
46
|
+
|
data/alertify-rails.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
lib = File.expand_path('../lib', __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'alertify/version'
|
4
|
+
require 'alertify/rails/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |gem|
|
7
7
|
gem.name = "alertify-rails"
|
@@ -9,7 +9,7 @@ Gem::Specification.new do |gem|
|
|
9
9
|
gem.authors = ["Rudolf Schmidt"]
|
10
10
|
|
11
11
|
gem.description = %q{Use Alertify.js with Rails 3}
|
12
|
-
gem.summary = %q{This gem provides the Alertify.js
|
12
|
+
gem.summary = %q{This gem provides the Alertify.js driver for Rails 3 applications}
|
13
13
|
gem.homepage = ""
|
14
14
|
|
15
15
|
gem.files = `git ls-files`.split($/)
|
data/lib/alertify-rails.rb
CHANGED
@@ -0,0 +1,14 @@
|
|
1
|
+
module Alertify #:nodoc:
|
2
|
+
module Rails #:nodoc:
|
3
|
+
|
4
|
+
class Railtie < ::Rails::Railtie
|
5
|
+
config.before_configuration do
|
6
|
+
if config.action_view.javascript_expansions
|
7
|
+
config.action_view.javascript_expansions[:defaults] << 'alertify'
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
@@ -1,393 +1,467 @@
|
|
1
1
|
/**
|
2
|
-
*
|
2
|
+
* alertify
|
3
3
|
* An unobtrusive customizable JavaScript notification system
|
4
4
|
*
|
5
5
|
* @author Fabien Doiron <fabien.doiron@gmail.com>
|
6
6
|
* @copyright Fabien Doiron 2012
|
7
|
-
* @license
|
7
|
+
* @license MIT <http://opensource.org/licenses/mit-license.php>
|
8
8
|
* @link http://www.github.com/fabien-d
|
9
|
-
* @module
|
10
|
-
* @version 0.
|
9
|
+
* @module alertify
|
10
|
+
* @version 0.2.12
|
11
11
|
*/
|
12
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
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
}
|
392
|
-
|
393
|
-
|
13
|
+
/*global define*/
|
14
|
+
(function (global, undefined) {
|
15
|
+
"use strict";
|
16
|
+
|
17
|
+
var document = global.document,
|
18
|
+
Alertify;
|
19
|
+
|
20
|
+
Alertify = function () {
|
21
|
+
|
22
|
+
var _alertify = {},
|
23
|
+
dialogs = {},
|
24
|
+
isopen = false,
|
25
|
+
keys = { ENTER: 13, ESC: 27, SPACE: 32 },
|
26
|
+
queue = [],
|
27
|
+
$, elCallee, elCover, elDialog, elLog;
|
28
|
+
|
29
|
+
/**
|
30
|
+
* Markup pieces
|
31
|
+
* @type {Object}
|
32
|
+
*/
|
33
|
+
dialogs = {
|
34
|
+
buttons : {
|
35
|
+
holder : "<nav class=\"alertify-buttons\">{{buttons}}</nav>",
|
36
|
+
submit : "<button type=\"submit\" class=\"alertify-button alertify-button-ok\" id=\"alertify-ok\" />{{ok}}</button>",
|
37
|
+
ok : "<a href=\"#\" class=\"alertify-button alertify-button-ok\" id=\"alertify-ok\">{{ok}}</a>",
|
38
|
+
cancel : "<a href=\"#\" class=\"alertify-button alertify-button-cancel\" id=\"alertify-cancel\">{{cancel}}</a>"
|
39
|
+
},
|
40
|
+
input : "<input type=\"text\" class=\"alertify-text\" id=\"alertify-text\">",
|
41
|
+
message : "<p class=\"alertify-message\">{{message}}</p>",
|
42
|
+
log : "<article class=\"alertify-log{{class}}\">{{message}}</article>"
|
43
|
+
};
|
44
|
+
|
45
|
+
/**
|
46
|
+
* Shorthand for document.getElementById()
|
47
|
+
*
|
48
|
+
* @param {String} id A specific element ID
|
49
|
+
* @return {Object} HTML element
|
50
|
+
*/
|
51
|
+
$ = function (id) {
|
52
|
+
return document.getElementById(id);
|
53
|
+
};
|
54
|
+
|
55
|
+
/**
|
56
|
+
* Alertify private object
|
57
|
+
* @type {Object}
|
58
|
+
*/
|
59
|
+
_alertify = {
|
60
|
+
|
61
|
+
/**
|
62
|
+
* Labels object
|
63
|
+
* @type {Object}
|
64
|
+
*/
|
65
|
+
labels : {
|
66
|
+
ok : "OK",
|
67
|
+
cancel : "Cancel"
|
68
|
+
},
|
69
|
+
|
70
|
+
/**
|
71
|
+
* Delay number
|
72
|
+
* @type {Number}
|
73
|
+
*/
|
74
|
+
delay : 5000,
|
75
|
+
|
76
|
+
/**
|
77
|
+
* Set the proper button click events
|
78
|
+
*
|
79
|
+
* @param {Function} fn [Optional] Callback function
|
80
|
+
*
|
81
|
+
* @return {undefined}
|
82
|
+
*/
|
83
|
+
addListeners : function (fn) {
|
84
|
+
var btnReset = $("alertify-resetFocus"),
|
85
|
+
btnOK = $("alertify-ok") || undefined,
|
86
|
+
btnCancel = $("alertify-cancel") || undefined,
|
87
|
+
input = $("alertify-text") || undefined,
|
88
|
+
form = $("alertify-form") || undefined,
|
89
|
+
hasOK = (typeof btnOK !== "undefined"),
|
90
|
+
hasCancel = (typeof btnCancel !== "undefined"),
|
91
|
+
hasInput = (typeof input !== "undefined"),
|
92
|
+
val = "",
|
93
|
+
self = this,
|
94
|
+
ok, cancel, common, key, reset;
|
95
|
+
|
96
|
+
// ok event handler
|
97
|
+
ok = function (event) {
|
98
|
+
if (typeof event.preventDefault !== "undefined") event.preventDefault();
|
99
|
+
common(event);
|
100
|
+
if (typeof input !== "undefined") val = input.value;
|
101
|
+
if (typeof fn === "function") fn(true, val);
|
102
|
+
};
|
103
|
+
|
104
|
+
// cancel event handler
|
105
|
+
cancel = function (event) {
|
106
|
+
if (typeof event.preventDefault !== "undefined") event.preventDefault();
|
107
|
+
common(event);
|
108
|
+
if (typeof fn === "function") fn(false);
|
109
|
+
};
|
110
|
+
|
111
|
+
// common event handler (keyup, ok and cancel)
|
112
|
+
common = function (event) {
|
113
|
+
self.hide();
|
114
|
+
self.unbind(document.body, "keyup", key);
|
115
|
+
self.unbind(btnReset, "focus", reset);
|
116
|
+
if (hasInput) self.unbind(form, "submit", ok);
|
117
|
+
if (hasOK) self.unbind(btnOK, "click", ok);
|
118
|
+
if (hasCancel) self.unbind(btnCancel, "click", cancel);
|
119
|
+
};
|
120
|
+
|
121
|
+
// keyup handler
|
122
|
+
key = function (event) {
|
123
|
+
var keyCode = event.keyCode;
|
124
|
+
if (keyCode === keys.SPACE && !hasInput) ok(event);
|
125
|
+
if (keyCode === keys.ESC && hasCancel) cancel(event);
|
126
|
+
};
|
127
|
+
|
128
|
+
// reset focus to first item in the dialog
|
129
|
+
reset = function (event) {
|
130
|
+
if (hasInput) input.focus();
|
131
|
+
else if (hasCancel) btnCancel.focus();
|
132
|
+
else btnOK.focus();
|
133
|
+
};
|
134
|
+
|
135
|
+
// handle reset focus link
|
136
|
+
// this ensures that the keyboard focus does not
|
137
|
+
// ever leave the dialog box until an action has
|
138
|
+
// been taken
|
139
|
+
this.bind(btnReset, "focus", reset);
|
140
|
+
// handle OK click
|
141
|
+
if (hasOK) this.bind(btnOK, "click", ok);
|
142
|
+
// handle Cancel click
|
143
|
+
if (hasCancel) this.bind(btnCancel, "click", cancel);
|
144
|
+
// listen for keys, Cancel => ESC
|
145
|
+
this.bind(document.body, "keyup", key);
|
146
|
+
// bind form submit
|
147
|
+
if (hasInput) this.bind(form, "submit", ok);
|
148
|
+
// set focus on OK button or the input text
|
149
|
+
global.setTimeout(function () {
|
150
|
+
if (input) {
|
151
|
+
input.focus();
|
152
|
+
input.select();
|
153
|
+
}
|
154
|
+
else btnOK.focus();
|
155
|
+
}, 50);
|
156
|
+
},
|
157
|
+
|
158
|
+
/**
|
159
|
+
* Bind events to elements
|
160
|
+
*
|
161
|
+
* @param {Object} el HTML Object
|
162
|
+
* @param {Event} event Event to attach to element
|
163
|
+
* @param {Function} fn Callback function
|
164
|
+
*
|
165
|
+
* @return {undefined}
|
166
|
+
*/
|
167
|
+
bind : function (el, event, fn) {
|
168
|
+
if (typeof el.addEventListener === "function") {
|
169
|
+
el.addEventListener(event, fn, false);
|
170
|
+
} else if (el.attachEvent) {
|
171
|
+
el.attachEvent("on" + event, fn);
|
172
|
+
}
|
173
|
+
},
|
174
|
+
|
175
|
+
/**
|
176
|
+
* Build the proper message box
|
177
|
+
*
|
178
|
+
* @param {Object} item Current object in the queue
|
179
|
+
*
|
180
|
+
* @return {String} An HTML string of the message box
|
181
|
+
*/
|
182
|
+
build : function (item) {
|
183
|
+
var html = "",
|
184
|
+
type = item.type,
|
185
|
+
message = item.message;
|
186
|
+
|
187
|
+
html += "<div class=\"alertify-dialog\">";
|
188
|
+
|
189
|
+
if (type === "prompt") html += "<form id=\"alertify-form\">";
|
190
|
+
|
191
|
+
html += "<article class=\"alertify-inner\">";
|
192
|
+
html += dialogs.message.replace("{{message}}", message);
|
193
|
+
|
194
|
+
if (type === "prompt") html += dialogs.input;
|
195
|
+
|
196
|
+
html += dialogs.buttons.holder;
|
197
|
+
html += "</article>";
|
198
|
+
|
199
|
+
if (type === "prompt") html += "</form>";
|
200
|
+
|
201
|
+
html += "<a id=\"alertify-resetFocus\" class=\"alertify-resetFocus\" href=\"#\">Reset Focus</a>";
|
202
|
+
html += "</div>";
|
203
|
+
|
204
|
+
switch (type) {
|
205
|
+
case "confirm":
|
206
|
+
html = html.replace("{{buttons}}", dialogs.buttons.cancel + dialogs.buttons.ok);
|
207
|
+
html = html.replace("{{ok}}", this.labels.ok).replace("{{cancel}}", this.labels.cancel);
|
208
|
+
break;
|
209
|
+
case "prompt":
|
210
|
+
html = html.replace("{{buttons}}", dialogs.buttons.cancel + dialogs.buttons.submit);
|
211
|
+
html = html.replace("{{ok}}", this.labels.ok).replace("{{cancel}}", this.labels.cancel);
|
212
|
+
break;
|
213
|
+
case "alert":
|
214
|
+
html = html.replace("{{buttons}}", dialogs.buttons.ok);
|
215
|
+
html = html.replace("{{ok}}", this.labels.ok);
|
216
|
+
break;
|
217
|
+
default:
|
218
|
+
break;
|
219
|
+
}
|
220
|
+
|
221
|
+
elDialog.className = "alertify alertify-show alertify-" + type;
|
222
|
+
elCover.className = "alertify-cover";
|
223
|
+
return html;
|
224
|
+
},
|
225
|
+
|
226
|
+
/**
|
227
|
+
* Close the log messages
|
228
|
+
*
|
229
|
+
* @param {Object} elem HTML Element of log message to close
|
230
|
+
* @param {Number} wait [optional] Time (in ms) to wait before automatically hiding the message
|
231
|
+
*
|
232
|
+
* @return {undefined}
|
233
|
+
*/
|
234
|
+
close : function (elem, wait) {
|
235
|
+
var timer = (wait && !isNaN(wait)) ? +wait : this.delay; // Unary Plus: +"2" === 2
|
236
|
+
this.bind(elem, "click", function () {
|
237
|
+
elLog.removeChild(elem);
|
238
|
+
});
|
239
|
+
setTimeout(function () {
|
240
|
+
if (typeof elem !== "undefined" && elem.parentNode === elLog) elLog.removeChild(elem);
|
241
|
+
}, timer);
|
242
|
+
},
|
243
|
+
|
244
|
+
/**
|
245
|
+
* Create a dialog box
|
246
|
+
*
|
247
|
+
* @param {String} message The message passed from the callee
|
248
|
+
* @param {String} type Type of dialog to create
|
249
|
+
* @param {Function} fn [Optional] Callback function
|
250
|
+
* @param {String} placeholder [Optional] Default value for prompt input field
|
251
|
+
*
|
252
|
+
* @return {Object}
|
253
|
+
*/
|
254
|
+
dialog : function (message, type, fn, placeholder) {
|
255
|
+
// set the current active element
|
256
|
+
// this allows the keyboard focus to be resetted
|
257
|
+
// after the dialog box is closed
|
258
|
+
elCallee = document.activeElement;
|
259
|
+
// check to ensure the alertify dialog element
|
260
|
+
// has been successfully created
|
261
|
+
var check = function () {
|
262
|
+
if (elDialog && elDialog.scrollTop !== null) return;
|
263
|
+
else check();
|
264
|
+
};
|
265
|
+
// error catching
|
266
|
+
if (typeof message !== "string") throw new Error("message must be a string");
|
267
|
+
if (typeof type !== "string") throw new Error("type must be a string");
|
268
|
+
if (typeof fn !== "undefined" && typeof fn !== "function") throw new Error("fn must be a function");
|
269
|
+
// initialize alertify if it hasn't already been done
|
270
|
+
if (typeof this.init === "function") {
|
271
|
+
this.init();
|
272
|
+
check();
|
273
|
+
}
|
274
|
+
|
275
|
+
queue.push({ type: type, message: message, callback: fn, placeholder: placeholder });
|
276
|
+
if (!isopen) this.setup();
|
277
|
+
|
278
|
+
return this;
|
279
|
+
},
|
280
|
+
|
281
|
+
/**
|
282
|
+
* Extend the log method to create custom methods
|
283
|
+
*
|
284
|
+
* @param {String} type Custom method name
|
285
|
+
*
|
286
|
+
* @return {Function}
|
287
|
+
*/
|
288
|
+
extend : function (type) {
|
289
|
+
return function (message, wait) { this.log(message, type, wait); };
|
290
|
+
},
|
291
|
+
|
292
|
+
/**
|
293
|
+
* Hide the dialog and rest to defaults
|
294
|
+
*
|
295
|
+
* @return {undefined}
|
296
|
+
*/
|
297
|
+
hide : function () {
|
298
|
+
// remove reference from queue
|
299
|
+
queue.splice(0,1);
|
300
|
+
// if items remaining in the queue
|
301
|
+
if (queue.length > 0) this.setup();
|
302
|
+
else {
|
303
|
+
isopen = false;
|
304
|
+
elDialog.className = "alertify alertify-hide alertify-hidden";
|
305
|
+
elCover.className = "alertify-cover alertify-hidden";
|
306
|
+
// set focus to the last element or body
|
307
|
+
// after the dialog is closed
|
308
|
+
elCallee.focus();
|
309
|
+
}
|
310
|
+
},
|
311
|
+
|
312
|
+
/**
|
313
|
+
* Initialize Alertify
|
314
|
+
* Create the 2 main elements
|
315
|
+
*
|
316
|
+
* @return {undefined}
|
317
|
+
*/
|
318
|
+
init : function () {
|
319
|
+
// ensure legacy browsers support html5 tags
|
320
|
+
document.createElement("nav");
|
321
|
+
document.createElement("article");
|
322
|
+
document.createElement("section");
|
323
|
+
// cover
|
324
|
+
elCover = document.createElement("div");
|
325
|
+
elCover.setAttribute("id", "alertify-cover");
|
326
|
+
elCover.className = "alertify-cover alertify-hidden";
|
327
|
+
document.body.appendChild(elCover);
|
328
|
+
// main element
|
329
|
+
elDialog = document.createElement("section");
|
330
|
+
elDialog.setAttribute("id", "alertify");
|
331
|
+
elDialog.className = "alertify alertify-hidden";
|
332
|
+
document.body.appendChild(elDialog);
|
333
|
+
// log element
|
334
|
+
elLog = document.createElement("section");
|
335
|
+
elLog.setAttribute("id", "alertify-logs");
|
336
|
+
elLog.className = "alertify-logs";
|
337
|
+
document.body.appendChild(elLog);
|
338
|
+
// set tabindex attribute on body element
|
339
|
+
// this allows script to give it focus
|
340
|
+
// after the dialog is closed
|
341
|
+
document.body.setAttribute("tabindex", "0");
|
342
|
+
// clean up init method
|
343
|
+
delete this.init;
|
344
|
+
},
|
345
|
+
|
346
|
+
/**
|
347
|
+
* Show a new log message box
|
348
|
+
*
|
349
|
+
* @param {String} message The message passed from the callee
|
350
|
+
* @param {String} type [Optional] Optional type of log message
|
351
|
+
* @param {Number} wait [Optional] Time (in ms) to wait before auto-hiding the log
|
352
|
+
*
|
353
|
+
* @return {Object}
|
354
|
+
*/
|
355
|
+
log : function (message, type, wait) {
|
356
|
+
// check to ensure the alertify dialog element
|
357
|
+
// has been successfully created
|
358
|
+
var check = function () {
|
359
|
+
if (elLog && elLog.scrollTop !== null) return;
|
360
|
+
else check();
|
361
|
+
};
|
362
|
+
// initialize alertify if it hasn't already been done
|
363
|
+
if (typeof this.init === "function") {
|
364
|
+
this.init();
|
365
|
+
check();
|
366
|
+
}
|
367
|
+
this.notify(message, type, wait);
|
368
|
+
return this;
|
369
|
+
},
|
370
|
+
|
371
|
+
/**
|
372
|
+
* Add new log message
|
373
|
+
* If a type is passed, a class name "alertify-log-{type}" will get added.
|
374
|
+
* This allows for custom look and feel for various types of notifications.
|
375
|
+
*
|
376
|
+
* @param {String} message The message passed from the callee
|
377
|
+
* @param {String} type [Optional] Type of log message
|
378
|
+
* @param {Number} wait [Optional] Time (in ms) to wait before auto-hiding
|
379
|
+
*
|
380
|
+
* @return {undefined}
|
381
|
+
*/
|
382
|
+
notify : function (message, type, wait) {
|
383
|
+
var log = document.createElement("article");
|
384
|
+
log.className = "alertify-log" + ((typeof type === "string" && type !== "") ? " alertify-log-" + type : "");
|
385
|
+
log.innerHTML = message;
|
386
|
+
// prepend child
|
387
|
+
elLog.insertBefore(log, elLog.firstChild);
|
388
|
+
// triggers the CSS animation
|
389
|
+
setTimeout(function() { log.className = log.className + " alertify-log-show"; }, 50);
|
390
|
+
this.close(log, wait);
|
391
|
+
},
|
392
|
+
|
393
|
+
/**
|
394
|
+
* Set properties
|
395
|
+
*
|
396
|
+
* @param {Object} args Passing parameters
|
397
|
+
*
|
398
|
+
* @return {undefined}
|
399
|
+
*/
|
400
|
+
set : function (args) {
|
401
|
+
var k;
|
402
|
+
// error catching
|
403
|
+
if (typeof args !== "object" && args instanceof Array) throw new Error("args must be an object");
|
404
|
+
// set parameters
|
405
|
+
for (k in args) {
|
406
|
+
if (args.hasOwnProperty(k)) {
|
407
|
+
this[k] = args[k];
|
408
|
+
}
|
409
|
+
}
|
410
|
+
},
|
411
|
+
|
412
|
+
/**
|
413
|
+
* Initiate all the required pieces for the dialog box
|
414
|
+
*
|
415
|
+
* @return {undefined}
|
416
|
+
*/
|
417
|
+
setup : function () {
|
418
|
+
var item = queue[0];
|
419
|
+
|
420
|
+
isopen = true;
|
421
|
+
elDialog.innerHTML = this.build(item);
|
422
|
+
if (typeof item.placeholder === "string") $("alertify-text").value = item.placeholder;
|
423
|
+
this.addListeners(item.callback);
|
424
|
+
},
|
425
|
+
|
426
|
+
/**
|
427
|
+
* Unbind events to elements
|
428
|
+
*
|
429
|
+
* @param {Object} el HTML Object
|
430
|
+
* @param {Event} event Event to detach to element
|
431
|
+
* @param {Function} fn Callback function
|
432
|
+
*
|
433
|
+
* @return {undefined}
|
434
|
+
*/
|
435
|
+
unbind : function (el, event, fn) {
|
436
|
+
if (typeof el.removeEventListener === "function") {
|
437
|
+
el.removeEventListener(event, fn, false);
|
438
|
+
} else if (el.detachEvent) {
|
439
|
+
el.detachEvent("on" + event, fn);
|
440
|
+
}
|
441
|
+
}
|
442
|
+
};
|
443
|
+
|
444
|
+
return {
|
445
|
+
alert : function (message, fn) { _alertify.dialog(message, "alert", fn); return this; },
|
446
|
+
confirm : function (message, fn) { _alertify.dialog(message, "confirm", fn); return this; },
|
447
|
+
extend : _alertify.extend,
|
448
|
+
init : _alertify.init,
|
449
|
+
log : function (message, type, wait) { _alertify.log(message, type, wait); return this; },
|
450
|
+
prompt : function (message, fn, placeholder) { _alertify.dialog(message, "prompt", fn, placeholder); return this; },
|
451
|
+
success : function (message, wait) { _alertify.log(message, "success", wait); return this; },
|
452
|
+
error : function (message, wait) { _alertify.log(message, "error", wait); return this; },
|
453
|
+
set : function (args) { _alertify.set(args); },
|
454
|
+
labels : _alertify.labels
|
455
|
+
};
|
456
|
+
};
|
457
|
+
|
458
|
+
// AMD and window support
|
459
|
+
if (typeof define === "function") {
|
460
|
+
define([], function () { return new Alertify(); });
|
461
|
+
} else {
|
462
|
+
if (typeof global.alertify === "undefined") {
|
463
|
+
global.alertify = new Alertify();
|
464
|
+
}
|
465
|
+
}
|
466
|
+
|
467
|
+
}(this));
|