doclist.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 EditSession = require("ace/edit_session").EditSession;
  33. var UndoManager = require("ace/undomanager").UndoManager;
  34. var net = require("ace/lib/net");
  35. var modelist = require("ace/ext/modelist");
  36. /*********** demo documents ***************************/
  37. var fileCache = {};
  38. function initDoc(file, path, doc) {
  39. if (doc.prepare)
  40. file = doc.prepare(file);
  41. var session = new EditSession(file);
  42. session.setUndoManager(new UndoManager());
  43. doc.session = session;
  44. doc.path = path;
  45. session.name = doc.name;
  46. if (doc.wrapped) {
  47. session.setUseWrapMode(true);
  48. session.setWrapLimitRange(80, 80);
  49. }
  50. var mode = modelist.getModeForPath(path);
  51. session.modeName = mode.name;
  52. session.setMode(mode.mode);
  53. return session;
  54. }
  55. function makeHuge(txt) {
  56. for (var i = 0; i < 5; i++)
  57. txt += txt;
  58. return txt;
  59. }
  60. var docs = {
  61. "docs/javascript.js": {order: 1, name: "JavaScript"},
  62. "docs/latex.tex": {name: "LaTeX", wrapped: true},
  63. "docs/markdown.md": {name: "Markdown", wrapped: true},
  64. "docs/mushcode.mc": {name: "MUSHCode", wrapped: true},
  65. "docs/pgsql.pgsql": {name: "pgSQL", wrapped: true},
  66. "docs/plaintext.txt": {name: "Plain Text", prepare: makeHuge, wrapped: true},
  67. "docs/sql.sql": {name: "SQL", wrapped: true},
  68. "docs/textile.textile": {name: "Textile", wrapped: true},
  69. "docs/c9search.c9search_results": "C9 Search Results",
  70. "docs/mel.mel": "MEL",
  71. "docs/Nix.nix": "Nix"
  72. };
  73. var ownSource = {
  74. /* filled from require*/
  75. };
  76. var hugeDocs = require.toUrl ? {
  77. "build/src/ace.js": "",
  78. "build/src-min/ace.js": ""
  79. } : {
  80. "src/ace.js": "",
  81. "src-min/ace.js": ""
  82. };
  83. modelist.modes.forEach(function(m) {
  84. var ext = m.extensions.split("|")[0];
  85. if (ext[0] === "^") {
  86. path = ext.substr(1);
  87. } else {
  88. var path = m.name + "." + ext;
  89. }
  90. path = "docs/" + path;
  91. if (!docs[path]) {
  92. docs[path] = {name: m.caption};
  93. } else if (typeof docs[path] == "object" && !docs[path].name) {
  94. docs[path].name = m.caption;
  95. }
  96. });
  97. if (window.require && window.require.s) try {
  98. for (var path in window.require.s.contexts._.defined) {
  99. if (path.indexOf("!") != -1)
  100. path = path.split("!").pop();
  101. else
  102. path = path + ".js";
  103. ownSource[path] = "";
  104. }
  105. } catch(e) {}
  106. function sort(list) {
  107. return list.sort(function(a, b) {
  108. var cmp = (b.order || 0) - (a.order || 0);
  109. return cmp || a.name && a.name.localeCompare(b.name);
  110. });
  111. }
  112. function prepareDocList(docs) {
  113. var list = [];
  114. for (var path in docs) {
  115. var doc = docs[path];
  116. if (typeof doc != "object")
  117. doc = {name: doc || path};
  118. doc.path = path;
  119. doc.desc = doc.name.replace(/^(ace|docs|demo|build)\//, "");
  120. if (doc.desc.length > 18)
  121. doc.desc = doc.desc.slice(0, 7) + ".." + doc.desc.slice(-9);
  122. fileCache[doc.name] = doc;
  123. list.push(doc);
  124. }
  125. return list;
  126. }
  127. function loadDoc(name, callback) {
  128. var doc = fileCache[name];
  129. if (!doc)
  130. return callback(null);
  131. if (doc.session)
  132. return callback(doc.session);
  133. // TODO: show load screen while waiting
  134. var path = doc.path;
  135. var parts = path.split("/");
  136. if (parts[0] == "docs")
  137. path = "demo/kitchen-sink/" + path;
  138. else if (parts[0] == "ace")
  139. path = "lib/" + path;
  140. net.get(path, function(x) {
  141. initDoc(x, path, doc);
  142. callback(doc.session);
  143. });
  144. }
  145. function saveDoc(name, callback) {
  146. var doc = fileCache[name] || name;
  147. if (!doc || !doc.session)
  148. return callback("Unknown document: " + name);
  149. var path = doc.path;
  150. var parts = path.split("/");
  151. if (parts[0] == "docs")
  152. path = "demo/kitchen-sink/" + path;
  153. else if (parts[0] == "ace")
  154. path = "lib/" + path;
  155. upload(path, doc.session.getValue(), callback);
  156. }
  157. function upload(url, data, callback) {
  158. url = net.qualifyURL(url);
  159. if (!/https?:/.test(url))
  160. return callback(new Error("Unsupported url scheme"));
  161. var xhr = new XMLHttpRequest();
  162. xhr.open("PUT", url, true);
  163. xhr.onreadystatechange = function () {
  164. if (xhr.readyState === 4) {
  165. callback(!/^2../.test(xhr.status));
  166. }
  167. };
  168. xhr.send(data);
  169. };
  170. module.exports = {
  171. fileCache: fileCache,
  172. docs: sort(prepareDocList(docs)),
  173. ownSource: prepareDocList(ownSource),
  174. hugeDocs: prepareDocList(hugeDocs),
  175. initDoc: initDoc,
  176. loadDoc: loadDoc,
  177. saveDoc: saveDoc,
  178. };
  179. module.exports.all = {
  180. "Mode Examples": module.exports.docs,
  181. "Huge documents": module.exports.hugeDocs,
  182. "own source": module.exports.ownSource
  183. };
  184. });