gitlab-org--gitlab-foss/app/assets/javascripts/user_tabs.js.coffee

147 lines
4.0 KiB
CoffeeScript
Raw Normal View History

2016-03-01 18:21:25 +00:00
# UserTabs
#
# Handles persisting and restoring the current tab selection and lazily-loading
# content on the Users#show page.
#
# ### Example Markup
#
# <ul class="nav-links">
# <li class="activity-tab active">
# <a data-action="activity" data-target="#activity" data-toggle="tab" href="/u/username">
# Activity
# </a>
# </li>
# <li class="groups-tab">
# <a data-action="groups" data-target="#groups" data-toggle="tab" href="/u/username/groups">
# Groups
# </a>
# </li>
# <li class="contributed-tab">
# <a data-action="contributed" data-target="#contributed" data-toggle="tab" href="/u/username/contributed">
# Contributed projects
# </a>
# </li>
# <li class="projects-tab">
# <a data-action="projects" data-target="#projects" data-toggle="tab" href="/u/username/projects">
# Personal projects
# </a>
# </li>
# </ul>
#
# <div class="tab-content">
# <div class="tab-pane" id="activity">
# Activity Content
# </div>
# <div class="tab-pane" id="groups">
# Groups Content
# </div>
# <div class="tab-pane" id="contributed">
# Contributed projects content
# </div>
# <div class="tab-pane" id="projects">
# Projects content
# </div>
# </div>
#
# <div class="loading-status">
# <div class="loading">
# Loading Animation
# </div>
# </div>
#
2016-02-29 21:39:09 +00:00
class @UserTabs
constructor: (opts) ->
{
@action = 'activity'
@defaultAction = 'activity'
@parentEl = $(document)
} = opts
# Make jQuery object if selector is provided
@parentEl = $(@parentEl) if typeof @parentEl is 'string'
2016-02-29 21:39:09 +00:00
# Store the `location` object, allowing for easier stubbing in tests
@_location = location
# Set tab states
2016-03-01 00:12:06 +00:00
@loaded = {}
for item in @parentEl.find('.nav-links a')
@loaded[$(item).attr 'data-action'] = false
2016-02-29 21:39:09 +00:00
# Actions
@actions = Object.keys @loaded
2016-03-01 00:12:06 +00:00
@bindEvents()
2016-03-01 16:20:43 +00:00
# Set active tab
@action = @defaultAction if @action is 'show'
@activateTab(@action)
2016-02-29 21:39:09 +00:00
bindEvents: ->
2016-03-03 00:00:12 +00:00
# Toggle event listeners
@parentEl
.off 'shown.bs.tab', '.nav-links a[data-toggle="tab"]'
.on 'shown.bs.tab', '.nav-links a[data-toggle="tab"]', @tabShown
2016-03-01 00:12:06 +00:00
2016-02-29 21:39:09 +00:00
tabShown: (event) =>
$target = $(event.target)
action = $target.data('action')
source = $target.attr('href')
2016-03-01 00:12:06 +00:00
@setTab(source, action)
2016-02-29 21:39:09 +00:00
@setCurrentAction(action)
2016-03-01 16:20:43 +00:00
activateTab: (action) ->
@parentEl.find(".nav-links .#{action}-tab a").tab('show')
2016-03-01 16:20:43 +00:00
2016-03-01 00:12:06 +00:00
setTab: (source, action) ->
return if @loaded[action] is true
if action is 'activity'
@loadActivities(source)
2016-03-02 15:40:07 +00:00
if action in ['groups', 'contributed', 'projects']
2016-03-01 00:12:06 +00:00
@loadTab(source, action)
2016-02-29 21:39:09 +00:00
loadTab: (source, action) ->
2016-03-01 00:12:06 +00:00
$.ajax
beforeSend: => @toggleLoading(true)
complete: => @toggleLoading(false)
dataType: 'json'
type: 'GET'
2016-02-29 21:39:09 +00:00
url: "#{source}.json"
success: (data) =>
tabSelector = 'div#' + action
@parentEl.find(tabSelector).html(data.html)
2016-03-01 00:12:06 +00:00
@loaded[action] = true
2016-02-29 21:39:09 +00:00
2016-03-01 00:12:06 +00:00
loadActivities: (source) ->
return if @loaded['activity'] is true
2016-02-29 21:39:09 +00:00
$calendarWrap = @parentEl.find('.user-calendar')
$calendarWrap.load($calendarWrap.data('href'))
2016-02-29 21:39:09 +00:00
2016-03-01 00:12:06 +00:00
new Activities()
@loaded['activity'] = true
2016-02-29 21:39:09 +00:00
2016-03-01 00:12:06 +00:00
toggleLoading: (status) ->
@parentEl.find('.loading-status .loading').toggle(status)
2016-02-29 21:39:09 +00:00
setCurrentAction: (action) ->
# Remove possible actions from URL
regExp = new RegExp('\/(' + @actions.join('|') + ')(\.html)?\/?$')
2016-03-02 17:26:02 +00:00
new_state = @_location.pathname
new_state = new_state.replace(/\/+$/, "") # remove trailing slashes
new_state = new_state.replace(regExp, '')
2016-02-29 21:39:09 +00:00
# Append the new action if we're on a tab other than 'activity'
unless action == @defaultAction
new_state += "/#{action}"
# Ensure parameters and hash come along for the ride
new_state += @_location.search + @_location.hash
history.replaceState {turbolinks: true, url: new_state}, document.title, new_state
new_state