浏览代码

AMBARI-7728 Dialog overlays do not receive keyboard focus (apenniston via jaoki)

Jun Aoki 10 年之前
父节点
当前提交
ad9b02b155

+ 1 - 0
ambari-web/app/assets/test/tests.js

@@ -140,6 +140,7 @@ var files = ['test/init_model_test',
   'test/views/common/table_view_test',
   'test/views/common/quick_link_view_test',
   'test/views/common/rolling_restart_view_test',
+  'test/views/common/modal_popup_test',
   'test/views/common/configs/config_history_flow_test',
   'test/views/main/dashboard_test',
   'test/views/main/menu_test',

+ 15 - 6
ambari-web/app/views/common/modal_popup.js

@@ -63,21 +63,30 @@ App.ModalPopup = Ember.View.extend({
 
   didInsertElement: function () {
     if (this.autoHeight) {
-      var block = this.$().find('#modal > .modal-body').first();
-      block.css('max-height', $(window).height() - block.offset().top - 300 + $(window).scrollTop()); // fix popup height
+      var block = $('#modal > .modal-body').first();
+      if(block.offset()) {
+        block.css('max-height', $(window).height() - block.offset().top  - 300 + $(window).scrollTop()); // fix popup height
+      }
     }
     // If popup is opened from another popup it should be displayed above
-    var existedPopups = $(document).find('.modal-backdrop');
+    var existedPopups = $('.modal-backdrop');
     if (existedPopups) {
       var maxZindex = 1;
       existedPopups.each(function(index, popup) {
         if ($(popup).css('z-index') > maxZindex) {
           maxZindex = $(popup).css('z-index');
-        }
+      }
       });
-      this.$().find('.modal-backdrop').css('z-index', maxZindex * 2);
-      this.$().find('.modal').css('z-index', maxZindex * 2 + 1);
+      $('.modal-backdrop').css('z-index', maxZindex * 2);
+      $('.modal').css('z-index', maxZindex * 2 + 1);
     }
+
+    var firstInputElement = $('#modal').find(':input').not(':disabled').first();
+    this.focusElement(firstInputElement);
+  },
+
+  focusElement: function(elem) {
+    elem.focus();
   },
 
   fitHeight: function () {

+ 46 - 0
ambari-web/test/views/common/modal_popup_test.js

@@ -0,0 +1,46 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+var App = require('app');
+require('views/common/modal_popup');
+
+describe('App.ModalPopup', function() {
+
+  var popup;
+
+  beforeEach(function () {
+    popup = App.ModalPopup.create(
+        {
+          primary: 'test',
+          secondary: 'test',
+          header: 'test',
+          body: '<p>text</p><input type="text"><input type="checkbox"><input type="button">'
+        }
+    );
+  });
+
+  describe('#didInsertElement', function () {
+
+    it('should focus on the first input element', function () {
+      var spy = sinon.spy(popup, "focusElement");
+      popup.didInsertElement();
+      expect(spy.called);
+    });
+  });
+
+});