/Backup/Src/Bowerbird.Website/js/bowerbird/views/sidebarProjectItemView.js

https://github.com/Bowerbird/bowerbird-web · JavaScript · 121 lines · 88 code · 23 blank · 10 comment · 7 complexity · eb6643617ef1cec014a73dc6d66dc2ff MD5 · raw file

  1. /// <reference path="../../libs/log.js" />
  2. /// <reference path="../../libs/require/require.js" />
  3. /// <reference path="../../libs/jquery/jquery-1.7.2.js" />
  4. /// <reference path="../../libs/underscore/underscore.js" />
  5. /// <reference path="../../libs/backbone/backbone.js" />
  6. /// <reference path="../../libs/backbone.marionette/backbone.marionette.js" />
  7. // SidebarProjectItemView
  8. // ----------------------
  9. define(['jquery', 'underscore', 'backbone', 'ich', 'app', 'models/project', 'tipsy'],
  10. function ($, _, Backbone, ich, app, Project) {
  11. var SidebarProjectItemView = Backbone.Marionette.ItemView.extend({
  12. tagName: 'li',
  13. className: 'menu-group-item',
  14. template: 'SidebarProjectItem',
  15. events: {
  16. 'click .chat-menu-item': 'startChat',
  17. 'click .sub-menu': 'showMenu',
  18. 'click .sub-menu a': 'selectMenuItem'
  19. },
  20. initialize: function () {
  21. this.activityCount = 0;
  22. },
  23. serializeData: function () {
  24. return {
  25. Id: this.model.id,
  26. Name: this.model.get('Name'),
  27. Avatar: this.model.get('Avatar'),
  28. IsMember: app.authenticatedUser.hasGroupRole(this.model.id, 'roles/projectmember'),
  29. IsAdministrator: app.authenticatedUser.hasGroupRole(this.model.id, 'roles/projectadministrator')
  30. };
  31. },
  32. onRender: function () {
  33. var that = this;
  34. this.$el.children('a').on('click', function (e) {
  35. e.preventDefault();
  36. Backbone.history.navigate($(this).attr('href'), { trigger: true });
  37. that.activityCount = 0;
  38. that.$el.find('p span').remove();
  39. return false;
  40. });
  41. app.vent.on('newactivity:' + this.model.id + ':sightingadded newactivity:' + this.model.id + ':postadded newactivity:' + this.model.id + ':sightingnoteadded', this.onNewActivityReceived, this);
  42. this.$el.find('#project-menu-group-list .sub-menu a, #project-menu-group-list .sub-menu span').tipsy({ gravity: 'w', live: true });
  43. },
  44. showMenu: function (e) {
  45. app.vent.trigger('close-sub-menus');
  46. $(e.currentTarget).addClass('active');
  47. var offsetFromTopOfViewport = $(e.currentTarget).offset().top - $(window).scrollTop();
  48. var offsetFromLeftOfViewport = ($(e.currentTarget).offset().left - $(window).scrollLeft()) + 25; // 25 = position popup away from arrow
  49. var windowHeight = $(window).height();
  50. var $popup = $(e.currentTarget).find('ul');
  51. var popupHeight = $popup.height();
  52. log('popupHeight', popupHeight);
  53. log('windowHeight', windowHeight);
  54. log('offsetFromTopOfViewport', offsetFromTopOfViewport);
  55. // if popup is being cut off at bottom of viewport, bring it back up into view
  56. if (offsetFromTopOfViewport + popupHeight > windowHeight) {
  57. offsetFromTopOfViewport = windowHeight - popupHeight;
  58. }
  59. // if the popup is still too low (ie low enough to be blocked by collapsed chat window, bring it up further
  60. var chatOverhang = (offsetFromTopOfViewport + popupHeight) - (windowHeight - 50);
  61. if (chatOverhang > 0) {
  62. offsetFromTopOfViewport = offsetFromTopOfViewport - chatOverhang;
  63. }
  64. $popup.css({
  65. position: 'fixed',
  66. top: offsetFromTopOfViewport + 'px',
  67. left: offsetFromLeftOfViewport + 'px'
  68. });
  69. e.stopPropagation();
  70. },
  71. selectMenuItem: function (e) {
  72. e.preventDefault();
  73. app.vent.trigger('close-sub-menus');
  74. Backbone.history.navigate($(e.currentTarget).attr('href'), { trigger: true });
  75. return false;
  76. },
  77. startChat: function (e) {
  78. e.preventDefault();
  79. app.vent.trigger('chats:joinGroupChat', this.model);
  80. },
  81. onNewActivityReceived: function (activity) {
  82. if (app.authenticatedUser.user.id != activity.get('User').Id) {
  83. _.each(activity.get('Groups'), function(group) {
  84. if (group.Id === this.model.id) {
  85. this.activityCount++;
  86. if (this.activityCount == 1) {
  87. this.$el.find('p').append('<span title=""></span>');
  88. }
  89. var title = this.activityCount.toString() + ' New Item' + (this.activityCount > 1 ? 's' : '');
  90. this.$el.find('p span').text(this.activityCount).attr('title', title);
  91. }
  92. },
  93. this);
  94. }
  95. }
  96. });
  97. return SidebarProjectItemView;
  98. });