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

https://github.com/Bowerbird/bowerbird-web · JavaScript · 154 lines · 116 code · 27 blank · 11 comment · 17 complexity · 9e4cf5be0b4f0d3704460c8cd7db1f91 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. // SidebarUserProjectItemView
  8. // --------------------------
  9. define(['jquery', 'underscore', 'backbone', 'ich', 'app', 'models/userproject', 'tipsy'],
  10. function ($, _, Backbone, ich, app, UserProject) {
  11. var SidebarUserProjectItemView = Backbone.Marionette.ItemView.extend({
  12. tagName: 'li',
  13. className: 'menu-group-item',
  14. template: 'SidebarUserProjectItem',
  15. events: {
  16. 'click .chat-menu-item': 'startChat',
  17. 'click .sub-menu': 'showMenu',
  18. 'click .sub-menu a': 'selectMenuItem'
  19. },
  20. initialize: function () {
  21. _.bindAll(this, 'onStatusChange');
  22. this.activityCount = 0;
  23. },
  24. onRender: function () {
  25. var that = this;
  26. this.$el.children('a').on('click', function (e) {
  27. e.preventDefault();
  28. Backbone.history.navigate($(this).attr('href'), { trigger: true });
  29. that.activityCount = 0;
  30. that.$el.find('p span').remove();
  31. return false;
  32. });
  33. app.vent.on('newactivity:' + this.model.id + ':sightingadded newactivity:' + this.model.id + ':postadded newactivity:' + this.model.id + ':sightingnoteadded', this.onNewActivityReceived, this);
  34. if (app.authenticatedUser) {
  35. //log('chat', this.model.get('CreatedBy'), app.onlineUsers.get(this.model.get('CreatedBy')));
  36. app.onlineUsers.on('add', function (item) {
  37. if (item.id === that.model.get('CreatedBy')) {
  38. that.chatUser = item;
  39. that.chatUser.on('statuschange', this.onStatusChange, this);
  40. that.$el.find('ul').append(ich.Buttons({ MenuChat: true, Id: that.model.id }));
  41. }
  42. });
  43. app.onlineUsers.on('remove', function (item) {
  44. if (item.id === that.model.get('CreatedBy')) {
  45. that.chatUser = null;
  46. that.chatUser.off('statuschange');
  47. that.$el.find('ul .chat-menu-item').remove();
  48. }
  49. });
  50. }
  51. this.$el.find('#userproject-menu-group-list .sub-menu a, #userproject-menu-group-list .sub-menu span').tipsy({ gravity: 'w', live: true });
  52. },
  53. serializeData: function () {
  54. return {
  55. Id: this.model.get('CreatedBy'),
  56. Name: this.model.get('Name'),
  57. Avatar: this.model.get('Avatar')
  58. };
  59. },
  60. showMenu: function (e) {
  61. app.vent.trigger('close-sub-menus');
  62. $(e.currentTarget).addClass('active');
  63. var offsetFromTopOfViewport = $(e.currentTarget).offset().top - $(window).scrollTop();
  64. var offsetFromLeftOfViewport = ($(e.currentTarget).offset().left - $(window).scrollLeft()) + 25; // 25 = position popup away from arrow
  65. var windowHeight = $(window).height();
  66. var $popup = $(e.currentTarget).find('ul');
  67. var popupHeight = $popup.height();
  68. log('popupHeight', popupHeight);
  69. log('windowHeight', windowHeight);
  70. log('offsetFromTopOfViewport', offsetFromTopOfViewport);
  71. // if popup is being cut off at bottom of viewport, bring it back up into view
  72. if (offsetFromTopOfViewport + popupHeight > windowHeight) {
  73. offsetFromTopOfViewport = windowHeight - popupHeight;
  74. }
  75. // if the popup is still too low (ie low enough to be blocked by collapsed chat window, bring it up further
  76. var chatOverhang = (offsetFromTopOfViewport + popupHeight) - (windowHeight - 50);
  77. if (chatOverhang > 0) {
  78. offsetFromTopOfViewport = offsetFromTopOfViewport - chatOverhang;
  79. }
  80. $popup.css({
  81. position: 'fixed',
  82. top: offsetFromTopOfViewport + 'px',
  83. left: offsetFromLeftOfViewport + 'px'
  84. });
  85. e.stopPropagation();
  86. },
  87. selectMenuItem: function (e) {
  88. e.preventDefault();
  89. app.vent.trigger('close-sub-menus');
  90. Backbone.history.navigate($(e.currentTarget).attr('href'), { trigger: true });
  91. return false;
  92. },
  93. startChat: function (e) {
  94. e.preventDefault();
  95. app.vent.trigger('close-sub-menus');
  96. if (this.chatUser.getCurrentStatus() != 'offline') {
  97. app.vent.trigger('chats:startPrivateChat', this.chatUser);
  98. }
  99. return false;
  100. },
  101. onNewActivityReceived: function (activity) {
  102. if (app.authenticatedUser.user.id != activity.get('User').Id) {
  103. _.each(activity.get('Groups'), function(group) {
  104. if (group.Id === this.model.id) {
  105. this.activityCount++;
  106. if (this.activityCount == 1) {
  107. this.$el.find('p').append('<span title=""></span>');
  108. }
  109. var title = this.activityCount.toString() + ' New Item' + (this.activityCount > 1 ? 's' : '');
  110. this.$el.find('p span').text(this.activityCount).attr('title', title);
  111. }
  112. },
  113. this);
  114. }
  115. },
  116. onStatusChange: function (userStatus) {
  117. if (userStatus.status === 'online') {
  118. this.$el.find('.chat-menu-item').show();
  119. } else if (userStatus.status === 'away') {
  120. this.$el.find('.chat-menu-item').show();
  121. } else if (userStatus.status === 'offline') {
  122. this.$el.find('.chat-menu-item').hide();
  123. }
  124. }
  125. });
  126. return SidebarUserProjectItemView;
  127. });