config.js 6.6 KB

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