util.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. define(function(require, exports, module) {
  31. "use strict";
  32. var dom = require("ace/lib/dom");
  33. var event = require("ace/lib/event");
  34. var EditSession = require("ace/edit_session").EditSession;
  35. var UndoManager = require("ace/undomanager").UndoManager;
  36. var Renderer = require("ace/virtual_renderer").VirtualRenderer;
  37. var Editor = require("ace/editor").Editor;
  38. var MultiSelect = require("ace/multi_select").MultiSelect;
  39. exports.createEditor = function(el) {
  40. return new Editor(new Renderer(el));
  41. };
  42. exports.createSplitEditor = function(el) {
  43. if (typeof(el) == "string")
  44. el = document.getElementById(el);
  45. var e0 = document.createElement("div");
  46. var s = document.createElement("splitter");
  47. var e1 = document.createElement("div");
  48. el.appendChild(e0);
  49. el.appendChild(e1);
  50. el.appendChild(s);
  51. e0.style.position = e1.style.position = s.style.position = "absolute";
  52. el.style.position = "relative";
  53. var split = {$container: el};
  54. split.editor0 = split[0] = new Editor(new Renderer(e0));
  55. split.editor1 = split[1] = new Editor(new Renderer(e1));
  56. split.splitter = s;
  57. s.ratio = 0.5;
  58. split.resize = function resize(){
  59. var height = el.parentNode.clientHeight - el.offsetTop;
  60. var total = el.clientWidth;
  61. var w1 = total * s.ratio;
  62. var w2 = total * (1- s.ratio);
  63. s.style.left = w1 - 1 + "px";
  64. s.style.height = el.style.height = height + "px";
  65. var st0 = split[0].container.style;
  66. var st1 = split[1].container.style;
  67. st0.width = w1 + "px";
  68. st1.width = w2 + "px";
  69. st0.left = 0 + "px";
  70. st1.left = w1 + "px";
  71. st0.top = st1.top = "0px";
  72. st0.height = st1.height = height + "px";
  73. split[0].resize();
  74. split[1].resize();
  75. };
  76. split.onMouseDown = function(e) {
  77. var rect = el.getBoundingClientRect();
  78. var x = e.clientX;
  79. var y = e.clientY;
  80. var button = e.button;
  81. if (button !== 0) {
  82. return;
  83. }
  84. var onMouseMove = function(e) {
  85. x = e.clientX;
  86. y = e.clientY;
  87. };
  88. var onResizeEnd = function(e) {
  89. clearInterval(timerId);
  90. };
  91. var onResizeInterval = function() {
  92. s.ratio = (x - rect.left) / rect.width;
  93. split.resize();
  94. };
  95. event.capture(s, onMouseMove, onResizeEnd);
  96. var timerId = setInterval(onResizeInterval, 40);
  97. return e.preventDefault();
  98. };
  99. event.addListener(s, "mousedown", split.onMouseDown);
  100. event.addListener(window, "resize", split.resize);
  101. split.resize();
  102. return split;
  103. };
  104. /***************************/
  105. exports.stripLeadingComments = function(str) {
  106. if(str.slice(0,2)=='/*') {
  107. var j = str.indexOf('*/')+2;
  108. str = str.substr(j);
  109. }
  110. return str.trim() + "\n";
  111. };
  112. /***************************/
  113. exports.saveOption = function(el, val) {
  114. if (!el.onchange && !el.onclick)
  115. return;
  116. if ("checked" in el) {
  117. if (val !== undefined)
  118. el.checked = val;
  119. localStorage && localStorage.setItem(el.id, el.checked ? 1 : 0);
  120. }
  121. else {
  122. if (val !== undefined)
  123. el.value = val;
  124. localStorage && localStorage.setItem(el.id, el.value);
  125. }
  126. };
  127. exports.bindCheckbox = function(id, callback, noInit) {
  128. if (typeof id == "string")
  129. var el = document.getElementById(id);
  130. else {
  131. var el = id;
  132. id = el.id;
  133. }
  134. var el = document.getElementById(id);
  135. if (localStorage && localStorage.getItem(id))
  136. el.checked = localStorage.getItem(id) == "1";
  137. var onCheck = function() {
  138. callback(!!el.checked);
  139. exports.saveOption(el);
  140. };
  141. el.onclick = onCheck;
  142. noInit || onCheck();
  143. return el;
  144. };
  145. exports.bindDropdown = function(id, callback, noInit) {
  146. if (typeof id == "string")
  147. var el = document.getElementById(id);
  148. else {
  149. var el = id;
  150. id = el.id;
  151. }
  152. if (localStorage && localStorage.getItem(id))
  153. el.value = localStorage.getItem(id);
  154. var onChange = function() {
  155. callback(el.value);
  156. exports.saveOption(el);
  157. };
  158. el.onchange = onChange;
  159. noInit || onChange();
  160. };
  161. exports.fillDropdown = function(el, values) {
  162. if (typeof el == "string")
  163. el = document.getElementById(el);
  164. dropdown(values).forEach(function(e) {
  165. el.appendChild(e);
  166. });
  167. };
  168. function elt(tag, attributes, content) {
  169. var el = dom.createElement(tag);
  170. if (typeof content == "string") {
  171. el.appendChild(document.createTextNode(content));
  172. } else if (content) {
  173. content.forEach(function(ch) {
  174. el.appendChild(ch);
  175. });
  176. }
  177. for (var i in attributes)
  178. el.setAttribute(i, attributes[i]);
  179. return el;
  180. }
  181. function optgroup(values) {
  182. return values.map(function(item) {
  183. if (typeof item == "string")
  184. item = {name: item, caption: item};
  185. return elt("option", {value: item.value || item.name}, item.caption || item.desc);
  186. });
  187. }
  188. function dropdown(values) {
  189. if (Array.isArray(values))
  190. return optgroup(values);
  191. return Object.keys(values).map(function(i) {
  192. return elt("optgroup", {"label": i}, optgroup(values[i]));
  193. });
  194. }
  195. });