modal_popup.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. onPrimary: function () {
  35. this.hide();
  36. },
  37. onSecondary: function () {
  38. this.hide();
  39. },
  40. onThird: function () {
  41. this.hide();
  42. },
  43. onClose: function () {
  44. this.hide();
  45. },
  46. hide: function () {
  47. this.destroy();
  48. },
  49. showFooter: true,
  50. /**
  51. * Hide or show 'X' button for closing popup
  52. */
  53. showCloseButton: true,
  54. didInsertElement: function () {
  55. if (this.autoHeight) {
  56. var block = this.$().find('#modal > .modal-body').first();
  57. block.css('max-height', $(window).height() - block.offset().top - 300 + $(window).scrollTop()); // fix popup height
  58. }
  59. // If popup is opened from another popup it should be displayed above
  60. var existedPopups = $(document).find('.modal-backdrop');
  61. if (existedPopups) {
  62. var maxZindex = 1;
  63. existedPopups.each(function(index, popup) {
  64. if ($(popup).css('z-index') > maxZindex) {
  65. maxZindex = $(popup).css('z-index');
  66. }
  67. });
  68. this.$().find('.modal-backdrop').css('z-index', maxZindex * 2);
  69. this.$().find('.modal').css('z-index', maxZindex * 2 + 1);
  70. }
  71. },
  72. fitHeight: function () {
  73. var popup = this.$().find('#modal');
  74. var block = this.$().find('#modal > .modal-body');
  75. var wh = $(window).height();
  76. var top = wh * .05;
  77. popup.css({
  78. 'top': top + 'px',
  79. 'marginTop': 0
  80. });
  81. block.css('max-height', $(window).height() - top * 2 - (popup.height() - block.height()));
  82. }
  83. });
  84. App.ModalPopup.reopenClass({
  85. show: function (options) {
  86. var popup = this.create(options);
  87. popup.appendTo('#wrapper');
  88. return popup;
  89. }
  90. });
  91. App.showReloadPopup = function () {
  92. return App.ModalPopup.show({
  93. primary: null,
  94. secondary: null,
  95. showFooter: false,
  96. header: this.t('app.reloadPopup.header'),
  97. 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>",
  98. encodeBody: false
  99. });
  100. };
  101. /**
  102. * Show confirmation popup
  103. *
  104. * @param {Function} primary - "OK" button click handler
  105. * @param {String} body - additional text constant. Will be placed in the popup-body
  106. * @param {Function} secondary
  107. * @return {*}
  108. */
  109. App.showConfirmationPopup = function (primary, body, secondary) {
  110. if (!primary) {
  111. return false;
  112. }
  113. return App.ModalPopup.show({
  114. encodeBody: false,
  115. header: Em.I18n.t('popup.confirmation.commonHeader'),
  116. body: body || Em.I18n.t('question.sure'),
  117. onPrimary: function () {
  118. this.hide();
  119. primary();
  120. },
  121. onSecondary: function () {
  122. this.hide();
  123. if (secondary) {
  124. secondary();
  125. }
  126. },
  127. onClose: function () {
  128. this.hide();
  129. if (secondary) {
  130. secondary();
  131. }
  132. }
  133. });
  134. };
  135. /**
  136. * Show confirmation popup
  137. * After sending command watch status of query,
  138. * and in case of failure provide ability to retry to launch an operation.
  139. *
  140. * @param {Function} primary - "OK" button click handler
  141. * @param {Object} bodyMessage - confirmMsg:{String},
  142. confirmButton:{String},
  143. additionalWarningMsg:{String},
  144. * @param {Function} secondary - "Cancel" button click handler
  145. * @return {*}
  146. */
  147. App.showConfirmationFeedBackPopup = function (primary, bodyMessage, secondary) {
  148. if (!primary) {
  149. return false;
  150. }
  151. return App.ModalPopup.show({
  152. header: Em.I18n.t('popup.confirmation.commonHeader'),
  153. bodyClass: Em.View.extend({
  154. templateName: require('templates/common/confirmation_feedback')
  155. }),
  156. query: Em.Object.create({status: "INIT"}),
  157. primary: function () {
  158. return bodyMessage? bodyMessage.confirmButton : Em.I18n.t('ok');
  159. }.property('bodyMessage'),
  160. onPrimary: function () {
  161. this.set('query.status', "INIT");
  162. this.set('disablePrimary', true);
  163. this.set('disableSecondary', true);
  164. this.set('statusMessage', Em.I18n.t('popup.confirmationFeedBack.sending'));
  165. this.hide();
  166. primary(this.get('query'), this.get('runMmOperation'));
  167. },
  168. statusMessage: function () {
  169. return bodyMessage? bodyMessage.confirmMsg : Em.I18n.t('question.sure');
  170. }.property('bodyMessage'),
  171. additionalWarningMsg: function () {
  172. return bodyMessage? bodyMessage.additionalWarningMsg : null;
  173. }.property('bodyMessage'),
  174. putInMaintenance: function () {
  175. return bodyMessage ? bodyMessage.putInMaintenance : null;
  176. }.property('bodyMessage'),
  177. runMmOperation: false,
  178. turnOnMmMsg: function () {
  179. return bodyMessage ? bodyMessage.turnOnMmMsg : null;
  180. }.property('bodyMessage'),
  181. watchStatus: function() {
  182. if (this.get('query.status') === "SUCCESS") {
  183. this.hide();
  184. } else if(this.get('query.status') === "FAIL") {
  185. this.set('primaryClass', 'btn-primary');
  186. this.set('primary', Em.I18n.t('common.retry'));
  187. this.set('disablePrimary', false);
  188. this.set('disableSecondary', false);
  189. this.set('statusMessage', Em.I18n.t('popup.confirmationFeedBack.query.fail'));
  190. }
  191. }.observes('query.status'),
  192. onSecondary: function () {
  193. this.hide();
  194. if (secondary) {
  195. secondary();
  196. }
  197. }
  198. });
  199. };
  200. /**
  201. * Show alert popup
  202. *
  203. * @param {String} header - header of the popup
  204. * @param {String} body - body of the popup
  205. * @param {Function} primary - function to call upon clicking the OK button
  206. * @return {*}
  207. */
  208. App.showAlertPopup = function (header, body, primary) {
  209. return App.ModalPopup.show({
  210. primary: Em.I18n.t('ok'),
  211. secondary: null,
  212. header: header,
  213. body: body,
  214. onPrimary: function () {
  215. this.hide();
  216. if (primary) {
  217. primary();
  218. }
  219. }
  220. });
  221. };
  222. /**
  223. * Show prompt popup
  224. *
  225. * @param {String} text - additional text constant. Will be placed on the top of the input field
  226. * @param {Function} primary - "OK" button click handler
  227. * @param {String} defaultValue - additional text constant. Will be default value for input field
  228. * @param {Function} secondary
  229. * @return {*}
  230. */
  231. App.showPromptPopup = function (text, primary, defaultValue, secondary) {
  232. if (!primary) {
  233. return false;
  234. }
  235. return App.ModalPopup.show({
  236. header: Em.I18n.t('popup.prompt.commonHeader'),
  237. bodyClass: Em.View.extend({
  238. templateName: require('templates/common/prompt_popup'),
  239. text: text
  240. }),
  241. inputValue: defaultValue || '',
  242. isInvalid: false,
  243. errorMessage: '',
  244. onPrimary: function () {
  245. this.hide();
  246. primary(this.get('inputValue'));
  247. },
  248. onSecondary: function () {
  249. this.hide();
  250. if (secondary) {
  251. secondary();
  252. }
  253. }
  254. });
  255. };