Advertisement
Guest User

Untitled

a guest
Jul 29th, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. (function () {
  2. var _app = global.true.app;
  3.  
  4. _app.directive("calendar", ['$timeout',
  5. function($timeout) {
  6. return {
  7. restrict: "E",
  8. templateUrl: "../components/common/calendar/calendar.tmpl.html",
  9. scope: {
  10. selected: "="
  11. },
  12. link: function(scope, elem) {
  13. scope.selected = _removeTime(scope.selected || moment());
  14. scope.month = scope.selected.clone();
  15.  
  16. var start = scope.selected.clone();
  17. start.date(1);
  18. _removeTime(start.day(0));
  19.  
  20. _buildMonth(scope, start, scope.month);
  21.  
  22. scope.select = function(day) {
  23. scope.selected = day.date;
  24. };
  25.  
  26. scope.next = function() {
  27. var next = scope.month.clone();
  28. _removeTime(next.month(next.month()+1).date(1).day(0));
  29. scope.month.month(scope.month.month()+1);
  30. console.log(scope, next, scope.month);
  31. _buildMonth(scope, next, scope.month);
  32. };
  33.  
  34. scope.previous = function() {
  35. var previous = scope.month.clone();
  36. _removeTime(previous.month(previous.month()-1).date(1).day(0));
  37. scope.month.month(scope.month.month()-1);
  38. _buildMonth(scope, previous, scope.month);
  39. };
  40.  
  41. // Programmatically adjust the height so we're always dealing with circles instead of ovals
  42. $timeout(function () {
  43. var adjustedDayWidth = $('span.adjustHeight')[0].offsetWidth;
  44. $('calendar > div.week > span.day').css('line-height', adjustedDayWidth + 'px');
  45. $('span.adjustHeight').css('height', adjustedDayWidth + 'px');
  46. }, 0);
  47.  
  48. // Re-adjust height when the user clicks, since changing months brings spans in and out without changes
  49. elem.on('click', function () {
  50. $timeout(function () {
  51. var adjustedDayWidth = $('span.adjustHeight')[0].offsetWidth;
  52. $('calendar > div.week > span.day').css('line-height', adjustedDayWidth + 'px');
  53. $('span.adjustHeight').css('height', adjustedDayWidth + 'px');
  54. }, 0);
  55. });
  56. }
  57. };
  58.  
  59. function _removeTime(date) {
  60. return date.hour(0).minute(0).second(0).millisecond(0);
  61. }
  62.  
  63. function _buildMonth(scope, start, month) {
  64. scope.weeks = [];
  65. var done = false, date = start.clone(), monthIndex = date.month(), count = 0;
  66. while (!done) {
  67. scope.weeks.push({ days: _buildWeek(date.clone(), month) });
  68. date.add(1, "w");
  69. done = count++ > 2 && monthIndex !== date.month();
  70. monthIndex = date.month();
  71. }
  72. }
  73.  
  74. function _buildWeek(date, month) {
  75. var days = [];
  76. for (var i = 0; i < 7; i++) {
  77. days.push({
  78. name: date.format("dd").substring(0, 1),
  79. number: date.date(),
  80. isCurrentMonth: date.month() === month.month(),
  81. isToday: date.isSame(new Date(), "day"),
  82. date: date
  83. });
  84. date = date.clone();
  85. date.add(1, "d");
  86. }
  87. return days;
  88. }
  89. }
  90. ]);
  91. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement