Skip to content

Instantly share code, notes, and snippets.

@NTaylorMullen
Created May 14, 2013 06:54
Show Gist options
  • Save NTaylorMullen/5574159 to your computer and use it in GitHub Desktop.
Save NTaylorMullen/5574159 to your computer and use it in GitHub Desktop.
Auto typescript codemirror loader
/// <reference path="../Scripts/jquery-2.0.0.js" />
/// <reference path="codemirror.js" />
(function () {
var fileCache = {},
fileListTemplate = $("<li class='fileNav'><a href='#'></a></li>"),
fileList = $("#_fileList"),
fileContent = $("#_fileContent"),
jsScriptHolder = $("#_scripts"),
newFileListNav = function (name) {
var ele = fileListTemplate.clone();
ele.find("a").html(name);
return ele;
},
editor = CodeMirror(fileContent[0], {
lineNumbers: true,
matchBrackets: true,
mode: "text/typescript",
readOnly: true,
theme: "solarized light"
});
$.each($("typescript"), function (i, ele) {
var $ele = $(ele),
tsSrc = $ele.attr("src"),
jsSrc = tsSrc.substr(0, tsSrc.length - 2) + "js",
split = tsSrc.split('.'),
fileName = split[split.length - 2],
fileNav = newFileListNav(fileName + ".ts"),
text;
// Load JS dynamically
$.getScript(jsSrc);
// Build TS cache
$.get(tsSrc, function (code) {
fileCache[fileName] = code;
// Bind click after we've received the typescript cache
fileNav.click(function (e) {
editor.setOption("value", fileCache[fileName]);
$("li.fileNav").removeClass("active");
fileNav.addClass("active");
e.preventDefault();
return false;
});
});
fileList.append(fileNav);
$ele.remove();
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment