1/* http://keith-wood.name/realPerson.html 2 Real Person Form Submission for jQuery v1.1.0. 3 Written by Keith Wood (kwood{at}iinet.com.au) June 2009. 4 Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and 5 MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses. 6 Please attribute the author if you use it. */ 7 8(function($) { // Hide scope, no $ conflict 9 10/* Real person manager. */ 11function RealPerson() { 12 this._defaults = { 13 length: 4, // Number of characters to use 14 includeNumbers: false, // True to use numbers as well as letters 15 regenerate: '<i class="fa fa-refresh"></i> å¥ã®ç»åã表示', // Instruction text to regenerate 16 hashName: '{n}Hash' // Name of the hash value field to compare with, 17 // use {n} to substitute with the original field name 18 }; 19} 20 21var CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; 22var DOTS = [ 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$.extend(RealPerson.prototype, { 61 /* Class name added to elements to indicate already configured with real person. */ 62 markerClassName: 'hasRealPerson', 63 /* Name of the data property for instance settings. */ 64 propertyName: 'realperson', 65 66 /* Override the default settings for all real person instances. 67 @param options (object) the new settings to use as defaults 68 @return (RealPerson) this object */ 69 setDefaults: function(options) { 70 $.extend(this._defaults, options || {}); 71 return this; 72 }, 73 74 /* Attach the real person functionality to an input field. 75 @param target (element) the control to affect 76 @param options (object) the custom options for this instance */ 77 _attachPlugin: function(target, options) { 78 target = $(target); 79 if (target.hasClass(this.markerClassName)) { 80 return; 81 } 82 var inst = {options: $.extend({}, this._defaults)}; 83 target.addClass(this.markerClassName).data(this.propertyName, inst); 84 this._optionPlugin(target, options); 85 }, 86 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) { 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 103 if (!target.hasClass(this.markerClassName)) { 104 return; 105 } 106 options = options || {}; 107 if (typeof options == 'string') { 108 var name = options; 109 options = {}; 110 options[name] = value; 111 } 112 113 $.extend(inst.options, options);
114 target.nextAll('.' + this.propertyName + '-challenge,.' + this.propertyName + '-hash'). 115 remove().end().after(this._generateHTML(target, inst)); 116 }, 117 118 /* Generate the additional content for this control. 119 @param target (jQuery) the input field 120 @param inst (object) the current instance settings 121 @return (string) the additional content */ 122 _generateHTML: function(target, inst) { 123 var text = ''; 124 for (var i = 0; i < inst.options.length; i++) { 125 text += CHARS.charAt(Math.floor(Math.random() * 126 (inst.options.includeNumbers ? 36 : 26))); 127 } 128 129 var html = '<div class="' + this.propertyName + '-challenge">' + 130 '<div class="' + this.propertyName + '-text">'; 131 for (var i = 0; i < DOTS[0].length; i++) { 132 for (var j = 0; j < text.length; j++) { 133 html += DOTS[CHARS.indexOf(text.charAt(j))][i].replace(/ /g, ' ') + 134 ' '; 135 } 136 html += '<br>'; 137 } 138 html += '</div><div class="' + this.propertyName + '-regen">' + inst.options.regenerate + 139 '</div></div><input type="hidden" class="' + this.propertyName + '-hash" name="' + 140 inst.options.hashName.replace(/\{n\}/, target.attr('name')) + 141 '" value="' + this._hash(text) + '">'; 142 return html; 143 }, 144 145 /* Enable the plugin functionality for a control. 146 @param target (element) the control to affect */ 147 _enablePlugin: function(target) { 148 target = $(target); 149 if (!target.hasClass(this.markerClassName)) { 150 return; 151 } 152 target.removeClass(this.propertyName + '-disabled').prop('disabled', false). 153 nextAll('.' + this.propertyName + '-challenge').removeClass(this.propertyName + '-disabled'); 154 }, 155 156 /* Disable the plugin functionality for a control. 157 @param target (element) the control to affect */ 158 _disablePlugin: function(target) { 159 target = $(target); 160 if (!target.hasClass(this.markerClassName)) { 161 return; 162 } 163 target.addClass(this.propertyName + '-disabled').prop('disabled', true). 164 nextAll('.' + this.propertyName + '-challenge').addClass(this.propertyName + '-disabled'); 165 }, 166 167 /* Remove the plugin functionality from a control. 168 @param target (element) the control to affect */ 169 _destroyPlugin: function(target) { 170 target = $(target); 171 if (!target.hasClass(this.markerClassName)) { 172 return; 173 } 174 target.removeClass(this.markerClassName). 175 removeData(this.propertyName). 176 nextAll('.' + this.propertyName + '-challenge,.' + this.propertyName + '-hash').remove(); 177 }, 178 179 /* Compute a hash value for the given text. 180 @param value (string) the text to hash 181 @return the corresponding hash value */ 182 _hash: function(value) { 183 var hash = 5381; 184 for (var i = 0; i < value.length; i++) { 185 hash = ((hash << 5) + hash) + value.charCodeAt(i); 186 } 187 return hash; 188 } 189}); 190 191// The list of commands that return values and don't permit chaining 192var getters = ['']; 193 194/* Determine whether a command is a getter and doesn't permit chaining. 195 @param command (string, optional) the command to run 196 @param otherArgs ([], optional) any other arguments for the command 197 @return true if the command is a getter, false if not */ 198function isNotChained(command, otherArgs) { 199 if (command == 'option' && (otherArgs.length == 0 || 200 (otherArgs.length == 1 && typeof otherArgs[0] == 'string'))) { 201 return true; 202 } 203 return $.inArray(command, getters) > -1; 204} 205 206/* Attach the real person functionality to a jQuery selection. 207 @param options (object) the new settings to use for these instances (optional) or 208 (string) the command to run (optional) 209 @return (jQuery) for chaining further calls or 210 (any) getter value */ 211$.fn.realperson = function(options) { 212 var otherArgs = Array.prototype.slice.call(arguments, 1); 213 if (isNotChained(options, otherArgs)) { 214 return plugin['_' + options + 'Plugin'].apply(plugin, [this[0]].concat(otherArgs)); 215 } 216 return this.each(function() { 217 if (typeof options == 'string') { 218 if (!plugin['_' + options + 'Plugin']) { 219 throw 'Unknown command: ' + options; 220 } 221 plugin['_' + options + 'Plugin'].apply(plugin, [this].concat(otherArgs)); 222 } 223 else { 224 plugin._attachPlugin(this, options || {}); 225 } 226 }); 227}; 228 229/* Initialise the real person functionality. */ 230var plugin = $.realperson = new RealPerson(); // Singleton instance 231
232$(document).on('click', 'div.' + plugin.propertyName + '-challenge', function() { 233 if (!$(this).hasClass(plugin.propertyName + '-disabled')) { 234 $(this).prevAll('.' + plugin.markerClassName).realperson('option', {}); 235 } 236}); 237 238})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.