modal_popup.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. App.ModalPopup = Ember.View.extend({
  20. viewName: 'modalPopup',
  21. templateName: require('templates/common/modal_popup'),
  22. header: ' ',
  23. body: ' ',
  24. encodeBody: true,
  25. // define bodyClass which extends Ember.View to use an arbitrary Handlebars template as the body
  26. primary: Em.I18n.t('ok'),
  27. secondary: Em.I18n.t('common.cancel'),
  28. third: null,
  29. autoHeight: true,
  30. disablePrimary: false,
  31. disableSecondary: false,
  32. disableThird: false,
  33. primaryClass: 'btn-success',
  34. secondaryClass: '',
  35. onPrimary: function () {
  36. this.hide();
  37. },
  38. onSecondary: function () {
  39. this.hide();
  40. },
  41. onThird: function () {
  42. this.hide();
  43. },
  44. onClose: function () {
  45. this.hide();
  46. },
  47. hide: function () {
  48. this.destroy();
  49. },
  50. showFooter: true,
  51. /**
  52. * Hide or show 'X' button for closing popup
  53. */
  54. showCloseButton: true,
  55. didInsertElement: function () {
  56. if (this.autoHeight) {
  57. var block = this.$().find('#modal > .modal-body').first();
  58. if(block.offset()) {
  59. block.css('max-height', $(window).height() - block.offset().top - 300 + $(window).scrollTop()); // fix popup height
  60. }
  61. }
  62. // If popup is opened from another popup it should be displayed above
  63. var existedPopups = $('.modal-backdrop');
  64. if (existedPopups) {
  65. var maxZindex = 1;
  66. existedPopups.each(function(index, popup) {
  67. if ($(popup).css('z-index') > maxZindex) {
  68. maxZindex = $(popup).css('z-index');
  69. }
  70. });
  71. this.$().find('.modal-backdrop').css('z-index', maxZindex * 2);
  72. this.$().find('.modal').css('z-index', maxZindex * 2 + 1);
  73. }
  74. var firstInputElement = this.$('#modal').find(':input').not(':disabled').first();
  75. this.focusElement(firstInputElement);
  76. },
  77. focusElement: function(elem) {
  78. elem.focus();
  79. },
  80. fitHeight: function () {
  81. var popup = this.$().find('#modal');
  82. var block = this.$().find('#modal > .modal-body');
  83. var wh = $(window).height();
  84. var top = wh * .05;
  85. popup.css({
  86. 'top': top + 'px',
  87. 'marginTop': 0
  88. });
  89. block.css('max-height', $(window).height() - top * 2 - (popup.height() - block.height()));
  90. }
  91. });
  92. App.ModalPopup.reopenClass({
  93. show: function (options) {
  94. var popup = this.create(options);
  95. popup.appendTo('#wrapper');
  96. return popup;
  97. }
  98. });