]> source.dussan.org Git - sonarqube.git/commitdiff
Add modal form view
authorStas Vilchik <vilchiks@gmail.com>
Fri, 26 Dec 2014 14:17:59 +0000 (15:17 +0100)
committerStas Vilchik <vilchiks@gmail.com>
Fri, 26 Dec 2014 14:17:59 +0000 (15:17 +0100)
server/sonar-web/src/main/coffee/common/modals.coffee [deleted file]
server/sonar-web/src/main/hbs/coding-rules-old/coding-rules-custom-rule-creation.hbs
server/sonar-web/src/main/hbs/coding-rules/rule/coding-rules-custom-rule-creation.hbs
server/sonar-web/src/main/js/coding-rules/rule/custom-rule-creation-view.js
server/sonar-web/src/main/js/common/modal-form.js [new file with mode: 0644]
server/sonar-web/src/main/js/common/modals.js [new file with mode: 0644]

diff --git a/server/sonar-web/src/main/coffee/common/modals.coffee b/server/sonar-web/src/main/coffee/common/modals.coffee
deleted file mode 100644 (file)
index c9e7c4a..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-define [
-  'backbone.marionette'
-], (
-  Marionette
-) ->
-
-  $ = jQuery
-  EVENT_SCOPE = 'modal'
-
-
-  class extends Marionette.ItemView
-    className: 'modal'
-    overlayClassName: 'modal-overlay'
-
-
-    events: ->
-      'click .js-modal-close': 'close'
-
-
-    onRender: ->
-      @$el.detach().appendTo $('body')
-      @renderOverlay()
-      key 'escape', (=> @close())
-
-
-    onClose: ->
-      @removeOverlay()
-
-
-    renderOverlay: ->
-      overlay = $(".#{@overlayClassName}")
-      if overlay.length == 0
-        overlay = $("<div class=\"#{@overlayClassName}\"></div>").appendTo $('body')
-
-
-    removeOverlay: ->
-      $('.modal-overlay').remove()
-
-
-    attachCloseEvents: ->
-      $('body').on "click.#{EVENT_SCOPE}", =>
-        $('body').off "click.#{EVENT_SCOPE}"
-        @close()
index c1244ff82e6922642eccab98123c12b57e462df6..94323fa40cfd447450212f2414c06413bc500c4f 100644 (file)
@@ -8,6 +8,7 @@
   </div>
 
   <div class="modal-body">
+    <div class="js-modal-messages"></div>
     <div class="modal-error"></div>
     <div class="modal-warning">{{t 'coding_rules.reactivate.help'}}</div>
 
index 24638406c3027b6abff0c7658dd5618db8651d60..fe89c48d2d51dae67825d86d39d68817a256644b 100644 (file)
@@ -8,6 +8,7 @@
   </div>
 
   <div class="modal-body">
+    <div class="js-modal-messages"></div>
     <div class="modal-error"></div>
     <div class="modal-warning">{{t 'coding_rules.reactivate.help'}}</div>
 
