From 2fc3bff79059a665e8da8f4df22b0660b2a58364 Mon Sep 17 00:00:00 2001 From: Stas Vilchik Date: Fri, 20 Jun 2014 16:30:51 +0600 Subject: [PATCH] SONAR-5209 Permalink --- sonar-server/Gruntfile.coffee | 4 + .../main/coffee/component-viewer/app.coffee | 93 +++++++++++++++++++ .../coffee/component-viewer/header.coffee | 26 ++++++ .../main/coffee/component-viewer/main.coffee | 5 +- .../src/main/hbs/component-viewer/header.hbs | 2 +- .../component_viewer_controller.rb | 28 ++++++ .../app/views/component_viewer/index.html.erb | 4 +- 7 files changed, 159 insertions(+), 3 deletions(-) create mode 100644 sonar-server/src/main/coffee/component-viewer/app.coffee create mode 100644 sonar-server/src/main/webapp/WEB-INF/app/controllers/component_viewer_controller.rb diff --git a/sonar-server/Gruntfile.coffee b/sonar-server/Gruntfile.coffee index 637192424e1..6417a05a876 100644 --- a/sonar-server/Gruntfile.coffee +++ b/sonar-server/Gruntfile.coffee @@ -187,6 +187,10 @@ module.exports = (grunt) -> name: 'dashboard/file-app' out: '<%= pkg.assets %>build/js/dashboard/file-app.js' + componentViewer: options: + name: 'component-viewer/app' + out: '<%= pkg.assets %>build/js/component-viewer/app.js' + handlebars: options: diff --git a/sonar-server/src/main/coffee/component-viewer/app.coffee b/sonar-server/src/main/coffee/component-viewer/app.coffee new file mode 100644 index 00000000000..eefb05a0836 --- /dev/null +++ b/sonar-server/src/main/coffee/component-viewer/app.coffee @@ -0,0 +1,93 @@ +requirejs.config + baseUrl: "#{baseUrl}/js" + + paths: + 'backbone': 'third-party/backbone' + 'backbone.marionette': 'third-party/backbone.marionette' + 'handlebars': 'third-party/handlebars' + 'jquery.mockjax': 'third-party/jquery.mockjax' + + shim: + 'backbone.marionette': + deps: ['backbone'] + exports: 'Marionette' + 'backbone': + exports: 'Backbone' + 'handlebars': + exports: 'Handlebars' + + +requirejs [ + 'backbone.marionette' + 'component-viewer/main' +], ( + Marionette + ComponentViewer +) -> + + $ = jQuery + API_ISSUE = "#{baseUrl}/api/issues/show" + App = new Marionette.Application() + + + App.addRegions + viewerRegion: '#component-viewer' + + + App.requestComponentViewer = (s) -> + if s? + settings = issues: false, coverage: false, duplications: false, scm: false, workspace: false + s.split(',').forEach (d) -> settings[d] = true + else settings = null + unless App.componentViewer? + App.componentViewer = new ComponentViewer settings: settings + App.viewerRegion.show App.componentViewer + App.componentViewer + + + App.addInitializer -> + # Define parameters + paramsHash = location.hash.substr(1) + params = {} + paramsHash.split('&').forEach (d) -> + t = d.split '=' + params[t[0]] = decodeURIComponent t[1] + + viewer = App.requestComponentViewer params.settings + if params.component? + loadIssue = (key) -> + $.get API_ISSUE, key: key, (r) => + viewer.showIssues false, r.issue + + if params.line? + viewer.sourceView.highlightedLine = params.line + viewer.open params.component + viewer.on 'loaded', -> + if params.tab? && params.item? && params.period? + viewer.headerView.enableBar(params.tab).done -> + viewer.enablePeriod +params.period, params.item + else if params.tab? && params.item? + viewer.state.set activeHeaderTab: params.tab, activeHeaderItem: params.item + viewer.headerView.render() + else if params.tab? && params.period? + viewer.headerView.enableBar(params.tab).done -> + viewer.enablePeriod params.period + else if params.tab? && params.currentIssue? + loadIssue(params.currentIssue).done -> + viewer.state.set activeHeaderTab: params.tab + viewer.headerView.render() + else if params.tab? + viewer.state.set activeHeaderTab: params.tab + viewer.headerView.render() + else if params.currentIssue? + loadIssue params.currentIssue + else viewer.showAllLines() + + + # Message bundles + l10nXHR = window.requestMessages() + + + $.when(l10nXHR).done -> + # Start the application + App.start() diff --git a/sonar-server/src/main/coffee/component-viewer/header.coffee b/sonar-server/src/main/coffee/component-viewer/header.coffee index 13c3a40488f..03a08c9954e 100644 --- a/sonar-server/src/main/coffee/component-viewer/header.coffee +++ b/sonar-server/src/main/coffee/component-viewer/header.coffee @@ -57,6 +57,7 @@ define [ 'click .js-favorite': 'toggleFavorite' 'click .js-extensions': 'showExtensionsPopup' 'click .js-extension-close': 'closeExtension' + 'click .js-permalink': 'getPermalink' 'click @ui.expandLinks': 'showExpandedBar' 'click .js-toggle-issues': 'toggleIssues' 'click .js-toggle-coverage': 'toggleCoverage' @@ -208,6 +209,31 @@ define [ method.call @options.main, extra + getPermalink: -> + params = [] + params.push key: 'component', value: @options.main.component.get 'key' + settings = [] + _.map @options.main.settings.toJSON(), (v, k) -> settings.push k if v + params.push key: 'settings', value: settings.join ',' + activeHeaderTab = @state.get 'activeHeaderTab' + if activeHeaderTab + params.push key: 'tab', value: activeHeaderTab + activeHeaderItem = @state.get 'activeHeaderItem' + if activeHeaderItem + params.push key: 'item', value: activeHeaderItem + highlightedLine = @options.main.sourceView.highlightedLine + period = @state.get 'period' + if period? + params.push key: 'period', value: period.get 'key' + if @options.main.currentIssue + params.push key: 'currentIssue', value: @options.main.currentIssue + if highlightedLine + params.push key: 'line', value: highlightedLine + hash = params.map((d) -> "#{d.key}=#{encodeURIComponent(d.value)}" ).join '&' + window.open "#{baseUrl}/component_viewer/index##{hash}" + + + serializeData: -> component = @component.toJSON() if component.measures diff --git a/sonar-server/src/main/coffee/component-viewer/main.coffee b/sonar-server/src/main/coffee/component-viewer/main.coffee index 891acfbcf0f..71c5147bdf3 100644 --- a/sonar-server/src/main/coffee/component-viewer/main.coffee +++ b/sonar-server/src/main/coffee/component-viewer/main.coffee @@ -77,7 +77,8 @@ define [ initialize: (options) -> @settings = new Backbone.Model @getDefaultSettings() - @settings.set options.settings + if options.settings? + @settings.set options.settings @state = new State() @@ -230,10 +231,12 @@ define [ if @settings.get('coverage') then @showCoverage() else @hideCoverage() if @settings.get('duplications') then @showDuplications() else @hideDuplications() if @settings.get('scm') then @showSCM() else @hideSCM() + @trigger 'loaded' .fail => @state.set 'removed', true @state.set 'hasSource', false @render() + @trigger 'loaded' toggleWorkspace: (store = false) -> diff --git a/sonar-server/src/main/hbs/component-viewer/header.hbs b/sonar-server/src/main/hbs/component-viewer/header.hbs index 99a36d1fbdf..1bfdf8b57e5 100644 --- a/sonar-server/src/main/hbs/component-viewer/header.hbs +++ b/sonar-server/src/main/hbs/component-viewer/header.hbs @@ -28,7 +28,7 @@ {{#unless state.removed}}