Advertisement
p4geoff

Untitled

Dec 11th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.10 KB | None | 0 0
  1. /**
  2.  * Perforce Swarm
  3.  *
  4.  * @copyright   2012 Perforce Software. All rights reserved.
  5.  * @license     Please see LICENSE.txt in top-level folder of this distribution.
  6.  * @version     <release>/<patch>
  7.  */
  8.  
  9. swarm.activity = {
  10.     _loading: false,
  11.  
  12.     init: function(stream) {
  13.         // honor user's type filter preference
  14.         // select by attr as the stream may contain special characters such as period
  15.         // that cause issue when doing a simple class selection
  16.         var table = $("[class~='stream-" + (stream || 'global') + "']");
  17.         var type  = swarm.localStorage.get('activity.type');
  18.         if (type) {
  19.             table.find('th .nav-pills a.type-' + type).closest('li').addClass('active');
  20.         }
  21.  
  22.         // wire up stream type filter buttons.
  23.         table.find('th .nav-pills a').click(function(){
  24.             var type   = $(this).attr('class').match(/type-([\w]+)/).pop();
  25.             var active = $(this).closest('li').is('.active');
  26.  
  27.             $(this).closest('.nav-pills').find('li').removeClass('active');
  28.             $(this).closest('li').toggleClass('active', !active);
  29.             swarm.activity.load(stream, true);
  30.  
  31.             // remember the user's selection.
  32.             swarm.localStorage.set('activity.type', !active ? type : null);
  33.  
  34.             return false;
  35.         });
  36.  
  37.         // wire-up activity dropdown to to switch between all/personal-activity
  38.         table.find('ul.dropdown-menu a').on('click', function (e) {
  39.             e.preventDefault();
  40.             var scope = $(this).closest('li').data('scope');
  41.             swarm.localStorage.set('activity.scope', scope);
  42.             swarm.activity.load(stream, true);
  43.         });
  44.  
  45.         // prevent from opening a dropdown when clicked on dropdown-toggle which
  46.         // is not inside a dropdown as we always put a dropdown menu in the markup
  47.         table.find('.dropdown-toggle').on('click', function (e) {
  48.             if (!$(this).closest('.dropdown').length) {
  49.                 e.stopPropagation();
  50.             }
  51.         });
  52.  
  53.         // update activity table when user logs in
  54.         $(document).on('swarm-login', function () {
  55.             swarm.activity.load(stream, true);
  56.         });
  57.     },
  58.  
  59.     load: function(stream, reset) {
  60.         if (swarm.activity._loading) {
  61.             if (!reset) {
  62.                 return;
  63.             }
  64.  
  65.             swarm.activity._loading.abort();
  66.             swarm.activity._loading = false;
  67.         }
  68.  
  69.         // select by attr as the stream may contain special characters such as period
  70.         // that cause issue when doing a simple class selection
  71.         var table = $("[class~='stream-" + (stream || 'global') + "']");
  72.  
  73.         // tweak table for authenticated user
  74.         var originalStream = stream,
  75.             scope          = swarm.localStorage.get('activity.scope') || 'user',
  76.             user           = swarm.user.getAuthenticatedUser(),
  77.             isSwitchable   = table.is('.switchable') && user !== null,
  78.             isPersonal     = isSwitchable && scope === 'user';
  79.  
  80.         // change stream to personal if in personal view
  81.         if (isPersonal) {
  82.             stream = 'personal-' + user.id;
  83.         }
  84.  
  85.         // apply type filter
  86.         // the data-type-filter trumps the filter buttons
  87.         // if data-type-filter is set, the filter buttons are disabled
  88.         var type  = table.data('type-filter');
  89.         if (type === null) {
  90.             type  = table.find('th .nav-pills li.active a');
  91.             type  = type.length && type.attr('class').match(/type-([\w]+)/).pop();
  92.         }
  93.  
  94.         // only load activity older than the last loaded row
  95.         var last  = !reset ? $('.activity-stream tbody tr.row-main:last').attr('id') : null;
  96.  
  97.         // prepare urls for activity stream
  98.         var url    = '/activity' + (stream ? '/streams/' + encodeURIComponent(stream) : '');
  99.  
  100.         swarm.activity._loading = $.ajax({
  101.             url:        url,
  102.             data:       {after: last, type: type || null},
  103.             dataType:   'json',
  104.             cache:      false,
  105.             success:    function(data){
  106.                 // we cancel and reload in global view if this is default personal view
  107.                 // and user's personal stream is empty
  108.                 if (!swarm.localStorage.get('activity.scope') && isPersonal && !data.length) {
  109.                     swarm.localStorage.set('activity.scope', 'all');
  110.                     swarm.activity._loading = false;
  111.                     swarm.activity.load(originalStream, true);
  112.                     return;
  113.                 }
  114.  
  115.                 // update title if this is a switchable stream
  116.                 // we can do this now because we know we are going to stay on this stream
  117.                 if (isSwitchable) {
  118.                     table.find('thead .activity-title').removeClass('default-title').text(
  119.                         isPersonal ? 'Followed Activity' : 'All Activity'
  120.                     );
  121.                 }
  122.  
  123.                 // enable dropdown if user is logged in
  124.                 table.find('thead .activity-dropdown').toggleClass('dropdown', isSwitchable);
  125.  
  126.                 // add 'stream-personal' class to the table if user is logged in
  127.                 table.toggleClass('stream-personal', isPersonal);
  128.  
  129.                 // update rss link url
  130.                 table.find('a.rss-link').attr('href', url + '/rss');
  131.  
  132.                 if (reset) {
  133.                     table.find('tbody').empty();
  134.                 }
  135.  
  136.                 // if we have no activity to show, let the user know.
  137.                 if (!table.find('tbody tr').length && !data.length) {
  138.                     table.find('tbody').append($(
  139.                         '<tr class="activity-info"><td><div class="alert border-box pad3">'
  140.                       + 'No ' + (type ? 'matching ' : '') + 'activity.'
  141.                       + '</div></td></tr>'
  142.                     ));
  143.                 }
  144.  
  145.                 var event, html;
  146.                 $.each(data, function(key, event){
  147.                     // prepare comment link
  148.                     var count      = event.comments;
  149.                     event.comments = {
  150.                         count:   count,
  151.                         label:   count
  152.                             ? count + ' comment' + (count !== 1 ? 's' : '')
  153.                             : 'Add a comment',
  154.                         url:     event.url + (event.url && !event.url.match('#') ? '#comments' : '')
  155.                     };
  156.                     event.condensed = table.is('.condensed');
  157.                     event.rowClass  = (event.topic  ? 'has-topic ' : '')
  158.                                     + (event.type   ? 'activity-type-'   + event.type + ' ' : '')
  159.                                     + (event.action ? 'activity-action-' + event.action.toLowerCase().replace(/ /g, '-') : '');
  160.                     html = $.templates(
  161.                           '<tr id="{{>id}}" class="row-main {{>rowClass}}">'
  162.                         + ' <td rowspan="2" width=64>{{:avatar}}</td>'
  163.                         + ' <td>'
  164.                         + '  {{if !condensed}}'
  165.                         + '    <small class="pull-right"><span class="timeago muted" title="{{>date}}"></span></small>'
  166.                         + '  {{/if}}'
  167.                         + '  {{if user}}{{if userExists}}<a href="/users/{{urlc:user}}">{{/if}}'
  168.                         + '    <strong>{{>user}}</strong>'
  169.                         + '  {{if userExists}}</a>{{/if}}{{/if}}'
  170.                         + '  {{>action}} '
  171.                         + '  {{if url}}<a href="{{:url}}">{{/if}}{{>target}}{{if url}}</a>{{/if}}'
  172.                         + '  {{if preposition && projectList}}{{>preposition}} {{:projectList}}{{/if}}'
  173.                         + '  <p class="description force-wrap">{{:description}}</p>'
  174.                         + ' </td>'
  175.                         + '</tr>'
  176.                         + '<tr id="{{>id}}-append" class="row-append">'
  177.                         + ' <td>'
  178.                         + '{{if condensed}}'
  179.                         + '  <small><span class="timeago muted" title="{{>date}}"></span></small>'
  180.                         + '{{/if}}'
  181.                         + '{{if topic}}'
  182.                         + '  <div class="comment-link{{if !comments.count}} no-comments{{/if}}">'
  183.                         + '   <small><i class="icon-comment"></i>'
  184.                         + '   <a href="{{:comments.url}}">{{:comments.label}}</a></small>'
  185.                         + '  </div>'
  186.                         + '{{/if}}'
  187.                         + ' </td>'
  188.                         + '</tr>'
  189.                     ).render(event);
  190.  
  191.                     var row = $(html);
  192.                     table.find('tbody').append(row);
  193.  
  194.                     // truncate the description
  195.                     row.find('p.description').expander();
  196.  
  197.                     // convert times to time-ago
  198.                     row.find('.timeago').timeago();
  199.                 });
  200.  
  201.                 // enforce a minimal delay between requests
  202.                 setTimeout(function(){ swarm.activity._loading = false; }, 500);
  203.             }
  204.         });
  205.     }
  206. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement