summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/user_tabs.js
blob: 8a657780eb6590bd875af29441b3f18a24a46310 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// 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>
//    <li class="snippets-tab">
//       <a data-action="snippets" data-target="#snippets" data-toggle="tab" href="/u/username/snippets">
//       </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 class="tab-pane" id="snippets">
//       Snippets content
//     </div>
//   </div>
//
//   <div class="loading-status">
//     <div class="loading">
//       Loading Animation
//     </div>
//   </div>
//
(function() {
  var bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };

  this.UserTabs = (function() {
    function UserTabs(opts) {
      this.tabShown = bind(this.tabShown, this);
      var i, item, len, ref, ref1, ref2, ref3;
      this.action = (ref = opts.action) != null ? ref : 'activity', this.defaultAction = (ref1 = opts.defaultAction) != null ? ref1 : 'activity', this.parentEl = (ref2 = opts.parentEl) != null ? ref2 : $(document);
      // Make jQuery object if selector is provided
      if (typeof this.parentEl === 'string') {
        this.parentEl = $(this.parentEl);
      }
      // Store the `location` object, allowing for easier stubbing in tests
      this._location = location;
      // Set tab states
      this.loaded = {};
      ref3 = this.parentEl.find('.nav-links a');
      for (i = 0, len = ref3.length; i < len; i++) {
        item = ref3[i];
        this.loaded[$(item).attr('data-action')] = false;
      }
      // Actions
      this.actions = Object.keys(this.loaded);
      this.bindEvents();
      // Set active tab
      if (this.action === 'show') {
        this.action = this.defaultAction;
      }
      this.activateTab(this.action);
    }

    UserTabs.prototype.bindEvents = function() {
      // Toggle event listeners
      return this.parentEl.off('shown.bs.tab', '.nav-links a[data-toggle="tab"]').on('shown.bs.tab', '.nav-links a[data-toggle="tab"]', this.tabShown);
    };

    UserTabs.prototype.tabShown = function(event) {
      var $target, action, source;
      $target = $(event.target);
      action = $target.data('action');
      source = $target.attr('href');
      this.setTab(source, action);
      return this.setCurrentAction(action);
    };

    UserTabs.prototype.activateTab = function(action) {
      return this.parentEl.find(".nav-links .js-" + action + "-tab a").tab('show');
    };

    UserTabs.prototype.setTab = function(source, action) {
      if (this.loaded[action] === true) {
        return;
      }
      if (action === 'activity') {
        this.loadActivities(source);
      }
      if (action === 'groups' || action === 'contributed' || action === 'projects' || action === 'snippets') {
        return this.loadTab(source, action);
      }
    };

    UserTabs.prototype.loadTab = function(source, action) {
      return $.ajax({
        beforeSend: (function(_this) {
          return function() {
            return _this.toggleLoading(true);
          };
        })(this),
        complete: (function(_this) {
          return function() {
            return _this.toggleLoading(false);
          };
        })(this),
        dataType: 'json',
        type: 'GET',
        url: source + ".json",
        success: (function(_this) {
          return function(data) {
            var tabSelector;
            tabSelector = 'div#' + action;
            _this.parentEl.find(tabSelector).html(data.html);
            _this.loaded[action] = true;
            // Fix tooltips
            return gl.utils.localTimeAgo($('.js-timeago', tabSelector));
          };
        })(this)
      });
    };

    UserTabs.prototype.loadActivities = function(source) {
      var $calendarWrap;
      if (this.loaded['activity'] === true) {
        return;
      }
      $calendarWrap = this.parentEl.find('.user-calendar');
      $calendarWrap.load($calendarWrap.data('href'));
      new Activities();
      return this.loaded['activity'] = true;
    };

    UserTabs.prototype.toggleLoading = function(status) {
      return this.parentEl.find('.loading-status .loading').toggle(status);
    };

    UserTabs.prototype.setCurrentAction = function(action) {
      var new_state, regExp;
      // Remove possible actions from URL
      regExp = new RegExp('\/(' + this.actions.join('|') + ')(\.html)?\/?$');
      new_state = this._location.pathname;
      // remove trailing slashes
      new_state = new_state.replace(/\/+$/, "");
      new_state = new_state.replace(regExp, '');
      // Append the new action if we're on a tab other than 'activity'
      if (action !== this.defaultAction) {
        new_state += "/" + action;
      }
      // Ensure parameters and hash come along for the ride
      new_state += this._location.search + this._location.hash;
      history.replaceState({
        turbolinks: true,
        url: new_state
      }, document.title, new_state);
      return new_state;
    };

    return UserTabs;

  })();

}).call(this);