index 0e8f6a8529a17e63a8856071e0f29818f044f07e..06b3e9e3d952ba4d6cce34608b82866a3daa3115 100644 (file)
@@ -1,27 +1,29 @@
 define([
-  'common/modals',
+  'common/modal-form',
   'templates/coding-rules'
-], function (Modal, Templates) {
+], function (ModalFormView, Templates) {
 
   var $ = jQuery;
 
-  return Modal.extend({
+  return ModalFormView.extend({
     template: Templates['coding-rules-custom-rule-creation'],
 
-    ui: {
-      customRuleCreationKey: '#coding-rules-custom-rule-creation-key',
-      customRuleCreationName: '#coding-rules-custom-rule-creation-name',
-      customRuleCreationHtmlDescription: '#coding-rules-custom-rule-creation-html-description',
-      customRuleCreationSeverity: '#coding-rules-custom-rule-creation-severity',
-      customRuleCreationStatus: '#coding-rules-custom-rule-creation-status',
-      customRuleCreationParameters: '[name]',
-      customRuleCreationCreate: '#coding-rules-custom-rule-creation-create',
-      customRuleCreationReactivate: '#coding-rules-custom-rule-creation-reactivate',
-      modalFoot: '.modal-foot'
+    ui: function () {
+      return _.extend(ModalFormView.prototype.ui.apply(this, arguments), {
+        customRuleCreationKey: '#coding-rules-custom-rule-creation-key',
+            customRuleCreationName: '#coding-rules-custom-rule-creation-name',
+          customRuleCreationHtmlDescription: '#coding-rules-custom-rule-creation-html-description',
+          customRuleCreationSeverity: '#coding-rules-custom-rule-creation-severity',
+          customRuleCreationStatus: '#coding-rules-custom-rule-creation-status',
+          customRuleCreationParameters: '[name]',
+          customRuleCreationCreate: '#coding-rules-custom-rule-creation-create',
+          customRuleCreationReactivate: '#coding-rules-custom-rule-creation-reactivate',
+          modalFoot: '.modal-foot'
+      });
     },
 
     events: function () {
-      return _.extend(Modal.prototype.events.apply(this, arguments), {
+      return _.extend(ModalFormView.prototype.events.apply(this, arguments), {
         'input @ui.customRuleCreationName': 'generateKey',
         'keydown @ui.customRuleCreationName': 'generateKey',
         'keyup @ui.customRuleCreationName': 'generateKey',
@@ -52,7 +54,7 @@ define([
     },
 
     onRender: function () {
-      Modal.prototype.onRender.apply(this, arguments);
+      ModalFormView.prototype.onRender.apply(this, arguments);
 
       this.keyModifiedByUser = false;
 
@@ -148,8 +150,7 @@ define([
           that.$('.modal-warning').show();
           that.ui.modalFoot.html(Templates['coding-rules-custom-rule-reactivation']());
         } else {
-          jQuery.ajaxSettings.error(jqXHR, textStatus, errorThrown);
-          that.ui.modalFoot.html(origFooter);
+          that.showErrors(jqXHR.responseJSON.errors, jqXHR.responseJSON.warnings);
         }
       });
     },
@@ -171,7 +172,7 @@ define([
         };
       });
 
-      return _.extend(Modal.prototype.serializeData.apply(this, arguments), {
+      return _.extend(ModalFormView.prototype.serializeData.apply(this, arguments), {
         change: this.model && this.model.has('key'),
         params: params,
         severities: ['BLOCKER', 'CRITICAL', 'MAJOR', 'MINOR', 'INFO'],
diff --git a/server/sonar-web/src/main/js/common/modal-form.js b/server/sonar-web/src/main/js/common/modal-form.js
new file mode 100644 (file)
index 0000000..39fac08
--- /dev/null
@@ -0,0 +1,38 @@
+define(['common/modals'], function (ModalView) {
+
+  return ModalView.extend({
+
+    ui: function () {
+      return {
+        messagesContainer: '.js-modal-messages'
+      };
+    },
+
+    events: function () {
+      return _.extend(ModalView.prototype.events.apply(this, arguments), {
+        'submit form': 'onFormSubmit'
+      });
+    },
+
+    onFormSubmit: function (e) {
+      e.preventDefault();
+    },
+
+    showErrors: function (errors, warnings) {
+      var container = this.ui.messagesContainer.empty();
+      if (_.isArray(errors)) {
+        errors.forEach(function (error) {
+          var html = '<div class="message-error">' + error.msg + '</div>';
+          container.append(html);
+        });
+      }
+      if (_.isArray(warnings)) {
+        warnings.forEach(function (warn) {
+          var html = '<div class="message-alert">' + warn.msg + '</div>';
+          container.append(html);
+        });
+      }
+    }
+  });
+
+});
diff --git a/server/sonar-web/src/main/js/common/modals.js b/server/sonar-web/src/main/js/common/modals.js
new file mode 100644 (file)
index 0000000..99917d9
--- /dev/null
@@ -0,0 +1,54 @@
+define(['backbone.marionette'], function (Marionette) {
+
+  var $ = jQuery,
+      EVENT_SCOPE = 'modal';
+
+  return Marionette.ItemView.extend({
+    className: 'modal',
+    overlayClassName: 'modal-overlay',
+
+    events: function () {
+      return {
+        'click .js-modal-close': 'close'
+      };
+    },
+
+    onRender: function () {
+      var that = this;
+      this.$el.detach().appendTo($('body'));
+      this.renderOverlay();
+      this.keyScope = key.getScope();
+      key.setScope('modal');
+      key('escape', 'modal', function () {
+        that.close();
+        return false;
+      });
+    },
+
+    onClose: function () {
+      this.removeOverlay();
+      key.deleteScope('modal');
+      key.setScope(this.keyScope);
+    },
+
+    renderOverlay: function () {
+      var overlay = $('.' + this.overlayClassName);
+      if (overlay.length === 0) {
+        $('<div class="' + this.overlayClassName + '"></div>').appendTo($('body'));
+      }
+    },
+
+    removeOverlay: function () {
+      $('.' + this.overlayClassName).remove();
+    },
+
+    attachCloseEvents: function () {
+      var that = this;
+      $('body').on('click.' + EVENT_SCOPE, function () {
+        $('body').off('click.' + EVENT_SCOPE);
+        that.close();
+      });
+    }
+  });
+
+});