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

Commit b158d15

Browse files
committed
feat(calendar): starting work for date-picker.
1 parent bc78501 commit b158d15

File tree

7 files changed

+113
-303
lines changed

7 files changed

+113
-303
lines changed

src/components/calendar/calendar.spec.js

Lines changed: 10 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11

2-
describe('md-calendar', function() {
3-
// When constructing a Date, the month is zero-based. This can be confusing, since people are
4-
// used to seeing them one-based. So we create these aliases to make reading the tests easier.
5-
var JAN = 0, FEB = 1, MAR = 2, APR = 3, MAY = 4, JUN = 5, JUL = 6, AUG = 7, SEP = 8, OCT = 9,
6-
NOV = 10, DEC = 11;
7-
8-
var ngElement, element, scope, pageScope, controller, $animate, $compile;
9-
var $rootScope, dateLocale;
2+
describe('md-checkbox', function() {
3+
var ngElement, element, scope, pageScope, controller, $animate;
104

115
/**
126
* To apply a change in the date, a scope $apply() AND a manual triggering of animation
@@ -17,158 +11,40 @@ describe('md-calendar', function() {
1711
$animate.triggerCallbacks();
1812
}
1913

20-
/**
21-
* Extracts text as an array (one element per cell) from a tr element.
22-
*/
23-
function extractRowText(tr) {
24-
var cellContents = [];
25-
angular.forEach(tr.children, function(tableElement) {
26-
cellContents.push(tableElement.textContent);
27-
});
28-
29-
return cellContents;
30-
}
31-
32-
/**
33-
* Finds a date td given a day of the month from an .md-calendar-month element.
34-
*/
35-
function findDateElement(monthElement, day) {
36-
var tds = monthElement.querySelectorAll('td');
37-
var td;
38-
39-
for (var i = 0; i < tds.length; i++) {
40-
td = tds[i];
41-
if (td.textContent === day.toString()) {
42-
return td;
43-
}
44-
}
45-
}
46-
47-
48-
/**
49-
* Creates and compiles an md-calendar element.
50-
*/
51-
function createElement(parentScope) {
52-
var directiveScope = parentScope || $rootScope.$new();
53-
var template = '<md-calendar ng-model="myDate"></md-calendar>';
54-
var newElement = $compile(template)(directiveScope);
55-
directiveScope.$apply();
56-
return newElement;
57-
}
58-
5914
beforeEach(module('material.components.calendar', 'ngAnimateMock'));
6015

61-
beforeEach(inject(function($injector) {
62-
$animate = $injector.get('$animate');
63-
$compile = $injector.get('$compile');
64-
$rootScope = $injector.get('$rootScope');
65-
dateLocale = $injector.get('$$mdDateLocale');
16+
beforeEach(inject(function($compile, $rootScope, _$animate_) {
17+
$animate = _$animate_;
6618

19+
var template = '<md-calendar ng-model="myDate"></md-calendar>';
6720
pageScope = $rootScope.$new();
6821
pageScope.myDate = null;
6922

70-
ngElement = createElement(pageScope);
23+
ngElement = $compile(template)(pageScope);
7124
element = ngElement[0];
7225
scope = ngElement.scope();
7326
controller = ngElement.controller('mdCalendar');
27+
28+
pageScope.$apply();
7429
}));
7530

7631
describe('ngModel binding', function() {
7732

7833
it('should update the calendar based on ngModel change', function() {
79-
pageScope.myDate = new Date(2014, MAY, 30);
34+
pageScope.myDate = new Date(2014, 4, 30);
8035
applyDateChange();
8136

8237
var displayedMonth = element.querySelector('.md-calendar-month-label');
8338
var selectedDate = element.querySelector('.md-calendar-selected-date');
8439

8540
expect(displayedMonth.textContent).toBe('May');
86-
expect(selectedDate.textContent).toBe('30');
41+
expect(selectedDate.textContent).toBe('30')
8742
});
8843

8944
});
9045

9146
describe('calendar construction', function() {
92-
describe('weeks header', function() {
93-
it('should display the weeks header in the first row', function() {
94-
var header = element.querySelector('.md-calendar-day-header tr');
9547

96-
expect(extractRowText(header)).toEqual(['S', 'M', 'T', 'W', 'T', 'F' ,'S']);
97-
});
98-
99-
it('should use $$mdDateLocale.shortDays as weeks header values', function() {
100-
var oldShortDays = dateLocale.shortDays;
101-
dateLocale.shortDays = ['SZ', 'MZ', 'TZ', 'WZ', 'TZ', 'FZ', 'SZ'];
102-
103-
var newElement = createElement()[0];
104-
var header = newElement.querySelector('.md-calendar-day-header tr');
105-
106-
expect(extractRowText(header)).toEqual(['SZ', 'MZ', 'TZ', 'WZ', 'TZ', 'FZ','SZ']);
107-
dateLocale.shortDays = oldShortDays;
108-
});
109-
});
110-
111-
describe('#buildCalendarForMonth', function() {
112-
it('should render a month correctly as a table', function() {
113-
var date = new Date(2014, MAY, 30);
114-
var monthElement = controller.buildCalendarForMonth(date);
115-
116-
var calendarRows = monthElement.querySelectorAll('tr');
117-
var calendarDates = [];
118-
119-
angular.forEach(calendarRows, function(tr) {
120-
calendarDates.push(extractRowText(tr));
121-
});
122-
123-
var expectedDates = [
124-
['May', '', '', '1', '2', '3'],
125-
['4', '5', '6', '7', '8', '9', '10'],
126-
['11', '12', '13', '14', '15', '16', '17'],
127-
['18', '19', '20', '21', '22', '23', '24'],
128-
['25', '26', '27', '28', '29', '30', '31'],
129-
];
130-
expect(calendarDates).toEqual(expectedDates);
131-
});
132-
133-
it('should show the month on its own row if the first day is before Tuesday', function() {
134-
var date = new Date(2014, JUN, 30); // 1st on Sunday
135-
var monthElement = controller.buildCalendarForMonth(date);
136-
137-
var firstRow = monthElement.querySelector('tr');
138-
expect(extractRowText(firstRow)).toEqual(['Jun']);
139-
});
140-
});
141-
142-
143-
it('should highlight today', function() {
144-
pageScope.myDate = controller.today;
145-
applyDateChange();
146-
147-
var monthElement = element.querySelector('.md-calendar-month');
148-
var day = controller.today.getDate();
149-
150-
var dateElement = findDateElement(monthElement, day);
151-
expect(dateElement.classList.contains('md-calendar-date-today')).toBe(true);
152-
});
153-
154-
it('should have ids for date elements unique to the directive instance', function() {
155-
pageScope.myDate = controller.today;
156-
applyDateChange();
157-
158-
var otherScope = $rootScope.$new();
159-
160-
otherScope.myDate = controller.today;
161-
var otherNgElement = createElement(otherScope);
162-
163-
var monthElement = element.querySelector('.md-calendar-month');
164-
var day = controller.today.getDate();
165-
var dateElement = findDateElement(monthElement, day);
166-
167-
var otherMonthElement = otherNgElement[0].querySelector('.md-calendar-month');
168-
var otherDateElement = findDateElement(otherMonthElement, day);
169-
170-
expect(dateElement.id).not.toEqual(otherDateElement.id);
171-
});
17248
});
17349

17450
describe('keyboard events', function() {

src/components/calendar/dateLocaleProvider.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
* @param $locale
4545
* @returns {DateLocale}
4646
*/
47-
DateLocaleProvider.prototype.$get = function($locale) {
47+
DateLocaleProvider.prototype.$get = function($locale, $filter) {
4848
/**
4949
* Default date-to-string formatting function.
5050
* @param {Date} date
@@ -63,29 +63,21 @@
6363
return new Date(dateString);
6464
}
6565

66-
// The default "short" day strings are the first character of each day,
67-
// e.g., "Monday" => "M".
66+
// The default "short" day strings are the first character of each day.
6867
var defaultShortDays = $locale.DATETIME_FORMATS.DAY.map(function(day) {
6968
return day[0];
7069
});
7170

72-
// The default dates are simply the numbers 1 through 31.
73-
var defaultDates = Array(32);
74-
for (var i = 1; i <= 31; i++) {
75-
defaultDates[i] = i;
76-
}
77-
7871
window.$locale = $locale;
72+
window.$filter = $filter;
7973

80-
// TODO(jelbourn): Freeze this object.
81-
return {
74+
var dateLocale = {
8275
months: this.months || $locale.DATETIME_FORMATS.MONTH,
8376
shortMonths: this.shortMonths || $locale.DATETIME_FORMATS.SHORTMONTH,
8477
days: this.days || $locale.DATETIME_FORMATS.DAY,
8578
shortDays: this.shortDays || defaultShortDays,
86-
dates: this.dates || defaultDates,
87-
formatDate: this.formatDate || defaultFormatDate,
88-
parseDate: this.parseDate || defaultParseDate
79+
formatDate: defaultFormatDate,
80+
parseDate: defaultParseDate
8981
};
9082
};
9183

src/components/calendar/dateUtil.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
incrementDays: incrementDays,
2020
incrementMonths: incrementMonths,
2121
getLastDateOfMonth: getLastDateOfMonth,
22-
isSameDay: isSameDay,
23-
isValidDate: isValidDate
22+
isSameDay: isSameDay
2423
};
2524

2625
/**
@@ -163,14 +162,5 @@
163162
function getLastDateOfMonth(date) {
164163
return new Date(date.getFullYear(), date.getMonth(), getNumberOfDaysInMonth(date));
165164
}
166-
167-
/**
168-
* Checks whether a date is valid.
169-
* @param {Date} date
170-
* @return {boolean} Whether the date is a valid Date.
171-
*/
172-
function isValidDate(date) {
173-
return date != null && date.getTime && !isNaN(date.getTime());
174-
}
175165
});
176166
})();

0 commit comments

Comments
 (0)