This repository has been archived by the owner on Oct 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
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 #658 from 18F/98157690-client-side-validation
[Delivers #98157690] add client-side form validation
- Loading branch information
Showing
18 changed files
with
281 additions
and
46 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
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,30 @@ | ||
class @FieldFilter | ||
constructor: (@$fieldOrWrappers) -> | ||
|
||
isInput: -> | ||
@$fieldOrWrappers.is(':input') | ||
|
||
toggleVisibility: (showOrHide) -> | ||
# https://www.paciellogroup.com/blog/2012/05/html5-accessibility-chops-hidden-and-aria-hidden/ | ||
@$fieldOrWrappers.attr('aria-hidden', !showOrHide) | ||
|
||
toggleEnabled: (enableOrDisable) -> | ||
@$fieldOrWrappers.attr('disabled', !enableOrDisable) | ||
|
||
toggleChildInputs: (enableOrDisable) -> | ||
@$fieldOrWrappers.find(':input').attr('disabled', !enableOrDisable) | ||
|
||
toggle: (showOrHide) -> | ||
@toggleVisibility(showOrHide) | ||
|
||
# hidden inputs need to be disabled, so they aren't submitted with the form | ||
if @isInput() | ||
@toggleEnabled(showOrHide) | ||
else | ||
@toggleChildInputs(showOrHide) | ||
|
||
show: -> | ||
@toggle(true) | ||
|
||
hide: -> | ||
@toggle(false) |
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 |
---|---|---|
@@ -1,45 +1,28 @@ | ||
class Filter | ||
constructor: ($root, @key) -> | ||
@$ = (selector) -> $root.find(selector) | ||
class @Filter | ||
constructor: (@$root, @$control) -> | ||
key = @$control.data('filter-control') | ||
val = @$control.val() | ||
@set = new FilterSet(@$root, key, val) | ||
|
||
addInput: ($el) -> | ||
$el.click () => @filter($el) | ||
# Initial state | ||
if $el.is(":checked") | ||
@filter($el) | ||
isSelected: -> | ||
@$control.is(':checked') | ||
|
||
addRadios: () -> | ||
@$("input:radio[data-filter-control=#{ @key }]").each (idx, control) => | ||
@addInput($(control)) | ||
update: -> | ||
if @isSelected() | ||
@set.show() | ||
else | ||
@set.hide() | ||
|
||
addChkBoxes: () -> | ||
@$("input:checkbox[data-filter-control=#{ @key }]").each (idx, control) => | ||
@addInput($(control)) | ||
|
||
filter: ($el) -> | ||
value = $el.val() | ||
if !$el.is(":checked") | ||
value = "!" + value | ||
@$("[data-filter-key=#{ @key }]").each (idx, el) -> | ||
hidden = el.getAttribute("data-filter-value") != value | ||
el.setAttribute("aria-hidden", hidden.toString()) | ||
|
||
hideAll: () -> | ||
@$("[data-filter-key=#{ @key }]").attr("aria-hidden", true) | ||
enable: -> | ||
@update() | ||
@$control.change => @update() | ||
|
||
@generateIn = ($scope) -> | ||
filters = {} | ||
$scope.find("[data-filter-control]").each (idx, el) -> | ||
key = el.getAttribute('data-filter-control') | ||
if !filters.hasOwnProperty(key) | ||
filters[key] = new Filter($scope, key) | ||
filters | ||
$scope.find('[data-filter-control]').map (idx, control) -> | ||
new Filter($scope, $(control)) | ||
|
||
$ -> | ||
# @todo - better scope | ||
$scope = $(document) | ||
$scope = $(document.body) | ||
filters = Filter.generateIn($scope) | ||
for key, filter of filters | ||
filter.hideAll() | ||
filter.addRadios() | ||
filter.addChkBoxes() | ||
for filter in filters | ||
filter.enable() |
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,27 @@ | ||
class @FilterSet | ||
constructor: (@$root, @key, @val) -> | ||
|
||
$: (selector) -> | ||
@$root.find(selector) | ||
|
||
children: -> | ||
@$("[data-filter-key=#{ @key }][data-filter-value=#{ @val }]") | ||
|
||
cousins: -> | ||
@$("[data-filter-key=#{ @key }][data-filter-value!=#{ @val }]") | ||
|
||
showChildren: -> | ||
filter = new FieldFilter(@children()) | ||
filter.show() | ||
|
||
hideCousins: -> | ||
filter = new FieldFilter(@cousins()) | ||
filter.hide() | ||
|
||
show: -> | ||
@showChildren() | ||
@hideCousins() | ||
|
||
hide: -> | ||
filter = new FieldFilter(@children()) | ||
filter.hide() |
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
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,6 @@ | ||
if defined?(Konacha) | ||
Konacha.configure do |config| | ||
require 'capybara/poltergeist' | ||
config.driver = :poltergeist | ||
end | ||
end |
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,73 @@ | ||
#= require jquery | ||
#= require field_filter | ||
|
||
describe 'FieldFilter', -> | ||
describe '#show()', -> | ||
it "enables inputs", -> | ||
$input = $('<input disabled="disabled">') | ||
|
||
filter = new FieldFilter($input) | ||
filter.show() | ||
|
||
expect($input.is(':disabled')).to.be.false | ||
|
||
it "enables text areas", -> | ||
$textarea = $('<textarea disabled="disabled">') | ||
|
||
filter = new FieldFilter($textarea) | ||
filter.show() | ||
|
||
expect($textarea.is(':disabled')).to.be.false | ||
|
||
it "enables nested inputs", -> | ||
$content = $('<div><input disabled="disabled"></div>') | ||
|
||
filter = new FieldFilter($content) | ||
filter.show() | ||
|
||
$input = $content.find('input') | ||
expect($input.is(':disabled')).to.be.false | ||
|
||
it "enables nested text areas", -> | ||
$content = $('<div><textarea disabled="disabled"></div>') | ||
|
||
filter = new FieldFilter($content) | ||
filter.show() | ||
|
||
$textarea = $content.find('textarea') | ||
expect($textarea.is(':disabled')).to.be.false | ||
|
||
describe '#hide()', -> | ||
it "disables inputs", -> | ||
$input = $('<input>') | ||
|
||
filter = new FieldFilter($input) | ||
filter.hide() | ||
|
||
expect($input.is(':disabled')).to.be.true | ||
|
||
it "disables text areas", -> | ||
$textarea = $('<textarea>') | ||
|
||
filter = new FieldFilter($textarea) | ||
filter.hide() | ||
|
||
expect($textarea.is(':disabled')).to.be.true | ||
|
||
it "disables nested inputs", -> | ||
$content = $('<div><input></div>') | ||
|
||
filter = new FieldFilter($content) | ||
filter.hide() | ||
|
||
$input = $content.find('input') | ||
expect($input.is(':disabled')).to.be.true | ||
|
||
it "disables nested text areas", -> | ||
$content = $('<div><textarea></div>') | ||
|
||
filter = new FieldFilter($content) | ||
filter.hide() | ||
|
||
$textarea = $content.find('textarea') | ||
expect($textarea.is(':disabled')).to.be.true |
Oops, something went wrong.