config.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 EventEmitter = require("./lib/event_emitter").EventEmitter;
  36. var global = (function() {
  37. return this;
  38. })();
  39. var options = {
  40. packaged: false,
  41. workerPath: null,
  42. modePath: null,
  43. themePath: null,
  44. basePath: "",
  45. suffix: ".js",
  46. $moduleUrls: {}
  47. };
  48. exports.get = function(key) {
  49. if (!options.hasOwnProperty(key))
  50. throw new Error("Unknown config key: " + key);
  51. return options[key];
  52. };
  53. exports.set = function(key, value) {
  54. if (!options.hasOwnProperty(key))
  55. throw new Error("Unknown config key: " + key);
  56. options[key] = value;
  57. };
  58. exports.all = function() {
  59. return lang.copyObject(options);
  60. };
  61. // module loading
  62. oop.implement(exports, EventEmitter);
  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 (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. exports.init = function() {
  124. options.packaged = require.packaged || module.packaged || (global.define && define.packaged);
  125. if (!global.document)
  126. return "";
  127. var scriptOptions = {};
  128. var scriptUrl = "";
  129. var scripts = document.getElementsByTagName("script");
  130. for (var i=0; i<scripts.length; i++) {
  131. var script = scripts[i];
  132. var src = script.src || script.getAttribute("src");
  133. if (!src)
  134. continue;
  135. var attributes = script.attributes;
  136. for (var j=0, l=attributes.length; j < l; j++) {
  137. var attr = attributes[j];
  138. if (attr.name.indexOf("data-ace-") === 0) {
  139. scriptOptions[deHyphenate(attr.name.replace(/^data-ace-/, ""))] = attr.value;
  140. }
  141. }
  142. var m = src.match(/^(.*)\/ace(\-\w+)?\.js(\?|$)/);
  143. if (m)
  144. scriptUrl = m[1];
  145. }
  146. if (scriptUrl) {
  147. scriptOptions.base = scriptOptions.base || scriptUrl;
  148. scriptOptions.packaged = true;
  149. }
  150. scriptOptions.basePath = scriptOptions.base;
  151. scriptOptions.workerPath = scriptOptions.workerPath || scriptOptions.base;
  152. scriptOptions.modePath = scriptOptions.modePath || scriptOptions.base;
  153. scriptOptions.themePath = scriptOptions.themePath || scriptOptions.base;
  154. delete scriptOptions.base;
  155. for (var key in scriptOptions)
  156. if (typeof scriptOptions[key] !== "undefined")
  157. exports.set(key, scriptOptions[key]);
  158. };
  159. function deHyphenate(str) {
  160. return str.replace(/-(.)/g, function(m, m1) { return m1.toUpperCase(); });
  161. }
  162. var optionsProvider = {
  163. setOptions: function(optList) {
  164. Object.keys(optList).forEach(function(key) {
  165. this.setOption(key, optList[key]);
  166. }, this);
  167. },
  168. getOptions: function(optionNames) {
  169. var result = {};
  170. if (!optionNames) {
  171. optionNames = Object.keys(this.$options);
  172. } else if (!Array.isArray(optionNames)) {
  173. result = optionNames;
  174. optionNames = Object.keys(result);
  175. }
  176. optionNames.forEach(function(key) {
  177. result[key] = this.getOption(key);
  178. }, this);
  179. return result;
  180. },
  181. setOption: function(name, value) {
  182. if (this["$" + name] === value)
  183. return;
  184. var opt = this.$options[name];
  185. if (!opt) {
  186. if (typeof console != "undefined" && console.warn)
  187. console.warn('misspelled option "' + name + '"');
  188. return undefined;
  189. }
  190. if (opt.forwardTo)
  191. return this[opt.forwardTo] && this[opt.forwardTo].setOption(name, value);
  192. if (!opt.handlesSet)
  193. this["$" + name] = value;
  194. if (opt && opt.set)
  195. opt.set.call(this, value);
  196. },
  197. getOption: function(name) {
  198. var opt = this.$options[name];
  199. if (!opt) {
  200. if (typeof console != "undefined" && console.warn)
  201. console.warn('misspelled option "' + name + '"');
  202. return undefined;
  203. }
  204. if (opt.forwardTo)
  205. return this[opt.forwardTo] && this[opt.forwardTo].getOption(name);
  206. return opt && opt.get ? opt.get.call(this) : this["$" + name];
  207. }
  208. };
  209. var defaultOptions = {};
  210. /*
  211. * option {name, value, initialValue, setterName, set, get }
  212. */
  213. exports.defineOptions = function(obj, path, options) {
  214. if (!obj.$options)
  215. defaultOptions[path] = obj.$options = {};
  216. Object.keys(options).forEach(function(key) {
  217. var opt = options[key];
  218. if (typeof opt == "string")
  219. opt = {forwardTo: opt};
  220. opt.name || (opt.name = key);
  221. obj.$options[opt.name] = opt;
  222. if ("initialValue" in opt)
  223. obj["$" + opt.name] = opt.initialValue;
  224. });
  225. // implement option provider interface
  226. oop.implement(obj, optionsProvider);
  227. return this;
  228. };
  229. exports.resetOptions = function(obj) {
  230. Object.keys(obj.$options).forEach(function(key) {
  231. var opt = obj.$options[key];
  232. if ("value" in opt)
  233. obj.setOption(key, opt.value);
  234. });
  235. };
  236. exports.setDefaultValue = function(path, name, value) {
  237. var opts = defaultOptions[path] || (defaultOptions[path] = {});
  238. if (opts[name]) {
  239. if (opts.forwardTo)
  240. exports.setDefaultValue(opts.forwardTo, name, value);
  241. else
  242. opts[name].value = value;
  243. }
  244. };
  245. exports.setDefaultValues = function(path, optionHash) {
  246. Object.keys(optionHash).forEach(function(key) {
  247. exports.setDefaultValue(path, key, optionHash[key]);
  248. });
  249. };
  250. });