From 77aaa16be4d1c93814c55386be7dee64e6891304 Mon Sep 17 00:00:00 2001 From: Matt Pass Date: Sun, 1 Apr 2012 12:43:18 +0100 Subject: [PATCH] Extra nestTagException for ?php (not that it makes difference) Removed ICEcoder.tD var as it was hardly used and theress no char saving really Switched a few areas to ternary if statements Code improvements, structure tidy up & improved commenting Removed unnecessary code, stopped a few global vars Also considering ?php as well as ? Breaking tagEnd var on new lines to avoid nesting false errors CTRL+I searches for highlighted word on Google in a context aware way (ie, highlight 'echo' in a PHP block and it will search for 'PHP echo', highlighting 'echo' in content area will search for just 'echo') New function to select/deselect file/folder by changing CSS class Only show New File and New Folder context menu options if we right clicked a folder Simplified getcMInstance function Improved commenting function to allow partial line commenting Fixed issue with clicking plugin icons and server queue deletion on return Fixed issue with CSS color blocks so they only show if they have a BG color --- lib/coder.js | 399 +++++++++++++++++++++++++-------------------------- 1 file changed, 195 insertions(+), 204 deletions(-) diff --git a/lib/coder.js b/lib/coder.js index 4957461..ec8bd88 100644 --- a/lib/coder.js +++ b/lib/coder.js @@ -27,16 +27,15 @@ var ICEcoder = { serverQueueItems: [], // Array of URLs to call in order // Don't consider these tags as part of nesting as they're singles, JS or PHP code blocks - tagNestExceptions: ["!DOCTYPE","meta","link","img","br","hr","input","script","?"], + tagNestExceptions: ["!DOCTYPE","meta","link","img","br","hr","input","script","?php","?"], // On load, set aliases, set the layout and get the nest location init: function() { var aliasArray = ["header","files","account","fmLock","filesFrame","editor","tabsBar","findBar","content","footer","nestValid","nestDisplay","charDisplay"]; - ICEcoder.tD = top.document; // Create our ID aliases for (var i=0;i value fileName = ICEcoder.openFiles[ICEcoder.selectedTab-1]; if (fileName.indexOf(".js")<0&&fileName.indexOf(".css")<0) { - - var cM, content; cM = ICEcoder.getcMInstance(); content = cM.getValue(); if (top.ICEcoder.codeAssist) {content = content.replace(/ & /g,' & ');}; @@ -101,6 +96,7 @@ var ICEcoder = { // Work out the nesting depth location on demand and update our display if required getNestLocation: function(updateNestDisplay) { var cM, openTag, nestCheck, startPos, tagStart, canDoTheEndTag, tagEnd, tagEndJS, fileName; + cM = ICEcoder.getcMInstance(); nestCheck = cM.getValue(); @@ -117,7 +113,7 @@ var ICEcoder = { // Get the tag name and if it's the start of a code block, set the var for that tagStart=nestCheck.substr(startPos,nestCheck.length).split(" ")[0].split(">")[0].split("\n")[0]; - if (tagStart=="script"||tagStart=="?") {ICEcoder.codeBlock=true} + if (tagStart=="script"||tagStart=="?php"||tagStart=="?") {ICEcoder.codeBlock=true} if (tagStart!="") {ICEcoder.tagStart = tagStart} }; @@ -145,6 +141,7 @@ var ICEcoder = { tagEnd=tagEnd.substr(tagEnd.lastIndexOf('<')+1,tagEnd.length); tagEnd=tagEnd.substr(tagEnd.lastIndexOf(' ')+1,tagEnd.length); tagEnd=tagEnd.substr(tagEnd.lastIndexOf('\t')+1,tagEnd.length); + tagEnd=tagEnd.substr(tagEnd.lastIndexOf('\n')+1,tagEnd.length); tagEnd=tagEnd.substr(tagEnd.lastIndexOf(';')+1,tagEnd.length); if (!ICEcoder.codeBlock) { @@ -159,7 +156,7 @@ var ICEcoder = { } } else if ( ((ICEcoder.tagStart=="script"||ICEcoder.tagStart=="/script")&&tagEndJS=="caretChunk.lastIndexOf("")&&caretLocType=="Unknown") {caretLocType = "JavaScript"}; @@ -450,7 +459,6 @@ var ICEcoder = { if (caretChunk.lastIndexOf("<")>caretChunk.lastIndexOf(">")&&caretLocType=="Unknown") {caretLocType = "HTML"}; if (caretLocType=="Unknown") {caretLocType = "Content"}; - var fileName; fileName = ICEcoder.openFiles[ICEcoder.selectedTab-1]; if (fileName.indexOf(".js")>0) {caretLocType="JavaScript"}; if (fileName.indexOf(".css")>0) {caretLocType="CSS"}; @@ -477,10 +485,9 @@ var ICEcoder = { // Close the tab upon request closeTab: function(closeTabNum) { - var cM, okToRemove; - cM = ICEcoder.getcMInstance(); + cM = ICEcoder.getcMInstance(); okToRemove = true; if (ICEcoder.changedContent[closeTabNum-1]==1) { okToRemove = confirm('You have made changes.\n\nAre you sure you want to close without saving?'); @@ -489,11 +496,11 @@ var ICEcoder = { if (okToRemove) { // recursively copy over all tabs & data from the tab to the right, if there is one for (var i=closeTabNum;i -1) { - var submenu=aMenus[i].childNodes; + var submenu=aMenus[i].childNodes; for (var j=0; j -1) ? "open" : "closed"; } - if (submenu[j].tagName == "UL") + if (submenu[j].tagName == "UL") { submenu[j].style.display = (mclass.indexOf("open") > -1) ? "block" : "none"; + } } } } @@ -576,69 +584,70 @@ var ICEcoder = { selectFileFolder: function() { var resetFile, shortURL, foundSelectedFile, foundShortURL, foundFile; - // If we've clicked somewhere other than a file/folder - if (top.ICEcoder.thisFileFolderLink=="") { - if (!top.ICEcoder.ctrlKeyDown) { - // Deselect all files - for (var i=0;i<=top.ICEcoder.selectedFiles.length;i++) { - if (top.ICEcoder.selectedFiles[i]) { - resetFile = top.ICEcoder.filesFrame.contentWindow.document.getElementById(top.ICEcoder.selectedFiles[i]); - resetFile.style.backgroundColor="#dddddd"; - resetFile.style.color="#000000"; + // If we've clicked somewhere other than a file/folder + if (top.ICEcoder.thisFileFolderLink=="") { + if (!top.ICEcoder.ctrlKeyDown) { + // Deselect all files + for (var i=0;i<=top.ICEcoder.selectedFiles.length;i++) { + if (top.ICEcoder.selectedFiles[i]) { + resetFile = top.ICEcoder.filesFrame.contentWindow.document.getElementById(top.ICEcoder.selectedFiles[i]); + ICEcoder.selectDeselectFile('deselect',resetFile); + } } + // Set our arrray to contain 0 items + top.ICEcoder.selectedFiles.length = 0; } - // Set our arrray to contain 0 items - top.ICEcoder.selectedFiles.length = 0; - } - } else { - // We clicked a file/folder. Work out a shortened URL for the file, with pipes instead of slashes - shortURL = top.ICEcoder.thisFileFolderLink.substr((top.ICEcoder.thisFileFolderLink.indexOf(shortURLStarts)+top.shortURLStarts.length),top.ICEcoder.thisFileFolderLink.length).replace(/\//g,"|"); - - // If we have the CTRL key down - if (top.ICEcoder.ctrlKeyDown) { - foundSelectedFile=false; - // Reset all files to not be highlighted - for (i=0;i<=top.ICEcoder.selectedFiles.length;i++) { - if (top.ICEcoder.selectedFiles[i]==shortURL) { - resetFile = ICEcoder.filesFrame.contentWindow.document.getElementById(top.ICEcoder.selectedFiles[i]); - resetFile.style.backgroundColor="#dddddd"; - resetFile.style.color="#000000"; - top.ICEcoder.selectedFiles.splice(i); - foundSelectedFile=true; - } - } - if (!foundSelectedFile) { - foundFile = ICEcoder.filesFrame.contentWindow.document.getElementById(shortURL); - foundFile.style.backgroundColor="#888888"; - foundFile.style.color="#f8f8f8"; - top.ICEcoder.selectedFiles.push(shortURL); - } - // We are single clicking } else { - // First deselect all files - for (i=0;iOpening File
'+top.ICEcoder.shortURL); + } else { + top.ICEcoder.createNewTab(); } } - } else { - // show a message because we have 10 files open - alert('Sorry, you can only have 10 files open at a time!'); - canOpenFile = false; } - - // if we're still OK to open it... - if (canOpenFile) { - top.ICEcoder.shortURL = shortURL; - if (shortURL!="/[NEW]") { - // replace forward slashes with pipes so it get be placed in a querystring - top.ICEcoder.thisFileFolderLink = top.ICEcoder.thisFileFolderLink.replace(/\//g,"|"); - top.ICEcoder.serverQueue("add","lib/file-control.php?action=load&file="+top.ICEcoder.thisFileFolderLink); - top.ICEcoder.serverMessage('Opening File
'+top.ICEcoder.shortURL); - } else { - top.ICEcoder.createNewTab(); - } - } - } }, // Save a file on demand saveFile: function(saveAs) { var saveType; - if (saveAs) {saveType = "saveAs"} else {saveType = "save"}; + saveAs ? saveType = "saveAs" : saveType = "save"; top.ICEcoder.serverQueue("add","lib/file-control.php?action=save&file="+ICEcoder.openFiles[ICEcoder.selectedTab-1].replace(/\//g,"|")+"&saveType="+saveType); top.ICEcoder.serverMessage('Saving
'+ICEcoder.openFiles[ICEcoder.selectedTab-1]); }, @@ -720,14 +727,10 @@ var ICEcoder = { var renamedFile, shortURL; shortURL = top.ICEcoder.rightClickedFile.substr((top.ICEcoder.rightClickedFile.indexOf(shortURLStarts)+top.shortURLStarts.length),top.ICEcoder.rightClickedFile.length).replace(/\|/g,"/"); - - renamedFile = false; renamedFile = prompt('Please enter the new name for',shortURL); - if (renamedFile) { for (var i=0;i'; @@ -744,13 +747,12 @@ var ICEcoder = { var delFiles, selectedFilesList; delFiles = confirm('Delete:\n\n'+top.ICEcoder.selectedFiles.toString().replace(/\|/g,"/").replace(/,/g,"\n")+'?'); - // Upon supply a new name, rename tabs and update filename on server if (delFiles) { selectedFilesList = ""; for (var i=0;iDeleting File
'+top.ICEcoder.selectedFiles.toString().replace(/\|/g,"/").replace(/,/g,"\n")); @@ -759,7 +761,12 @@ var ICEcoder = { // Show menu on right clicking in file manager showMenu: function() { + var menuType, folderMenuItems; + if ("undefined" != typeof top.ICEcoder.thisFileFolderLink && top.ICEcoder.thisFileFolderLink!="") { + top.ICEcoder.selectedFiles[0].indexOf(".")>0 ? menuType = "file" : menuType = "folder"; + folderMenuItems = top.document.getElementById('folderMenuItems'); + menuType == "folder" ? folderMenuItems.style.display = "block" : folderMenuItems.style.display = "none"; document.getElementById('fileMenu').style.display = "inline-block"; document.getElementById('fileMenu').style.left = (top.ICEcoder.mouseX+20) + "px"; document.getElementById('fileMenu').style.top = (top.ICEcoder.mouseY-top.document.getElementById('filesFrame').contentWindow.document.body.scrollTop+80) + "px"; @@ -814,11 +821,9 @@ var ICEcoder = { // If we have results if (ICEcoder.results.length>0) { - // Show results only if (resultsOnly) { parent.parent.document.getElementById('results').innerHTML = ICEcoder.results.length + " results"; - // We need to take action instead } else { lineCount=1; @@ -874,8 +879,8 @@ var ICEcoder = { // Go to a specific line number goToLine: function() { var cM; - cM = ICEcoder.getcMInstance(); + cM = ICEcoder.getcMInstance(); cM.setCursor(document.getElementById('goToLineNo').value-1); cM.focus(); return false; @@ -884,10 +889,9 @@ var ICEcoder = { // Switch the CodeMirror mode on demand switchMode: function() { var cM; + cM = ICEcoder.getcMInstance(); - fileName = ICEcoder.openFiles[ICEcoder.selectedTab-1]; - if (fileName.indexOf('.js')>0) { cM.setOption("mode","javascript"); } else if (fileName.indexOf('.css')>0) { @@ -900,6 +904,7 @@ var ICEcoder = { // Lock & unlock the file manager navigation on demand lockUnlockNav: function() { var lockIcon; + lockIcon = top.document.getElementById('fmLock'); ICEcoder.lockedNav ? ICEcoder.lockedNav = false : ICEcoder.lockedNav = true; ICEcoder.lockedNav ? lockIcon.src="images/file-manager-icons/padlock.png" : lockIcon.src="images/file-manager-icons/padlock-disabled.png"; @@ -909,12 +914,10 @@ var ICEcoder = { getcMInstance: function(newTab) { var cM; - if (newTab=="new") { + if (newTab=="new"||(newTab!="new" && ICEcoder.openFiles.length>0)) { cM = top.ICEcoder.content.contentWindow['cM'+ICEcoder.cMInstances[ICEcoder.selectedTab-1]]; - } else if (ICEcoder.openFiles.length==0) { - cM = top.ICEcoder.content.contentWindow['cM1']; } else { - cM = top.ICEcoder.content.contentWindow['cM'+ICEcoder.cMInstances[ICEcoder.selectedTab-1]]; + cM = top.ICEcoder.content.contentWindow['cM1']; } return cM; }, @@ -925,7 +928,7 @@ var ICEcoder = { if (plugTarget=="_parent"||plugTarget=="_top"||plugTarget=="_self"||plugTarget=="") { setInterval('window.location=\''+plugURL+'\'',plugTimer*1000*60); - // for pluginActions iframe instances + // for fileControl iframe instances } else if (plugTarget.indexOf("fileControl")==0) { setInterval(function() {top.ICEcoder.serverQueue("add",plugURL);top.ICEcoder.serverMessage(plugTarget.split(":")[1]);},plugTimer*1000*60); @@ -937,37 +940,44 @@ var ICEcoder = { // Comment or uncomment line on keypress lineCommentToggle: function() { - var cM, lineContent, cursorPos; + var cM, cursorPos, linePos, lineContent, lCLen, adjustCursor; + cM = ICEcoder.getcMInstance(); - cursorPos = cM.getCursor().ch; - lineContent = cM.getLine(cM.getCursor().line); + linePos = cM.getCursor().line; + lineContent = cM.getLine(linePos); + lCLen = lineContent.length; + adjustCursor = 3; - if (ICEcoder.caretLocType=="JavaScript"||ICEcoder.caretLocType=="PHP") { - if (lineContent.slice(0,3)!="// ") { - cM.setLine(cM.getCursor().line, "// " + lineContent); - cM.setCursor(cM.getCursor().line, cursorPos+3); + if (ICEcoder.caretLocType=="JavaScript"||ICEcoder.caretLocType=="PHP"||ICEcoder.caretLocType=="CSS") { + if (cM.somethingSelected()) { + if (cM.getSelection().slice(0,2)!="/*") { + cM.replaceSelection("/*" + cM.getSelection() + "*/"); + } else { + cM.replaceSelection(cM.getSelection().slice(2,cM.getSelection().length-2)); + } } else { - cM.setLine(cM.getCursor().line, lineContent.slice(3,lineContent.length)); - cM.setCursor(cM.getCursor().line, cursorPos-3); - } - } else if (ICEcoder.caretLocType=="CSS") { - if (lineContent.slice(0,3)!="/* ") { - cM.setLine(cM.getCursor().line, "/* " + lineContent + " */"); - cM.setCursor(cM.getCursor().line, cursorPos+3); - } else { - cM.setLine(cM.getCursor().line, lineContent.slice(3,lineContent.length).slice(0,lineContent.length-5)); - cM.setCursor(cM.getCursor().line, cursorPos-3); + if (ICEcoder.caretLocType=="CSS") { + lineContent.slice(0,3)!="/* " ? cM.setLine(linePos, "/* " + lineContent + " */") : cM.setLine(linePos, lineContent.slice(3,lCLen).slice(0,lCLen-5)); + if (lineContent.slice(0,3)=="/* ") {adjustCursor = -adjustCursor}; + } else { + lineContent.slice(0,3)!="// " ? cM.setLine(linePos, "// " + lineContent) : cM.setLine(linePos, lineContent.slice(3,lCLen)); + if (lineContent.slice(0,3)=="// ") {adjustCursor = -adjustCursor}; + } } } else { - if (lineContent.slice(0,4)!=""); - cM.setCursor(cM.getCursor().line, cursorPos+4); + if (cM.somethingSelected()) { + if (cM.getSelection().slice(0,4)!=""); + } else { + cM.replaceSelection(cM.getSelection().slice(4,cM.getSelection().length-5)); + } } else { - cM.setLine(cM.getCursor().line, lineContent.slice(4,lineContent.length).slice(0,lineContent.length-9)); - cM.setCursor(cM.getCursor().line, cursorPos-4); + lineContent.slice(0,4)!="") : cM.setLine(linePos, lineContent.slice(4,lCLen).slice(0,lCLen-9)); + lineContent.slice(0,4)=="