modal_popup.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. });
  99. App.showReloadPopup = function () {
  100. return App.ModalPopup.show({
  101. primary: null,
  102. secondary: null,
  103. showFooter: false,
  104. header: this.t('app.reloadPopup.header'),
  105. body: "<div id='reload_popup' class='alert alert-info'><div class='spinner'><span>" + this.t('app.reloadPopup.text') + "</span></div></div><div><a href='#' onclick='location.reload();'>" + this.t('app.reloadPopup.link') + "</a></div>",
  106. encodeBody: false
  107. });
  108. };
  109. /**
  110. * Show confirmation popup
  111. *
  112. * @param {Function} primary - "OK" button click handler
  113. * @param {String} body - additional text constant. Will be placed in the popup-body
  114. * @param {Function} secondary
  115. * @param {String} header
  116. * @param {String} primaryText
  117. * @return {*}
  118. */
  119. App.showConfirmationPopup = function (primary, body, secondary, header, primaryText) {
  120. if (!primary) {
  121. return false;
  122. }
  123. return App.ModalPopup.show({
  124. encodeBody: false,
  125. primary: primaryText || Em.I18n.t('ok'),
  126. header: header || Em.I18n.t('popup.confirmation.commonHeader'),
  127. body: body || Em.I18n.t('question.sure'),
  128. onPrimary: function () {
  129. this.hide();
  130. primary();
  131. },
  132. onSecondary: function () {
  133. this.hide();
  134. if (secondary) {
  135. secondary();
  136. }
  137. },
  138. onClose: function () {
  139. this.hide();
  140. if (secondary) {
  141. secondary();
  142. }
  143. }
  144. });
  145. };
  146. /**
  147. * Show confirmation popup
  148. * After sending command watch status of query,
  149. * and in case of failure provide ability to retry to launch an operation.
  150. *
  151. * @param {Function} primary - "OK" button click handler
  152. * @param {Object} bodyMessage - confirmMsg:{String},
  153. confirmButton:{String},
  154. additionalWarningMsg:{String},
  155. * @param {Function} secondary - "Cancel" button click handler
  156. * @return {*}
  157. */
  158. App.showConfirmationFeedBackPopup = function (primary, bodyMessage, secondary) {
  159. if (!primary) {
  160. return false;
  161. }
  162. return App.ModalPopup.show({
  163. header: Em.I18n.t('popup.confirmation.commonHeader'),
  164. bodyClass: Em.View.extend({
  165. templateName: require('templates/common/confirmation_feedback')
  166. }),
  167. query: Em.Object.create({status: "INIT"}),
  168. primary: function () {
  169. return bodyMessage? bodyMessage.confirmButton : Em.I18n.t('ok');
  170. }.property('bodyMessage'),
  171. onPrimary: function () {
  172. this.set('query.status', "INIT");
  173. this.set('disablePrimary', true);
  174. this.set('disableSecondary', true);
  175. this.set('statusMessage', Em.I18n.t('popup.confirmationFeedBack.sending'));
  176. this.hide();
  177. primary(this.get('query'), this.get('runMmOperation'));
  178. },
  179. statusMessage: function () {
  180. return bodyMessage? bodyMessage.confirmMsg : Em.I18n.t('question.sure');
  181. }.property('bodyMessage'),
  182. additionalWarningMsg: function () {
  183. return bodyMessage? bodyMessage.additionalWarningMsg : null;
  184. }.property('bodyMessage'),
  185. putInMaintenance: function () {
  186. return bodyMessage ? bodyMessage.putInMaintenance : null;
  187. }.property('bodyMessage'),
  188. runMmOperation: false,
  189. turnOnMmMsg: function () {
  190. return bodyMessage ? bodyMessage.turnOnMmMsg : null;
  191. }.property('bodyMessage'),
  192. watchStatus: function() {
  193. if (this.get('query.status') === "SUCCESS") {
  194. this.hide();
  195. } else if(this.get('query.status') === "FAIL") {
  196. this.set('primaryClass', 'btn-primary');
  197. this.set('primary', Em.I18n.t('common.retry'));
  198. this.set('disablePrimary', false);
  199. this.set('disableSecondary', false);
  200. this.set('statusMessage', Em.I18n.t('popup.confirmationFeedBack.query.fail'));
  201. }
  202. }.observes('query.status'),
  203. onSecondary: function () {
  204. this.hide();
  205. if (secondary) {
  206. secondary();
  207. }
  208. }
  209. });
  210. };
  211. /**
  212. * Show alert popup
  213. *
  214. * @param {String} header - header of the popup
  215. * @param {String} body - body of the popup
  216. * @param {Function} primary - function to call upon clicking the OK button
  217. * @return {*}
  218. */
  219. App.showAlertPopup = function (header, body, primary) {
  220. return App.ModalPopup.show({
  221. primary: Em.I18n.t('ok'),
  222. secondary: null,
  223. header: header,
  224. body: body,
  225. onPrimary: function () {
  226. this.hide();
  227. if (primary) {
  228. primary();
  229. }
  230. }
  231. });
  232. };
  233. /**
  234. * Show prompt popup
  235. *
  236. * @param {String} text - additional text constant. Will be placed on the top of the input field
  237. * @param {Function} primary - "OK" button click handler
  238. * @param {String} defaultValue - additional text constant. Will be default value for input field
  239. * @param {Function} secondary
  240. * @return {*}
  241. */
  242. App.showPromptPopup = function (text, primary, defaultValue, secondary) {
  243. if (!primary) {
  244. return false;
  245. }
  246. return App.ModalPopup.show({
  247. header: Em.I18n.t('popup.prompt.commonHeader'),
  248. bodyClass: Em.View.extend({
  249. templateName: require('templates/common/prompt_popup'),
  250. text: text
  251. }),
  252. inputValue: defaultValue || '',
  253. isInvalid: false,
  254. errorMessage: '',
  255. onPrimary: function () {
  256. this.hide();
  257. primary(this.get('inputValue'));
  258. },
  259. onSecondary: function () {
  260. this.hide();
  261. if (secondary) {
  262. secondary();
  263. }
  264. }
  265. });
  266. };