modal_popup.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. template: Ember.Handlebars.compile([
  22. '<div class="modal-backdrop"></div><div class="modal" id="modal" tabindex="-1" role="dialog" aria-labelledby="modal-label" aria-hidden="true">',
  23. '<div class="modal-header">',
  24. '{{#if showCloseButton}}<a class="close" {{action onClose target="view"}}>x</a>{{/if}}',
  25. '<h3 id="modal-label">',
  26. '{{#if headerClass}}{{view headerClass}}',
  27. '{{else}}{{header}}{{/if}}',
  28. '</h3>',
  29. '</div>',
  30. '<div class="modal-body">',
  31. '{{#if bodyClass}}{{view bodyClass}}',
  32. '{{else}}{{#if encodeBody}}{{body}}{{else}}{{{body}}}{{/if}}{{/if}}',
  33. '</div>',
  34. '{{#if showFooter}}',
  35. '{{#if footerClass}}{{view footerClass}}',
  36. '{{else}}',
  37. '<div class="modal-footer">',
  38. '{{#if view.secondary}}<a class="btn" {{action onSecondary target="view"}}>{{view.secondary}}</a>{{/if}}',
  39. '{{#if view.primary}}<a class="btn btn-success" {{action onPrimary target="view"}}>{{view.primary}}</a>{{/if}}',
  40. '</div>',
  41. '{{/if}}',
  42. '{{/if}}',
  43. '</div>'
  44. ].join('\n')),
  45. header: '&nbsp;',
  46. body: '&nbsp;',
  47. encodeBody: true,
  48. // define bodyClass which extends Ember.View to use an arbitrary Handlebars template as the body
  49. primary: Em.I18n.t('ok'),
  50. secondary: Em.I18n.t('common.cancel'),
  51. autoHeight: true,
  52. onPrimary: function() {
  53. this.hide();
  54. },
  55. onSecondary: function() {
  56. this.hide();
  57. },
  58. onClose: function() {
  59. this.hide();
  60. },
  61. hide: function() {
  62. this.destroy();
  63. },
  64. showFooter: true,
  65. /**
  66. * Hide or show 'X' button for closing popup
  67. */
  68. showCloseButton: true,
  69. didInsertElement: function(){
  70. if(this.autoHeight){
  71. var block = this.$().find('#modal > .modal-body').first();
  72. block.css('max-height', $(window).height() - block.offset().top - 300 + $(window).scrollTop()); // fix popup height
  73. }
  74. },
  75. fitHeight: function(){
  76. var popup = this.$().find('#modal');
  77. var block = this.$().find('#modal > .modal-body');
  78. var wh = $(window).height();
  79. var top = wh * .05;
  80. popup.css({
  81. 'top' : top + 'px',
  82. 'marginTop' : 0
  83. });
  84. block.css('max-height', $(window).height()- top * 2 - 100);
  85. }
  86. });
  87. App.ModalPopup.reopenClass({
  88. show: function(options) {
  89. var popup = this.create(options);
  90. popup.appendTo('#wrapper');
  91. return popup;
  92. }
  93. })
  94. App.showReloadPopup = function(){
  95. return App.ModalPopup.show({
  96. primary: null,
  97. secondary: null,
  98. showFooter: false,
  99. header: this.t('app.reloadPopup.header'),
  100. body: "<div class='alert alert-info'><div class='spinner'>" + this.t('app.reloadPopup.text') + "</div></div><div><a href='#' onclick='location.reload();'>" + this.t('app.reloadPopup.link') + "</a></div>",
  101. encodeBody: false
  102. });
  103. }
  104. /**
  105. * Show confirmation popup
  106. *
  107. * @param {Function} primary - "OK" button click handler
  108. * @param {String} body - additional text constant. Will be placed in the popup-body
  109. * @return {*}
  110. */
  111. App.showConfirmationPopup = function(primary, body) {
  112. if (!primary) {
  113. return false;
  114. }
  115. return App.ModalPopup.show({
  116. primary: Em.I18n.t('yes'),
  117. secondary: Em.I18n.t('no'),
  118. header: Em.I18n.t('popup.confirmation.commonHeader'),
  119. body: Em.I18n.t('question.sure').format(body || ''),
  120. onPrimary: function() {
  121. primary();
  122. this.hide();
  123. }
  124. });
  125. }