function editorsm (containerMan, fileMan) { this.containerMan = containerMan; this.fileMan = fileMan; /*********************************************/ /* Création d'une nouvelle fenetre d'edition */ /*********************************************/ this.newDoc = function (content) { var span = document.createElement('span'); span.addClassName('editorLine'); var rTa = richTextarea(span); var editors = this; rTa.addClassName('editor'); rTa.saved = true; rTa.path = false; rTa.filename = 'new'; rTa.container = this.containerMan.newContainer('new', rTa); rTa.container.setDetails('new document'); rTa.container.prependChild(span); /*************************************************/ /* Affectation des nouvelles méthodes de l'objet */ /*************************************************/ rTa.newDoc = function () { // CTRL + N editors.newDoc(); } rTa.savez = function () { // CTRL + S editors.save(rTa); } rTa.close = function () { // CTRL + Q rTa.container.close(); } rTa.makeOnTop = function () { rTa.container.makeOnTop(); } rTa.HTMLAutoComplete = function () { var value = rTa.value.substring(0, rTa.selectionStart); if (value[value.length - 1] != '<') return rTa.insert("/"); var tag = /<[^\!\? ][^>]*>/g; var tagname = /<([^ ]*)/; var opentag = /<[^!\?\/> ].*[^\/]?>/; var closetag = /<\/[^>]*>/; var autoclosetag = /<[^!\?][^>]*\/>/; var stri = value.match(tag); var nodes = new Array(); var deep = 0; var closer = false; var opener = false; var autocloser = false; for (var i in stri) { closer = closetag.test(stri[i]); opener = opentag.test(stri[i]); autocloser = autoclosetag.test(stri[i]); if (opener && !closer && !autocloser) { deep++; if ( !nodes[deep] ) nodes[deep] = new Array(); //if ( nodes[deep].length > 0 ) nodes[deep].pop(); str = stri[i].match(tagname)[0].replace('<', '').replace('>', ''); nodes[deep][nodes[deep].length] = str; //alert(stri[i] + "\n\nOPEN"); } else if (!opener && closer && !autocloser) { nodes[deep].pop(); deep--; //alert(stri[i] + "\nCLOSE"); } else if (opener && !closer && autocloser) { //deep++; //if ( !nodes[deep] ) nodes[deep] = new Array(); //nodes[deep][nodes[deep].length] = stri[i].replace('<', '').replace('>', ''); //deep--; //tag = ''; //alert(stri[i] + "\nAUTOCLOSE"); } else { //tag = ''; //alert(stri[i] + "\nerror\n\nopener : " + opener + "\ncloser : " + closer + "\n autoclose : " + autocloser); } } if (!deep) return rTa.insert("/"); return rTa.insert("/" + nodes[deep][nodes[deep].length - 1] + ">"); } /*************************************/ /* Affectation des nouveau événement */ /*************************************/ rTa.onvaluechange = function () { rTa.saved = false; rTa.container.setTitle(rTa.filename + "*"); } /*******************************************/ /* Affectation des évènements au conteneur */ /*******************************************/ rTa.container.onmakeontop = function () { rTa.focus(); } rTa.container.onbeforeclose = function () { return editors.confirme(rTa); } /********************************/ /* Ajout des raccourcis clavier */ /********************************/ rTa.addShortKey(1, 1, 0, 78, rTa.newDoc); // CTRL + N rTa.addShortKey(1, 1, 0, 81, rTa.close); // CTRL + Q rTa.addShortKey(1, 0, 1, 83, rTa.savez); // CTRL + S rTa.addShortKey(1, 1, 0, 83, rTa.savez); // CTRL + S rTa.addShortKey(0, 0, 1, 191, rTa.HTMLAutoComplete); // HTMLAutoComplete rTa.container.makeOnTop(); return rTa; } /**********************************************/ /* Fonction d'ouverture d'un fichier existant */ /**********************************************/ this.edit = function (file) { var xhr = new xhrQuery('editor.php?action=read', 'POST'); xhr.addPost('file', file); xhr.callback = function (respo) { rTa.setValue(respo.text); textarea.value = respo.text; textarea.scrollTop = 0; textarea.container.setTitle(textarea.filename); textarea.saved = true; } var filename = file.match("[^/]*$", 'g')[0]; var path = file.replace("/" + filename, ''); var textarea = this.newDoc("Chargement en cour..."); textarea.newi = false; textarea.path = path; textarea.filename = filename; textarea.value = "Chargement en cour..."; textarea.container.setDetails(textarea.path + '/' + textarea.filename); textarea.container.setUrl('#' + textarea.path + '/' + textarea.filename); textarea.container.setTitle(textarea.filename); //senddata('file.php?action=read', 'file=' + file, function (data) {textarea.value = data; textarea.scrollTop = 0; textarea.setSelectionRange(0, 0);}); var ext = filename.split('.'); if ( ext.length == 1 ) ext = ''; else ext = ext[ext.length - 1]; if ( ext == 'html' ) ext = 'htmlmixed'; else if ( ext == 'tpl' ) ext = 'htmlmixed'; else if (ext == 'js') ext = 'javascript'; var rTa = CodeMirror.fromTextArea(textarea, { 'value': "Chargement en cour...", 'mode': 'php.', 'indentUnit': 8, 'indentWithTabs': true, 'tabMode': 'classic', 'enterMode': 'keep', 'lineNumbers': 1, 'electricChars': 0, 'matchBrackets': true, 'mode': ext, 'onChange': function (aaa) {textarea.onvaluechange(); if (aaa) { aaa.save(); } }, 'onKeyEvent': function (aaa, bbb) { if ( bbb.ctrlKey && bbb.keyCode == 83 ) { // SAVING bbb.stop(); aaa.save(); editors.save(textarea); return true; } else if ( bbb.ctrlKey && bbb.keyCode == 68 ) { // DUPLICATE LINE / SELECTION bbb.stop(); var sel = aaa.getSelection(); if ( !sel ) { var cur = aaa.getCursor(); aaa.replaceRange(aaa.getLine(cur.line) + "\n", {'line': cur.line, 'ch': 0}); } else aaa.replaceSelection(sel + sel); return true; } else if ( (bbb.type == 'keydown') && bbb.keyCode == 36 ) { // HOME CURSOR bbb.stop(); var cur = aaa.getCursor(); var ch = aaa.getLine(cur.line).match(/^\s+/); if ( ch ) ch = ch[0].length; else ch = 0; if ( ch != cur.ch ) aaa.setCursor({'line': cur.line, 'ch': ch}); else aaa.setCursor({'line': cur.line, 'ch': 0}); return true; } else if ( bbb.type == 'keydown' && bbb.ctrlKey && bbb.keyCode == 81 ) { var cur = aaa.getCursor(); var line = aaa.getLine(cur.line); if ( line.match(/^\/\//) ) line = line.replace(/^\/\//, ''); else line = '//' + line; aaa.setLine(cur.line, line); return true; } return false; }, }); //rTa.setOption(''); xhrProc.process(xhr); return true; } /****************************************/ /* Fonction d'enregistrement du fichier */ /****************************************/ this.save = function (textarea) { var xhr = new xhrQuery('editor.php?action=save', 'POST'); xhr.addPost('file', textarea.path + '/' + textarea.filename); xhr.addPost('content', textarea.value); xhr.callback = function (respo) { if (respo.text) return this.onerror("DSFS Error :\n" + respo.text); textarea.saved = true; textarea.container.setTitle(textarea.filename); return true; } xhr.onerror = function (status, statusText) { alert(status + " : " + statusText + "\n Erreur lors de la sauvegarde."); } if (textarea.saved) return true; if (!textarea.path) { var path = prompt('Dossier pour le nouveau fichier :'); if (!path) return false; var filename = prompt('Nom du nouveau fichier :'); if (!filename) return false; /* en attendant le filemanager... */ //var filepath = 'scripts/class/editor.js'; //var filename = filepath.match("[^/]*$", 'g')[0]; //var path = filepath.replace("/" + filename, ''); textarea.path = path; textarea.filename = filename; textarea.container.setDetails(textarea.path + '/' + textarea.filename); } //this.fileMan.save(textarea.path + '/' + textarea.filename, textarea.value); //senddata('file.php?action=save', 'file=' + textarea.path + '/' + textarea.filename + '&content=' + encodeURIComponent(textarea.value), function (data) {if (data) alert(data);}); xhr.addPost('file', textarea.path + '/' + textarea.filename); xhrProc.process(xhr); return true; } /*************************************************************/ /* Fonction de confirmation de fermeture sans enregistrement */ /*************************************************************/ this.confirme = function (textarea) { var cont = true; if (!textarea.saved) { return cont = window.confirm( 'Le fichier "' + textarea.path + '/' + textarea.filename + '" n\'a pas été enregistré, cliquez sur OK pour ignorer.'); } else { return true; } } return this; }