bootstrap-browser.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491
  1. (function (scope) {
  2. var ephox = scope.ephox = scope.ephox || {};
  3. var bolt = ephox.bolt = ephox.bolt || {};
  4. var def = function (deps, factory) {
  5. return factory.apply(null, deps);
  6. };
  7. var kernel = bolt.kernel = bolt.kernel || {};
  8. kernel.api = kernel.api || {};
  9. kernel.async = kernel.api || {};
  10. kernel.fp = kernel.fp || {};
  11. kernel.modulator = kernel.modulator || {};
  12. kernel.module = kernel.module || {};
  13. kernel.fp.array = def(
  14. [
  15. ],
  16. function () {
  17. var equals = function (a1, a2) {
  18. if (a1.length !== a2.length)
  19. return false;
  20. for (var i = 0; i < a1.length; ++i)
  21. if (a1[i] !== a2[i])
  22. return false;
  23. return true;
  24. };
  25. var forall = function (a, f) {
  26. var fn = f || function (x) {
  27. return x === true;
  28. };
  29. for (var i = 0; i < a.length; ++i)
  30. if (fn(a[i]) !== true)
  31. return false;
  32. return true;
  33. };
  34. var map = function (a, f) {
  35. var r = [];
  36. for (var i = 0; i < a.length; ++i)
  37. r.push(f(a[i], i));
  38. return r;
  39. };
  40. var flatten = function (a) {
  41. var r = [];
  42. for (var i = 0; i < a.length; ++i)
  43. r = r.concat(a[i]);
  44. return r;
  45. };
  46. var flatmap = function (a, f) {
  47. return flatten(map(a, f));
  48. };
  49. var filter = function (a, f) {
  50. var r = [];
  51. for (var i = 0; i < a.length; ++i)
  52. if (f(a[i]))
  53. r.push(a[i]);
  54. return r;
  55. };
  56. var each = map;
  57. var contains = function (a, x) {
  58. return !forall(a, function (v) {
  59. return v !== x;
  60. });
  61. };
  62. var indexof = function (a, x) {
  63. for (var i = 0; i < a.length; ++i)
  64. if (a[i] === x)
  65. return i;
  66. return -1;
  67. };
  68. return {
  69. equals: equals,
  70. forall: forall,
  71. map: map,
  72. flatten: flatten,
  73. flatmap: flatmap,
  74. filter: filter,
  75. each: each,
  76. contains: contains,
  77. indexof: indexof
  78. };
  79. }
  80. );
  81. kernel.fp.object = def(
  82. [
  83. ],
  84. function () {
  85. var map = function (o, f) {
  86. var r = {};
  87. for (var i in o)
  88. if (o.hasOwnProperty(i))
  89. r[i] = f(i, o[i]);
  90. return r;
  91. };
  92. var each = map;
  93. var merge = function (d, s) {
  94. each(s, function (k, v) {
  95. d[k] = v;
  96. });
  97. };
  98. var keys = function (o) {
  99. var r = [];
  100. each(o, function (k) {
  101. r.push(k);
  102. });
  103. return r;
  104. };
  105. return {
  106. each: each,
  107. keys: keys,
  108. map: map,
  109. merge: merge
  110. };
  111. }
  112. );
  113. kernel.fp.functions = def(
  114. [
  115. ],
  116. function () {
  117. var curry = function (f) {
  118. var slice = Array.prototype.slice;
  119. var args = slice.call(arguments, 1);
  120. return function () {
  121. var all = args.concat(slice.call(arguments, 0));
  122. return f.apply(null, all);
  123. };
  124. };
  125. var not = function (z) {
  126. return function () {
  127. var slice = Array.prototype.slice;
  128. return !z.apply(null, slice.call(arguments, 0));
  129. };
  130. };
  131. var apply = function (f) {
  132. var slice = Array.prototype.slice;
  133. return f.apply(null, slice.call(arguments, 0));
  134. };
  135. return {
  136. curry: curry,
  137. not: not,
  138. apply: apply
  139. };
  140. }
  141. );kernel.async.map = def(
  142. [
  143. kernel.fp.array
  144. ],
  145. function (ar) {
  146. var amap = function (data, f, oncomplete) {
  147. var total = data.length;
  148. var count = 0;
  149. var results = [];
  150. ar.each(data, function (datum, i) {
  151. f(datum, function (result) {
  152. ++count;
  153. results[i] = result;
  154. if (count === total)
  155. oncomplete(results);
  156. });
  157. });
  158. };
  159. return {
  160. amap: amap
  161. };
  162. }
  163. );
  164. /**
  165. * This module has a dual responsibility:
  166. * 1. Ensures that asynchronous function calls, 'f', that share the same
  167. * 'key' are not executed in parallel.
  168. * 2. In the case where an attempt to call in parallel is prevented,
  169. * the 'action' callbacks are executed when the asynchronous call is
  170. * completed.
  171. *
  172. * Example:
  173. * When we async-map to remotely fetch module definition, it is
  174. * important that only a single define is evaluated, but the
  175. * notification that the definition has completed is propagated
  176. * to all interested parties.
  177. *
  178. * 1. we require dependencies 'x' and 'y'
  179. *
  180. * 2. both x and y are defined in the same file (i.e. compiled together), 'a.js'.
  181. *
  182. * 3. we resolve x and y, to their load spec using a modulator
  183. * x_spec = {load: function () { -- load a.js -- }, url: a.js, serial: false};
  184. * y_spec = {load: function () { -- load a.js -- }, url: a.js, serial: false};
  185. *
  186. * 4. we make the piggyback call for x:
  187. * piggybacker.piggyback(x_spec.url, x_spec.load, xdone);
  188. *
  189. * this will register the 'xdone' action, and actually
  190. * trigger the load call, with a synthetic callback
  191. * responsible for triggering all registered actions.
  192. *
  193. * 5. we make the piggyback call for y:
  194. * piggybacker.piggyback(y_spec.url, y_spec.load, ydone);
  195. *
  196. * this will register the 'ydone' action, but NOT trigger
  197. * the load call.
  198. *
  199. * 6. the load call completes, and calls the synthetic callback,
  200. * which is responsible for triggering both 'xdone' and 'ydone'.
  201. *
  202. * 7. something else happens that means we have to load 'a.js' again,
  203. * the piggybacker DOES NOT prevent this call, and will follow
  204. * the above process.
  205. */
  206. kernel.async.piggybacker = def(
  207. [
  208. kernel.fp.array,
  209. kernel.fp.functions
  210. ],
  211. function (ar, fn) {
  212. var create = function () {
  213. var queue = {}; // key -> [actions]
  214. var process = function (key) {
  215. var actions = queue[key];
  216. delete queue[key];
  217. ar.each(actions, fn.apply);
  218. };
  219. var piggyback = function (key, f, action) {
  220. if (queue[key] === undefined) {
  221. queue[key] = [ action ];
  222. f(fn.curry(process, key));
  223. } else {
  224. queue[key].push(action);
  225. }
  226. };
  227. return {
  228. piggyback: piggyback
  229. };
  230. };
  231. return {
  232. create: create
  233. };
  234. }
  235. );
  236. kernel.modulator.globalator = def(
  237. [
  238. ],
  239. function () {
  240. var create = function () {
  241. // FIX pull out
  242. var resolve = function (name, scope) {
  243. var parts = name.split('.');
  244. var r = scope;
  245. for (var i = 0; i < parts.length && r !== undefined; ++i)
  246. r = r[parts[i]];
  247. return r;
  248. };
  249. var global = Function('return this')();
  250. var can = function (id) {
  251. return id.indexOf('global!') === 0;
  252. };
  253. var get = function (id, define, require) {
  254. var name = id.substring('global!'.length);
  255. var load = function (onsuccess, onfailure) {
  256. var instance = resolve(name, global);
  257. if (instance !== undefined) {
  258. define(id, [], function () { return instance; });
  259. onsuccess();
  260. } else {
  261. onfailure('Modulator error: could not resolve global [' + name + ']');
  262. }
  263. };
  264. return {
  265. url: id, // this just needs to be unique, no download required.
  266. load: load,
  267. serial: true
  268. };
  269. };
  270. return {
  271. can: can,
  272. get: get
  273. }
  274. };
  275. return {
  276. create: create
  277. };
  278. }
  279. );
  280. kernel.modulator.bolt = def(
  281. [
  282. kernel.fp.functions
  283. ],
  284. function (fn) {
  285. var create = function (loader, pather, namespace, path, idTransformer, options) {
  286. var can = function (id) {
  287. return id === namespace || id.indexOf(namespace + '.') === 0 || id.indexOf(namespace + '/') === 0;
  288. };
  289. var get = function (id) {
  290. var before = options !== undefined && options.absolute === true ? path : pather(path);
  291. var after = options !== undefined && options.fresh === true ? '?cachebuster=' + new Date().getTime() : '';
  292. var url = before + "/" + idTransformer(id) + '.js' + after;
  293. var load = fn.curry(loader.load, url);
  294. return {
  295. url: url,
  296. load: load,
  297. serial: false
  298. };
  299. };
  300. return {
  301. can: can,
  302. get: get
  303. };
  304. };
  305. return {
  306. create: create
  307. };
  308. }
  309. );kernel.module.stratifier = def(
  310. [
  311. kernel.fp.array
  312. ],
  313. function (ar) {
  314. var stratify = function (specs) {
  315. var parallels = ar.filter(specs, function (spec) {
  316. return !spec.serial;
  317. });
  318. return parallels.length > 0 ? parallels : specs.slice(0, 1);
  319. };
  320. return {
  321. stratify: stratify
  322. };
  323. }
  324. );
  325. /**
  326. * This module performs dependency analysis of strings that depend on sets of
  327. * strings.
  328. *
  329. * The input is an array of root strings to start analysis from, and an object
  330. * that contains a mapping of each string to the strings it depends on.
  331. *
  332. * Performing an analysis results in either:
  333. * 1. an empty array, indicating that all dependencies are satisfied,
  334. * 2. an array of strings that are, at the minimum, still needed in order to
  335. * satisfy the given dependency trees, or
  336. * 3. an array of strings that form a dependency cycle.
  337. */
  338. kernel.module.analyser = def(
  339. [
  340. kernel.fp.array
  341. ],
  342. function (array) {
  343. var collect = function (path, name) {
  344. var i = array.indexof(path, name);
  345. var p = path.slice(i);
  346. return p.concat([name]);
  347. };
  348. /**
  349. * @param {array} roots Contains a list of root ids
  350. * @param {object} modules Contains dependency information in format: { id: [ 'id1', 'id2' ] }
  351. */
  352. var analyse = function (roots, modules) {
  353. var done = {};
  354. var path = [];
  355. var missing = [];
  356. var cycle;
  357. var children = function (name) {
  358. array.each(modules[name], attempt);
  359. };
  360. var examine = function (name) {
  361. if (modules[name])
  362. children(name);
  363. else
  364. missing.push(name);
  365. };
  366. var descend = function (name) {
  367. path.push(name);
  368. examine(name);
  369. path.pop();
  370. };
  371. var decycle = function (name) {
  372. if (array.contains(path, name))
  373. cycle = collect(path, name);
  374. else
  375. descend(name);
  376. };
  377. var attempt = function (name) {
  378. if (!done[name]) {
  379. decycle(name);
  380. done[name] = true;
  381. }
  382. };
  383. array.each(roots, attempt);
  384. return cycle ? { cycle: cycle } : { load: missing };
  385. };
  386. return {
  387. analyse: analyse
  388. };
  389. }
  390. );
  391. kernel.module.fetcher = def(
  392. [
  393. kernel.fp.array,
  394. kernel.fp.functions,
  395. kernel.async.map,
  396. kernel.async.piggybacker,
  397. kernel.module.stratifier
  398. ],
  399. function (ar, fn, map, piggybacker, stratifier) {
  400. var create = function (regulator, validator, onerror, define, require, demand) {
  401. var piggyback = piggybacker.create();
  402. var validate = function (onsuccess, results) {
  403. var failed = ar.filter(results, fn.not(validator));
  404. if (failed.length > 0)
  405. onerror('Fetcher error: modules were not defined: ' + failed.join(', '));
  406. else
  407. onsuccess();
  408. };
  409. var mapper = function (spec, onresult) {
  410. var action = fn.curry(onresult, spec.id);
  411. var load = function (callback) {
  412. spec.load(callback, onerror);
  413. };
  414. piggyback.piggyback(spec.url, load, action);
  415. };
  416. var asyncfetch = function (specs, onsuccess) {
  417. var oncomplete = fn.curry(validate, onsuccess);
  418. var strata = stratifier.stratify(specs);
  419. map.amap(strata, mapper, oncomplete);
  420. };
  421. var fetch = function (ids, onsuccess) {
  422. regulator.regulate(ids, define, require, demand, function (specs) {
  423. asyncfetch(specs, onsuccess);
  424. }, onerror);
  425. };
  426. return {
  427. fetch: fetch
  428. };
  429. };
  430. return {
  431. create: create
  432. };
  433. }
  434. );
  435. kernel.module.loader = def(
  436. [
  437. kernel.module.analyser
  438. ],
  439. function (analyser) {
  440. var load = function (roots, deps, fetcher, oncontinue, onsuccess, onerror) {
  441. var result = analyser.analyse(roots, deps);
  442. if (result.cycle)
  443. onerror('Dependency error: a circular module dependency exists from ' + result.cycle.join(' ~> '));
  444. else if (result.load.length === 0)
  445. onsuccess();
  446. else
  447. fetcher.fetch(result.load, oncontinue);
  448. };
  449. return {
  450. load: load
  451. };
  452. }
  453. );
  454. kernel.module.manager = def(
  455. [
  456. kernel.fp.array,
  457. kernel.fp.object,
  458. kernel.module.loader,
  459. kernel.module.fetcher
  460. ],
  461. function (ar, obj, loader, fetcher) {
  462. var create = function (regulator, onerror) {
  463. var blueprints = {}; // id -> { id: string, dependencies: [ string ], definition: function }
  464. var modules = {}; // id -> module
  465. // Adds a module to the system.
  466. var define = function (id, dependencies, definition) {
  467. if (id === undefined)
  468. onerror("Define error: module id can not be undefined");
  469. else if (blueprints[id] !== undefined)
  470. onerror("Define error: module '" + id + "' is already defined");
  471. else
  472. blueprints[id] = { id: id, dependencies: dependencies, definition: definition };
  473. };
  474. // Loads a set of modules asynchronously.
  475. var require = function (ids, callback) {
  476. var onsuccess = function () {
  477. var instances = ar.map(ids, demand);
  478. callback.apply(null, instances);
  479. };
  480. var oncontinue = function () {
  481. var deps = obj.map(blueprints, function (k, v) {
  482. return v.dependencies;
  483. });
  484. loader.load(ids, deps, fetch, oncontinue, onsuccess, onerror);
  485. };
  486. oncontinue();
  487. };
  488. // Instantiates a module and all of its dependencies.
  489. var demand = function (id) {
  490. if (modules[id] !== undefined)
  491. return modules[id];
  492. if (blueprints[id] === undefined)
  493. throw "module '" + id + "' is not defined";
  494. var result = instantiate(id);
  495. if (result === undefined)
  496. throw "module '" + id + "' returned undefined from definition function";
  497. modules[id] = result;
  498. return result;
  499. };
  500. var instantiate = function (id) {
  501. var blueprint = blueprints[id];
  502. var args = ar.map(blueprint.dependencies, demand); // Instantiate dependencies
  503. return blueprint.definition.apply(null, args); // Instantiate self
  504. };
  505. var validator = function (id) { return blueprints[id] !== undefined; };
  506. var fetch = fetcher.create(regulator, validator, onerror, define, require, demand);
  507. return {
  508. define: define,
  509. require: require,
  510. demand: demand
  511. };
  512. };
  513. return {
  514. create: create
  515. };
  516. }
  517. );
  518. kernel.api.sources = def(
  519. [
  520. kernel.fp.array,
  521. kernel.fp.object,
  522. kernel.modulator.globalator
  523. ],
  524. function (ar, obj, globalator) {
  525. var create = function (builtins, configuration) {
  526. var data = {
  527. 'global': { instance: globalator }
  528. };
  529. obj.each(builtins, function (key, value) {
  530. data[key] = { instance: value };
  531. });
  532. ar.each(configuration.types, function (spec) {
  533. data[spec.type] = { id: spec.modulator };
  534. });
  535. var sourcespecs = configuration.sources.slice(0);
  536. var sources = [ globalator.create() ];
  537. var guard = function (type) {
  538. if (data[type] === undefined)
  539. throw 'Unknown modulator type [' + type + '].';
  540. };
  541. var isResolved = function (type) {
  542. guard(type);
  543. return data[type].instance !== undefined;
  544. };
  545. var idOf = function (type) {
  546. guard(type);
  547. return data[type].id;
  548. };
  549. var instanceOf = function (type) {
  550. guard(type);
  551. return data[type].instance;
  552. };
  553. var register = function (type, instance) {
  554. guard(type);
  555. data[type].instance = instance;
  556. };
  557. var find = function (id) {
  558. for (var i = 0; i < sources.length; ++i)
  559. if (sources[i].can(id))
  560. return { found: sources[i] };
  561. return { notfound: true };
  562. };
  563. var crank = function () {
  564. var left = [];
  565. ar.each(sourcespecs, function (spec) {
  566. if (isResolved(spec.type)) {
  567. var instance = instanceOf(spec.type);
  568. var source = instance.create.apply(null, spec.args);
  569. sources.push(source);
  570. } else
  571. left.push(spec);
  572. });
  573. sourcespecs = left;
  574. };
  575. return {
  576. isResolved: isResolved,
  577. idOf: idOf,
  578. instanceOf: instanceOf,
  579. register: register,
  580. find: find,
  581. crank: crank
  582. };
  583. };
  584. return {
  585. create: create
  586. };
  587. }
  588. );
  589. kernel.api.regulator = def(
  590. [
  591. kernel.fp.array,
  592. kernel.fp.functions
  593. ],
  594. function (ar, fn) {
  595. var create = function (sources) {
  596. /*
  597. * 1. Resolve configuration as much as possible
  598. * 2. Check for unresolved modulator types that are required to continue.
  599. * a) Go ahead and resolve, if we have everything we need.
  600. * b) Delay, requiring the modulators, then retry.
  601. */
  602. var regulate = function (ids, define, require, demand, onsuccess, onerror) {
  603. sources.crank();
  604. var required = ar.map(ids, determinetype);
  605. var unresolved = ar.filter(required, fn.not(sources.isResolved));
  606. if (unresolved.length === 0)
  607. resolve(ids, define, require, demand, onsuccess, onerror);
  608. else
  609. delay(unresolved, ids, define, require, demand, onsuccess, onerror);
  610. };
  611. var resolve = function (ids, define, require, demand, onsuccess, onerror) {
  612. var r = [];
  613. for (var i = 0; i < ids.length; ++i) {
  614. var id = ids[i];
  615. var source = sources.find(id);
  616. if (source.notfound) {
  617. onerror('Could not find source for module [' + id + ']');
  618. return;
  619. }
  620. var spec = source.found.get(id, define, require, demand);
  621. r[i] = build(id, spec);
  622. }
  623. onsuccess(r);
  624. };
  625. var build = function (id, spec) {
  626. return {
  627. id: id,
  628. url: spec.url,
  629. load: spec.load,
  630. serial: spec.serial
  631. };
  632. };
  633. var delay = function (types, ids, define, require, demand, onsuccess, onerror) {
  634. var modulatorids = ar.map(types, sources.idOf);
  635. require(modulatorids, function (/* modulators */) {
  636. var modulators = arguments;
  637. ar.each(types, function (type, i) {
  638. sources.register(type, modulators[i]);
  639. });
  640. regulate(ids, define, require, demand, onsuccess, onerror);
  641. });
  642. };
  643. var determinetype = function (id) {
  644. var index = id.indexOf('!');
  645. return index === -1 ? 'bolt' : id.substring(0, index);
  646. };
  647. return {
  648. regulate: regulate
  649. };
  650. };
  651. return {
  652. create: create
  653. };
  654. }
  655. );
  656. kernel.api.config = def(
  657. [
  658. kernel.module.manager,
  659. kernel.api.regulator,
  660. kernel.api.sources
  661. ],
  662. function (manager, regulator, sources) {
  663. var configure = function (configuration, builtins, onerror) {
  664. var s = sources.create(builtins, configuration);
  665. var r = regulator.create(s);
  666. var engine = manager.create(r, onerror);
  667. return {
  668. define: engine.define,
  669. require: engine.require,
  670. demand: engine.demand
  671. };
  672. };
  673. return {
  674. configure: configure
  675. };
  676. }
  677. );
  678. })(Function('return this')());
  679. (function (scope) {
  680. var ephox = scope.ephox = scope.ephox || {};
  681. var bolt = ephox.bolt = ephox.bolt || {};
  682. var def = function (deps, factory) {
  683. return factory.apply(null, deps);
  684. };
  685. var loader = bolt.loader = bolt.loader || {};
  686. loader.executor = loader.executor || {};
  687. loader.api = loader.api || {};
  688. loader.transporter = loader.transporter || {};
  689. loader.tag = loader.tag || {};
  690. loader.tag.script = def(
  691. [
  692. ],
  693. function () {
  694. var guard = function (callback) {
  695. return function (evt) {
  696. if (evt.srcElement.readyState === "loaded" || evt.srcElement.readyState === "complete")
  697. callback();
  698. };
  699. };
  700. var ie = function (el) {
  701. return el.attachEvent && !window.opera;
  702. };
  703. var onload = function (el, callback) {
  704. if (ie(el))
  705. el.attachEvent("onreadystatechange", guard(callback));
  706. else
  707. el.addEventListener("load", callback, false);
  708. };
  709. var createtag = function (callback) {
  710. var el = document.createElement("script");
  711. el.type = "text/javascript";
  712. onload(el, callback);
  713. return el;
  714. };
  715. var insert = function (decorator, callback) {
  716. var el = createtag(callback);
  717. decorator(el);
  718. var head = document.getElementsByTagName("head")[0];
  719. head.appendChild(el);
  720. };
  721. return {
  722. insert: insert
  723. };
  724. }
  725. );
  726. loader.transporter.commonjs = def(
  727. [
  728. ],
  729. function () {
  730. var read = function (url, success, error) {
  731. var fs = require('fs');
  732. fs.exists(url, function (exists) {
  733. if (exists)
  734. fs.readFile(url, 'UTF-8', function (err, data) {
  735. if (err)
  736. error('Error reading file [' + url + '], error [' + err + ']');
  737. else
  738. success(data);
  739. });
  740. else
  741. error('File does not exist [' + url + ']');
  742. });
  743. };
  744. return {
  745. read: read
  746. };
  747. }
  748. );
  749. loader.transporter.xhr = def(
  750. [
  751. ],
  752. function () {
  753. var requestObject = function () {
  754. // Correct way to use XMLHttpRequest in IE:
  755. // http://blogs.msdn.com/b/ie/archive/2006/01/23/516393.aspx
  756. var factories = [
  757. function () { return new XMLHttpRequest() },
  758. function () { return new ActiveXObject("Microsoft.XMLHTTP") }
  759. ];
  760. return fallback(factories);
  761. };
  762. var fallback = function (items) {
  763. for (var i = 0; i < items.length; ++i) {
  764. try {
  765. return items[i]();
  766. } catch (e) {
  767. }
  768. }
  769. };
  770. var handler = function (req, url, success, error) {
  771. return function () {
  772. if (req.readyState === 4)
  773. done(req, url, success, error);
  774. };
  775. };
  776. var done = function (req, url, success, error) {
  777. if (req.status === 200 || req.status === 304)
  778. success(req.responseText);
  779. else
  780. error('Transport error: ' + req.status + ' ' + req.statusText + ' for resource: "' + url + '"');
  781. };
  782. var getUrl = function (req, url, success, error) {
  783. req.open('GET', url, true);
  784. req.onreadystatechange = handler(req, url, success, error);
  785. req.send();
  786. };
  787. var request = function (url, success, error) {
  788. var req = requestObject();
  789. if (req)
  790. getUrl(req, url, success, error);
  791. else
  792. error('Transport error: browser does not support XMLHttpRequest.');
  793. };
  794. return {
  795. request: request
  796. };
  797. }
  798. );
  799. loader.executor.evaller = def(
  800. [
  801. ],
  802. function () {
  803. var execute = function (data, onsuccess, onfailure) {
  804. try {
  805. eval(data);
  806. } catch(e) {
  807. onfailure(e);
  808. return;
  809. }
  810. onsuccess();
  811. };
  812. return {
  813. execute: execute
  814. };
  815. }
  816. );
  817. loader.executor.injector = def(
  818. [
  819. loader.tag.script
  820. ],
  821. function (script) {
  822. var execute = function (data, onsuccess, onfailure) {
  823. var inject = function (tag) {
  824. tag.text = data;
  825. };
  826. var noop = function () {};
  827. // Injection does not fire events, but execution happens synchronously,
  828. // so we just make an explicit callback
  829. script.insert(inject, noop);
  830. onsuccess();
  831. };
  832. return {
  833. execute: execute
  834. };
  835. }
  836. );
  837. loader.api.commonjsevaller = def(
  838. [
  839. loader.transporter.commonjs,
  840. loader.executor.evaller
  841. ],
  842. function (commonjs, evaller) {
  843. var load = function (url, onsuccess, onfailure) {
  844. var inject = function (data) {
  845. evaller.execute(data, onsuccess, onfailure);
  846. };
  847. commonjs.read(url, inject, onfailure);
  848. };
  849. return {
  850. load: load
  851. };
  852. }
  853. );
  854. loader.api.scripttag = def(
  855. [
  856. loader.tag.script
  857. ],
  858. function (script) {
  859. var load = function (url, onsuccess, onfailure) {
  860. var sourcer = function (tag) {
  861. tag.src = url;
  862. };
  863. script.insert(sourcer, onsuccess);
  864. };
  865. return {
  866. load: load
  867. };
  868. }
  869. );
  870. loader.api.xhrevaller = def(
  871. [
  872. loader.transporter.xhr,
  873. loader.executor.evaller
  874. ],
  875. function (xhr, evaller) {
  876. var load = function (url, onsuccess, onfailure) {
  877. var inject = function (data) {
  878. evaller.execute(data, onsuccess, onfailure);
  879. };
  880. xhr.request(url, inject, onfailure);
  881. };
  882. return {
  883. load: load
  884. };
  885. }
  886. );
  887. loader.api.xhrinjector = def(
  888. [
  889. loader.transporter.xhr,
  890. loader.executor.injector
  891. ],
  892. function (xhr, injector) {
  893. var load = function (url, onsuccess, onfailure) {
  894. var inject = function (data) {
  895. injector.execute(data, onsuccess);
  896. };
  897. xhr.request(url, inject, onfailure);
  898. };
  899. return {
  900. load: load
  901. };
  902. }
  903. );
  904. })(Function('return this')());
  905. (function (scope) {
  906. var ephox = scope.ephox = scope.ephox || {};
  907. var bolt = ephox.bolt = ephox.bolt || {};
  908. var def = function (deps, factory) {
  909. return factory.apply(null, deps);
  910. };
  911. var module = bolt.module = bolt.module || {};
  912. module.bootstrap = module.bootstrap || {};
  913. module.config = module.config || {};
  914. module.error = module.error || {};
  915. module.modulator = module.modulator || {};
  916. module.reader = module.reader || {};
  917. module.runtime = module.runtime || {};
  918. module.util = module.util || {};
  919. module.error.error = def(
  920. [
  921. ],
  922. function () {
  923. var die = function (msg) {
  924. throw msg || new Error('unknown error');
  925. };
  926. return {
  927. die: die
  928. };
  929. }
  930. );
  931. module.config.mapper = def(
  932. [
  933. ],
  934. function () {
  935. var flat = function (id) {
  936. return id;
  937. };
  938. var hierarchical = function (id) {
  939. return id.replace(/\./g, '/');
  940. };
  941. var constant = function (name) {
  942. return function () {
  943. return name;
  944. };
  945. };
  946. return {
  947. flat: flat,
  948. hierarchical: hierarchical,
  949. constant: constant
  950. };
  951. }
  952. );
  953. module.api = def(
  954. [
  955. module.runtime
  956. ],
  957. function (runtime) {
  958. var delegate = function (method) {
  959. return function () {
  960. return runtime[method].apply(null, arguments);
  961. };
  962. };
  963. return {
  964. define: delegate('define'),
  965. require: delegate('require'),
  966. demand: delegate('demand'),
  967. main: delegate('main'),
  968. load: delegate('load'),
  969. loadscript: delegate('loadscript')
  970. };
  971. }
  972. );
  973. module.util.path = def(
  974. [
  975. ],
  976. function () {
  977. var dirname = function (file) {
  978. var normalized = file.replace(/\\/g, '/');
  979. var end = normalized.lastIndexOf('/');
  980. return normalized.substring(0, end);
  981. };
  982. var basename = function (file) {
  983. var normalized = file.replace(/\\/g, '/');
  984. var end = normalized.lastIndexOf('/');
  985. return normalized.substring(end + 1);
  986. };
  987. return {
  988. basename: basename,
  989. dirname: dirname
  990. };
  991. }
  992. );
  993. module.util.locator = def(
  994. [
  995. ],
  996. function () {
  997. var browser = function () {
  998. var scripts = document.getElementsByTagName("script");
  999. return scripts[scripts.length - 1].src;
  1000. };
  1001. var runtime = module.runtime.locate;
  1002. var locate = function () {
  1003. var f = runtime || browser;
  1004. return f();
  1005. };
  1006. return {
  1007. locate: locate
  1008. };
  1009. }
  1010. );
  1011. module.util.pather = def(
  1012. [
  1013. module.util.path
  1014. ],
  1015. function (path) {
  1016. var create = function (relativeto) {
  1017. var base = path.dirname(relativeto);
  1018. return function (path) {
  1019. return base + '/' + path;
  1020. };
  1021. };
  1022. return {
  1023. create: create
  1024. };
  1025. }
  1026. );module.modulator.modulators = def(
  1027. [
  1028. ephox.bolt.kernel.fp.functions,
  1029. ephox.bolt.kernel.modulator.bolt,
  1030. ephox.bolt.loader.api.commonjsevaller,
  1031. ephox.bolt.loader.api.scripttag,
  1032. ephox.bolt.loader.api.xhrevaller,
  1033. ephox.bolt.loader.api.xhrinjector
  1034. ],
  1035. function (fn, bolt, commonjsevaller, scripttag, xhrevaller, xhrinjector) {
  1036. var wrap = function (modulator, loader) {
  1037. var create = fn.curry(modulator.create, loader);
  1038. return {
  1039. create: create
  1040. }
  1041. };
  1042. return {
  1043. boltcommonjs: wrap(bolt, commonjsevaller),
  1044. boltscripttag: wrap(bolt, scripttag),
  1045. boltxhreval: wrap(bolt, xhrevaller),
  1046. boltxhrinjector: wrap(bolt, xhrinjector)
  1047. };
  1048. }
  1049. );
  1050. module.config.builtins = def(
  1051. [
  1052. ephox.bolt.module.modulator.modulators.boltscripttag,
  1053. ephox.bolt.module.modulator.modulators.boltcommonjs
  1054. ],
  1055. function (boltscripttag, boltcommonjs) {
  1056. return {
  1057. // TODO: 'amd' is maintained for backwards compatibility, will be removed
  1058. // at some point.
  1059. browser: { bolt: boltscripttag, amd: boltscripttag },
  1060. commonjs: { bolt: boltcommonjs, amd: boltcommonjs }
  1061. };
  1062. }
  1063. );
  1064. module.config.specs = def(
  1065. [
  1066. module.util.pather
  1067. ],
  1068. function (pather) {
  1069. var type = function (type, implementation) {
  1070. return {
  1071. type: type,
  1072. implementation: implementation,
  1073. modulator: implementation + '.Modulator',
  1074. compiler: implementation + '.Compiler'
  1075. };
  1076. };
  1077. var source = function (relativeto) {
  1078. return function (type /*, args */) {
  1079. return {
  1080. type: type,
  1081. relativeto: relativeto,
  1082. args: [ pather.create(relativeto) ].concat(Array.prototype.slice.call(arguments, 1))
  1083. };
  1084. }
  1085. };
  1086. return {
  1087. type: type,
  1088. source: source
  1089. };
  1090. }
  1091. );
  1092. module.reader.bouncing = def(
  1093. [
  1094. ephox.bolt.kernel.fp.array,
  1095. module.error.error,
  1096. module.config.specs
  1097. ],
  1098. function (ar, error, specs) {
  1099. var bounce = function (done, read, acc) {
  1100. var next = acc.configs.shift();
  1101. read(next.relativeto, next.config, done, acc);
  1102. };
  1103. var tick = function (file, cfg, done, read, acc) {
  1104. var munged = ar.map(cfg.configs || [], function (config) {
  1105. return { relativeto: file, config: config };
  1106. });
  1107. var accumulated = {
  1108. sources: acc.sources.concat(cfg.sources || []),
  1109. types: acc.types.concat(cfg.types || []),
  1110. configs: munged.concat(acc.configs)
  1111. };
  1112. if (accumulated.configs.length > 0)
  1113. bounce(done, read, accumulated);
  1114. else
  1115. done({ sources: accumulated.sources, types: accumulated.types });
  1116. };
  1117. /*
  1118. * All precedence is depth-first, pre-order. Example:
  1119. *
  1120. * A
  1121. * /-\
  1122. * B C
  1123. * /| |\
  1124. * D E F G
  1125. *
  1126. * Configs are read in A, B, D, E, C, F, G.
  1127. *
  1128. * If configs mixed delegation and sources, the
  1129. * sources would be ordered the same: A, B, D, E, C, F, G.
  1130. */
  1131. var evaluate = function (file, payload, done, read, acc) {
  1132. var result = {};
  1133. /* eval scope */
  1134. var mapper = module.config.mapper;
  1135. var type = specs.type;
  1136. var source = specs.source(file);
  1137. var configure = function (configuration) {
  1138. result = configuration;
  1139. };
  1140. try {
  1141. eval(payload);
  1142. } catch (e) {
  1143. throw 'Could not load configuration [' + file + '], with: ' + e;
  1144. }
  1145. tick(file, result, done, read, acc);
  1146. };
  1147. return {
  1148. evaluate: evaluate
  1149. };
  1150. }
  1151. );
  1152. module.reader.browser = def(
  1153. [
  1154. module.error.error,
  1155. module.reader.bouncing,
  1156. module.util.path,
  1157. ephox.bolt.loader.transporter.xhr
  1158. ],
  1159. function (error, bouncing, path, xhr) {
  1160. var read = function (relativeto, file, done, acc) {
  1161. var accumulated = acc || { sources: [], types: [], configs: [] };
  1162. var base = path.dirname(relativeto);
  1163. var absolute = base + '/' + file;
  1164. xhr.request(absolute, function (payload) {
  1165. bouncing.evaluate(absolute, payload, done, read, accumulated);
  1166. }, error.die);
  1167. };
  1168. return {
  1169. read: read
  1170. };
  1171. }
  1172. );
  1173. module.reader.node = def(
  1174. [
  1175. module.reader.bouncing
  1176. ],
  1177. function (bouncing, path, fs) {
  1178. var read = function (relativeto, file, done, acc) {
  1179. var fs = require('fs');
  1180. var path = require('path');
  1181. var accumulated = acc || { sources: [], types: [], configs: [] };
  1182. var base = path.dirname(relativeto);
  1183. var absolute = path.resolve(base, file);
  1184. var payload = fs.readFileSync(absolute, 'UTF-8');
  1185. bouncing.evaluate(absolute, payload, done, read, accumulated);
  1186. };
  1187. return {
  1188. read: read
  1189. };
  1190. }
  1191. );
  1192. module.reader.direct = def(
  1193. [
  1194. ],
  1195. function () {
  1196. var create = function (configuration) {
  1197. return function (done) {
  1198. done({
  1199. sources: configuration.sources || [],
  1200. types: configuration.types || [],
  1201. configs: configuration.configs || []
  1202. });
  1203. };
  1204. };
  1205. return {
  1206. create: create
  1207. };
  1208. }
  1209. );
  1210. module.bootstrap.configloader = def(
  1211. [
  1212. module.util.locator,
  1213. module.reader.browser
  1214. ],
  1215. function (locator, browser) {
  1216. var create = function (file) {
  1217. var script = locator.locate();
  1218. return function (done) {
  1219. browser.read(script, file, done);
  1220. };
  1221. };
  1222. return {
  1223. create: create
  1224. };
  1225. }
  1226. );module.bootstrap.deferred = def(
  1227. [
  1228. ephox.bolt.kernel.fp.array
  1229. ],
  1230. function (ar) {
  1231. var deferred = [];
  1232. var require = function (ids, fn) {
  1233. var r = function (real) {
  1234. real(ids, fn);
  1235. };
  1236. deferred.push(r);
  1237. };
  1238. var configured = function (require) {
  1239. ar.each(deferred, function (action) {
  1240. action(require);
  1241. });
  1242. deferred = [];
  1243. };
  1244. return {
  1245. require: require,
  1246. configured: configured
  1247. };
  1248. }
  1249. );
  1250. module.bootstrap.main = def(
  1251. [
  1252. ephox.bolt.kernel.api.config,
  1253. module.bootstrap.deferred,
  1254. module.runtime
  1255. ],
  1256. function (config, deferred, runtime) {
  1257. var main = function (id, args, configids, callback) {
  1258. runtime.require(configids || [], function () {
  1259. callback && callback.apply(null, arguments);
  1260. runtime.require([ id ], function (module) {
  1261. module.apply(null, args || []);
  1262. });
  1263. });
  1264. };
  1265. return {
  1266. main: main
  1267. };
  1268. }
  1269. );
  1270. module.bootstrap.install = def(
  1271. [
  1272. ephox.bolt.kernel.api.config,
  1273. module.bootstrap.deferred,
  1274. module.bootstrap.main,
  1275. module.runtime,
  1276. module.error.error
  1277. ],
  1278. function (config, deferred, main, runtime, error) {
  1279. var notready = function () { throw 'bolt not initialised, can not call define or demand, did you mean to use require or main?'; };
  1280. var install = function (reader, builtins, load, loadscript) {
  1281. runtime.define = notready;
  1282. runtime.demand = notready;
  1283. runtime.require = deferred.require;
  1284. runtime.main = main.main;
  1285. runtime.load = load;
  1286. runtime.loadscript = loadscript;
  1287. reader(function (configuration) {
  1288. var bolt = config.configure(configuration, builtins, error.die);
  1289. runtime.define = bolt.define;
  1290. runtime.require = bolt.require;
  1291. runtime.demand = bolt.demand;
  1292. deferred.configured(runtime.require);
  1293. });
  1294. };
  1295. return {
  1296. install: install
  1297. };
  1298. }
  1299. );
  1300. })(Function('return this')());
  1301. (function (global) {
  1302. var obj = ephox.bolt.kernel.fp.object;
  1303. var api = ephox.bolt.module.api;
  1304. var builtins = ephox.bolt.module.config.builtins.browser;
  1305. var install = ephox.bolt.module.bootstrap.install;
  1306. var reader = ephox.bolt.module.bootstrap.configloader.create("browser.js");
  1307. var transport = ephox.bolt.loader.transporter.xhr.request;
  1308. var script = ephox.bolt.loader.api.scripttag.load;
  1309. install.install(reader, builtins, transport, script);
  1310. obj.merge(global, api);
  1311. })(Function("return this;")());