modal_popup.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. marginBottom: 300,
  31. disablePrimary: false,
  32. disableSecondary: false,
  33. disableThird: false,
  34. primaryClass: 'btn-success',
  35. secondaryClass: '',
  36. onPrimary: function () {
  37. this.hide();
  38. },
  39. onSecondary: function () {
  40. this.hide();
  41. },
  42. onThird: function () {
  43. this.hide();
  44. },
  45. onClose: function () {
  46. this.hide();
  47. },
  48. hide: function () {
  49. this.destroy();
  50. },
  51. showFooter: true,
  52. /**
  53. * Hide or show 'X' button for closing popup
  54. */
  55. showCloseButton: true,
  56. didInsertElement: function () {
  57. if (this.autoHeight) {
  58. var block = this.$().find('#modal > .modal-body').first();
  59. if(block.offset()) {
  60. block.css('max-height', $(window).height() - block.offset().top - this.marginBottom + $(window).scrollTop()); // fix popup height
  61. }
  62. }
  63. // If popup is opened from another popup it should be displayed above
  64. var existedPopups = $('.modal-backdrop');
  65. if (existedPopups) {
  66. var maxZindex = 1;
  67. existedPopups.each(function(index, popup) {
  68. if ($(popup).css('z-index') > maxZindex) {
  69. maxZindex = $(popup).css('z-index');
  70. }
  71. });
  72. this.$().find('.modal-backdrop').css('z-index', maxZindex * 2);
  73. this.$().find('.modal').css('z-index', maxZindex * 2 + 1);
  74. }
  75. var firstInputElement = this.$('#modal').find(':input').not(':disabled').first();
  76. this.focusElement(firstInputElement);
  77. },
  78. focusElement: function(elem) {
  79. elem.focus();
  80. },
  81. fitHeight: function () {
  82. var popup = this.$().find('#modal');
  83. var block = this.$().find('#modal > .modal-body');
  84. var wh = $(window).height();
  85. var top = wh * .05;
  86. popup.css({
  87. 'top': top + 'px',
  88. 'marginTop': 0
  89. });
  90. block.css('max-height', $(window).height() - top * 2 - (popup.height() - block.height()));
  91. }
  92. });
  93. App.ModalPopup.reopenClass({
  94. show: function (options) {
  95. var popup = this.create(options);
  96. popup.appendTo('#wrapper');
  97. return popup;
  98. }
  99. });