-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.ui.confirmButton.js
More file actions
161 lines (143 loc) · 3.92 KB
/
jquery.ui.confirmButton.js
File metadata and controls
161 lines (143 loc) · 3.92 KB
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
/**
*
* JQuery UI Confirm Button plugin
*
* Author: Stephen Gregory
* Date: 10/19/2011
*
* This plugin expands the functionality of the jquery button plugin to make it modal. This provides a simple mechanism
* for hiding ejection levers.
*
* There are two modes 'normal' and 'confirm'.
*
* In normal mode clicking the button changs the button to 'confirm' mode.
* In confirm mode clicking the button triggers a 'confirmedclick' event. Clicking elsewhere on the screen
* will cause the button to revert to normal mode.
*
**/
(function($){
$.widget('ui.confirmButton',{
options: {
confirmStyle: 'ui-confirm',
normalStyle : 'ui-confirm-normal',
button: {},
confirmLabel: 'Confirm'
},
//
// Set up the widget
//
_create: function(){
var self = this,
o = self.options,
el = self.element;
this.current_mode = 'normal';
el.button(o.button);
el.addClass(this.options.normalStyle);
if(typeof o.button.label === 'undefined' ){
self.originalLabel = "";
}else{
self.originalLabel = o.button.label;
}
$(document).bind('click',this, this._click_handler );
},
//
// Options
//
// button options should be set via .button()
//
_setOption: function(option, value){
if(option == 'confirmLabel'){
this.options.confirmLabel =value;
if(this.current_mode == 'confirm'){
this.element.button('option', 'label', value);
}
}
if(option == 'confirmStyle'){
var oldStyle = this.options.confirmStyle;
this.options.confirmStyle = value;
if(this.current_mode === 'confirm'){
this.element.removeStyle(oldStyle);
this.element.addStyle(value);
}
}
if(option == 'normalStyle'){
var oldNormalStyle = this.options.normalStyle;
this.options.normalStyle = value;
if(this.current_mode === 'normal'){
this.element.removeStyle(oldNormalStyle);
this.element.addStyle(value);
}
}
},
//
// Destroy object
//
destroy: function(){
this.el.unbind('click');
$(document).unbind('click', this._click_handler);
},
//
// transition back to normal mode
//
_back_to_normal: function(){
var self = this;
self.mode('normal');
},
// transition animation to next state
//
_transition: function(self, targetmode, trans){
self.current_mode = 'transition';
var el = self.element;
el.effect('slide',{ direction: "left",mode: 'hide' }, 250, function(){
el.removeClass(trans.remove );
el.button('option', 'label',targetmode === 'normal'? self.originalLabel : self.options.confirmLabel);
el.effect('slide',{ direction: "left", mode: 'show' }, 250, function(){
self.current_mode = targetmode;
el.trigger('modechanged', self.current_mode);
el.addClass(trans.add, 250);
});
});
},
//
// Wrap click events
//
_click_handler: function(event){
var self = event.data;
if(self.element[0] !== event.originalTarget && self.element[0] !== event.srcElement && self.element[0] !== event.target ){
if(self.current_mode === 'confirm'){
self._back_to_normal();
}
return;
}
if(self.current_mode === 'normal'){
self.mode('confirm');
}else if (self.current_mode === 'confirm'){
self.element.trigger('confirmedclick');
}
},
//
// set/get the mode
//
// Will set the mode to the new value
//
//
mode: function(value){
result = this.current_mode;
if (typeof value !== 'undefined'){
if(value != 'confirm' && value != 'normal' ){
throw "Unknown mode value: "+value+". Valid values are 'confirm' or 'normal'";
}
if(this.current_mode != value){
//if value == confirm
var styles = {add: this.options.confirmStyle, remove: this.options.normalStyle};
if(value == 'normal'){
//swap if targeting normal mode
styles = {remove: this.options.confirmStyle, add: this.options.normalStyle};
}
this._transition(this, value, styles);
}
}
return result;
}
});
}(jQuery));