summaryrefslogtreecommitdiff
path: root/app/assets
diff options
context:
space:
mode:
authorAlfredo Sumaran <alfredo@gitlab.com>2016-02-29 16:39:09 -0500
committerAlfredo Sumaran <alfredo@gitlab.com>2016-02-29 16:39:09 -0500
commitd941aee4001e002045b10997cd268c87820832a8 (patch)
treeaf506dcd5f4f7e601d034586608b5fb2e430922d /app/assets
parent99d75a2770c39dcd430da9805e8f16d20f460555 (diff)
downloadgitlab-ce-d941aee4001e002045b10997cd268c87820832a8.tar.gz
Create user_tabs.js.coffee
Diffstat (limited to 'app/assets')
-rw-r--r--app/assets/javascripts/user_tabs.js.coffee58
1 files changed, 58 insertions, 0 deletions
diff --git a/app/assets/javascripts/user_tabs.js.coffee b/app/assets/javascripts/user_tabs.js.coffee
new file mode 100644
index 00000000000..d2bb7d9c4ed
--- /dev/null
+++ b/app/assets/javascripts/user_tabs.js.coffee
@@ -0,0 +1,58 @@
+class @UserTabs
+ actions: ['activity', 'groups', 'contributed', 'personal'],
+ defaultAction: 'activity',
+
+ constructor: ->
+ # Store the `location` object, allowing for easier stubbing in tests
+ @_location = location
+
+ @bindEvents()
+
+ bindEvents: ->
+ $(document).on 'shown.bs.tab', '.nav-links a[data-toggle="tab"]', @tabShown
+
+ tabShown: (event) =>
+ $target = $(event.target)
+ action = $target.data('action')
+ source = $target.attr('href')
+
+ @loadTab(source, action)
+ @setCurrentAction(action)
+
+ loadTab: (source, action) ->
+ @_get
+ url: "#{source}.json"
+ success: (data) =>
+ tabSelector = 'div#' + action
+ document.querySelector(tabSelector).innerHTML = data.html
+
+ toggleLoading: (status) ->
+ $('.loading-status .loading').toggle(status)
+
+ _get: (options) ->
+ defaults = {
+ beforeSend: => @toggleLoading(true)
+ complete: => @toggleLoading(false)
+ dataType: 'json'
+ type: 'GET'
+ }
+
+ options = $.extend({}, defaults, options)
+
+ $.ajax(options)
+
+ setCurrentAction: (action) ->
+ # Remove possible actions from URL
+ regExp = new RegExp('\/(' + @actions.join('|') + ')(\.html)?\/?$')
+ new_state = @_location.pathname.replace(regExp, '')
+
+ # 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