Commit d3756188 authored by Robert Knight's avatar Robert Knight

Remove `form-input` and `form-validate` directives.

These directives were used only by the inline login form which was
removed as part of the removal of cookie auth support.

There are still a couple of references to "form-input" in templates but
these are CSS classes used for styling purposes.
parent 5a14c16b
...@@ -160,8 +160,6 @@ module.exports = angular.module('h', [ ...@@ -160,8 +160,6 @@ module.exports = angular.module('h', [
.component('timestamp', require('./components/timestamp')) .component('timestamp', require('./components/timestamp'))
.component('topBar', require('./components/top-bar')) .component('topBar', require('./components/top-bar'))
.directive('formInput', require('./directive/form-input'))
.directive('formValidate', require('./directive/form-validate'))
.directive('hAutofocus', require('./directive/h-autofocus')) .directive('hAutofocus', require('./directive/h-autofocus'))
.directive('hBranding', require('./directive/h-branding')) .directive('hBranding', require('./directive/h-branding'))
.directive('hOnTouch', require('./directive/h-on-touch')) .directive('hOnTouch', require('./directive/h-on-touch'))
......
module.exports = ->
link: (scope, elem, attr, [model, validator]) ->
return unless model
fieldClassName = 'form-field'
errorClassName = 'form-field-error'
render = model.$render
resetResponse = (value) ->
model.$setValidity('response', true)
value
toggleClass = (addClass) ->
elem.toggleClass(errorClassName, addClass)
elem.parent().toggleClass(errorClassName, addClass)
model.$parsers.unshift(resetResponse)
model.$render = ->
toggleClass(model.$invalid and model.$dirty)
render()
if validator?
validator.addControl(model)
scope.$on '$destroy', -> validator.removeControl model
scope.$watch ->
if model.$modelValue? or model.$pristine
toggleClass(model.$invalid and model.$dirty)
return
require: ['?ngModel', '^?formValidate']
restrict: 'C'
module.exports = ->
controller: ->
controls = {}
addControl: (control) ->
if control.$name
controls[control.$name] = control
removeControl: (control) ->
if control.$name
delete controls[control.$name]
submit: ->
# make all the controls dirty and re-render them
for _, control of controls
control.$setViewValue(control.$viewValue)
control.$render()
link: (scope, elem, attr, ctrl) ->
elem.on 'submit', ->
ctrl.submit()
{module, inject} = angular.mock
describe 'form-input', ->
$compile = null
$field = null
$scope = null
before ->
angular.module('h', ['ng'])
.directive('formInput', require('../form-input'))
beforeEach module('h')
beforeEach inject (_$compile_, _$rootScope_) ->
$compile = _$compile_
$scope = _$rootScope_.$new()
beforeEach ->
$scope.model = {username: undefined}
template = '''
<div class="form-field">
<input type="text" class="form-input" name="username"
ng-model="model.username" name="username"
required ng-minlength="3" />
</div>
'''
$field = $compile(angular.element(template))($scope)
$scope.$digest()
it 'should remove an error class to an valid field on change', ->
$field.addClass('form-field-error')
$input = $field.find('[name=username]').addClass('form-field-error')
$input.controller('ngModel').$setViewValue('abc')
$scope.$digest()
assert.notInclude($field.prop('className'), 'form-field-error')
assert.notInclude($input.prop('className'), 'form-field-error')
it 'should apply an error class to an invalid field on render', ->
$input = $field.find('[name=username]')
$input.triggerHandler('input') # set dirty
$input.controller('ngModel').$render()
assert.include($field.prop('className'), 'form-field-error')
it 'should remove an error class from a valid field on render', ->
$field.addClass('form-field-error')
$input = $field.find('[name=username]')
$input.val('abc').triggerHandler('input')
$input.controller('ngModel').$render()
assert.notInclude($field.prop('className'), 'form-field-error')
it 'should remove an error class on valid input', ->
$field.addClass('form-field-error')
$input = $field.find('[name=username]')
$input.val('abc').triggerHandler('input')
assert.notInclude($field.prop('className'), 'form-field-error')
it 'should not add an error class on invalid input', ->
$input = $field.find('[name=username]')
$input.val('ab').triggerHandler('input')
assert.notInclude($field.prop('className'), 'form-field-error')
it 'should reset the "response" error when the view changes', ->
$input = $field.find('[name=username]')
controller = $input.controller('ngModel')
controller.$setViewValue('abc')
controller.$setValidity('response', false)
controller.responseErrorMessage = 'fail'
$scope.$digest()
assert.include($field.prop('className'), 'form-field-error', 'Fail fast check')
controller.$setViewValue('def')
$scope.$digest()
assert.notInclude($field.prop('className'), 'form-field-error')
it 'should hide errors if the model is marked as pristine', ->
$field.addClass('form-field-error')
$input = $field.find('[name=username]')
controller = $input.controller('ngModel')
$input.triggerHandler('input') # set dirty
controller.$setValidity('response', false)
controller.responseErrorMessage = 'fail'
$scope.$digest()
assert.include($field.prop('className'), 'form-field-error', 'Fail fast check')
# Then clear it out and mark it as pristine
controller.$setPristine()
$scope.$digest()
assert.notInclude($field.prop('className'), 'form-field-error')
describe 'with form-validate', ->
link = require('../form-input')().link
it 'should register its model with the validator', ->
model = {'$parsers': []}
validator = {addControl: sinon.spy(), removeControl: sinon.spy()}
link($scope, $field, null, [model, validator])
assert.calledOnce(validator.addControl)
assert.calledWith(validator.addControl, model)
assert.notCalled(validator.removeControl)
$scope.$destroy()
assert.calledOnce(validator.removeControl)
assert.calledWith(validator.removeControl, model)
{module, inject} = angular.mock
describe 'form-validate', ->
$compile = null
$element = null
$scope = null
controller = null
before ->
angular.module('h', [])
.directive('formValidate', require('../form-validate'))
beforeEach module('h')
beforeEach inject (_$compile_, _$rootScope_) ->
$compile = _$compile_
$scope = _$rootScope_.$new()
template = '<form form-validate onsubmit="return false"></form>'
$element = $compile(angular.element(template))($scope)
controller = $element.controller('formValidate')
it 'performs validation and rendering on registered controls on submit', ->
mockControl =
'$name': 'babbleflux'
'$setViewValue': sinon.spy()
'$render': sinon.spy()
controller.addControl(mockControl)
$element.triggerHandler('submit')
assert.calledOnce(mockControl.$setViewValue)
assert.calledOnce(mockControl.$render)
mockControl2 =
'$name': 'dubbledabble'
'$setViewValue': sinon.spy()
'$render': sinon.spy()
controller.removeControl(mockControl)
controller.addControl(mockControl2)
$element.triggerHandler('submit')
assert.calledOnce(mockControl.$setViewValue)
assert.calledOnce(mockControl.$render)
assert.calledOnce(mockControl2.$setViewValue)
assert.calledOnce(mockControl2.$render)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment