-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from Snugug/feature/js-gen
Combine Validation and UX script generation
- Loading branch information
Showing
8 changed files
with
175 additions
and
306 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* eslint-env browser */ | ||
/* global allPlugins, allInputs, allSettings, allIDs */ | ||
|
||
// TODO: Add In-Browser Tests | ||
(function formUX() { | ||
var script = function (e) { | ||
var target = e.target; | ||
var inp = allInputs[target.id]; | ||
var input = {}; | ||
var settings = {}; | ||
|
||
// Set up script input | ||
input.target = target; | ||
input.all = {}; | ||
input.all[inp.name] = target; | ||
|
||
// Set up script settings | ||
settings.target = inp.settings; | ||
settings.all = {}; | ||
settings.all[inp.name] = inp.settings; | ||
|
||
// Set all sibling input and settings | ||
Object.keys(inp.siblings).forEach(function (sibling) { | ||
input.all[sibling] = allIDs[inp.siblings[sibling]]; | ||
settings.all[sibling] = allSettings[inp.parent.id][sibling]; | ||
}); | ||
|
||
// Run the script | ||
script = allPlugins[inp.parent.type].scripts[inp.script.function](input, settings); | ||
|
||
}; | ||
|
||
// Add all event listeners | ||
Object.keys(allInputs).forEach(function (id) { | ||
if (allInputs[id].hasOwnProperty('script')) { | ||
allIDs[id].addEventListener(allInputs[id].script.on, script); | ||
} | ||
}); | ||
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* eslint-env browser */ | ||
/* global allPlugins, allInputs, allSettings, allIDs */ | ||
|
||
// TODO: Add In-Browser Tests | ||
(function formValidation() { | ||
var valueCheck = function (input, settings, validation) { | ||
if (input.target.required && (input.target.required === 'save' || input.target.required === 'publish') && input.target.value === '') { | ||
return 'Field cannot be left blank!'; | ||
} | ||
|
||
return validation(input, settings); | ||
}; | ||
|
||
// Validation Function | ||
var validate = function (e) { | ||
var target = e.target; | ||
var value = target.value; | ||
var inp = allInputs[target.id]; | ||
var parent = allIDs[inp.parent.id]; | ||
var input = {}; | ||
var settings = {}; | ||
var validation = false; | ||
var message; | ||
var currentMessage = parent.querySelector('[role="alert"][for="' + target.id + '"]'); | ||
var insertRef; | ||
|
||
// Set up validation input | ||
input.target = { | ||
value: value, | ||
name: inp.name, | ||
required: inp.required, | ||
}; | ||
input.all = {}; | ||
input.all[inp.name] = value; | ||
|
||
// Set up validation settings | ||
settings.target = inp.settings; | ||
settings.all = {}; | ||
settings.all[inp.name] = inp.settings; | ||
|
||
// Set all sibling input and settings | ||
Object.keys(inp.siblings).forEach(function (sibling) { | ||
input.all[sibling] = allIDs[inp.siblings[sibling]].value; | ||
settings.all[sibling] = allSettings[inp.parent.id][sibling]; | ||
}); | ||
|
||
// Run the validation | ||
validation = valueCheck(input, settings, allPlugins[inp.parent.type].validation[inp.validation.function]); | ||
|
||
// Add/Remove validation | ||
if (validation === true) { | ||
// Check to see if there is a current alert message for this input | ||
if (currentMessage) { | ||
// Remove invalid | ||
target.removeAttribute('aria-invalid'); | ||
|
||
// Delete the current message if it exists | ||
parent.removeChild(currentMessage); | ||
} | ||
} | ||
else { | ||
// Create error message | ||
message = document.createElement('p'); | ||
message.className = 'form--alert'; | ||
message.setAttribute('role', 'alert'); | ||
message.setAttribute('for', target.id); | ||
message.textContent = validation; | ||
|
||
// Check to see if there is a current alert message for this input | ||
if (!message.isEqualNode(currentMessage)) { | ||
// Set element to invalid | ||
target.setAttribute('aria-invalid', 'true'); | ||
|
||
if (parent.nodeName.toLowerCase() !== 'fieldset') { | ||
// If parent isn't a fieldset, add message before first child | ||
insertRef = parent.firstChild; | ||
parent.insertBefore(message, insertRef); | ||
} | ||
else { | ||
// If parent is a fieldset, add message after either the description, if it exists, or the legend | ||
insertRef = parent.querySelector('.form--description') || parent.querySelector('legend'); | ||
parent.insertBefore(message, insertRef.nextSibling); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
// Add all event listeners | ||
Object.keys(allInputs).forEach(function (id) { | ||
allIDs[id].addEventListener(allInputs[id].validation.on, validate); | ||
}); | ||
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.