Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README_tempate.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<!--
Note that if you're looking at README.md, that file is *generated* from README_template.md - so do *not*
directly edit README.md, or your changes will be lost.
-->
# Introduction

Goodforms-js is a Javascript library that enables easy, browser-based validation of email addresses, by looking at the
Expand Down
8 changes: 4 additions & 4 deletions auto.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import { duplicate } from "./utils.js"
export default function (options) {
let activated_forms=[]
for(let form = 0 ; form < document.forms.length ; form++ ) { //olde-skoole DOM0 FTW!
log.debug("Checking form: "+form+" for verifiable email address fields...")
log.verbose("Checking form: "+form+" for verifiable email address fields...")
for(let i = 0; i < document.forms[form].elements.length ; i ++ ) {
log.debug("Checking field #"+i+" to see if it's an email address field")
log.verbose("Checking field #"+i+" to see if it's an email address field")
let this_field = document.forms[form].elements[i]
log.debug("It's type is: "+this_field.type+" its name is: "+this_field.name+" and its id is: "+this_field.id)
log.verbose("It's type is: "+this_field.type+" its name is: "+this_field.name+" and its id is: "+this_field.id)
//log.debugdir(this_field)
if(this_field.type == "email" || this_field.name == "email" || this_field.id == "email") {
let options_copy = duplicate(options)
log.debug("Found candidate field. Name: "+this_field.name+" Type: "+this_field.type+" ID: "+this_field.id)
log.debug("Found candidate email field. Name: "+this_field.name+" Type: "+this_field.type+" ID: "+this_field.id)
options_copy.form = document.forms[form]
options_copy.email_field = this_field
activated_forms.push(new Form(options_copy))
Expand Down
2 changes: 1 addition & 1 deletion dist/verify.js

Large diffs are not rendered by default.

52 changes: 31 additions & 21 deletions form.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const options_hash = {
email_field: "DOMNode",
form: "DOMNodeOrBoolean",
submit_button: "DOMNodeOrArrayOfDOMNodes",
debug: "boolean",
debug: "BooleanOrVerbose",
onGood: "function",
onBad: "function",
onChallenge: "function",
Expand All @@ -38,11 +38,11 @@ const visuals_all_off = {good:false, bad:false, challenge:false}

export default class Form {
constructor(options) {
log.debug("Invoking Class constructor!")
log.debugdir(options)
log.verbose("Invoking Class constructor!")
log.verbosedir(options)

for(let key in options) {
log.debug("Setting: "+key+" to "+options[key])
log.verbose("Setting: "+key+" to "+options[key])
//this[key] = options[key] //this will initialize all the callbacks, btw. Even if 'manual' is turned on! Do we...want that? TODO
if(!this.unwrap_assign(key, options[key])) {
//bail out if any options weren't assignable
Expand All @@ -68,20 +68,20 @@ export default class Form {
return log.error("No Email Field set!")
}
if(typeof this.form == "undefined" || this.form === true ) { // 'true' is just an explicit way of saying 'automatically figure out what form this lives in'
log.debug("Trying to guess Form value")
log.verbose("Trying to guess Form value")
//try and guess form from email field's 'form' property
this.form = this.email_field.form
log.debug("Picked: "+this.form)
log.verbose("Picked: "+this.form)
}
if(!this.form && this.form !== false) { // 'false' means "don't mess with the form, or maybe there isn't one"
return log.error("Could not determine Form!")
}
if(this.form && !this.submit_button && this.submit_button !== false) { //'false' means "don't disable submit buttons"
log.debug("Trying to find submit buttons...")
log.verbose("Trying to find submit buttons...")
let submit_buttons=[]
for(let i=0; i < this.form.elements.length; i++) {
let element = this.form.elements[i]
log.debug("Checking element: "+element+" - nodeName: '"+element.nodeName+"' Type: '"+element.type+"'")
log.verbose("Checking element: "+element+" - nodeName: '"+element.nodeName+"' Type: '"+element.type+"'")
if((element.nodeName == "INPUT" && element.type == "submit") || (element.nodeName == "BUTTON" && element.type != "reset" && element.type != "button")) {
log.debug("Found a submit button")
submit_buttons.push(element)
Expand Down Expand Up @@ -129,6 +129,7 @@ export default class Form {
this.visuals = duplicate(visuals_all_on) //any missing keys should default to 'on' (true)
for(var key in element) {
if(!visuals_all_on[key]) { //if the 'visuals_all_on' constant doesn't have 'true' for this, this thing has unneeded keys
log.error(name+" key "+key+" was unexpected")
return false
}
this.visuals[key] = element[key]
Expand All @@ -137,6 +138,7 @@ export default class Form {
//now make sure to set default 'true' for anything missed.
return true
}
log.error(name+" is incorrect type - got:"+typeof(element))
return false
break

Expand All @@ -148,6 +150,14 @@ export default class Form {
return this.unwrap_domnode(name,element,false)
}
break

case "BooleanOrVerbose":
if(typeof element === "boolean" || (typeof element === "string" && element === "verbose")) {
return true;
} else {
log.error('Wanted either boolean true or false, or the string "verbose" for key '+name)
return false
}

default:
if(typeof element == options_hash[name]) {
Expand All @@ -164,7 +174,7 @@ export default class Form {
// if it's a jquery element, return the real DOM element underneath.
// if it's still bad, error.
if(typeof(element) === 'object' && element['jquery'] && element['get']) {
log.debug("jQuery-like object found for "+name)
log.verbose("jQuery-like object found for "+name)
if(element.length == 0 ) {
log.error("No elements found in jQuery selector for "+name)
return false
Expand Down Expand Up @@ -208,7 +218,7 @@ export default class Form {
this[name] = element
return true
}
log.debug("Unknown element type passed for "+name+": "+typeof(element)+", and its prototype is: "+(element['prototype'] ? element.prototype : '<unknown>')+", and its source: "+(element && element['prototype'] && element['prototype']['toSource'] ? element.prototype.toSource() : '<unavailable>'))
log.error("Unknown element type passed for "+name+": "+typeof(element)+", and its prototype is: "+(element['prototype'] ? element.prototype : '<unknown>')+", and its source: "+(element && element['prototype'] && element['prototype']['toSource'] ? element.prototype.toSource() : '<unavailable>'))
return false
}

Expand All @@ -228,7 +238,7 @@ export default class Form {
if(this.form) {
let old_onsubmit = this.form.onsubmit
this.form.onsubmit = (event) => {
log.debug("On Submit handler firing!")
log.verbose("On Submit handler firing!")
var results
if(old_onsubmit) {
results = old_onsubmit(event) //TODO - confusing, *their* old onsubmit handler fires *first*?
Expand All @@ -254,9 +264,9 @@ export default class Form {
set_submit_button_disabled(state) {
this.submittable = !state // if disabled == true, submittable = false; if disabled == false, submittable = true
if(this.submit_button) {
log.debug("Trying to disable submit button...")
log.verbose("Trying to disable submit button...")
if(is_array(this.submit_button)) {
log.debug("Submit button IS ARRAY")
log.verbose("Submit button IS ARRAY")
for(let x in this.submit_button) {
this.submit_button[x].disabled = state
}
Expand Down Expand Up @@ -295,7 +305,7 @@ export default class Form {
}

fire_hooks(name, behavior, visuals, ...params) {
log.debug("Firing hooks for: "+name)
log.verbose("Firing hooks for: "+name)
if(this.manual) {
return
}
Expand Down Expand Up @@ -370,8 +380,8 @@ export default class Form {
return
}
this.challenge(this.email_field.value, challenge_key, (results) => {
log.debug("Challenge results are: ")
log.debugdir(results)
log.verbose("Challenge results are: ")
log.verbosedir(results)
if(results.status == "ACCEPTED") {
this.modal.pin_input()
this.modal.set_modal_action( () => {
Expand All @@ -386,7 +396,7 @@ export default class Form {
* their hooks fire correctly. But also, we *do* want to update the checksums and all the
* other default behavior of a 'good' verification
*/
this.ongood_handler(response.status, response.checksum)
this.ongood_handler(response.status, response.checksum) //FIXME - that's supposed to be a 'detailed status' - not just GOOD
} else {
this.modal.bad_pin()
}
Expand Down Expand Up @@ -417,9 +427,9 @@ export default class Form {
if(this.submittable && this.email_field.value === this.verifying) {
return true
}
log.debug("Cannot submit form - submittable? "+this.submittable+" our field value? "+this.email_field.value+" what we're verifying? "+this.verifying)
log.verbose("Cannot submit form - submittable? "+this.submittable+" our field value? "+this.email_field.value+" what we're verifying? "+this.verifying)
if(this.email_field.value !== this.verifying) {
log.debug("sending new verification!")
log.verbose("sending new verification!")
this.verify(this.email_field.value, (results) => { //FIXME - this could double-verify!
if(this.submittable && this.form) { //don't directly inspect 'results', assume the onBlah handlers will update 'submittable'
this.form.submit()
Expand Down Expand Up @@ -470,14 +480,14 @@ export default class Form {
}

verify(email, callback) {
log.debug("VERIFY low-level method invoked!")
log.verbose("VERIFY low-level method invoked!")
this.jsp("verify", {email: email},
(data) => {
if(data.error) { //out-of-band type of error, or does this *never* fire?
log.error(data.error)
}
let detailed_status = null
if(data && data.checksum) {
if(data && data.checksum) { // FIXME - grabbing detailed status via SPLIT
detailed_status = data.checksum.split(";")[2] //what happens if there's a semicolon in the email? Well, it's gonna mess up.
}

Expand Down
12 changes: 6 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { duplicate } from "./utils.js"


export default function (form_key, options) {
log.debug("MAIN INIT ROUTINE RUNNING!")
if(options.debug) {
log.debug_enabled = options.debug
}
log.verbose("MAIN INIT ROUTINE RUNNING!")
if(typeof options === 'undefined') {
// handle either Goodforms({blah:'blah',blah:'blah'})
// or Goodforms()
Expand All @@ -15,14 +18,11 @@ export default function (form_key, options) {
if(!options) {
options = {}
}
if(options.debug) {
log.debug_enabled = options.debug
}
if(!form_key && (!options['form_key'])) {
log.debug('Form key was not set (root-level)')
}
log.debug("CREATING NEW FORM WITH FORMKEY: "+form_key)
log.debugdir(options)
log.verbosedir(options) //FIXME - verbosedir?
let my_opts = duplicate(options)
// if form_key was passed as part of the options, don't try and set it
// but if it wasn't, and you don't even *have* a form key - you should also try not to set it.
Expand All @@ -31,7 +31,7 @@ export default function (form_key, options) {
}
if(!options.email_field && !options.manual) {
log.debug("Engaging 'auto' - ")
log.debugdir(my_opts)
log.verbosedir(my_opts)
return auto(my_opts)
}
return new Form(my_opts)
Expand Down
22 changes: 21 additions & 1 deletion logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,17 @@ class Log {
}

debug(msg) {
if(this.debug_enabled) {
// 'debug' messages show as debug when debug_enabled is just 'true'
// 'but show as 'info' if debug_enabled is 'verbose'
if(this.debug_enabled === "verbose") {
this.log_at_level('info',msg)
} else if(this.debug_enabled) {
this.log_at_level('debug',msg)
}
}

verbose(msg) {
if(this.debug_enabled === "verbose") {
this.log_at_level('debug',msg)
}
}
Expand All @@ -25,6 +35,16 @@ class Log {
}
}

verbosedir(msg) {
if(this.debug_enabled === "verbose") {
if(typeof console !== "undefined" && console["dir"]) {
this.log_at_level('dir',msg)
} else {
this.log_at_level('debug',msg)
}
}
}

log_at_level(level,msg) {
if(typeof console !== "undefined" && console[level]) {
console[level](msg)
Expand Down
6 changes: 3 additions & 3 deletions visuals.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export class modal {
}

get_modal(challenge_key) { //TODO - this needs breaking up, it's a little rambly
log.debug("Getting modal - challenge key is: "+challenge_key)
log.verbose("Getting modal - challenge key is: "+challenge_key)
if(!this.modal) {

//TODO prolly need to rename all of these classes to something unique
Expand Down Expand Up @@ -203,9 +203,9 @@ export class modal {
MicroModal.show('goodforms-modal',{
debugMode: true,
awaitCloseAnimation: true,
onShow: modal => log.debug(`${modal.id} is shown`),
onShow: modal => log.verbose(`${modal.id} is shown`),
onClose: modal => {
log.debug(`${modal.id} is hidden`)
log.verbose(`${modal.id} is hidden`)
if(this.modal) {
document.body.removeChild(this.modal)
delete this.modal
Expand Down