custom-dialog.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * Blockly Demos: Custom Dialogs
  3. *
  4. * Copyright 2016 Google Inc.
  5. * https://developers.google.com/blockly/
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. /**
  20. * An example implementation of how one might replace Blockly's browser
  21. * dialogs. This is just an example, and applications are not encouraged to use
  22. * it verbatim.
  23. *
  24. * @namespace
  25. */
  26. CustomDialog = {};
  27. /** Override Blockly.alert() with custom implementation. */
  28. Blockly.alert = function(message, callback) {
  29. console.log('Alert: ' + message);
  30. CustomDialog.show('Alert', message, {
  31. onCancel: callback
  32. });
  33. };
  34. /** Override Blockly.confirm() with custom implementation. */
  35. Blockly.confirm = function(message, callback) {
  36. console.log('Confirm: ' + message);
  37. CustomDialog.show('Confirm', message, {
  38. showOkay: true,
  39. onOkay: function() {
  40. callback(true)
  41. },
  42. showCancel: true,
  43. onCancel: function() {
  44. callback(false)
  45. }
  46. });
  47. };
  48. /** Override Blockly.prompt() with custom implementation. */
  49. Blockly.prompt = function(message, defaultValue, callback) {
  50. console.log('Prompt: ' + message);
  51. CustomDialog.show('Prompt', message, {
  52. showInput: true,
  53. showOkay: true,
  54. onOkay: function() {
  55. callback(CustomDialog.inputField.value)
  56. },
  57. showCancel: true,
  58. onCancel: function() {
  59. callback(null)
  60. }
  61. });
  62. CustomDialog.inputField.value = defaultValue;
  63. };
  64. /** Hides any currently visible dialog. */
  65. CustomDialog.hide = function() {
  66. if (CustomDialog.backdropDiv_) {
  67. CustomDialog.backdropDiv_.style.display = 'none'
  68. CustomDialog.dialogDiv_.style.display = 'none'
  69. }
  70. };
  71. /**
  72. * Shows the dialog.
  73. * Allowed options:
  74. * - showOkay: Whether to show the OK button.
  75. * - showCancel: Whether to show the Cancel button.
  76. * - showInput: Whether to show the text input field.
  77. * - onOkay: Callback to handle the okay button.
  78. * - onCancel: Callback to handle the cancel button and backdrop clicks.
  79. */
  80. CustomDialog.show = function(title, message, options) {
  81. var backdropDiv = CustomDialog.backdropDiv_;
  82. var dialogDiv = CustomDialog.dialogDiv_;
  83. if (!dialogDiv) {
  84. // Generate HTML
  85. backdropDiv = document.createElement('div');
  86. backdropDiv.id = 'customDialogBackdrop';
  87. backdropDiv.style.cssText =
  88. 'position: absolute;' +
  89. 'top: 0; left: 0; right: 0; bottom: 0;' +
  90. 'background-color: rgba(0, 0, 0, .7);';
  91. document.body.appendChild(backdropDiv);
  92. dialogDiv = document.createElement('div');
  93. dialogDiv.id = 'customDialog';
  94. dialogDiv.style.cssText =
  95. 'background-color: #fff;' +
  96. 'width: 400px;' +
  97. 'margin: 20px auto 0;' +
  98. 'padding: 10px;';
  99. backdropDiv.appendChild(dialogDiv);
  100. dialogDiv.onclick = function(event) {
  101. event.stopPropagation();
  102. };
  103. CustomDialog.backdropDiv_ = backdropDiv;
  104. CustomDialog.dialogDiv_ = dialogDiv;
  105. }
  106. backdropDiv.style.display = 'block';
  107. dialogDiv.style.display = 'block';
  108. dialogDiv.innerHTML =
  109. '<header class="customDialogTitle"></header>' +
  110. '<p class="customDialogMessage"></p>' +
  111. (options.showInput ? '<div><input id="customDialogInput"></div>' : '') +
  112. '<div class="customDialogButtons">' +
  113. (options.showCancel ? '<button id="customDialogCancel">Cancel</button>': '') +
  114. (options.showOkay ? '<button id="customDialogOkay">OK</button>': '') +
  115. '</div>';
  116. dialogDiv.getElementsByClassName('customDialogTitle')[0]
  117. .appendChild(document.createTextNode(title));
  118. dialogDiv.getElementsByClassName('customDialogMessage')[0]
  119. .appendChild(document.createTextNode(message));
  120. var onOkay = function(event) {
  121. CustomDialog.hide();
  122. options.onOkay && options.onOkay();
  123. event && event.stopPropagation();
  124. };
  125. var onCancel = function(event) {
  126. CustomDialog.hide();
  127. options.onCancel && options.onCancel();
  128. event && event.stopPropagation();
  129. };
  130. var dialogInput = document.getElementById('customDialogInput');
  131. CustomDialog.inputField = dialogInput;
  132. if (dialogInput) {
  133. dialogInput.focus();
  134. dialogInput.onkeyup = function(event) {
  135. if (event.keyCode == 13) {
  136. // Process as OK when user hits enter.
  137. onOkay();
  138. return false;
  139. } else if (event.keyCode == 27) {
  140. // Process as cancel when user hits esc.
  141. onCancel();
  142. return false;
  143. }
  144. };
  145. } else {
  146. var okay = document.getElementById('customDialogOkay');
  147. okay && okay.focus();
  148. }
  149. if (options.showOkay) {
  150. document.getElementById('customDialogOkay')
  151. .addEventListener('click', onOkay);
  152. }
  153. if (options.showCancel) {
  154. document.getElementById('customDialogCancel')
  155. .addEventListener('click', onCancel);
  156. }
  157. backdropDiv.onclick = onCancel;
  158. };