@@ 1645-1757 (lines=113) @@ | ||
1642 | } |
|
1643 | }]) |
|
1644 | ||
1645 | .controller('UibDaypickerController', ['$scope', '$element', 'dateFilter', function(scope, $element, dateFilter) { |
|
1646 | var DAYS_IN_MONTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; |
|
1647 | ||
1648 | this.step = { months: 1 }; |
|
1649 | this.element = $element; |
|
1650 | function getDaysInMonth(year, month) { |
|
1651 | return month === 1 && year % 4 === 0 && |
|
1652 | (year % 100 !== 0 || year % 400 === 0) ? 29 : DAYS_IN_MONTH[month]; |
|
1653 | } |
|
1654 | ||
1655 | this.init = function(ctrl) { |
|
1656 | angular.extend(ctrl, this); |
|
1657 | scope.showWeeks = ctrl.showWeeks; |
|
1658 | ctrl.refreshView(); |
|
1659 | }; |
|
1660 | ||
1661 | this.getDates = function(startDate, n) { |
|
1662 | var dates = new Array(n), current = new Date(startDate), i = 0, date; |
|
1663 | while (i < n) { |
|
1664 | date = new Date(current); |
|
1665 | dates[i++] = date; |
|
1666 | current.setDate(current.getDate() + 1); |
|
1667 | } |
|
1668 | return dates; |
|
1669 | }; |
|
1670 | ||
1671 | this._refreshView = function() { |
|
1672 | var year = this.activeDate.getFullYear(), |
|
1673 | month = this.activeDate.getMonth(), |
|
1674 | firstDayOfMonth = new Date(this.activeDate); |
|
1675 | ||
1676 | firstDayOfMonth.setFullYear(year, month, 1); |
|
1677 | ||
1678 | var difference = this.startingDay - firstDayOfMonth.getDay(), |
|
1679 | numDisplayedFromPreviousMonth = difference > 0 ? |
|
1680 | 7 - difference : - difference, |
|
1681 | firstDate = new Date(firstDayOfMonth); |
|
1682 | ||
1683 | if (numDisplayedFromPreviousMonth > 0) { |
|
1684 | firstDate.setDate(-numDisplayedFromPreviousMonth + 1); |
|
1685 | } |
|
1686 | ||
1687 | // 42 is the number of days on a six-week calendar |
|
1688 | var days = this.getDates(firstDate, 42); |
|
1689 | for (var i = 0; i < 42; i ++) { |
|
1690 | days[i] = angular.extend(this.createDateObject(days[i], this.formatDay), { |
|
1691 | secondary: days[i].getMonth() !== month, |
|
1692 | uid: scope.uniqueId + '-' + i |
|
1693 | }); |
|
1694 | } |
|
1695 | ||
1696 | scope.labels = new Array(7); |
|
1697 | for (var j = 0; j < 7; j++) { |
|
1698 | scope.labels[j] = { |
|
1699 | abbr: dateFilter(days[j].date, this.formatDayHeader), |
|
1700 | full: dateFilter(days[j].date, 'EEEE') |
|
1701 | }; |
|
1702 | } |
|
1703 | ||
1704 | scope.title = dateFilter(this.activeDate, this.formatDayTitle); |
|
1705 | scope.rows = this.split(days, 7); |
|
1706 | ||
1707 | if (scope.showWeeks) { |
|
1708 | scope.weekNumbers = []; |
|
1709 | var thursdayIndex = (4 + 7 - this.startingDay) % 7, |
|
1710 | numWeeks = scope.rows.length; |
|
1711 | for (var curWeek = 0; curWeek < numWeeks; curWeek++) { |
|
1712 | scope.weekNumbers.push( |
|
1713 | getISO8601WeekNumber(scope.rows[curWeek][thursdayIndex].date)); |
|
1714 | } |
|
1715 | } |
|
1716 | }; |
|
1717 | ||
1718 | this.compare = function(date1, date2) { |
|
1719 | var _date1 = new Date(date1.getFullYear(), date1.getMonth(), date1.getDate()); |
|
1720 | var _date2 = new Date(date2.getFullYear(), date2.getMonth(), date2.getDate()); |
|
1721 | _date1.setFullYear(date1.getFullYear()); |
|
1722 | _date2.setFullYear(date2.getFullYear()); |
|
1723 | return _date1 - _date2; |
|
1724 | }; |
|
1725 | ||
1726 | function getISO8601WeekNumber(date) { |
|
1727 | var checkDate = new Date(date); |
|
1728 | checkDate.setDate(checkDate.getDate() + 4 - (checkDate.getDay() || 7)); // Thursday |
|
1729 | var time = checkDate.getTime(); |
|
1730 | checkDate.setMonth(0); // Compare with Jan 1 |
|
1731 | checkDate.setDate(1); |
|
1732 | return Math.floor(Math.round((time - checkDate) / 86400000) / 7) + 1; |
|
1733 | } |
|
1734 | ||
1735 | this.handleKeyDown = function(key, evt) { |
|
1736 | var date = this.activeDate.getDate(); |
|
1737 | ||
1738 | if (key === 'left') { |
|
1739 | date = date - 1; |
|
1740 | } else if (key === 'up') { |
|
1741 | date = date - 7; |
|
1742 | } else if (key === 'right') { |
|
1743 | date = date + 1; |
|
1744 | } else if (key === 'down') { |
|
1745 | date = date + 7; |
|
1746 | } else if (key === 'pageup' || key === 'pagedown') { |
|
1747 | var month = this.activeDate.getMonth() + (key === 'pageup' ? - 1 : 1); |
|
1748 | this.activeDate.setMonth(month, 1); |
|
1749 | date = Math.min(getDaysInMonth(this.activeDate.getFullYear(), this.activeDate.getMonth()), date); |
|
1750 | } else if (key === 'home') { |
|
1751 | date = 1; |
|
1752 | } else if (key === 'end') { |
|
1753 | date = getDaysInMonth(this.activeDate.getFullYear(), this.activeDate.getMonth()); |
|
1754 | } |
|
1755 | this.activeDate.setDate(date); |
|
1756 | }; |
|
1757 | }]) |
|
1758 | ||
1759 | .controller('UibMonthpickerController', ['$scope', '$element', 'dateFilter', function(scope, $element, dateFilter) { |
|
1760 | this.step = { years: 1 }; |
@@ 1644-1756 (lines=113) @@ | ||
1641 | } |
|
1642 | }]) |
|
1643 | ||
1644 | .controller('UibDaypickerController', ['$scope', '$element', 'dateFilter', function(scope, $element, dateFilter) { |
|
1645 | var DAYS_IN_MONTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; |
|
1646 | ||
1647 | this.step = { months: 1 }; |
|
1648 | this.element = $element; |
|
1649 | function getDaysInMonth(year, month) { |
|
1650 | return month === 1 && year % 4 === 0 && |
|
1651 | (year % 100 !== 0 || year % 400 === 0) ? 29 : DAYS_IN_MONTH[month]; |
|
1652 | } |
|
1653 | ||
1654 | this.init = function(ctrl) { |
|
1655 | angular.extend(ctrl, this); |
|
1656 | scope.showWeeks = ctrl.showWeeks; |
|
1657 | ctrl.refreshView(); |
|
1658 | }; |
|
1659 | ||
1660 | this.getDates = function(startDate, n) { |
|
1661 | var dates = new Array(n), current = new Date(startDate), i = 0, date; |
|
1662 | while (i < n) { |
|
1663 | date = new Date(current); |
|
1664 | dates[i++] = date; |
|
1665 | current.setDate(current.getDate() + 1); |
|
1666 | } |
|
1667 | return dates; |
|
1668 | }; |
|
1669 | ||
1670 | this._refreshView = function() { |
|
1671 | var year = this.activeDate.getFullYear(), |
|
1672 | month = this.activeDate.getMonth(), |
|
1673 | firstDayOfMonth = new Date(this.activeDate); |
|
1674 | ||
1675 | firstDayOfMonth.setFullYear(year, month, 1); |
|
1676 | ||
1677 | var difference = this.startingDay - firstDayOfMonth.getDay(), |
|
1678 | numDisplayedFromPreviousMonth = difference > 0 ? |
|
1679 | 7 - difference : - difference, |
|
1680 | firstDate = new Date(firstDayOfMonth); |
|
1681 | ||
1682 | if (numDisplayedFromPreviousMonth > 0) { |
|
1683 | firstDate.setDate(-numDisplayedFromPreviousMonth + 1); |
|
1684 | } |
|
1685 | ||
1686 | // 42 is the number of days on a six-week calendar |
|
1687 | var days = this.getDates(firstDate, 42); |
|
1688 | for (var i = 0; i < 42; i ++) { |
|
1689 | days[i] = angular.extend(this.createDateObject(days[i], this.formatDay), { |
|
1690 | secondary: days[i].getMonth() !== month, |
|
1691 | uid: scope.uniqueId + '-' + i |
|
1692 | }); |
|
1693 | } |
|
1694 | ||
1695 | scope.labels = new Array(7); |
|
1696 | for (var j = 0; j < 7; j++) { |
|
1697 | scope.labels[j] = { |
|
1698 | abbr: dateFilter(days[j].date, this.formatDayHeader), |
|
1699 | full: dateFilter(days[j].date, 'EEEE') |
|
1700 | }; |
|
1701 | } |
|
1702 | ||
1703 | scope.title = dateFilter(this.activeDate, this.formatDayTitle); |
|
1704 | scope.rows = this.split(days, 7); |
|
1705 | ||
1706 | if (scope.showWeeks) { |
|
1707 | scope.weekNumbers = []; |
|
1708 | var thursdayIndex = (4 + 7 - this.startingDay) % 7, |
|
1709 | numWeeks = scope.rows.length; |
|
1710 | for (var curWeek = 0; curWeek < numWeeks; curWeek++) { |
|
1711 | scope.weekNumbers.push( |
|
1712 | getISO8601WeekNumber(scope.rows[curWeek][thursdayIndex].date)); |
|
1713 | } |
|
1714 | } |
|
1715 | }; |
|
1716 | ||
1717 | this.compare = function(date1, date2) { |
|
1718 | var _date1 = new Date(date1.getFullYear(), date1.getMonth(), date1.getDate()); |
|
1719 | var _date2 = new Date(date2.getFullYear(), date2.getMonth(), date2.getDate()); |
|
1720 | _date1.setFullYear(date1.getFullYear()); |
|
1721 | _date2.setFullYear(date2.getFullYear()); |
|
1722 | return _date1 - _date2; |
|
1723 | }; |
|
1724 | ||
1725 | function getISO8601WeekNumber(date) { |
|
1726 | var checkDate = new Date(date); |
|
1727 | checkDate.setDate(checkDate.getDate() + 4 - (checkDate.getDay() || 7)); // Thursday |
|
1728 | var time = checkDate.getTime(); |
|
1729 | checkDate.setMonth(0); // Compare with Jan 1 |
|
1730 | checkDate.setDate(1); |
|
1731 | return Math.floor(Math.round((time - checkDate) / 86400000) / 7) + 1; |
|
1732 | } |
|
1733 | ||
1734 | this.handleKeyDown = function(key, evt) { |
|
1735 | var date = this.activeDate.getDate(); |
|
1736 | ||
1737 | if (key === 'left') { |
|
1738 | date = date - 1; |
|
1739 | } else if (key === 'up') { |
|
1740 | date = date - 7; |
|
1741 | } else if (key === 'right') { |
|
1742 | date = date + 1; |
|
1743 | } else if (key === 'down') { |
|
1744 | date = date + 7; |
|
1745 | } else if (key === 'pageup' || key === 'pagedown') { |
|
1746 | var month = this.activeDate.getMonth() + (key === 'pageup' ? - 1 : 1); |
|
1747 | this.activeDate.setMonth(month, 1); |
|
1748 | date = Math.min(getDaysInMonth(this.activeDate.getFullYear(), this.activeDate.getMonth()), date); |
|
1749 | } else if (key === 'home') { |
|
1750 | date = 1; |
|
1751 | } else if (key === 'end') { |
|
1752 | date = getDaysInMonth(this.activeDate.getFullYear(), this.activeDate.getMonth()); |
|
1753 | } |
|
1754 | this.activeDate.setDate(date); |
|
1755 | }; |
|
1756 | }]) |
|
1757 | ||
1758 | .controller('UibMonthpickerController', ['$scope', '$element', 'dateFilter', function(scope, $element, dateFilter) { |
|
1759 | this.step = { years: 1 }; |