Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

Commit

Permalink
chore(timepicker): unify code style
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleycho committed Aug 10, 2015
1 parent 94acfda commit 20f525b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 55 deletions.
30 changes: 15 additions & 15 deletions src/timepicker/test/timepicker.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
describe('timepicker directive', function () {
describe('timepicker directive', function() {
var $rootScope, $compile, element;

beforeEach(module('ui.bootstrap.timepicker'));
Expand Down Expand Up @@ -26,7 +26,7 @@ describe('timepicker directive', function () {
state.push(inputs.eq(i).val());
}
if ( withoutMeridian !== true ) {
state.push( getMeridianButton().text() );
state.push(getMeridianButton().text());
}
return state;
}
Expand All @@ -36,7 +36,7 @@ describe('timepicker directive', function () {
}

function getArrow(isUp, tdIndex) {
return element.find('tr').eq( (isUp) ? 0 : 2 ).find('td').eq( tdIndex ).find('a').eq(0);
return element.find('tr').eq(isUp ? 0 : 2).find('td').eq(tdIndex).find('a').eq(0);
}

function getHoursButton(isUp) {
Expand Down Expand Up @@ -462,7 +462,7 @@ describe('timepicker directive', function () {
expect(getModelState()).toEqual([14, 40]);
});

describe('attributes', function () {
describe('attributes', function() {
beforeEach(function() {
$rootScope.hstep = 2;
$rootScope.mstep = 30;
Expand Down Expand Up @@ -626,7 +626,7 @@ describe('timepicker directive', function () {

});

describe('12 / 24 hour mode', function () {
describe('12 / 24 hour mode', function() {
beforeEach(function() {
$rootScope.meridian = false;
$rootScope.time = newTime(14, 10);
Expand Down Expand Up @@ -762,12 +762,12 @@ describe('timepicker directive', function () {
angular.extend(timepickerConfig, originalConfig);
}));

it('displays correctly', function () {
it('displays correctly', function() {
expect(getTimeState()).toEqual(['02', '40', 'μ.μ.']);
expect(getModelState()).toEqual([14, 40]);
});

it('toggles correctly', function () {
it('toggles correctly', function() {
$rootScope.time = newTime(2, 40);
$rootScope.$digest();

Expand All @@ -776,28 +776,28 @@ describe('timepicker directive', function () {
});
});

describe('$formatter', function () {
describe('$formatter', function() {
var ngModel,
date;

beforeEach(function () {
beforeEach(function() {
ngModel = element.controller('ngModel');
date = new Date('Mon Mar 23 2015 14:40:11 GMT-0700 (PDT)');
});

it('should have one formatter', function () {
it('should have one formatter', function() {
expect(ngModel.$formatters.length).toBe(1);
});

it('should convert a date to a new reference representing the same date', function () {
it('should convert a date to a new reference representing the same date', function() {
expect(ngModel.$formatters[0](date)).toEqual(date);
});

it('should convert a valid date string to a date object', function () {
it('should convert a valid date string to a date object', function() {
expect(ngModel.$formatters[0]('Mon Mar 23 2015 14:40:11 GMT-0700 (PDT)')).toEqual(date);
});

it('should set falsy values as null', function () {
it('should set falsy values as null', function() {
expect(ngModel.$formatters[0](undefined)).toBe(null);
expect(ngModel.$formatters[0](null)).toBe(null);
expect(ngModel.$formatters[0]('')).toBe(null);
Expand All @@ -806,11 +806,11 @@ describe('timepicker directive', function () {
});
});

describe('user input validation', function () {
describe('user input validation', function() {
var changeInputValueTo;

beforeEach(inject(function($sniffer) {
changeInputValueTo = function (inputEl, value) {
changeInputValueTo = function(inputEl, value) {
inputEl.val(value);
inputEl.trigger($sniffer.hasEvent('input') ? 'input' : 'change');
$rootScope.$digest();
Expand Down
82 changes: 42 additions & 40 deletions src/timepicker/timepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ angular.module('ui.bootstrap.timepicker', [])
ngModelCtrl = ngModelCtrl_;
ngModelCtrl.$render = this.render;

ngModelCtrl.$formatters.unshift(function (modelValue) {
ngModelCtrl.$formatters.unshift(function(modelValue) {
return modelValue ? new Date( modelValue ) : null;
});

Expand Down Expand Up @@ -119,18 +119,18 @@ angular.module('ui.bootstrap.timepicker', [])
}

// Get $scope.hours in 24H mode if valid
function getHoursFromTemplate ( ) {
var hours = parseInt( $scope.hours, 10 );
var valid = ( $scope.showMeridian ) ? (hours > 0 && hours < 13) : (hours >= 0 && hours < 24);
if ( !valid ) {
function getHoursFromTemplate() {
var hours = parseInt($scope.hours, 10);
var valid = $scope.showMeridian ? (hours > 0 && hours < 13) : (hours >= 0 && hours < 24);
if (!valid) {
return undefined;
}

if ( $scope.showMeridian ) {
if ( hours === 12 ) {
if ($scope.showMeridian) {
if (hours === 12) {
hours = 0;
}
if ( $scope.meridian === meridians[1] ) {
if ($scope.meridian === meridians[1]) {
hours = hours + 12;
}
}
Expand All @@ -139,15 +139,15 @@ angular.module('ui.bootstrap.timepicker', [])

function getMinutesFromTemplate() {
var minutes = parseInt($scope.minutes, 10);
return ( minutes >= 0 && minutes < 60 ) ? minutes : undefined;
return (minutes >= 0 && minutes < 60) ? minutes : undefined;
}

function pad( value ) {
return ( angular.isDefined(value) && value.toString().length < 2 ) ? '0' + value : value.toString();
function pad(value) {
return (angular.isDefined(value) && value.toString().length < 2) ? '0' + value : value.toString();
}

// Respond on mousewheel spin
this.setupMousewheelEvents = function( hoursInputEl, minutesInputEl ) {
this.setupMousewheelEvents = function(hoursInputEl, minutesInputEl) {
var isScrollingUp = function(e) {
if (e.originalEvent) {
e = e.originalEvent;
Expand All @@ -158,48 +158,46 @@ angular.module('ui.bootstrap.timepicker', [])
};

hoursInputEl.bind('mousewheel wheel', function(e) {
$scope.$apply( (isScrollingUp(e)) ? $scope.incrementHours() : $scope.decrementHours() );
$scope.$apply(isScrollingUp(e) ? $scope.incrementHours() : $scope.decrementHours());
e.preventDefault();
});

minutesInputEl.bind('mousewheel wheel', function(e) {
$scope.$apply( (isScrollingUp(e)) ? $scope.incrementMinutes() : $scope.decrementMinutes() );
$scope.$apply(isScrollingUp(e) ? $scope.incrementMinutes() : $scope.decrementMinutes());
e.preventDefault();
});

};

// Respond on up/down arrowkeys
this.setupArrowkeyEvents = function( hoursInputEl, minutesInputEl ) {
this.setupArrowkeyEvents = function(hoursInputEl, minutesInputEl) {
hoursInputEl.bind('keydown', function(e) {
if ( e.which === 38 ) { // up
if (e.which === 38) { // up
e.preventDefault();
$scope.incrementHours();
$scope.$apply();
}
else if ( e.which === 40 ) { // down
} else if (e.which === 40) { // down
e.preventDefault();
$scope.decrementHours();
$scope.$apply();
}
});

minutesInputEl.bind('keydown', function(e) {
if ( e.which === 38 ) { // up
if (e.which === 38) { // up
e.preventDefault();
$scope.incrementMinutes();
$scope.$apply();
}
else if ( e.which === 40 ) { // down
} else if (e.which === 40) { // down
e.preventDefault();
$scope.decrementMinutes();
$scope.$apply();
}
});
};

this.setupInputEvents = function( hoursInputEl, minutesInputEl ) {
if ( $scope.readonlyInput ) {
this.setupInputEvents = function(hoursInputEl, minutesInputEl) {
if ($scope.readonlyInput) {
$scope.updateHours = angular.noop;
$scope.updateMinutes = angular.noop;
return;
Expand All @@ -221,20 +219,20 @@ angular.module('ui.bootstrap.timepicker', [])
minutes = getMinutesFromTemplate();

if (angular.isDefined(hours) && angular.isDefined(minutes)) {
selected.setHours( hours );
selected.setHours(hours);
if (selected < min || selected > max) {
invalidate(true);
} else {
refresh( 'h' );
refresh('h');
}
} else {
invalidate(true);
}
};

hoursInputEl.bind('blur', function(e) {
if ( !$scope.invalidHours && $scope.hours < 10) {
$scope.$apply( function() {
if (!$scope.invalidHours && $scope.hours < 10) {
$scope.$apply(function() {
$scope.hours = pad( $scope.hours );
});
}
Expand All @@ -249,17 +247,17 @@ angular.module('ui.bootstrap.timepicker', [])
if (selected < min || selected > max) {
invalidate(undefined, true);
} else {
refresh( 'm' );
refresh('m');
}
} else {
invalidate(undefined, true);
}
};

minutesInputEl.bind('blur', function(e) {
if ( !$scope.invalidMinutes && $scope.minutes < 10 ) {
if (!$scope.invalidMinutes && $scope.minutes < 10) {
$scope.$apply( function() {
$scope.minutes = pad( $scope.minutes );
$scope.minutes = pad($scope.minutes);
});
}
});
Expand All @@ -269,11 +267,11 @@ angular.module('ui.bootstrap.timepicker', [])
this.render = function() {
var date = ngModelCtrl.$viewValue;

if ( isNaN(date) ) {
if (isNaN(date)) {
ngModelCtrl.$setValidity('time', false);
$log.error('Timepicker directive: "ng-model" value must be a Date object, a number of milliseconds since 01.01.1970 or a string representing an RFC2822 or ISO 8601 date.');
} else {
if ( date ) {
if (date) {
selected = date;
}

Expand All @@ -289,9 +287,9 @@ angular.module('ui.bootstrap.timepicker', [])
};

// Call internally when we know that model is valid.
function refresh( keyboardChange ) {
function refresh(keyboardChange) {
makeValid();
ngModelCtrl.$setViewValue( new Date(selected) );
ngModelCtrl.$setViewValue(new Date(selected));
updateTemplate( keyboardChange );
}

Expand All @@ -301,10 +299,10 @@ angular.module('ui.bootstrap.timepicker', [])
$scope.invalidMinutes = false;
}

function updateTemplate( keyboardChange ) {
function updateTemplate(keyboardChange) {
var hours = selected.getHours(), minutes = selected.getMinutes();

if ( $scope.showMeridian ) {
if ($scope.showMeridian) {
hours = ( hours === 0 || hours === 12 ) ? 12 : hours % 12; // Convert 24 to 12 hour system
}

Expand All @@ -322,7 +320,7 @@ angular.module('ui.bootstrap.timepicker', [])
return newDate;
}

function addMinutesToSelected( minutes ) {
function addMinutesToSelected(minutes) {
selected = addMinutes( selected, minutes );
refresh();
}
Expand All @@ -335,29 +333,33 @@ angular.module('ui.bootstrap.timepicker', [])
addMinutesToSelected(hourStep * 60);
}
};

$scope.decrementHours = function() {
if (!$scope.noDecrementHours()) {
addMinutesToSelected(-hourStep * 60);
}
};

$scope.incrementMinutes = function() {
if (!$scope.noIncrementMinutes()) {
addMinutesToSelected(minuteStep);
}
};

$scope.decrementMinutes = function() {
if (!$scope.noDecrementMinutes()) {
addMinutesToSelected(-minuteStep);
}
};

$scope.toggleMeridian = function() {
if (!$scope.noToggleMeridian()) {
addMinutesToSelected(12 * 60 * (selected.getHours() < 12 ? 1 : -1));
}
};
}])

.directive('timepicker', function () {
.directive('timepicker', function() {
return {
restrict: 'EA',
require: ['timepicker', '?^ngModel'],
Expand All @@ -368,8 +370,8 @@ angular.module('ui.bootstrap.timepicker', [])
link: function(scope, element, attrs, ctrls) {
var timepickerCtrl = ctrls[0], ngModelCtrl = ctrls[1];

if ( ngModelCtrl ) {
timepickerCtrl.init( ngModelCtrl, element.find('input') );
if (ngModelCtrl) {
timepickerCtrl.init(ngModelCtrl, element.find('input'));
}
}
};
Expand Down

0 comments on commit 20f525b

Please sign in to comment.