overlay_page.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2013 Matthew Christopher Kastor-Inare III, Atropa Inc. Intl
  5. * All rights reserved.
  6. *
  7. * Contributed to Ajax.org under the BSD license.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * * Neither the name of Ajax.org B.V. nor the
  17. * names of its contributors may be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  22. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  24. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  25. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  27. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. * ***** END LICENSE BLOCK ***** */
  32. /*jslint indent: 4, maxerr: 50, white: true, browser: true, vars: true*/
  33. /*global define, require */
  34. /**
  35. * Overlay Page
  36. * @fileOverview Overlay Page <br />
  37. * Generates an overlay for displaying menus. The overlay is an absolutely
  38. * positioned div.
  39. * @author <a href="mailto:matthewkastor@gmail.com">
  40. * Matthew Christopher Kastor-Inare III </a><br />
  41. * ☭ Hial Atropa!! ☭
  42. */
  43. define(function(require, exports, module) {
  44. 'use strict';
  45. var dom = require("../../lib/dom");
  46. var cssText = require("../../requirejs/text!./settings_menu.css");
  47. dom.importCssString(cssText);
  48. /**
  49. * Generates an overlay for displaying menus. The overlay is an absolutely
  50. * positioned div.
  51. * @author <a href="mailto:matthewkastor@gmail.com">
  52. * Matthew Christopher Kastor-Inare III </a><br />
  53. * ☭ Hial Atropa!! ☭
  54. * @param {DOMElement} contentElement Any element which may be presented inside
  55. * a div.
  56. * @param {string|number} top absolute position value.
  57. * @param {string|number} right absolute position value.
  58. * @param {string|number} bottom absolute position value.
  59. * @param {string|number} left absolute position value.
  60. */
  61. module.exports.overlayPage = function overlayPage(editor, contentElement, top, right, bottom, left) {
  62. top = top ? 'top: ' + top + ';' : '';
  63. bottom = bottom ? 'bottom: ' + bottom + ';' : '';
  64. right = right ? 'right: ' + right + ';' : '';
  65. left = left ? 'left: ' + left + ';' : '';
  66. var closer = document.createElement('div');
  67. var contentContainer = document.createElement('div');
  68. function documentEscListener(e) {
  69. if (e.keyCode === 27) {
  70. closer.click();
  71. }
  72. }
  73. closer.style.cssText = 'margin: 0; padding: 0; ' +
  74. 'position: fixed; top:0; bottom:0; left:0; right:0;' +
  75. 'z-index: 9990; ' +
  76. 'background-color: rgba(0, 0, 0, 0.3);';
  77. closer.addEventListener('click', function() {
  78. document.removeEventListener('keydown', documentEscListener);
  79. closer.parentNode.removeChild(closer);
  80. editor.focus();
  81. closer = null;
  82. });
  83. // click closer if esc key is pressed
  84. document.addEventListener('keydown', documentEscListener);
  85. contentContainer.style.cssText = top + right + bottom + left;
  86. contentContainer.addEventListener('click', function(e) {
  87. e.stopPropagation();
  88. });
  89. var wrapper = dom.createElement("div");
  90. wrapper.style.position = "relative";
  91. var closeButton = dom.createElement("div");
  92. closeButton.className = "ace_closeButton";
  93. closeButton.addEventListener('click', function() {
  94. closer.click();
  95. });
  96. wrapper.appendChild(closeButton);
  97. contentContainer.appendChild(wrapper);
  98. contentContainer.appendChild(contentElement);
  99. closer.appendChild(contentContainer);
  100. document.body.appendChild(closer);
  101. editor.blur();
  102. };
  103. });