MessageBox.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * MessageBox.js
  3. *
  4. * Copyright, Moxiecode Systems AB
  5. * Released under LGPL License.
  6. *
  7. * License: http://www.tinymce.com/license
  8. * Contributing: http://www.tinymce.com/contributing
  9. */
  10. /**
  11. * This class is used to create MessageBoxes like alerts/confirms etc.
  12. *
  13. * @class tinymce.ui.Window
  14. * @extends tinymce.ui.FloatPanel
  15. */
  16. define("tinymce/ui/MessageBox", [
  17. "tinymce/ui/Window"
  18. ], function(Window) {
  19. "use strict";
  20. var MessageBox = Window.extend({
  21. /**
  22. * Constructs a instance with the specified settings.
  23. *
  24. * @constructor
  25. * @param {Object} settings Name/value object with settings.
  26. */
  27. init: function(settings) {
  28. settings = {
  29. border: 1,
  30. padding: 20,
  31. layout: 'flex',
  32. pack: "center",
  33. align: "center",
  34. containerCls: 'panel',
  35. autoScroll: true,
  36. buttons: {type: "button", text: "Ok", action: "ok"},
  37. items: {
  38. type: "label",
  39. multiline: true,
  40. maxWidth: 500,
  41. maxHeight: 200
  42. }
  43. };
  44. this._super(settings);
  45. },
  46. Statics: {
  47. /**
  48. * Ok buttons constant.
  49. *
  50. * @static
  51. * @final
  52. * @field {Number} OK
  53. */
  54. OK: 1,
  55. /**
  56. * Ok/cancel buttons constant.
  57. *
  58. * @static
  59. * @final
  60. * @field {Number} OK_CANCEL
  61. */
  62. OK_CANCEL: 2,
  63. /**
  64. * yes/no buttons constant.
  65. *
  66. * @static
  67. * @final
  68. * @field {Number} YES_NO
  69. */
  70. YES_NO: 3,
  71. /**
  72. * yes/no/cancel buttons constant.
  73. *
  74. * @static
  75. * @final
  76. * @field {Number} YES_NO_CANCEL
  77. */
  78. YES_NO_CANCEL: 4,
  79. /**
  80. * Constructs a new message box and renders it to the body element.
  81. *
  82. * @static
  83. * @method msgBox
  84. * @param {Object} settings Name/value object with settings.
  85. */
  86. msgBox: function(settings) {
  87. var buttons, callback = settings.callback || function() {};
  88. switch (settings.buttons) {
  89. case MessageBox.OK_CANCEL:
  90. buttons = [
  91. {type: "button", text: "Ok", subtype: "primary", onClick: function(e) {
  92. e.control.parents()[1].close();
  93. callback(true);
  94. }},
  95. {type: "button", text: "Cancel", onClick: function(e) {
  96. e.control.parents()[1].close();
  97. callback(false);
  98. }}
  99. ];
  100. break;
  101. case MessageBox.YES_NO:
  102. buttons = [
  103. {type: "button", text: "Ok", subtype: "primary", onClick: function(e) {
  104. e.control.parents()[1].close();
  105. callback(true);
  106. }}
  107. ];
  108. break;
  109. case MessageBox.YES_NO_CANCEL:
  110. buttons = [
  111. {type: "button", text: "Ok", subtype: "primary", onClick: function(e) {
  112. e.control.parents()[1].close();
  113. }}
  114. ];
  115. break;
  116. default:
  117. buttons = [
  118. {type: "button", text: "Ok", subtype: "primary", onClick: function(e) {
  119. e.control.parents()[1].close();
  120. callback(true);
  121. }}
  122. ];
  123. break;
  124. }
  125. return new Window({
  126. padding: 20,
  127. x: settings.x,
  128. y: settings.y,
  129. minWidth: 300,
  130. minHeight: 100,
  131. layout: "flex",
  132. pack: "center",
  133. align: "center",
  134. buttons: buttons,
  135. title: settings.title,
  136. role: 'alertdialog',
  137. items: {
  138. type: "label",
  139. multiline: true,
  140. maxWidth: 500,
  141. maxHeight: 200,
  142. text: settings.text
  143. },
  144. onPostRender: function() {
  145. this.aria('describedby', this.items()[0]._id);
  146. },
  147. onClose: settings.onClose,
  148. onCancel: function() {
  149. callback(false);
  150. }
  151. }).renderTo(document.body).reflow();
  152. },
  153. /**
  154. * Creates a new alert dialog.
  155. *
  156. * @method alert
  157. * @param {Object} settings Settings for the alert dialog.
  158. * @param {function} [callback] Callback to execute when the user makes a choice.
  159. */
  160. alert: function(settings, callback) {
  161. if (typeof(settings) == "string") {
  162. settings = {text: settings};
  163. }
  164. settings.callback = callback;
  165. return MessageBox.msgBox(settings);
  166. },
  167. /**
  168. * Creates a new confirm dialog.
  169. *
  170. * @method confirm
  171. * @param {Object} settings Settings for the confirm dialog.
  172. * @param {function} [callback] Callback to execute when the user makes a choice.
  173. */
  174. confirm: function(settings, callback) {
  175. if (typeof(settings) == "string") {
  176. settings = {text: settings};
  177. }
  178. settings.callback = callback;
  179. settings.buttons = MessageBox.OK_CANCEL;
  180. return MessageBox.msgBox(settings);
  181. }
  182. }
  183. });
  184. return MessageBox;
  185. });