Created
May 12, 2016 19:09
-
-
Save leebrandt/8c95ad5f80e383ff480921fb8b0632c6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(function () { | |
var timePicker = function () { | |
return { | |
restrict: 'E', | |
replace: true, | |
require: 'ngModel', | |
scope:{}, | |
template: '<input type="text" />', | |
link: function (scope, elem, attrs, ctrl) { | |
elem.on('blur', function () { | |
scope.convertDate(); | |
}); | |
scope.convertDate = function () { | |
if (scope.ngModel) { | |
if (scope.isValid()) { | |
} else { | |
scope.$apply(function () { | |
ctrl.$setValidity('invalidTime', false); | |
}); | |
} | |
} | |
}; | |
scope.isValid = function () { | |
var timeRegEx = /^ *(1[0-2]|[1-9]):[0-5][0-9] *(a|p|A|P)(m|M) *$/i; | |
return scope.ngModel.match(timeRegEx); | |
}; | |
} | |
}; | |
}; | |
angular.module('common') | |
.directive('timePicker', [timePicker]); | |
})(); |
This file contains hidden or 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
describe('Common Module', function () { | |
'use strict'; | |
beforeEach(module('common')); | |
fdescribe('Time Picker Directive', function () { | |
var elem, scope, model, innerScope; | |
beforeEach(inject(function ($rootScope, $compile) { | |
scope = $rootScope.$new(); | |
model = scope.ngModel; | |
var el = angular.element('<time-picker name="startTime" data-ng-model="model"/>'); | |
elem = $compile(el)(scope); | |
scope.$apply(); | |
innerScope = elem.isolateScope(); | |
})); | |
describe('invalid times', function () { | |
it('should mark the field as invalid if the entered value has any alpha characters beside A, M and P', function () { | |
innerScope.ngModel = '1245PR'; | |
innerScope.convertDate(); | |
expect(elem.hasClass('.ng-invalid')).toBe(true); | |
}); | |
}) | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment