Skip to content

Commit 2c154ea

Browse files
author
Keith Wood
committed
Release 1.1.0
1 parent 73e6458 commit 2c154ea

4 files changed

+111
-58
lines changed

jquery.realperson.css

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Real Person jQuery plugin styles v1.0.1. */
1+
/* Real Person jQuery plugin styles v1.1.0. */
22
.realperson-challenge {
33
display: block;
44
color: #000;
@@ -16,3 +16,10 @@
1616
text-align: center;
1717
cursor: pointer;
1818
}
19+
.realperson-disabled {
20+
opacity: 0.5;
21+
filter: Alpha(Opacity=50);
22+
}
23+
.realperson-disabled .realperson-regen {
24+
cursor: default;
25+
}

jquery.realperson.js

+101-48
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
/* http://keith-wood.name/realPerson.html
2-
Real Person Form Submission for jQuery v1.0.1.
2+
Real Person Form Submission for jQuery v1.1.0.
33
Written by Keith Wood (kwood{at}iinet.com.au) June 2009.
44
Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and
55
MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses.
66
Please attribute the author if you use it. */
77

88
(function($) { // Hide scope, no $ conflict
99

10-
var PROP_NAME = 'realPerson';
11-
1210
/* Real person manager. */
1311
function RealPerson() {
1412
this._defaults = {
@@ -48,7 +46,7 @@ var DOTS = [
4846
['* *', ' * * ', ' * * ', ' * ', ' * * ', ' * * ', '* *'],
4947
['* *', ' * * ', ' * * ', ' * ', ' * ', ' * ', ' * '],
5048
['*******', ' * ', ' * ', ' * ', ' * ', ' * ', '*******'],
51-
[' *** ', ' * * ', '* *', '* *', '* *', ' * * ', ' *** '],
49+
[' *** ', ' * * ', '* * *', '* * *', '* * *', ' * * ', ' *** '],
5250
[' * ', ' ** ', ' * * ', ' * ', ' * ', ' * ', '*******'],
5351
[' ***** ', '* *', ' *', ' * ', ' ** ', ' ** ', '*******'],
5452
[' ***** ', '* *', ' *', ' ** ', ' *', '* *', ' ***** '],
@@ -62,49 +60,58 @@ var DOTS = [
6260
$.extend(RealPerson.prototype, {
6361
/* Class name added to elements to indicate already configured with real person. */
6462
markerClassName: 'hasRealPerson',
63+
/* Name of the data property for instance settings. */
64+
propertyName: 'realperson',
6565

6666
/* Override the default settings for all real person instances.
67-
@param settings (object) the new settings to use as defaults
67+
@param options (object) the new settings to use as defaults
6868
@return (RealPerson) this object */
69-
setDefaults: function(settings) {
70-
$.extend(this._defaults, settings || {});
69+
setDefaults: function(options) {
70+
$.extend(this._defaults, options || {});
7171
return this;
7272
},
7373

7474
/* Attach the real person functionality to an input field.
75-
@param target (element) the control to affect
76-
@param settings (object) the custom options for this instance */
77-
_attachRealPerson: function(target, settings) {
75+
@param target (element) the control to affect
76+
@param options (object) the custom options for this instance */
77+
_attachPlugin: function(target, options) {
7878
target = $(target);
7979
if (target.hasClass(this.markerClassName)) {
8080
return;
8181
}
82-
target.addClass(this.markerClassName);
83-
var inst = {settings: $.extend({}, this._defaults)};
84-
$.data(target[0], PROP_NAME, inst);
85-
this._changeRealPerson(target, settings);
82+
var inst = {options: $.extend({}, this._defaults)};
83+
target.addClass(this.markerClassName).data(this.propertyName, inst);
84+
this._optionPlugin(target, options);
8685
},
8786

88-
/* Reconfigure the settings for a real person control.
89-
@param target (element) the control to affect
90-
@param settings (object) the new options for this instance or
91-
(string) an individual property name
92-
@param value (any) the individual property value (omit if settings is an object) */
93-
_changeRealPerson: function(target, settings, value) {
87+
/* Retrieve or reconfigure the settings for a control.
88+
@param target (element) the control to affect
89+
@param options (object) the new options for this instance or
90+
(string) an individual property name
91+
@param value (any) the individual property value (omit if options
92+
is an object or to retrieve the value of a setting)
93+
@return (any) if retrieving a value */
94+
_optionPlugin: function(target, options, value) {
9495
target = $(target);
96+
var inst = target.data(this.propertyName);
97+
if (!options || (typeof options == 'string' && value == null)) { // Get option
98+
var name = options;
99+
options = (inst || {}).options;
100+
return (options && name ? options[name] : options);
101+
}
102+
95103
if (!target.hasClass(this.markerClassName)) {
96104
return;
97105
}
98-
settings = settings || {};
99-
if (typeof settings == 'string') {
100-
var name = settings;
101-
settings = {};
102-
settings[name] = value;
106+
options = options || {};
107+
if (typeof options == 'string') {
108+
var name = options;
109+
options = {};
110+
options[name] = value;
103111
}
104-
var inst = $.data(target[0], PROP_NAME);
105-
$.extend(inst.settings, settings);
106-
target.prevAll('.realperson-challenge,.realperson-hash').remove().end().
107-
before(this._generateHTML(target, inst));
112+
$.extend(inst.options, options);
113+
target.prevAll('.' + this.propertyName + '-challenge,.' + this.propertyName + '-hash').
114+
remove().end().before(this._generateHTML(target, inst));
108115
},
109116

110117
/* Generate the additional content for this control.
@@ -113,35 +120,58 @@ $.extend(RealPerson.prototype, {
113120
@return (string) the additional content */
114121
_generateHTML: function(target, inst) {
115122
var text = '';
116-
for (var i = 0; i < inst.settings.length; i++) {
123+
for (var i = 0; i < inst.options.length; i++) {
117124
text += CHARS.charAt(Math.floor(Math.random() *
118-
(inst.settings.includeNumbers ? 36 : 26)));
125+
(inst.options.includeNumbers ? 36 : 26)));
119126
}
120-
var html = '<div class="realperson-challenge"><div class="realperson-text">';
127+
var html = '<div class="' + this.propertyName + '-challenge">' +
128+
'<div class="' + this.propertyName + '-text">';
121129
for (var i = 0; i < DOTS[0].length; i++) {
122130
for (var j = 0; j < text.length; j++) {
123131
html += DOTS[CHARS.indexOf(text.charAt(j))][i].replace(/ /g, '&nbsp;') +
124132
'&nbsp;&nbsp;';
125133
}
126134
html += '<br>';
127135
}
128-
html += '</div><div class="realperson-regen">' + inst.settings.regenerate +
129-
'</div></div><input type="hidden" class="realperson-hash" name="' +
130-
inst.settings.hashName.replace(/\{n\}/, target.attr('name')) +
136+
html += '</div><div class="' + this.propertyName + '-regen">' + inst.options.regenerate +
137+
'</div></div><input type="hidden" class="' + this.propertyName + '-hash" name="' +
138+
inst.options.hashName.replace(/\{n\}/, target.attr('name')) +
131139
'" value="' + this._hash(text) + '">';
132140
return html;
133141
},
134142

135-
/* Remove the real person functionality from a control.
143+
/* Enable the plugin functionality for a control.
136144
@param target (element) the control to affect */
137-
_destroyRealPerson: function(target) {
145+
_enablePlugin: function(target) {
146+
target = $(target);
147+
if (!target.hasClass(this.markerClassName)) {
148+
return;
149+
}
150+
target.removeClass(this.propertyName + '-disabled').prop('disabled', false).
151+
prevAll('.' + this.propertyName + '-challenge').removeClass(this.propertyName + '-disabled');
152+
},
153+
154+
/* Disable the plugin functionality for a control.
155+
@param target (element) the control to affect */
156+
_disablePlugin: function(target) {
157+
target = $(target);
158+
if (!target.hasClass(this.markerClassName)) {
159+
return;
160+
}
161+
target.addClass(this.propertyName + '-disabled').prop('disabled', true).
162+
prevAll('.' + this.propertyName + '-challenge').addClass(this.propertyName + '-disabled');
163+
},
164+
165+
/* Remove the plugin functionality from a control.
166+
@param target (element) the control to affect */
167+
_destroyPlugin: function(target) {
138168
target = $(target);
139169
if (!target.hasClass(this.markerClassName)) {
140170
return;
141171
}
142172
target.removeClass(this.markerClassName).
143-
prevAll('.realperson-challenge,.realperson-hash').remove();
144-
$.removeData(target[0], PROP_NAME);
173+
removeData(this.propertyName).
174+
prevAll('.' + this.propertyName + '-challenge,.' + this.propertyName + '-hash').remove();
145175
},
146176

147177
/* Compute a hash value for the given text.
@@ -156,28 +186,51 @@ $.extend(RealPerson.prototype, {
156186
}
157187
});
158188

189+
// The list of commands that return values and don't permit chaining
190+
var getters = [''];
191+
192+
/* Determine whether a command is a getter and doesn't permit chaining.
193+
@param command (string, optional) the command to run
194+
@param otherArgs ([], optional) any other arguments for the command
195+
@return true if the command is a getter, false if not */
196+
function isNotChained(command, otherArgs) {
197+
if (command == 'option' && (otherArgs.length == 0 ||
198+
(otherArgs.length == 1 && typeof otherArgs[0] == 'string'))) {
199+
return true;
200+
}
201+
return $.inArray(command, getters) > -1;
202+
}
203+
159204
/* Attach the real person functionality to a jQuery selection.
160-
@param command (string) the command to run (optional, default 'attach')
161-
@param options (object) the new settings to use for these instances (optional)
162-
@return (jQuery) for chaining further calls */
205+
@param options (object) the new settings to use for these instances (optional) or
206+
(string) the command to run (optional)
207+
@return (jQuery) for chaining further calls or
208+
(any) getter value */
163209
$.fn.realperson = function(options) {
164210
var otherArgs = Array.prototype.slice.call(arguments, 1);
211+
if (isNotChained(options, otherArgs)) {
212+
return plugin['_' + options + 'Plugin'].apply(plugin, [this[0]].concat(otherArgs));
213+
}
165214
return this.each(function() {
166215
if (typeof options == 'string') {
167-
$.realperson['_' + options + 'RealPerson'].
168-
apply($.realperson, [this].concat(otherArgs));
216+
if (!plugin['_' + options + 'Plugin']) {
217+
throw 'Unknown command: ' + options;
218+
}
219+
plugin['_' + options + 'Plugin'].apply(plugin, [this].concat(otherArgs));
169220
}
170221
else {
171-
$.realperson._attachRealPerson(this, options || {});
222+
plugin._attachPlugin(this, options || {});
172223
}
173224
});
174225
};
175226

176227
/* Initialise the real person functionality. */
177-
$.realperson = new RealPerson(); // singleton instance
228+
var plugin = $.realperson = new RealPerson(); // Singleton instance
178229

179-
$('.realperson-challenge').live('click', function() {
180-
$(this).next().next().realperson('change');
230+
$('div.' + plugin.propertyName + '-challenge').live('click', function() {
231+
if (!$(this).hasClass(plugin.propertyName + '-disabled')) {
232+
$(this).nextAll('.' + plugin.markerClassName).realperson('option', {});
233+
}
181234
});
182235

183236
})(jQuery);

0 commit comments

Comments
 (0)