From 3203cdf1597fe567a56942f5e1dd45170496199a Mon Sep 17 00:00:00 2001 From: Stas Vilchik Date: Tue, 2 Jun 2015 18:32:00 +0200 Subject: refactor creation of manual issues --- .../main/js/components/issue/manual-issue-view.js | 66 ++++++---------------- .../src/main/js/components/issue/models/issue.js | 34 ++++++++++- .../js/components/issue/templates/manual-issue.hbs | 42 +++----------- .../src/main/js/components/source-viewer/main.js | 4 +- 4 files changed, 60 insertions(+), 86 deletions(-) diff --git a/server/sonar-web/src/main/js/components/issue/manual-issue-view.js b/server/sonar-web/src/main/js/components/issue/manual-issue-view.js index c1f60b486e6..1b043544a44 100644 --- a/server/sonar-web/src/main/js/components/issue/manual-issue-view.js +++ b/server/sonar-web/src/main/js/components/issue/manual-issue-view.js @@ -1,10 +1,9 @@ define([ + './models/issue', './templates' -], function () { +], function (Issue) { - var $ = jQuery, - API_ISSUE = baseUrl + '/api/issues/show', - API_ADD_MANUAL_ISSUE = baseUrl + '/api/issues/create'; + var $ = jQuery; return Marionette.ItemView.extend({ template: Templates['manual-issue'], @@ -44,68 +43,35 @@ define([ } }, - showSpinner: function () { - this.$('.js-submit').hide(); - this.$('.js-spinner').show(); - }, - - hideSpinner: function () { - this.$('.js-submit').show(); - this.$('.js-spinner').hide(); - }, - - validateFields: function () { - var message = this.$('[name=message]'); - if (!message.val()) { - message.addClass('invalid').focus(); - return false; - } - return true; - }, - formSubmit: function (e) { var that = this; e.preventDefault(); - if (!this.validateFields()) { - return; - } - this.showSpinner(); - var data = $(e.currentTarget).serialize(); - $.post(API_ADD_MANUAL_ISSUE, data) - .done(function (r) { - if (typeof r === 'string') { - r = JSON.parse(r); - } - that.addIssue(r.issue.key); - }).fail(function (r) { - that.hideSpinner(); - if (r.responseJSON && r.responseJSON.errors) { - that.showError(_.pluck(r.responseJSON.errors, 'msg').join('. ')); - } - }); + var issue = new Issue({ + component: this.options.component, + line: this.options.line, + message: this.$('[name="message"]').val(), + rule: this.$('[name="rule"]').val() + }); + issue.save().done(function () { + that.addIssue(issue); + }); }, - addIssue: function (key) { + addIssue: function (issue) { var that = this; - return $.get(API_ISSUE, { key: key }).done(function (r) { - that.trigger('add', r.issue); + return issue.fetch().done(function () { + that.trigger('add', issue); that.close(); }); }, - showError: function (msg) { - this.$('.code-issue-errors').removeClass('hidden').text(msg); - }, - cancel: function (e) { e.preventDefault(); this.close(); }, serializeData: function () { - return _.extend(Marionette.ItemView.prototype.serializeData.apply(this, arguments), { - line: this.options.line, - component: this.options.component, + return _.extend(this._super(), { rules: _.sortBy(this.rules, 'name') }); } diff --git a/server/sonar-web/src/main/js/components/issue/models/issue.js b/server/sonar-web/src/main/js/components/issue/models/issue.js index 1c5e928818e..af5dd980c27 100644 --- a/server/sonar-web/src/main/js/components/issue/models/issue.js +++ b/server/sonar-web/src/main/js/components/issue/models/issue.js @@ -1,4 +1,4 @@ -define([], function () { +define(function () { return Backbone.Model.extend({ idAttribute: 'key', @@ -7,8 +7,40 @@ define([], function () { return baseUrl + '/api/issues/show?key=' + this.get('key'); }, + urlRoot: function () { + return baseUrl + '/api/issues'; + }, + parse: function (r) { return r.issue ? r.issue : r; + }, + + sync: function (method, model, options) { + var opts = options || {}; + opts.contentType = 'application/x-www-form-urlencoded'; + if (method === 'read') { + _.extend(opts, { + type: 'GET', + url: this.urlRoot() + '/show', + data: { key: model.id } + }); + } + if (method === 'create') { + _.extend(opts, { + type: 'POST', + url: this.urlRoot() + '/create', + data: { + component: model.get('component'), + line: model.get('line'), + message: model.get('message'), + rule: model.get('rule'), + severity: model.get('severity') + } + }); + } + var xhr = options.xhr = Backbone.ajax(opts); + model.trigger('request', model, xhr, options); + return xhr; } }); diff --git a/server/sonar-web/src/main/js/components/issue/templates/manual-issue.hbs b/server/sonar-web/src/main/js/components/issue/templates/manual-issue.hbs index bbcf333395b..0a38fa2136f 100644 --- a/server/sonar-web/src/main/js/components/issue/templates/manual-issue.hbs +++ b/server/sonar-web/src/main/js/components/issue/templates/manual-issue.hbs @@ -1,19 +1,6 @@ -
- {{! no manual rules }} - {{!
}} - {{! <% if is_admin %> }} - {{! <%= message('issue.manual.no_rules.admin') -%> }} - {{!  <%= message('manage') -%> }} - {{! <% else %> }} - {{! <%= message('issue.manual.no_rules.non_admin') -%> }} - {{! <% end %> }} - {{!  <%= link_to_function message('cancel'), 'closeCreateIssueForm(this)' -%> }} - {{!
}} + - - - -
+
-
- - - - - - - - -
- -
- - {{t 'cancel'}} -
- +
+ +
+ + diff --git a/server/sonar-web/src/main/js/components/source-viewer/main.js b/server/sonar-web/src/main/js/components/source-viewer/main.js index 07dfc55580d..a007145f354 100644 --- a/server/sonar-web/src/main/js/components/source-viewer/main.js +++ b/server/sonar-web/src/main/js/components/source-viewer/main.js @@ -479,8 +479,8 @@ define([ line: line, row: $(e.currentTarget).closest('.source-line') }); - popup.on('onManualIssueAdded', function (data) { - that.addIssue(new Issue(data)); + popup.on('onManualIssueAdded', function (issue) { + that.addIssue(issue); }); popup.render(); }, -- cgit v1.2.3