PageRenderTime 53ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/js/trello_calendar.js

https://bitbucket.org/mendisahsp/trello-calendar
JavaScript | 176 lines | 138 code | 22 blank | 16 comment | 9 complexity | 84caa3df6a6d902bb0285ba5947cc82f MD5 | raw file
Possible License(s): AGPL-3.0
  1. var App = {
  2. model: {},
  3. collection: {},
  4. view: {},
  5. };
  6. /**
  7. * Card model
  8. */
  9. App.model.Card = Backbone.Model.extend({
  10. sync: function(method, model, options) {
  11. if (method == 'update') {
  12. // only support update due date
  13. Trello.put('/cards/'+ model.id, {due: model.get('badges').due}, options.success, options.error);
  14. }
  15. }
  16. });
  17. /**
  18. * Cards collection
  19. */
  20. App.collection.Cards = Backbone.Collection.extend({});
  21. /**
  22. * Board model
  23. */
  24. App.model.Board = Backbone.Model.extend({});
  25. /**
  26. * Board collection
  27. */
  28. App.collection.Boards = Backbone.Collection.extend({});
  29. /**
  30. * Render a card on fullcalendar
  31. */
  32. App.view.Card = Backbone.View.extend({
  33. initialize: function() {
  34. this.model.bind('change', this.render, this);
  35. },
  36. render: function() {
  37. if (this.model.get('hidden')) {
  38. $(this.el).fullCalendar('removeEvents', this.model.id);
  39. } else {
  40. $(this.el).fullCalendar('removeEvents', this.model.id);
  41. $(this.el).fullCalendar('renderEvent', {
  42. id: this.model.id,
  43. allDay: false,
  44. title: this.model.get('name'),
  45. start: this.model.get('badges').due,
  46. color: _(this.model.get('idMembers')).include(this.options.me.id) ? 'red' : 'green',
  47. url: this.model.get('url')
  48. }, true);
  49. }
  50. return this;
  51. }
  52. });
  53. App.view.Board = Backbone.View.extend({
  54. events: {
  55. "click input": "click"
  56. },
  57. tagName: 'label',
  58. click: function(e) {
  59. var hidden = true;
  60. if ($(e.target).is(':checked')) {
  61. hidden = false
  62. }
  63. this.model.set({hidden: hidden});
  64. $(this.el).toggleClass('checked');
  65. },
  66. render: function() {
  67. var input = this.make('input', {type: 'checkbox',
  68. value: this.model.id,
  69. checked: true});
  70. $(this.el).toggleClass('checked')
  71. .attr('title', 'Show cards from the board '+ this.model.get('name'))
  72. .text(this.model.get('name'))
  73. .append(input);
  74. return this;
  75. }
  76. });
  77. $(document).ready(function() {
  78. var defaultOptions = {
  79. scope: {
  80. write: true
  81. },
  82. success: onAuthorize
  83. }
  84. Trello.authorize(_.extend({}, defaultOptions, {
  85. interactive: false
  86. }));
  87. if (!Trello.authorized()) {
  88. return Trello.authorize(defaultOptions);
  89. }
  90. var currentUser;
  91. var boards = new App.collection.Boards();
  92. var cards = new App.collection.Cards();
  93. var calendar = $('#calendar').fullCalendar({
  94. header: {
  95. left: 'prev,next today',
  96. center: 'title',
  97. right: 'month,agendaWeek,agendaDay'
  98. },
  99. height: $(document).height() - 50,
  100. editable: true,
  101. disableResizing: true,
  102. ignoreTimezone: false,
  103. timeFormat: "H'h'(mm)",
  104. eventDrop: function(event, dayDelta, minuteDelta, allDay, revertFunc) {
  105. var card = cards.get(event.id);
  106. var date = moment(event.start).format("YYYY-MM-DDTHH:mm:ssZ");
  107. var badges = _.extend({}, card.get('badges'), {due: date});
  108. card.set({badges: badges});
  109. card.save();
  110. }
  111. });
  112. $(window).resize(function() {
  113. calendar.fullCalendar('option', 'height', $(document).height() - 50);
  114. });
  115. function renderCard(card) {
  116. new App.view.Card({model: card,
  117. me: currentUser,
  118. el: calendar.get(0)}).render();
  119. }
  120. function renderBoard(board) {
  121. var view = new App.view.Board({model: board}).render();
  122. $(view.el).appendTo($('#boards'));
  123. board.bind('change:hidden', function() {
  124. cards.chain().filter(function(card) {
  125. return card.get('idBoard') == board.id;
  126. }).each(function(card) {
  127. card.set({hidden: board.get('hidden')});
  128. });
  129. });
  130. }
  131. cards.bind('add', renderCard);
  132. boards.bind('add', renderBoard);
  133. function listBoards(me) {
  134. return function(tBoards) {
  135. _(tBoards).each(function(board) {
  136. boards.add(new App.model.Board(board));
  137. Trello.get('/boards/'+ board.id +'/cards/all', {badges: true}).done(function(tCards) {
  138. _(tCards).each(function(card) {
  139. if (!card.badges.due) return;
  140. cards.add(new App.model.Card(card));
  141. });
  142. });
  143. });
  144. }
  145. }
  146. function showMe(me) {
  147. currentUser = me;
  148. Trello.get('/members/my/boards', {filter: 'open'}).done(listBoards(me));
  149. }
  150. function onAuthorize() {
  151. if (!Trello.authorized()) return Trello.authorize(defaultOptions);
  152. Trello.members.get('me').done(showMe);
  153. }
  154. });