From cfb03b3d06a3dbd16c6e193a128bb737a0a327fd Mon Sep 17 00:00:00 2001 From: Bryce Johnson Date: Thu, 8 Sep 2016 14:26:43 +0200 Subject: Refactor UserTabs to ES6. --- app/assets/javascripts/user.js.es6 | 2 +- app/assets/javascripts/user_tabs.js.es6 | 160 ++++++++++++++++++++++++++++++++ 2 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 app/assets/javascripts/user_tabs.js.es6 (limited to 'app') diff --git a/app/assets/javascripts/user.js.es6 b/app/assets/javascripts/user.js.es6 index 6889d3a7491..c5893745d74 100644 --- a/app/assets/javascripts/user.js.es6 +++ b/app/assets/javascripts/user.js.es6 @@ -14,7 +14,7 @@ } initTabs() { - return new UserTabs({ + return new global.UserTabs({ parentEl: '.user-profile', action: this.opts.action }); diff --git a/app/assets/javascripts/user_tabs.js.es6 b/app/assets/javascripts/user_tabs.js.es6 new file mode 100644 index 00000000000..b787700070e --- /dev/null +++ b/app/assets/javascripts/user_tabs.js.es6 @@ -0,0 +1,160 @@ +/* +UserTabs + +Handles persisting and restoring the current tab selection and lazily-loading +content on the Users#show page. + +### Example Markup + + + +
+
+ Activity Content +
+
+ Groups Content +
+
+ Contributed projects content +
+
+ Projects content +
+
+ Snippets content +
+
+ +
+
+ Loading Animation +
+
+*/ +(global => { + class UserTabs { + constructor (opts) { + this.loaded = {}; + this.defaultAction = opts.defaultAction || 'activity'; + this.action = opts.action || 'activity'; + this.$parentEl = $(opts.parentEl) || $(document); + this._location = window.location; + this.$parentEl.find('.nav-links a') + .each((i, navLink) => { + this.loaded[$(navLink).attr('data-action')] = false; + }); + this.actions = Object.keys(this.loaded); + this.bindEvents(); + + if (this.action === 'show') { + this.action = this.defaultAction; + } + + this.activateTab(this.action); + } + + bindEvents() { + return this.$parentEl.off('shown.bs.tab', '.nav-links a[data-toggle="tab"]') + .on('shown.bs.tab', '.nav-links a[data-toggle="tab"]', (event) => this.tabShown(event)); + } + + tabShown(event) { + const $target = $(event.target); + const action = $target.data('action'); + const source = $target.attr('href'); + this.setTab(source, action); + return this.setCurrentAction(action); + } + + activateTab(action) { + return this.$parentEl.find(".nav-links .js-" + action + "-tab a") + .tab('show'); + } + + setTab(source, action) { + if (this.loaded[action]) { + return; + } + if (action === 'activity') { + this.loadActivities(source); + } + if (action === 'groups' || action === 'contributed' || action === 'projects' || action === 'snippets') { + return this.loadTab(source, action); + } + } + + loadTab(source, action) { + return $.ajax({ + beforeSend: () => this.toggleLoading(true), + complete: () => this.toggleLoading(false), + dataType: 'json', + type: 'GET', + url: source + ".json", + success: (data) => { + const tabSelector = 'div#' + action; + this.$parentEl.find(tabSelector).html(data.html); + this.loaded[action] = true; + return gl.utils.localTimeAgo($('.js-timeago', tabSelector)); + } + }); + } + + loadActivities(source) { + if (this.loaded['activity']) { + return; + } + const $calendarWrap = this.$parentEl.find('.user-calendar'); + $calendarWrap.load($calendarWrap.data('href')); + new Activities(); + return this.loaded['activity'] = true; + } + + toggleLoading(status) { + return this.$parentEl.find('.loading-status .loading') + .toggle(status); + } + + setCurrentAction(action) { + const regExp = new RegExp('\/(' + this.actions.join('|') + ')(\.html)?\/?$'); + let new_state = this._location.pathname; + new_state = new_state.replace(/\/+$/, ""); + new_state = new_state.replace(regExp, ''); + if (action !== this.defaultAction) { + new_state += "/" + action; + } + new_state += this._location.search + this._location.hash; + history.replaceState({ + turbolinks: true, + url: new_state + }, document.title, new_state); + return new_state; + }; + } + global.UserTabs = UserTabs; +})(window.gl || (window.gl = {})); -- cgit v1.2.1