Skip to content

Instantly share code, notes, and snippets.

@mustafa0x
Created April 1, 2012 15:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mustafa0x/2276550 to your computer and use it in GitHub Desktop.
Save mustafa0x/2276550 to your computer and use it in GitHub Desktop.
CodeMirror Lazy Load
var __CODEMIRROR_LOADED, __LOAD_COUNT = 0, __CODEMIRROR_MODE, __CODEMIRROR;
codeMirrorClick = function(syntax){
/*
* Check if syntax has been changed.
*
* If it has, we have to load the files
* for the new syntax.
*/
if (syntax != __CODEMIRROR_MODE)
/*
* We should store the previously-loaded
* syntaxes, so that they aren't reloaded.
*/
__CODEMIRROR_LOADED = false;
__CODEMIRROR_MODE = syntax;
__CODEMIRROR_LOADED ? codeMirrorRun() : codeMirrorLoad();
};
codeMirrorRun = function(){
__CODEMIRROR = CodeMirror.fromTextArea("#mytextarea", {
mode: __CODEMIRROR_MODE
//other options
});
};
codeMirrorResourceLoad = function(){
if (++__LOAD_COUNT == (languages[__CODEMIRROR_MODE].length + 2)) {
__CODEMIRROR_LOADED = true;
codeMirrorRun();
}
};
codeMirrorLoad = function(){
json2markup([
"script",
{
attributes : {
"src" : "codemirror/lib/codemirror.js",
"type" : "text/javascript"
},
events : {
load : codeMirrorResourceLoad
}
},
"link",
{
attributes : {
"rel" : "stylesheet",
"href" : "codemirror/lib/codemirror.css"
},
events : {
load : codeMirrorResourceLoad
}
},
], document.getElementsByTagName("head")[0]);
var i = 0, len = CodeMirror.dependencies[__CODEMIRROR_MODE].length;
for ( ; i < len; i++) {
item = CodeMirror.dependencies[__CODEMIRROR_MODE][i];
json2markup([
"script",
{
attributes : {
"src" : "codemirror/mode/" + item + "/" + item + ".js",
"type" : "text/javascript"
},
events : {
load : codeMirrorResourceLoad
}
}
], document.getElementsByTagName("head")[0]);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment