Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
7283 kshitij.so 1
// jQuery Alert Dialogs Plugin
2
//
3
// Version 1.0
4
// Download by http://www.bvbsoft.com 
5
// Cory S.N. LaViska
6
// A Beautiful Site (http://abeautifulsite.net/)
7
// 29 December 2008
8
//
9
// Visit http://abeautifulsite.net/notebook/87 for more information
10
//
11
// Usage:
12
//		jAlert( message, [title, callback] )
13
//		jConfirm( message, [title, callback] )
14
//		jPrompt( message, [value, title, callback] )
15
// 
16
// History:
17
//
18
//		1.00 - Released (29 December 2008)
19
//
20
// License:
21
// 
22
//		This plugin is licensed under the GNU General Public License: http://www.gnu.org/licenses/gpl.html
23
//
24
(function($) {
25
 
26
	$.alerts = {
27
 
28
		// These properties can be read/written by accessing $.alerts.propertyName from your scripts at any time
29
 
30
		verticalOffset: -75,                // vertical offset of the dialog from center screen, in pixels
31
		horizontalOffset: 0,                // horizontal offset of the dialog from center screen, in pixels/
32
		repositionOnResize: true,           // re-centers the dialog on window resize
33
		overlayOpacity: .01,                // transparency level of overlay
34
		overlayColor: '#FFF',               // base color of overlay
35
		draggable: true,                    // make the dialogs draggable (requires UI Draggables plugin)
36
		okButton: ' OK ',         // text for the OK button
37
		cancelButton: ' Cancel ', // text for the Cancel button
38
		dialogClass: null,                  // if specified, this class will be applied to all dialogs
39
 
40
		// Public methods
41
 
42
		alert: function(message, title, callback) {
43
			if( title == null ) title = 'Alert';
44
			$.alerts._show(title, message, null, 'alert', function(result) {
45
				if( callback ) callback(result);
46
			});
47
		},
48
 
49
		confirm: function(message, title, callback) {
50
			if( title == null ) title = 'Confirm';
51
			$.alerts._show(title, message, null, 'confirm', function(result) {
52
				if( callback ) callback(result);
53
			});
54
		},
55
 
56
		prompt: function(message, value, title, callback) {
57
			if( title == null ) title = 'Prompt';
58
			$.alerts._show(title, message, value, 'prompt', function(result) {
59
				if( callback ) callback(result);
60
			});
61
		},
62
 
63
		// Private methods
64
 
65
		_show: function(title, msg, value, type, callback) {
66
 
67
			$.alerts._hide();
68
			$.alerts._overlay('show');
69
 
70
			$("BODY").append(
71
			  '<div id="popup_container">' +
72
			    '<h1 id="popup_title"></h1>' +
73
			    '<div id="popup_content">' +
74
			      '<div id="popup_message"></div>' +
75
				'</div>' +
76
			  '</div>');
77
 
78
			if( $.alerts.dialogClass ) $("#popup_container").addClass($.alerts.dialogClass);
79
 
80
			// IE6 Fix
81
			var pos = ($.browser.msie && parseInt($.browser.version) <= 6 ) ? 'absolute' : 'fixed'; 
82
 
83
			$("#popup_container").css({
84
				position: pos,
85
				zIndex: 99999,
86
				padding: 0,
87
				margin: 0
88
			});
89
 
90
			$("#popup_title").text(title);
91
			$("#popup_content").addClass(type);
92
			$("#popup_message").text(msg);
93
			$("#popup_message").html( $("#popup_message").text().replace(/\n/g, '<br />') );
94
 
95
			$("#popup_container").css({
96
				minWidth: $("#popup_container").outerWidth(),
97
				maxWidth: $("#popup_container").outerWidth()
98
			});
99
 
100
			$.alerts._reposition();
101
			$.alerts._maintainPosition(true);
102
 
103
			switch( type ) {
104
				case 'alert':
105
					$("#popup_message").after('<div id="popup_panel"><input type="button" value="' + $.alerts.okButton + '" id="popup_ok" /></div>');
106
					$("#popup_ok").click( function() {
107
						$.alerts._hide();
108
						callback(true);
109
					});
110
					$("#popup_ok").focus().keypress( function(e) {
111
						if( e.keyCode == 13 || e.keyCode == 27 ) $("#popup_ok").trigger('click');
112
					});
113
				break;
114
				case 'confirm':
115
					$("#popup_message").after('<div id="popup_panel"><input type="button" value="' + $.alerts.okButton + '" id="popup_ok" /> <input type="button" value="' + $.alerts.cancelButton + '" id="popup_cancel" /></div>');
116
					$("#popup_ok").click( function() {
117
						$.alerts._hide();
118
						if( callback ) callback(true);
119
					});
120
					$("#popup_cancel").click( function() {
121
						$.alerts._hide();
122
						if( callback ) callback(false);
123
					});
124
					$("#popup_ok").focus();
125
					$("#popup_ok, #popup_cancel").keypress( function(e) {
126
						if( e.keyCode == 13 ) $("#popup_ok").trigger('click');
127
						if( e.keyCode == 27 ) $("#popup_cancel").trigger('click');
128
					});
129
				break;
130
				case 'prompt':
131
					$("#popup_message").append('<br /><input type="text" size="30" id="popup_prompt" />').after('<div id="popup_panel"><input type="button" value="' + $.alerts.okButton + '" id="popup_ok" /> <input type="button" value="' + $.alerts.cancelButton + '" id="popup_cancel" /></div>');
132
					$("#popup_prompt").width( $("#popup_message").width() );
133
					$("#popup_ok").click( function() {
134
						var val = $("#popup_prompt").val();
135
						$.alerts._hide();
136
						if( callback ) callback( val );
137
					});
138
					$("#popup_cancel").click( function() {
139
						$.alerts._hide();
140
						if( callback ) callback( null );
141
					});
142
					$("#popup_prompt, #popup_ok, #popup_cancel").keypress( function(e) {
143
						if( e.keyCode == 13 ) $("#popup_ok").trigger('click');
144
						if( e.keyCode == 27 ) $("#popup_cancel").trigger('click');
145
					});
146
					if( value ) $("#popup_prompt").val(value);
147
					$("#popup_prompt").focus().select();
148
				break;
149
			}
150
 
151
			// Make draggable
152
			if( $.alerts.draggable ) {
153
				try {
154
					$("#popup_container").draggable({ handle: $("#popup_title") });
155
					$("#popup_title").css({ cursor: 'move' });
156
				} catch(e) { /* requires jQuery UI draggables */ }
157
			}
158
		},
159
 
160
		_hide: function() {
161
			$("#popup_container").remove();
162
			$.alerts._overlay('hide');
163
			$.alerts._maintainPosition(false);
164
		},
165
 
166
		_overlay: function(status) {
167
			switch( status ) {
168
				case 'show':
169
					$.alerts._overlay('hide');
170
					$("BODY").append('<div id="popup_overlay"></div>');
171
					$("#popup_overlay").css({
172
						position: 'absolute',
173
						zIndex: 99998,
174
						top: '0px',
175
						left: '0px',
176
						width: '100%',
177
						height: $(document).height(),
178
						background: $.alerts.overlayColor,
179
						opacity: $.alerts.overlayOpacity
180
					});
181
				break;
182
				case 'hide':
183
					$("#popup_overlay").remove();
184
				break;
185
			}
186
		},
187
 
188
		_reposition: function() {
189
			var top = (($(window).height() / 2) - ($("#popup_container").outerHeight() / 2)) + $.alerts.verticalOffset;
190
			var left = (($(window).width() / 2) - ($("#popup_container").outerWidth() / 2)) + $.alerts.horizontalOffset;
191
			if( top < 0 ) top = 0;
192
			if( left < 0 ) left = 0;
193
 
194
			// IE6 fix
195
			if( $.browser.msie && parseInt($.browser.version) <= 6 ) top = top + $(window).scrollTop();
196
 
197
			$("#popup_container").css({
198
				top: top + 'px',
199
				left: left + 'px'
200
			});
201
			$("#popup_overlay").height( $(document).height() );
202
		},
203
 
204
		_maintainPosition: function(status) {
205
			if( $.alerts.repositionOnResize ) {
206
				switch(status) {
207
					case true:
208
						$(window).bind('resize', function() {
209
							$.alerts._reposition();
210
						});
211
					break;
212
					case false:
213
						$(window).unbind('resize');
214
					break;
215
				}
216
			}
217
		}
218
 
219
	}
220
 
221
	// Shortuct functions
222
	jAlert = function(message, title, callback) {
223
		$.alerts.alert(message, title, callback);
224
	}
225
 
226
	jConfirm = function(message, title, callback) {
227
		$.alerts.confirm(message, title, callback);
228
	};
229
 
230
	jPrompt = function(message, value, title, callback) {
231
		$.alerts.prompt(message, value, title, callback);
232
	};
233
 
234
})(jQuery);