Commit c090ebc0 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge branch 'rs-persist-tab-selection-more-betterer' into 'master'

Improve MergeRequest tab-persisting behavior

Now uses the path instead of the hash.

See discussion in !728

See merge request !737
parents f95eeee1 3f156ed4
...@@ -186,7 +186,7 @@ gem 'charlock_holmes' ...@@ -186,7 +186,7 @@ gem 'charlock_holmes'
gem "sass-rails", '~> 4.0.2' gem "sass-rails", '~> 4.0.2'
gem "coffee-rails" gem "coffee-rails"
gem "uglifier" gem "uglifier"
gem 'turbolinks' gem 'turbolinks', '~> 2.5.0'
gem 'jquery-turbolinks' gem 'jquery-turbolinks'
gem 'select2-rails' gem 'select2-rails'
......
...@@ -101,13 +101,13 @@ GEM ...@@ -101,13 +101,13 @@ GEM
coderay (1.1.0) coderay (1.1.0)
coercible (1.0.0) coercible (1.0.0)
descendants_tracker (~> 0.0.1) descendants_tracker (~> 0.0.1)
coffee-rails (4.0.1) coffee-rails (4.1.0)
coffee-script (>= 2.2.0) coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.0) railties (>= 4.0.0, < 5.0)
coffee-script (2.2.0) coffee-script (2.4.1)
coffee-script-source coffee-script-source
execjs execjs
coffee-script-source (1.6.3) coffee-script-source (1.9.1.1)
colored (1.2) colored (1.2)
colorize (0.5.8) colorize (0.5.8)
columnize (0.9.0) columnize (0.9.0)
...@@ -418,7 +418,7 @@ GEM ...@@ -418,7 +418,7 @@ GEM
quiet_assets (1.0.2) quiet_assets (1.0.2)
railties (>= 3.1, < 5.0) railties (>= 3.1, < 5.0)
racc (1.4.10) racc (1.4.10)
rack (1.5.2) rack (1.5.3)
rack-accept (0.4.5) rack-accept (0.4.5)
rack (>= 0.4) rack (>= 0.4)
rack-attack (4.3.0) rack-attack (4.3.0)
...@@ -633,7 +633,7 @@ GEM ...@@ -633,7 +633,7 @@ GEM
multi_json (~> 1.7) multi_json (~> 1.7)
twitter-stream (~> 0.1) twitter-stream (~> 0.1)
tins (0.13.1) tins (0.13.1)
turbolinks (2.0.0) turbolinks (2.5.3)
coffee-rails coffee-rails
twitter-stream (0.1.16) twitter-stream (0.1.16)
eventmachine (>= 0.12.8) eventmachine (>= 0.12.8)
...@@ -806,7 +806,7 @@ DEPENDENCIES ...@@ -806,7 +806,7 @@ DEPENDENCIES
test_after_commit test_after_commit
thin thin
tinder (~> 1.9.2) tinder (~> 1.9.2)
turbolinks turbolinks (~> 2.5.0)
uglifier uglifier
underscore-rails (~> 1.4.4) underscore-rails (~> 1.4.4)
unf unf
......
...@@ -26,7 +26,7 @@ class @MergeRequest ...@@ -26,7 +26,7 @@ class @MergeRequest
@commits_loaded = @opts.commits_loaded or false @commits_loaded = @opts.commits_loaded or false
this.bindEvents() this.bindEvents()
this.activateTabFromHash() this.activateTabFromPath()
this.initMergeWidget() this.initMergeWidget()
this.$('.show-all-commits').on 'click', => this.$('.show-all-commits').on 'click', =>
...@@ -82,19 +82,16 @@ class @MergeRequest ...@@ -82,19 +82,16 @@ class @MergeRequest
bindEvents: -> bindEvents: ->
this.$('.merge-request-tabs a[data-toggle="tab"]').on 'shown.bs.tab', (e) => this.$('.merge-request-tabs a[data-toggle="tab"]').on 'shown.bs.tab', (e) =>
$target = $(e.target) $target = $(e.target)
tab_action = $target.data('action')
# Nothing else to be done if we're on the first tab
return if $target.data('action') == 'notes'
# Persist current tab selection via URL
href = $target.attr('href')
if href.substr(0,1) == '#'
location.replace("#!#{href.substr(1)}")
# Lazy-load diffs # Lazy-load diffs
if $target.data('action') == 'diffs' if tab_action == 'diffs'
this.loadDiff() unless @diffs_loaded this.loadDiff() unless @diffs_loaded
$('.diff-header').trigger("sticky_kit:recalc") $('.diff-header').trigger('sticky_kit:recalc')
# Skip tab-persisting behavior on MergeRequests#new
unless @opts.action == 'new'
@setCurrentAction(tab_action)
this.$('.accept_merge_request').on 'click', -> this.$('.accept_merge_request').on 'click', ->
$('.automerge_widget.can_be_merged').hide() $('.automerge_widget.can_be_merged').hide()
...@@ -112,27 +109,51 @@ class @MergeRequest ...@@ -112,27 +109,51 @@ class @MergeRequest
this.$('.remove_source_branch_in_progress').hide() this.$('.remove_source_branch_in_progress').hide()
this.$('.remove_source_branch_widget.failed').show() this.$('.remove_source_branch_widget.failed').show()
# Activates a tab section based on the `#!` URL hash # Activate a tab based on the current URL path
# #
# If no hash value is present (i.e., on the initial page load), the first tab # If the current action is 'show' or 'new' (i.e., initial page load),
# is selected by default. # activates the first tab, otherwise activates the tab corresponding to the
# current action (diffs, commits).
activateTabFromPath: ->
if @opts.action == 'show' || @opts.action == 'new'
this.$('.merge-request-tabs a[data-toggle="tab"]:first').tab('show')
else
this.$(".merge-request-tabs a[data-action='#{@opts.action}']").tab('show')
# Replaces the current Merge Request-specific action in the URL with a new one
# #
# ... unless the current controller action is `diffs`, in which case that tab # If the action is "notes", the URL is reset to the standard
# is selected instead. Fun, right? # `MergeRequests#show` route.
# #
# Note: We use a `#!` instead of a standard URL hash for two reasons: # Examples:
# #
# 1. Prevents the hash acting like an anchor and scrolling the page. # location.pathname # => "/namespace/project/merge_requests/1"
# 2. Prevents mutating browser history. # setCurrentAction('diffs')
activateTabFromHash: -> # location.pathname # => "/namespace/project/merge_requests/1/diffs"
# Correct the hash if we came here directly via the `/diffs` path #
if location.hash == '' and @opts.action == 'diffs' # location.pathname # => "/namespace/project/merge_requests/1/diffs"
location.replace('#!diffs') # setCurrentAction('notes')
# location.pathname # => "/namespace/project/merge_requests/1"
if location.hash == '' #
this.$('.merge-request-tabs a[data-toggle="tab"]:first').tab('show') # location.pathname # => "/namespace/project/merge_requests/1/diffs"
else if location.hash.substr(0,2) == '#!' # setCurrentAction('commits')
this.$(".merge-request-tabs a[href='##{location.hash.substr(2)}']").tab("show") # location.pathname # => "/namespace/project/merge_requests/1/commits"
setCurrentAction: (action) ->
# Normalize action, just to be safe
action = 'notes' if action == 'show'
# Remove a trailing '/commits' or '/diffs'
new_state = location.pathname.replace(/\/(commits|diffs)\/?$/, '')
# Append the new action if we're on a tab other than 'notes'
unless action == 'notes'
new_state += "/#{action}"
# Replace the current history state with the new one without breaking
# Turbolinks' history.
#
# See https://github.com/rails/turbolinks/issues/363
history.replaceState {turbolinks: true, url: new_state}, '', new_state
showState: (state) -> showState: (state) ->
$('.automerge_widget').hide() $('.automerge_widget').hide()
...@@ -161,7 +182,7 @@ class @MergeRequest ...@@ -161,7 +182,7 @@ class @MergeRequest
loadDiff: (event) -> loadDiff: (event) ->
$.ajax $.ajax
type: 'GET' type: 'GET'
url: this.$('.merge-request-tabs .diffs-tab a').data('source') + ".json" url: this.$('.merge-request-tabs .diffs-tab a').attr('href') + ".json"
beforeSend: => beforeSend: =>
this.$('.mr-loading-status .loading').show() this.$('.mr-loading-status .loading').show()
complete: => complete: =>
......
...@@ -2,10 +2,13 @@ require 'gitlab/satellite/satellite' ...@@ -2,10 +2,13 @@ require 'gitlab/satellite/satellite'
class Projects::MergeRequestsController < Projects::ApplicationController class Projects::MergeRequestsController < Projects::ApplicationController
before_action :module_enabled before_action :module_enabled
before_action :merge_request, only: [:edit, :update, :show, :diffs, :automerge, :automerge_check, :ci_status, :toggle_subscription] before_action :merge_request, only: [
before_action :closes_issues, only: [:edit, :update, :show, :diffs] :edit, :update, :show, :diffs, :commits, :automerge, :automerge_check,
before_action :validates_merge_request, only: [:show, :diffs] :ci_status, :toggle_subscription
before_action :define_show_vars, only: [:show, :diffs] ]
before_action :closes_issues, only: [:edit, :update, :show, :diffs, :commits]
before_action :validates_merge_request, only: [:show, :diffs, :commits]
before_action :define_show_vars, only: [:show, :diffs, :commits]
# Allow read any merge_request # Allow read any merge_request
before_action :authorize_read_merge_request! before_action :authorize_read_merge_request!
...@@ -27,7 +30,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController ...@@ -27,7 +30,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController
@merge_requests = @merge_requests.full_search(terms) @merge_requests = @merge_requests.full_search(terms)
end end
end end
@merge_requests = @merge_requests.page(params[:page]).per(PER_PAGE) @merge_requests = @merge_requests.page(params[:page]).per(PER_PAGE)
respond_to do |format| respond_to do |format|
...@@ -67,6 +70,10 @@ class Projects::MergeRequestsController < Projects::ApplicationController ...@@ -67,6 +70,10 @@ class Projects::MergeRequestsController < Projects::ApplicationController
end end
end end
def commits
render 'show'
end
def new def new
params[:merge_request] ||= ActionController::Parameters.new(source_project: @project) params[:merge_request] ||= ActionController::Parameters.new(source_project: @project)
@merge_request = MergeRequests::BuildService.new(project, current_user, merge_request_params).execute @merge_request = MergeRequests::BuildService.new(project, current_user, merge_request_params).execute
......
...@@ -20,12 +20,12 @@ ...@@ -20,12 +20,12 @@
.mr-compare.merge-request .mr-compare.merge-request
%ul.nav.nav-tabs.merge-request-tabs %ul.nav.nav-tabs.merge-request-tabs
%li.commits-tab %li.commits-tab
= link_to '#commits', data: {action: 'commits', toggle: 'tab'} do = link_to url_for(params), data: {target: '#commits', action: 'commits', toggle: 'tab'} do
= icon('history') = icon('history')
Commits Commits
%span.badge= @commits.size %span.badge= @commits.size
%li.diffs-tab %li.diffs-tab
= link_to '#diffs', data: {action: 'diffs', toggle: 'tab'} do = link_to url_for(params), data: {target: '#diffs', action: 'diffs', toggle: 'tab'} do
= icon('list-alt') = icon('list-alt')
Changes Changes
%span.badge= @diffs.size %span.badge= @diffs.size
...@@ -56,7 +56,7 @@ ...@@ -56,7 +56,7 @@
:javascript :javascript
var merge_request var merge_request
merge_request = new MergeRequest({ merge_request = new MergeRequest({
action: 'diffs', action: 'new',
diffs_loaded: true, diffs_loaded: true,
commits_loaded: true commits_loaded: true
}); });
......
...@@ -38,17 +38,17 @@ ...@@ -38,17 +38,17 @@
- if @commits.present? - if @commits.present?
%ul.nav.nav-tabs.merge-request-tabs %ul.nav.nav-tabs.merge-request-tabs
%li.notes-tab %li.notes-tab
= link_to '#notes', data: {action: 'notes', toggle: 'tab'} do = link_to namespace_project_merge_request_path(@project.namespace, @project, @merge_request), data: {target: '#notes', action: 'notes', toggle: 'tab'} do
= icon('comments') = icon('comments')
Discussion Discussion
%span.badge= @merge_request.mr_and_commit_notes.user.count %span.badge= @merge_request.mr_and_commit_notes.user.count
%li.commits-tab %li.commits-tab
= link_to '#commits', data: {action: 'commits', toggle: 'tab'} do = link_to commits_namespace_project_merge_request_path(@project.namespace, @project, @merge_request), data: {target: '#commits', action: 'commits', toggle: 'tab'} do
= icon('history') = icon('history')
Commits Commits
%span.badge= @commits.size %span.badge= @commits.size
%li.diffs-tab %li.diffs-tab
= link_to '#diffs', data: {source: diffs_namespace_project_merge_request_path(@project.namespace, @project, @merge_request), action: 'diffs', toggle: 'tab'} do = link_to diffs_namespace_project_merge_request_path(@project.namespace, @project, @merge_request), data: {target: '#diffs', action: 'diffs', toggle: 'tab'} do
= icon('list-alt') = icon('list-alt')
Changes Changes
%span.badge= @merge_request.diffs.size %span.badge= @merge_request.diffs.size
......
...@@ -450,6 +450,7 @@ Gitlab::Application.routes.draw do ...@@ -450,6 +450,7 @@ Gitlab::Application.routes.draw do
resources :merge_requests, constraints: { id: /\d+/ }, except: [:destroy] do resources :merge_requests, constraints: { id: /\d+/ }, except: [:destroy] do
member do member do
get :diffs get :diffs
get :commits
post :automerge post :automerge
get :automerge_check get :automerge_check
get :ci_status get :ci_status
......
...@@ -208,23 +208,31 @@ describe Projects::RefsController, 'routing' do ...@@ -208,23 +208,31 @@ describe Projects::RefsController, 'routing' do
end end
end end
# diffs_project_merge_request GET /:project_id/merge_requests/:id/diffs(.:format) projects/merge_requests#diffs # diffs_namespace_project_merge_request GET /:namespace_id/:project_id/merge_requests/:id/diffs(.:format) projects/merge_requests#diffs
# automerge_project_merge_request POST /:project_id/merge_requests/:id/automerge(.:format) projects/merge_requests#automerge # commits_namespace_project_merge_request GET /:namespace_id/:project_id/merge_requests/:id/commits(.:format) projects/merge_requests#commits
# automerge_check_project_merge_request GET /:project_id/merge_requests/:id/automerge_check(.:format) projects/merge_requests#automerge_check # automerge_namespace_project_merge_request POST /:namespace_id/:project_id/merge_requests/:id/automerge(.:format) projects/merge_requests#automerge
# branch_from_project_merge_requests GET /:project_id/merge_requests/branch_from(.:format) projects/merge_requests#branch_from # automerge_check_namespace_project_merge_request GET /:namespace_id/:project_id/merge_requests/:id/automerge_check(.:format) projects/merge_requests#automerge_check
# branch_to_project_merge_requests GET /:project_id/merge_requests/branch_to(.:format) projects/merge_requests#branch_to # ci_status_namespace_project_merge_request GET /:namespace_id/:project_id/merge_requests/:id/ci_status(.:format) projects/merge_requests#ci_status
# project_merge_requests GET /:project_id/merge_requests(.:format) projects/merge_requests#index # toggle_subscription_namespace_project_merge_request POST /:namespace_id/:project_id/merge_requests/:id/toggle_subscription(.:format) projects/merge_requests#toggle_subscription
# POST /:project_id/merge_requests(.:format) projects/merge_requests#create # branch_from_namespace_project_merge_requests GET /:namespace_id/:project_id/merge_requests/branch_from(.:format) projects/merge_requests#branch_from
# new_project_merge_request GET /:project_id/merge_requests/new(.:format) projects/merge_requests#new # branch_to_namespace_project_merge_requests GET /:namespace_id/:project_id/merge_requests/branch_to(.:format) projects/merge_requests#branch_to
# edit_project_merge_request GET /:project_id/merge_requests/:id/edit(.:format) projects/merge_requests#edit # update_branches_namespace_project_merge_requests GET /:namespace_id/:project_id/merge_requests/update_branches(.:format) projects/merge_requests#update_branches
# project_merge_request GET /:project_id/merge_requests/:id(.:format) projects/merge_requests#show # namespace_project_merge_requests GET /:namespace_id/:project_id/merge_requests(.:format) projects/merge_requests#index
# PUT /:project_id/merge_requests/:id(.:format) projects/merge_requests#update # POST /:namespace_id/:project_id/merge_requests(.:format) projects/merge_requests#create
# DELETE /:project_id/merge_requests/:id(.:format) projects/merge_requests#destroy # new_namespace_project_merge_request GET /:namespace_id/:project_id/merge_requests/new(.:format) projects/merge_requests#new
# edit_namespace_project_merge_request GET /:namespace_id/:project_id/merge_requests/:id/edit(.:format) projects/merge_requests#edit
# namespace_project_merge_request GET /:namespace_id/:project_id/merge_requests/:id(.:format) projects/merge_requests#show
# PATCH /:namespace_id/:project_id/merge_requests/:id(.:format) projects/merge_requests#update
# PUT /:namespace_id/:project_id/merge_requests/:id(.:format) projects/merge_requests#update
describe Projects::MergeRequestsController, 'routing' do describe Projects::MergeRequestsController, 'routing' do
it 'to #diffs' do it 'to #diffs' do
expect(get('/gitlab/gitlabhq/merge_requests/1/diffs')).to route_to('projects/merge_requests#diffs', namespace_id: 'gitlab', project_id: 'gitlabhq', id: '1') expect(get('/gitlab/gitlabhq/merge_requests/1/diffs')).to route_to('projects/merge_requests#diffs', namespace_id: 'gitlab', project_id: 'gitlabhq', id: '1')
end end
it 'to #commits' do
expect(get('/gitlab/gitlabhq/merge_requests/1/commits')).to route_to('projects/merge_requests#commits', namespace_id: 'gitlab', project_id: 'gitlabhq', id: '1')
end
it 'to #automerge' do it 'to #automerge' do
expect(post('/gitlab/gitlabhq/merge_requests/1/automerge')).to route_to( expect(post('/gitlab/gitlabhq/merge_requests/1/automerge')).to route_to(
'projects/merge_requests#automerge', 'projects/merge_requests#automerge',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment