Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function () {
- var _app = global.true.app;
- _app.directive("calendar", ['$timeout',
- function($timeout) {
- return {
- restrict: "E",
- templateUrl: "../components/common/calendar/calendar.tmpl.html",
- scope: {
- selected: "="
- },
- link: function(scope, elem) {
- scope.selected = _removeTime(scope.selected || moment());
- scope.month = scope.selected.clone();
- var start = scope.selected.clone();
- start.date(1);
- _removeTime(start.day(0));
- _buildMonth(scope, start, scope.month);
- scope.select = function(day) {
- scope.selected = day.date;
- };
- scope.next = function() {
- var next = scope.month.clone();
- _removeTime(next.month(next.month()+1).date(1).day(0));
- scope.month.month(scope.month.month()+1);
- console.log(scope, next, scope.month);
- _buildMonth(scope, next, scope.month);
- };
- scope.previous = function() {
- var previous = scope.month.clone();
- _removeTime(previous.month(previous.month()-1).date(1).day(0));
- scope.month.month(scope.month.month()-1);
- _buildMonth(scope, previous, scope.month);
- };
- // Programmatically adjust the height so we're always dealing with circles instead of ovals
- $timeout(function () {
- var adjustedDayWidth = $('span.adjustHeight')[0].offsetWidth;
- $('calendar > div.week > span.day').css('line-height', adjustedDayWidth + 'px');
- $('span.adjustHeight').css('height', adjustedDayWidth + 'px');
- }, 0);
- // Re-adjust height when the user clicks, since changing months brings spans in and out without changes
- elem.on('click', function () {
- $timeout(function () {
- var adjustedDayWidth = $('span.adjustHeight')[0].offsetWidth;
- $('calendar > div.week > span.day').css('line-height', adjustedDayWidth + 'px');
- $('span.adjustHeight').css('height', adjustedDayWidth + 'px');
- }, 0);
- });
- }
- };
- function _removeTime(date) {
- return date.hour(0).minute(0).second(0).millisecond(0);
- }
- function _buildMonth(scope, start, month) {
- scope.weeks = [];
- var done = false, date = start.clone(), monthIndex = date.month(), count = 0;
- while (!done) {
- scope.weeks.push({ days: _buildWeek(date.clone(), month) });
- date.add(1, "w");
- done = count++ > 2 && monthIndex !== date.month();
- monthIndex = date.month();
- }
- }
- function _buildWeek(date, month) {
- var days = [];
- for (var i = 0; i < 7; i++) {
- days.push({
- name: date.format("dd").substring(0, 1),
- number: date.date(),
- isCurrentMonth: date.month() === month.month(),
- isToday: date.isSame(new Date(), "day"),
- date: date
- });
- date = date.clone();
- date.add(1, "d");
- }
- return days;
- }
- }
- ]);
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement