2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-01-31 12:49:34 +00:00

www/codemirror/fhem_codemirror.js: additional modules loads now dynamically based on codemirrorParam, fixed bug in cm_wait() setTimeout, some further improvements.

git-svn-id: https://svn.fhem.de/fhem/trunk@9700 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
rapster 2015-10-27 23:38:08 +00:00
parent 1205ee8dce
commit de1dd5e7af

View File

@ -1,72 +1,21 @@
/* $Id$ */
var cm_loaded = 0; var cm_loaded = 0;
var cm_active = 0;
$(document).ready(function(){ var cm_attr = {
var els = document.getElementsByTagName("textarea"); matchBrackets: true,
if(els.length == 0) autoRefresh: true,
return; search: true,
comment: true,
if($(els[0]).closest("div#edit").css("display")=="none") { // DEF special autocomplete: true,
$("table.internals a").each(function(){ autoCloseBrackets:false,
var oc = $(this).attr("onclick"); indentUnit: 4,
if(oc) { type: "fhem",
$(this).attr("onclick", oc+ theme: "blackboard",
's=document.getElementById("edit").getElementsByTagName("textarea");'+ indentWithTabs: false,
'if(!s[0].editor) { s[0].editor=true; AddCodeMirror(s[0]);}'); autofocus: true,
} lineNumbers: true,
});
} else {
AddCodeMirror(els[0]);
}
});
function
AddCodeMirror(e, cb)
{
if(cm_loaded == 9)
return cm_wait(e, cb);
loadLink("codemirror/codemirror.css");
loadLink("codemirror/show-hint.css");
loadLink("codemirror/dialog.css");
loadScript("codemirror/codemirror.js", function(){ cm_loaded++;} );
loadScript("codemirror/closebrackets.js",function(){ cm_loaded++;} );
loadScript("codemirror/matchbrackets.js",function(){ cm_loaded++;} );
loadScript("codemirror/search.js", function(){ cm_loaded++;} );
loadScript("codemirror/searchcursor.js", function(){ cm_loaded++;} );
loadScript("codemirror/dialog.js", function(){ cm_loaded++;} );
loadScript("codemirror/comment.js", function(){ cm_loaded++;} );
loadScript("codemirror/autorefresh.js", function(){ cm_loaded++;} );
loadScript("codemirror/show-hint.js", function(){
cm_loaded++;
cm_wait(e, cb);
});
}
function
cm_wait(cm_editor, callback)
{
if(cm_loaded != 9) {
setTimeout(cm_wait, 10);
return;
}
var ltype,type="fhem"; // get the type from the hidden filename extension
$("input[name=save]").each(function(){
ltype = $(this).attr("value");
ltype = ltype.substr(ltype.lastIndexOf(".")+1);
if(ltype=="css") type = "css";
if(ltype=="svg") type = "xml";
});
var attr = {
indentUnit: 4,
indentWithTabs: false,
autoCloseBrackets: false,
matchBrackets: true,
autofocus: true,
theme: "blackboard",
lineNumbers: true,
autoRefresh: true,
extraKeys: { extraKeys: {
'Ctrl-Space': 'autocomplete',
'Tab': function(cm) { 'Tab': function(cm) {
if (cm.somethingSelected()) { if (cm.somethingSelected()) {
var sel = cm.getSelection("\n"); var sel = cm.getSelection("\n");
@ -81,9 +30,6 @@ cm_wait(cm_editor, callback)
'Shift-Tab': function(cm) { 'Shift-Tab': function(cm) {
cm.indentSelection("subtract"); cm.indentSelection("subtract");
}, },
'Ctrl-Q': function(cm) { // Needs comment.js
cm.toggleComment({ indent: false, lineComment: "#" });
},
'Ctrl-Up': function(cm) { 'Ctrl-Up': function(cm) {
var info = cm.getScrollInfo(); var info = cm.getScrollInfo();
if (!cm.somethingSelected()) { if (!cm.somethingSelected()) {
@ -103,23 +49,104 @@ cm_wait(cm_editor, callback)
cm.scrollTo(null, info.top + cm.defaultTextHeight()); cm.scrollTo(null, info.top + cm.defaultTextHeight());
} }
} }
}; };
var userAttr = scriptAttribute("fhem_codemirror.js");
for(var a in userAttr)
attr[a] = userAttr[a];
loadLink("codemirror/"+attr.theme+".css"); $(document).ready(function(){
$("head").append( var els = document.getElementsByTagName("textarea");
'<style type="text/css">'+ if(els.length == 0)
(ltype ? return;
'.CodeMirror {height: ' + (window.innerHeight - 150) + 'px;}':
'.CodeMirror {width: ' + (window.innerWidth - 300) + 'px;}')+
'</style>');
loadScript("codemirror/"+type+".js", function(){ if($(els[0]).closest("div#edit").css("display")=="none") { // DEF special
log("Calling CodeMirror"); $("table.internals a").each(function(){
var cm = CodeMirror.fromTextArea(cm_editor, attr); var oc = $(this).attr("onclick");
if(callback) if(oc) {
callback(cm); $(this).attr("onclick", oc+
}); 's=document.getElementById("edit").getElementsByTagName("textarea");'+
'if(!s[0].editor) { s[0].editor=true; AddCodeMirror(s[0]);}');
}
});
} else {
AddCodeMirror(els[0]);
}
});
function AddCodeMirror(e, cb) {
var userAttr = scriptAttribute("fhem_codemirror.js");
for(var a in userAttr)
cm_attr[a] = userAttr[a];
if(cm_active && cm_loaded == cm_active)
return cm_wait(e, cb);
cm_active++;
loadLink("codemirror/codemirror.css");
loadScript("codemirror/codemirror.js", function(){cm_loaded++;} );
if (cm_attr.autoCloseBrackets) {
cm_active++; loadScript("codemirror/closebrackets.js", function(){cm_loaded++;} );
}
if (cm_attr.matchBrackets) {
cm_active++; loadScript("codemirror/matchbrackets.js", function(){cm_loaded++;} );
}
if (cm_attr.search) {
cm_active++; loadScript("codemirror/search.js", function(){cm_loaded++;} );
cm_active++; loadScript("codemirror/searchcursor.js", function(){cm_loaded++;} );
cm_active++;
loadLink("codemirror/dialog.css");
loadScript("codemirror/dialog.js", function(){cm_loaded++;} );
}
if (cm_attr.comment) {
cm_active++; loadScript("codemirror/comment.js", function(){cm_loaded++;} );
cm_attr.extraKeys['Ctrl-Q'] = function(cm) {
cm.toggleComment({ indent: false, lineComment: "#" });
};
}
if (cm_attr.autocomplete) {
cm_active++;
loadLink("codemirror/show-hint.css");
loadScript("codemirror/show-hint.js", function(){cm_loaded++;});
cm_attr.extraKeys['Ctrl-Space'] = 'autocomplete';
}
if (cm_attr.autoRefresh) {
cm_active++; loadScript("codemirror/autorefresh.js", function(){cm_loaded++;} );
}
// get the type from hidden filename extension, load the type-file.js, theme.css and call cm_wait
var ltype;
$("input[name=save]").each(function() {
ltype = $(this).attr("value");
ltype = ltype.substr(ltype.lastIndexOf(".")+1);
if(ltype=="css") cm_attr.type = "css";
if(ltype=="svg") cm_attr.type = "xml";
});
loadLink("codemirror/"+cm_attr.theme+".css");
$("head").append(
'<style type="text/css">'+
(ltype ?
'.CodeMirror {height: ' + (window.innerHeight - 150) + 'px;}':
'.CodeMirror {width: ' + (window.innerWidth - 300) + 'px;}')+
'</style>');
cm_active++;
loadScript("codemirror/"+cm_attr.type+".js", function(){
cm_loaded++;
cm_wait(e, cb);
});
}
function cm_wait(cm_editor, callback, recursions) {
recursions = typeof recursions !== 'undefined' ? recursions : 0;
if(cm_loaded != cm_active) {
if(recursions < 100) {
recursions++;
setTimeout(function(){ cm_wait(cm_editor, callback, recursions) }, 20);
}
return;
}
var cm = CodeMirror.fromTextArea(cm_editor, cm_attr);
if(callback)
callback(cm);
} }