1// Happy.js v0.8 by Henrik Joreteg... little bit modified 2(function wmdHappyJS($) { 3 function trim(el) { 4 return (''.trim) ? el.val().trim() : $.trim(el.val()); 5 } 6 $.fn.wmdIsHappy = function wmdIsHappy(config) { 7 var fields = [], item; 8 var pauseMessages = false; 9 10 function isFunction(obj) { 11 return !!(obj && obj.constructor && obj.call && obj.apply); 12 } 13 function handleSubmit() { 14 var i, l; 15 var errors = false; 16 for (i = 0, l = fields.length; i < l; i += 1) { 17 if (!fields[i].testValid(true)) { 18 errors = true; 19 } 20 } 21 if (errors) { 22 if (isFunction(config.unHappy)) config.unHappy(); 23 return false; 24 } else if (config.testMode) { 25 if (isFunction(config.happy)) return config.happy(); 26 if (window.console) console.warn('would have submitted'); 27 return false; 28 } 29 if (isFunction(config.happy)) return config.happy(); 30 } 31 function handleMouseUp() { 32 pauseMessages = false; 33 } 34 function handleMouseDown() { 35 pauseMessages = true; 36 $(window).bind('mouseup', handleMouseUp); 37 } 38 function processField(opts, selector) { 39 var field = $(selector); 40 var handleBlur = function handleBlur() { 41 if (!pauseMessages) { 42 field.testValid(); 43 } else { 44 $(window).bind('mouseup', field.testValid.bind(this)); 45 } 46 }; 47 48 fields.push(field); 49 field.testValid = function testValid(submit) { 50 var val, gotFunc, temp; 51 var el = $(this); 52 53 if(!el.length || el.is(':disabled')) 54 return true; 55 56 var errorTarget = (opts.errorTarget && $(opts.errorTarget)) || el.parent(); 57 var error = false; 58 var required = !!el.get(0).attributes.getNamedItem('required') || opts.required; 59 var password = (field.attr('type') === 'password'); 60 var arg = isFunction(opts.arg) ? opts.arg() : opts.arg; 61 var fieldErrorClass = config.classes && config.classes.field || 'unhappy'; 62 63 var orginal_val = el.val(); 64 65 // handle control groups (checkboxes, radio) 66 if (el.length > 1) { 67 val = []; 68 el.each(function(i,obj) { 69 val.push($(obj).val()); 70 }); 71 val = val.join(','); 72 } else { 73 // clean it or trim it 74 if(el.attr('type') == 'checkbox') { 75 if(el.is(':checked')) { 76 val = el.val(); 77 } 78 else 79 val = ''; 80 } else if (isFunction(opts.clean)) { 81 val = opts.clean(el.val()); 82 } else if (!password && typeof opts.trim === 'undefined' || opts.trim) { 83 val = trim(el); 84 } else { 85 val = el.val(); 86 } 87 88 if (el.attr('type') != 'checkbox' && (typeof opts.nospace === 'undefined' || opts.nospace)) { 89 val = el.val().replace(/\s+/g, ''); 90 } 91 92 // write it back to the field 93 if(el.attr('type') != 'checkbox' && orginal_val != val) 94 el.val(val); 95 } 96 97 // get the value 98 gotFunc = ((val.length > 0 || required === 'sometimes') && isFunction(opts.test)); 99 100 // check if we've got an error on our hands 101 if (submit === true && required === true && val.length === 0) { 102 error = true; 103 } else if (gotFunc) { 104 error = !opts.test(val, arg); 105 } 106 107 if (error) { 108 errorTarget.find('.jm-unhappy-message-js').attr('aria-hidden', 'false'); 109 /* 110 errorTarget.find('.jm-unhappy-message-js').attr('aria-hidden', 'false').attr('tabindex', 0); 111 112 if(!submit) { 113 el.focus(); 114 115 errorTarget.find('.jm-unhappy-message-js').attr('role', 'alert'); 116 } 117 */ 118 119 errorTarget.addClass(fieldErrorClass); 120 return false; 121 } else { 122 errorTarget.find('.jm-unhappy-message-js').attr('aria-hidden', 'true').attr('role', false); 123 errorTarget.removeClass(fieldErrorClass); 124 return true; 125 } 126 }; 127 field.bind(opts.when || config.when || 'blur', handleBlur); 128 } 129 130 for (item in config.fields) { 131 processField(config.fields[item], item); 132 } 133 134 $(config.submitButton || this).bind('mousedown', handleMouseDown); 135 136 if (config.submitButton) { 137 $(config.submitButton).click(handleSubmit); 138 } else { 139 this.bind('validate', handleSubmit); 140 } 141 return this; 142 }; 143})(this.jQuery || this.Zepto);
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.