Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
feat(datepicker): binding for the date-picker
Browse files Browse the repository at this point in the history
  • Loading branch information
jelbourn committed Aug 13, 2015
1 parent 36eae11 commit cc10fa9
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 12 deletions.
66 changes: 55 additions & 11 deletions src/components/calendar/datePicker.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,56 @@
(function() {
'use strict';

// TODO(jelbourn): md-calendar shown in floating panel.
// TODO(jelbourn): little calendar icon next to input
// TODO(jelbourn): only one open md-calendar panel at a time per application


angular.module('material.components.calendar')
.directive('mdDatePicker', datePickerDirective);

function datePickerDirective() {
return {
template:
'<div>' +
'<input ng-model="textValue"> <br>' +
'<md-calendar ng-model="dateValue"></md-calendar>' +
'<div class="md-date-picker">' +
'<input> <br>' +
'<md-calendar ng-model="ctrl.date"></md-calendar>' +
'</div>',
require: ['ngModel', 'mdDatePicker'],
scope: {},
controller: DatePickerCtrl,
controllerAs: 'ctrl'
controllerAs: 'ctrl',
link: function(scope, element, attr, controllers) {
var ngModelCtrl = controllers[0];
var mdDatePickerCtrl = controllers[1];
mdDatePickerCtrl.configureNgModel(ngModelCtrl);
}
};
}

function DatePickerCtrl($$mdDateLocale) {
function DatePickerCtrl($scope, $element, $$mdDateLocale, $$mdDateUtil) {
/** @final */
this.dateLocale = $$mdDateLocale;

/** @final */
this.dateUtil = $$mdDateUtil;

/** @type {!angular.NgModelController} */
this.ngModelCtrl = null;

/** @type {string} */
this.textValue = '';
/** @type {HTMLInputElement} */
this.inputElement = $element[0].querySelector('input');

/** @type {Date} */
this.dateValue = null;
this.date = null;

/** @final {!angular.JQLite} */
this.$element = $element;

/** @final {!angular.Scope} */
this.$scope = $scope;

this.attachChangeListeners();
}

/**
Expand All @@ -41,8 +62,31 @@

var self = this;
ngModelCtrl.$render = function() {
self.dateValue = self.ngModelCtrl.$viewValue
self.textValue = self.dateLocale.format(self.ngModelCtrl.$viewValue);
self.date = self.ngModelCtrl.$viewValue;
self.inputElement.value = self.dateLocale.formatDate(self.date);
};
}
};

/**
* Attach event listeners for both the text input and the md-calendar.
* Events are used instead of ng-model so that updates don't infinitely update the other
* on a change. This should also be more performant than using a $watch.
*/
DatePickerCtrl.prototype.attachChangeListeners = function() {
var self = this;

self.$scope.$on('md-calendar-change', function(event, date) {
self.ngModelCtrl.$setViewValue(date);
self.inputElement.value = self.dateLocale.formatDate(date);
});

// TODO(jelbourn): debounce
self.inputElement.addEventListener('input', function() {
var parsedDate = self.dateLocale.parseDate(self.inputElement.value);
if (self.dateUtil.isValidDate(parsedDate)) {
self.date = parsedDate;
self.$scope.$apply();
}
});
};
})();
12 changes: 11 additions & 1 deletion src/components/calendar/dateUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
incrementDays: incrementDays,
incrementMonths: incrementMonths,
getLastDateOfMonth: getLastDateOfMonth,
isSameDay: isSameDay
isSameDay: isSameDay,
isValidDate: isValidDate
};

/**
Expand Down Expand Up @@ -162,5 +163,14 @@
function getLastDateOfMonth(date) {
return new Date(date.getFullYear(), date.getMonth(), getNumberOfDaysInMonth(date));
}

/**
* Checks whether a date is valid.
* @param {Date} date
* @return {boolean} Whether the date is a valid Date.
*/
function isValidDate(date) {
return date != null && date.getTime && !isNaN(date.getTime());
}
});
})();
3 changes: 3 additions & 0 deletions src/components/calendar/demoDatePicker/style.css
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
/** Demo styles for mdCalendar. */
.md-date-picker {
border: 2px solid darkred;
}

0 comments on commit cc10fa9

Please sign in to comment.