1
1
/* 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 .
3
3
Written by Keith Wood (kwood{at}iinet.com.au) June 2009.
4
4
Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and
5
5
MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses.
6
6
Please attribute the author if you use it. */
7
7
8
8
( function ( $ ) { // Hide scope, no $ conflict
9
9
10
- var PROP_NAME = 'realPerson' ;
11
-
12
10
/* Real person manager. */
13
11
function RealPerson ( ) {
14
12
this . _defaults = {
@@ -48,7 +46,7 @@ var DOTS = [
48
46
[ '* *' , ' * * ' , ' * * ' , ' * ' , ' * * ' , ' * * ' , '* *' ] ,
49
47
[ '* *' , ' * * ' , ' * * ' , ' * ' , ' * ' , ' * ' , ' * ' ] ,
50
48
[ '*******' , ' * ' , ' * ' , ' * ' , ' * ' , ' * ' , '*******' ] ,
51
- [ ' *** ' , ' * * ' , '* *' , '* *' , '* *' , ' * * ' , ' *** ' ] ,
49
+ [ ' *** ' , ' * * ' , '* * *' , '* * *' , '* * *' , ' * * ' , ' *** ' ] ,
52
50
[ ' * ' , ' ** ' , ' * * ' , ' * ' , ' * ' , ' * ' , '*******' ] ,
53
51
[ ' ***** ' , '* *' , ' *' , ' * ' , ' ** ' , ' ** ' , '*******' ] ,
54
52
[ ' ***** ' , '* *' , ' *' , ' ** ' , ' *' , '* *' , ' ***** ' ] ,
@@ -62,49 +60,58 @@ var DOTS = [
62
60
$ . extend ( RealPerson . prototype , {
63
61
/* Class name added to elements to indicate already configured with real person. */
64
62
markerClassName : 'hasRealPerson' ,
63
+ /* Name of the data property for instance settings. */
64
+ propertyName : 'realperson' ,
65
65
66
66
/* 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
68
68
@return (RealPerson) this object */
69
- setDefaults : function ( settings ) {
70
- $ . extend ( this . _defaults , settings || { } ) ;
69
+ setDefaults : function ( options ) {
70
+ $ . extend ( this . _defaults , options || { } ) ;
71
71
return this ;
72
72
} ,
73
73
74
74
/* 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 ) {
78
78
target = $ ( target ) ;
79
79
if ( target . hasClass ( this . markerClassName ) ) {
80
80
return ;
81
81
}
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 ) ;
86
85
} ,
87
86
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 ) {
94
95
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
+
95
103
if ( ! target . hasClass ( this . markerClassName ) ) {
96
104
return ;
97
105
}
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 ;
103
111
}
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 ) ) ;
108
115
} ,
109
116
110
117
/* Generate the additional content for this control.
@@ -113,35 +120,58 @@ $.extend(RealPerson.prototype, {
113
120
@return (string) the additional content */
114
121
_generateHTML : function ( target , inst ) {
115
122
var text = '' ;
116
- for ( var i = 0 ; i < inst . settings . length ; i ++ ) {
123
+ for ( var i = 0 ; i < inst . options . length ; i ++ ) {
117
124
text += CHARS . charAt ( Math . floor ( Math . random ( ) *
118
- ( inst . settings . includeNumbers ? 36 : 26 ) ) ) ;
125
+ ( inst . options . includeNumbers ? 36 : 26 ) ) ) ;
119
126
}
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">' ;
121
129
for ( var i = 0 ; i < DOTS [ 0 ] . length ; i ++ ) {
122
130
for ( var j = 0 ; j < text . length ; j ++ ) {
123
131
html += DOTS [ CHARS . indexOf ( text . charAt ( j ) ) ] [ i ] . replace ( / / g, ' ' ) +
124
132
' ' ;
125
133
}
126
134
html += '<br>' ;
127
135
}
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' ) ) +
131
139
'" value="' + this . _hash ( text ) + '">' ;
132
140
return html ;
133
141
} ,
134
142
135
- /* Remove the real person functionality from a control.
143
+ /* Enable the plugin functionality for a control.
136
144
@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 ) {
138
168
target = $ ( target ) ;
139
169
if ( ! target . hasClass ( this . markerClassName ) ) {
140
170
return ;
141
171
}
142
172
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 ( ) ;
145
175
} ,
146
176
147
177
/* Compute a hash value for the given text.
@@ -156,28 +186,51 @@ $.extend(RealPerson.prototype, {
156
186
}
157
187
} ) ;
158
188
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
+
159
204
/* 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 */
163
209
$ . fn . realperson = function ( options ) {
164
210
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
+ }
165
214
return this . each ( function ( ) {
166
215
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 ) ) ;
169
220
}
170
221
else {
171
- $ . realperson . _attachRealPerson ( this , options || { } ) ;
222
+ plugin . _attachPlugin ( this , options || { } ) ;
172
223
}
173
224
} ) ;
174
225
} ;
175
226
176
227
/* Initialise the real person functionality. */
177
- $ . realperson = new RealPerson ( ) ; // singleton instance
228
+ var plugin = $ . realperson = new RealPerson ( ) ; // Singleton instance
178
229
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
+ }
181
234
} ) ;
182
235
183
236
} ) ( jQuery ) ;
0 commit comments