diff options
author | Douwe Maan <douwe@selenight.nl> | 2017-04-21 21:33:48 +0300 |
---|---|---|
committer | Douwe Maan <douwe@selenight.nl> | 2017-04-27 20:23:26 +0300 |
commit | a7fd95cd22062f18474ee038d72fa9e1139a1a84 (patch) | |
tree | 030507d4479a93eaf0fa737d31b778d9872dadc9 /app | |
parent | c69a0779fb499fb3c8352eede0b5c6d7bb1117d1 (diff) |
Render viewer error synchronously so that 'view the source' link will work
Diffstat (limited to 'app')
-rw-r--r-- | app/assets/javascripts/blob/viewer/index.js | 25 | ||||
-rw-r--r-- | app/controllers/concerns/renders_blob.rb | 2 | ||||
-rw-r--r-- | app/helpers/blob_helper.rb | 2 | ||||
-rw-r--r-- | app/views/projects/blob/_content.html.haml | 4 | ||||
-rw-r--r-- | app/views/projects/blob/_viewer.html.haml | 19 | ||||
-rw-r--r-- | app/views/projects/blob/_viewer_switcher.html.haml | 6 | ||||
-rw-r--r-- | app/views/projects/blob/_viewer_wrapper.html.haml | 8 |
7 files changed, 34 insertions, 32 deletions
diff --git a/app/assets/javascripts/blob/viewer/index.js b/app/assets/javascripts/blob/viewer/index.js index a322351219c..8a5fb187a71 100644 --- a/app/assets/javascripts/blob/viewer/index.js +++ b/app/assets/javascripts/blob/viewer/index.js @@ -2,7 +2,8 @@ /* global Flash */ export default class BlobViewer { constructor() { - this.switcherBtns = document.querySelectorAll('.js-blob-viewer-switcher'); + this.switcher = document.querySelector('.js-blob-viewer-switcher'); + this.switcherBtns = document.querySelectorAll('.js-blob-viewer-switch-btn'); this.copySourceBtn = document.querySelector('.js-copy-blob-source-btn'); this.simpleViewer = document.querySelector('.blob-viewer[data-type="simple"]'); this.richViewer = document.querySelector('.blob-viewer[data-type="rich"]'); @@ -10,22 +11,22 @@ export default class BlobViewer { let initialViewerName = document.querySelector('.blob-viewer:not(.hidden)').getAttribute('data-type'); - if (this.switcherBtns.length) { - this.initBindings(); + this.initBindings(); - if (location.hash.indexOf('#L') === 0) { - initialViewerName = 'simple'; - } + if (this.switcher && location.hash.indexOf('#L') === 0) { + initialViewerName = 'simple'; } this.switchToViewer(initialViewerName); } initBindings() { - Array.from(this.switcherBtns) - .forEach((el) => { - el.addEventListener('click', this.switchViewHandler.bind(this)); - }); + if (this.switcherBtns.length) { + Array.from(this.switcherBtns) + .forEach((el) => { + el.addEventListener('click', this.switchViewHandler.bind(this)); + }); + } if (this.copySourceBtn) { this.copySourceBtn.addEventListener('click', () => { @@ -91,8 +92,8 @@ export default class BlobViewer { const newViewer = document.querySelector(`.blob-viewer[data-type='${name}']`); if (this.activeViewer === newViewer) return; - const oldButton = document.querySelector('.js-blob-viewer-switcher.active'); - const newButton = document.querySelector(`.js-blob-viewer-switcher[data-viewer='${name}']`); + const oldButton = document.querySelector('.js-blob-viewer-switch-btn.active'); + const newButton = document.querySelector(`.js-blob-viewer-switch-btn[data-viewer='${name}']`); const oldViewer = document.querySelector(`.blob-viewer:not([data-type='${name}'])`); if (oldButton) { diff --git a/app/controllers/concerns/renders_blob.rb b/app/controllers/concerns/renders_blob.rb index ab9f0d01c7b..d478c3bb6ca 100644 --- a/app/controllers/concerns/renders_blob.rb +++ b/app/controllers/concerns/renders_blob.rb @@ -11,7 +11,7 @@ module RendersBlob return render_404 unless viewer render json: { - html: view_to_html_string("projects/blob/_viewer", viewer: viewer) + html: view_to_html_string("projects/blob/_viewer", viewer: viewer, load_asynchronously: false) } end end diff --git a/app/helpers/blob_helper.rb b/app/helpers/blob_helper.rb index 472662c4ba9..7db9cf0ff01 100644 --- a/app/helpers/blob_helper.rb +++ b/app/helpers/blob_helper.rb @@ -233,7 +233,7 @@ module BlobHelper end if viewer.rich? && viewer.blob.rendered_as_text?(override_max_size: true) - options << link_to('view the source', '#', class: 'js-blob-viewer-switcher', data: { viewer: 'simple' }) + options << link_to('view the source', '#', class: 'js-blob-viewer-switch-btn', data: { viewer: 'simple' }) end options << link_to('download it', blob_raw_url, target: '_blank', rel: 'noopener noreferrer') diff --git a/app/views/projects/blob/_content.html.haml b/app/views/projects/blob/_content.html.haml index cd4d85591ae..7afbd85cd6d 100644 --- a/app/views/projects/blob/_content.html.haml +++ b/app/views/projects/blob/_content.html.haml @@ -2,7 +2,7 @@ - rich_viewer = blob.rich_viewer - rich_viewer_active = rich_viewer && params[:viewer] != 'simple' -= render 'projects/blob/viewer_wrapper', viewer: simple_viewer, hidden: rich_viewer_active += render 'projects/blob/viewer', viewer: simple_viewer, hidden: rich_viewer_active - if rich_viewer - = render 'projects/blob/viewer_wrapper', viewer: rich_viewer, hidden: !rich_viewer_active + = render 'projects/blob/viewer', viewer: rich_viewer, hidden: !rich_viewer_active diff --git a/app/views/projects/blob/_viewer.html.haml b/app/views/projects/blob/_viewer.html.haml index 19f1d1251ae..e0cfe39d1ec 100644 --- a/app/views/projects/blob/_viewer.html.haml +++ b/app/views/projects/blob/_viewer.html.haml @@ -1,5 +1,14 @@ -- if error = viewer.render_error(override_max_size: params[:override_max_size]) - = render 'projects/blob/render_error', viewer: viewer, error: error -- else - - viewer.prepare! - = render viewer.partial_path, viewer: viewer +- hidden = local_assigns.fetch(:hidden, false) +- render_error = viewer.render_error(override_max_size: params[:override_max_size]) +- load_asynchronously = local_assigns.fetch(:load_asynchronously, viewer.server_side?) && render_error.nil? + +- url = url_for(params.merge(viewer: viewer.type, format: :json)) if load_asynchronously +.blob-viewer{ data: { type: viewer.type, url: url }, class: ('hidden' if hidden) } + - if load_asynchronously + .text-center.prepend-top-default.append-bottom-default + = icon('spinner spin 2x', 'aria-hidden' => 'true', 'aria-label' => 'Loading content') + - elsif render_error + = render 'projects/blob/render_error', viewer: viewer, error: render_error + - else + - viewer.prepare! + = render viewer.partial_path, viewer: viewer diff --git a/app/views/projects/blob/_viewer_switcher.html.haml b/app/views/projects/blob/_viewer_switcher.html.haml index 6d69660e656..6a521069418 100644 --- a/app/views/projects/blob/_viewer_switcher.html.haml +++ b/app/views/projects/blob/_viewer_switcher.html.haml @@ -2,11 +2,11 @@ - simple_viewer = blob.simple_viewer - rich_viewer = blob.rich_viewer - .btn-group{ role: "group" } + .btn-group.js-blob-viewer-switcher{ role: "group" } - simple_label = "Display #{simple_viewer.switcher_title}" - %button.btn.btn-default.btn-sm.js-blob-viewer-switcher.has-tooltip{ 'aria-label' => simple_label, title: simple_label, data: { viewer: 'simple', container: 'body' } }> + %button.btn.btn-default.btn-sm.js-blob-viewer-switch-btn.has-tooltip{ 'aria-label' => simple_label, title: simple_label, data: { viewer: 'simple', container: 'body' } }> = icon(simple_viewer.switcher_icon) - rich_label = "Display #{rich_viewer.switcher_title}" - %button.btn.btn-default.btn-sm.js-blob-viewer-switcher.has-tooltip{ 'aria-label' => rich_label, title: rich_label, data: { viewer: 'rich', container: 'body' } }> + %button.btn.btn-default.btn-sm.js-blob-viewer-switch-btn.has-tooltip{ 'aria-label' => rich_label, title: rich_label, data: { viewer: 'rich', container: 'body' } }> = icon(rich_viewer.switcher_icon) diff --git a/app/views/projects/blob/_viewer_wrapper.html.haml b/app/views/projects/blob/_viewer_wrapper.html.haml deleted file mode 100644 index 8da5022fbea..00000000000 --- a/app/views/projects/blob/_viewer_wrapper.html.haml +++ /dev/null @@ -1,8 +0,0 @@ -- hidden = local_assigns.fetch(:hidden, false) -- url = url_for(params.merge(format: :json, viewer: viewer.type)) if viewer.server_side? -.blob-viewer{ data: { type: viewer.type, url: url }, class: ('hidden' if hidden) } - - if viewer.server_side? - .text-center.prepend-top-default.append-bottom-default - = icon('spinner spin 2x', 'aria-hidden' => 'true', 'aria-label' => 'Loading content') - - else - = render 'projects/blob/viewer', viewer: viewer |