config.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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(ace_require, exports, module) {
  31. "no use strict";
  32. var lang = ace_require("./lib/lang");
  33. var oop = ace_require("./lib/oop");
  34. var net = ace_require("./lib/net");
  35. var AppConfig = ace_require("./lib/app_config").AppConfig;
  36. module.exports = exports = new AppConfig();
  37. var global = (function() {
  38. return this || typeof window != "undefined" && window;
  39. })();
  40. var options = {
  41. packaged: false,
  42. workerPath: null,
  43. modePath: null,
  44. themePath: null,
  45. basePath: "",
  46. suffix: ".js",
  47. $moduleUrls: {},
  48. loadWorkerFromBlob: true,
  49. sharedPopups: false
  50. };
  51. exports.get = function(key) {
  52. if (!options.hasOwnProperty(key))
  53. throw new Error("Unknown config key: " + key);
  54. return options[key];
  55. };
  56. exports.set = function(key, value) {
  57. if (options.hasOwnProperty(key))
  58. options[key] = value;
  59. else if (this.setDefaultValue("", key, value) == false)
  60. throw new Error("Unknown config key: " + key);
  61. };
  62. exports.all = function() {
  63. return lang.copyObject(options);
  64. };
  65. exports.$ace_modes = {};
  66. // module loading
  67. exports.moduleUrl = function(name, component) {
  68. if (options.$moduleUrls[name])
  69. return options.$moduleUrls[name];
  70. var parts = name.split("/");
  71. component = component || parts[parts.length - 2] || "";
  72. // todo make this configurable or get rid of '-'
  73. var sep = component == "snippets" ? "/" : "-";
  74. var base = parts[parts.length - 1];
  75. if (component == "worker" && sep == "-") {
  76. var re = new RegExp("^" + component + "[\\-_]|[\\-_]" + component + "$", "g");
  77. base = base.replace(re, "");
  78. }
  79. if ((!base || base == component) && parts.length > 1)
  80. base = parts[parts.length - 2];
  81. var path = options[component + "Path"];
  82. if (path == null) {
  83. path = options.basePath;
  84. } else if (sep == "/") {
  85. component = sep = "";
  86. }
  87. if (path && path.slice(-1) != "/")
  88. path += "/";
  89. return path + component + sep + base + this.get("suffix");
  90. };
  91. exports.setModuleUrl = function(name, subst) {
  92. return options.$moduleUrls[name] = subst;
  93. };
  94. exports.$loading = {};
  95. exports.loadModule = function(moduleName, onLoad) {
  96. var module, moduleType;
  97. if (Array.isArray(moduleName)) {
  98. moduleType = moduleName[0];
  99. moduleName = moduleName[1];
  100. }
  101. try {
  102. module = ace_require(moduleName);
  103. } catch (e) {}
  104. // ace_require(moduleName) can return empty object if called after ace_require([moduleName], callback)
  105. if (module && !exports.$loading[moduleName])
  106. return onLoad && onLoad(module);
  107. if (!exports.$loading[moduleName])
  108. exports.$loading[moduleName] = [];
  109. exports.$loading[moduleName].push(onLoad);
  110. if (exports.$loading[moduleName].length > 1)
  111. return;
  112. var afterLoad = function() {
  113. ace_require([moduleName], function(module) {
  114. exports._emit("load.module", {name: moduleName, module: module});
  115. var listeners = exports.$loading[moduleName];
  116. exports.$loading[moduleName] = null;
  117. listeners.forEach(function(onLoad) {
  118. onLoad && onLoad(module);
  119. });
  120. });
  121. };
  122. if (!exports.get("packaged"))
  123. return afterLoad();
  124. net.loadScript(exports.moduleUrl(moduleName, moduleType), afterLoad);
  125. reportErrorIfPathIsNotConfigured();
  126. };
  127. var reportErrorIfPathIsNotConfigured = function() {
  128. if (
  129. !options.basePath && !options.workerPath
  130. && !options.modePath && !options.themePath
  131. && !Object.keys(options.$moduleUrls).length
  132. ) {
  133. console.error(
  134. "Unable to infer path to ace from script src,",
  135. "use ace.config.set('basePath', 'path') to enable dynamic loading of modes and themes",
  136. "or with webpack use ace/webpack-resolver"
  137. );
  138. reportErrorIfPathIsNotConfigured = function() {};
  139. }
  140. };
  141. // initialization
  142. function init(packaged) {
  143. if (!global || !global.document)
  144. return;
  145. options.packaged = packaged || ace_require.packaged || module.packaged || (global.define && define.packaged);
  146. var scriptOptions = {};
  147. var scriptUrl = "";
  148. // Use currentScript.ownerDocument in case this file was loaded from imported document. (HTML Imports)
  149. var currentScript = (document.currentScript || document._currentScript ); // native or polyfill
  150. var currentDocument = currentScript && currentScript.ownerDocument || document;
  151. var scripts = currentDocument.getElementsByTagName("script");
  152. for (var i=0; i<scripts.length; i++) {
  153. var script = scripts[i];
  154. var src = script.src || script.getAttribute("src");
  155. if (!src)
  156. continue;
  157. var attributes = script.attributes;
  158. for (var j=0, l=attributes.length; j < l; j++) {
  159. var attr = attributes[j];
  160. if (attr.name.indexOf("data-ace-") === 0) {
  161. scriptOptions[deHyphenate(attr.name.replace(/^data-ace-/, ""))] = attr.value;
  162. }
  163. }
  164. var m = src.match(/^(.*)\/ace(\-\w+)?\.js(\?|$)/);
  165. if (m)
  166. scriptUrl = m[1];
  167. }
  168. if (scriptUrl) {
  169. scriptOptions.base = scriptOptions.base || scriptUrl;
  170. scriptOptions.packaged = true;
  171. }
  172. scriptOptions.basePath = scriptOptions.base;
  173. scriptOptions.workerPath = scriptOptions.workerPath || scriptOptions.base;
  174. scriptOptions.modePath = scriptOptions.modePath || scriptOptions.base;
  175. scriptOptions.themePath = scriptOptions.themePath || scriptOptions.base;
  176. delete scriptOptions.base;
  177. for (var key in scriptOptions)
  178. if (typeof scriptOptions[key] !== "undefined")
  179. exports.set(key, scriptOptions[key]);
  180. }
  181. exports.init = init;
  182. function deHyphenate(str) {
  183. return str.replace(/-(.)/g, function(m, m1) { return m1.toUpperCase(); });
  184. }
  185. exports.version = "1.4.10";
  186. });