Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Perforce Swarm
- *
- * @copyright 2012 Perforce Software. All rights reserved.
- * @license Please see LICENSE.txt in top-level folder of this distribution.
- * @version <release>/<patch>
- */
- swarm.activity = {
- _loading: false,
- init: function(stream) {
- // honor user's type filter preference
- // select by attr as the stream may contain special characters such as period
- // that cause issue when doing a simple class selection
- var table = $("[class~='stream-" + (stream || 'global') + "']");
- var type = swarm.localStorage.get('activity.type');
- if (type) {
- table.find('th .nav-pills a.type-' + type).closest('li').addClass('active');
- }
- // wire up stream type filter buttons.
- table.find('th .nav-pills a').click(function(){
- var type = $(this).attr('class').match(/type-([\w]+)/).pop();
- var active = $(this).closest('li').is('.active');
- $(this).closest('.nav-pills').find('li').removeClass('active');
- $(this).closest('li').toggleClass('active', !active);
- swarm.activity.load(stream, true);
- // remember the user's selection.
- swarm.localStorage.set('activity.type', !active ? type : null);
- return false;
- });
- // wire-up activity dropdown to to switch between all/personal-activity
- table.find('ul.dropdown-menu a').on('click', function (e) {
- e.preventDefault();
- var scope = $(this).closest('li').data('scope');
- swarm.localStorage.set('activity.scope', scope);
- swarm.activity.load(stream, true);
- });
- // prevent from opening a dropdown when clicked on dropdown-toggle which
- // is not inside a dropdown as we always put a dropdown menu in the markup
- table.find('.dropdown-toggle').on('click', function (e) {
- if (!$(this).closest('.dropdown').length) {
- e.stopPropagation();
- }
- });
- // update activity table when user logs in
- $(document).on('swarm-login', function () {
- swarm.activity.load(stream, true);
- });
- },
- load: function(stream, reset) {
- if (swarm.activity._loading) {
- if (!reset) {
- return;
- }
- swarm.activity._loading.abort();
- swarm.activity._loading = false;
- }
- // select by attr as the stream may contain special characters such as period
- // that cause issue when doing a simple class selection
- var table = $("[class~='stream-" + (stream || 'global') + "']");
- // tweak table for authenticated user
- var originalStream = stream,
- scope = swarm.localStorage.get('activity.scope') || 'user',
- user = swarm.user.getAuthenticatedUser(),
- isSwitchable = table.is('.switchable') && user !== null,
- isPersonal = isSwitchable && scope === 'user';
- // change stream to personal if in personal view
- if (isPersonal) {
- stream = 'personal-' + user.id;
- }
- // apply type filter
- // the data-type-filter trumps the filter buttons
- // if data-type-filter is set, the filter buttons are disabled
- var type = table.data('type-filter');
- if (type === null) {
- type = table.find('th .nav-pills li.active a');
- type = type.length && type.attr('class').match(/type-([\w]+)/).pop();
- }
- // only load activity older than the last loaded row
- var last = !reset ? $('.activity-stream tbody tr.row-main:last').attr('id') : null;
- // prepare urls for activity stream
- var url = '/activity' + (stream ? '/streams/' + encodeURIComponent(stream) : '');
- swarm.activity._loading = $.ajax({
- url: url,
- data: {after: last, type: type || null},
- dataType: 'json',
- cache: false,
- success: function(data){
- // we cancel and reload in global view if this is default personal view
- // and user's personal stream is empty
- if (!swarm.localStorage.get('activity.scope') && isPersonal && !data.length) {
- swarm.localStorage.set('activity.scope', 'all');
- swarm.activity._loading = false;
- swarm.activity.load(originalStream, true);
- return;
- }
- // update title if this is a switchable stream
- // we can do this now because we know we are going to stay on this stream
- if (isSwitchable) {
- table.find('thead .activity-title').removeClass('default-title').text(
- isPersonal ? 'Followed Activity' : 'All Activity'
- );
- }
- // enable dropdown if user is logged in
- table.find('thead .activity-dropdown').toggleClass('dropdown', isSwitchable);
- // add 'stream-personal' class to the table if user is logged in
- table.toggleClass('stream-personal', isPersonal);
- // update rss link url
- table.find('a.rss-link').attr('href', url + '/rss');
- if (reset) {
- table.find('tbody').empty();
- }
- // if we have no activity to show, let the user know.
- if (!table.find('tbody tr').length && !data.length) {
- table.find('tbody').append($(
- '<tr class="activity-info"><td><div class="alert border-box pad3">'
- + 'No ' + (type ? 'matching ' : '') + 'activity.'
- + '</div></td></tr>'
- ));
- }
- var event, html;
- $.each(data, function(key, event){
- // prepare comment link
- var count = event.comments;
- event.comments = {
- count: count,
- label: count
- ? count + ' comment' + (count !== 1 ? 's' : '')
- : 'Add a comment',
- url: event.url + (event.url && !event.url.match('#') ? '#comments' : '')
- };
- event.condensed = table.is('.condensed');
- event.rowClass = (event.topic ? 'has-topic ' : '')
- + (event.type ? 'activity-type-' + event.type + ' ' : '')
- + (event.action ? 'activity-action-' + event.action.toLowerCase().replace(/ /g, '-') : '');
- html = $.templates(
- '<tr id="{{>id}}" class="row-main {{>rowClass}}">'
- + ' <td rowspan="2" width=64>{{:avatar}}</td>'
- + ' <td>'
- + ' {{if !condensed}}'
- + ' <small class="pull-right"><span class="timeago muted" title="{{>date}}"></span></small>'
- + ' {{/if}}'
- + ' {{if user}}{{if userExists}}<a href="/users/{{urlc:user}}">{{/if}}'
- + ' <strong>{{>user}}</strong>'
- + ' {{if userExists}}</a>{{/if}}{{/if}}'
- + ' {{>action}} '
- + ' {{if url}}<a href="{{:url}}">{{/if}}{{>target}}{{if url}}</a>{{/if}}'
- + ' {{if preposition && projectList}}{{>preposition}} {{:projectList}}{{/if}}'
- + ' <p class="description force-wrap">{{:description}}</p>'
- + ' </td>'
- + '</tr>'
- + '<tr id="{{>id}}-append" class="row-append">'
- + ' <td>'
- + '{{if condensed}}'
- + ' <small><span class="timeago muted" title="{{>date}}"></span></small>'
- + '{{/if}}'
- + '{{if topic}}'
- + ' <div class="comment-link{{if !comments.count}} no-comments{{/if}}">'
- + ' <small><i class="icon-comment"></i>'
- + ' <a href="{{:comments.url}}">{{:comments.label}}</a></small>'
- + ' </div>'
- + '{{/if}}'
- + ' </td>'
- + '</tr>'
- ).render(event);
- var row = $(html);
- table.find('tbody').append(row);
- // truncate the description
- row.find('p.description').expander();
- // convert times to time-ago
- row.find('.timeago').timeago();
- });
- // enforce a minimal delay between requests
- setTimeout(function(){ swarm.activity._loading = false; }, 500);
- }
- });
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement