-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathjquery-confirms.js
112 lines (100 loc) · 3.54 KB
/
jquery-confirms.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
/*!
jQuery Confirms - v0.0.1
© 2013 - Caleb Troughton
MIT Licensed: http://opensource.org/licenses/MIT
For more information, visit https://github.com/imakewebthings/jquery-confirms
*/
(function(undefined) {
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
return define(['jquery'], function($) {
return factory($, root);
});
}
return factory(root.jQuery, root);
})(this, function($, window) {
var fillTemplateText = function($template, options) {
$template.find(options.promptSelector).text(options.promptText);
$template.find(options.yesSelector).text(options.yesText);
$template.find(options.noSelector).text(options.noText);
};
var setSubjectData = function($element, options) {
$element.data('confirms-event', options.event);
};
var bindSubjectEvents = function($element, $confirmation, options) {
var eventName = options.event + '.confirms';
$element.on(eventName, function(event) {
$element.after($confirmation).detach();
event.preventDefault();
});
};
var bindConfirmationEvents = function($element, $confirmation, options) {
var $yes = $confirmation.find(options.yesSelector);
var $no = $confirmation.find(options.noSelector);
var element = $element[0];
var eventName = 'click.confirms';
var restoreOriginal = function(event) {
$confirmation.after($element).detach();
event.preventDefault();
};
$yes.on(eventName, restoreOriginal)
.on(eventName, $.proxy(options.onYes, element));
$no.on(eventName, restoreOriginal)
.on(eventName, $.proxy(options.onNo, element));
};
var methods = {
init: function(userOptions) {
this.each(function() {
var $element = $(this);
var dataOptions = {
event: $element.data('confirms-event'),
promptText: $element.data('confirms-prompt-text'),
yesText: $element.data('confirms-yes-text'),
noText: $element.data('confirms-no-text')
};
var options = $.extend(
{},
$.fn.confirms.defaults,
dataOptions,
userOptions
);
var $confirmation = $(options.confirmsTemplate);
fillTemplateText($confirmation, options);
setSubjectData($element, options);
bindSubjectEvents($element, $confirmation, options);
bindConfirmationEvents($element, $confirmation, options);
});
}
};
$.fn.confirms = function() {
var method = arguments[0];
var args = Array.prototype.slice.call(arguments, 1);
if (methods[method]) {
return methods[method].apply(this, args);
}
return methods.init.call(this, method);
};
$.fn.confirms.defaults = {
event: 'click',
promptText: 'Are you sure?',
yesText: 'Yes',
noText: 'No',
onYes: function(event) {
var $this = $(this);
var originalEvent = $this.data('confirms-event');
$this.unbind('.confirms');
if ($this.is('a') && originalEvent === 'click') {
window.location.href = this.href;
}
else {
$this.trigger(originalEvent);
}
},
onNo: $.noop,
confirmsTemplate: '<div class="confirms"><p class="confirms-prompt"></p><a class="confirms-yes" href="#"></a><a class="confirms-no" href="#"></a></div>',
promptSelector: '.confirms-prompt',
yesSelector: '.confirms-yes',
noSelector: '.confirms-no'
};
});
})();