Today was a whirlwind of a day, and I feel like I got a lot of code done. The first thing I did today was work on automatically changing the langauge of the new snippet code entry box when the user changes the dropdown. This turned out to be pretty easy, and I will show the code in a couple lines.

First, though, we need to talk. CoffeeScript, we’ve been seeing each other for almost six days now, and I know it’s shallow, but your syntax just looks so unelegant. Don’t cry, you’re just making this harder than it already is. So yeah I’m switching back to JavaScript. So here’s the code for what I talked about above (not included in this post so as to keep it semi-short).

Now this code downloads the associated JavaScript file whenever a change happens to the dropdown. This code changed around a lot, but we’ll talk about that later. Firstly, why isn’t there better syntax for dynamically loading a JavaScript file?! If I want to do that, then I have to literally insert a script tag into the head tag. This really is unacceptable, but I guess Google is right…

So the other thing that I worked on today was integrating the CodeMirror editor into the code display in addition to the snippet new page. It turns out that you can make CodeMirror read-only and turn off line numbers to make it display a snippet really nicely! Here is my final function to download language data and stick it into a code editor:

function set_language(lang_id) {
	var code_editor_name = null

	// If the language has already been downloaded (along with it's JSON)
	if (lang_id < downloaded.length && downloaded[lang_id] != undefined) {
		code_editor_name = downloaded[lang_id]

		if (editor != null) {
		editor.setOption("mode", code_editor_name)
		}
	}
	else {
		$.ajax("/admin/languages/" + lang_id + ".json").done(function (json_lang) {
		$("head").append($("<script src=\"/assets/codemirror/modes/" + json_lang["code_editor_name"] + ".js\"></script>"))
		downloaded[json_lang["id"]] = json_lang["code_editor_name"]

		code_editor_name = json_lang["code_editor_name"]

		if (editor != null) {
			editor.setOption("mode", code_editor_name)
		}
		})
	}
}

You can see a demo version of this app on Heroku.