From 673c779ca5f8953614ee5b5314159d248311df5b Mon Sep 17 00:00:00 2001 From: Matt Pass Date: Thu, 18 Jul 2013 18:04:45 +0100 Subject: [PATCH] Condense, improve, reduce & fix find/replace Only action replace on buttonClick (same as replace all), as having a result selected and changing replace value can replace selection without a button click, which is undesirable if condition was wrong, should be based on if we already have that char ref point in ICEcoder.results Remove old code re counting no of chars previous to cursors line, and other old chunks of code that seem to do nothing Use a much shorter and more efficient way to count chars to our cursor now Same for building up filesQS, just join the array values with a colon --- lib/ice-coder.js | 47 +++++++++++++------------------------------- lib/ice-coder.min.js | 34 ++++++++++++++++---------------- 2 files changed, 31 insertions(+), 50 deletions(-) diff --git a/lib/ice-coder.js b/lib/ice-coder.js index 16b5e0e..a2c9dc9 100644 --- a/lib/ice-coder.js +++ b/lib/ice-coder.js @@ -1017,7 +1017,7 @@ var ICEcoder = { findReplace: function(findString,resultsOnly,buttonClick) { var find, replace, results, cM, content, lineCount, numChars, charsToCursor, charCount, cursor, replaceQS, targetQS; - // Determine our find & replace strings and the length of them + // Determine our find & replace strings and results display find = findString.toLowerCase(); replace = top.get('replace').value; results = top.get('results'); @@ -1027,10 +1027,10 @@ var ICEcoder = { if (cM && find.length>0 && document.findAndReplace.target.value=="this document") { content = cM.getValue().toLowerCase(); // Find & replace the next instance, or all? - if (document.findAndReplace.connector.value=="and") { + if (document.findAndReplace.connector.value=="and" && buttonClick) { if (document.findAndReplace.replaceAction.value=="replace" && cM.getSelection().toLowerCase()==find) { cM.replaceSelection(replace); - } else if (document.findAndReplace.replaceAction.value=="replace all" && buttonClick) { + } else if (document.findAndReplace.replaceAction.value=="replace all") { var rExp = new RegExp(find,"gi"); cM.setValue(cM.getValue().replace(rExp,replace)); } @@ -1042,7 +1042,7 @@ var ICEcoder = { ICEcoder.results = []; for (var i=0;iICEcoder.results.length-1) {ICEcoder.findResult=0}; + + // Update results display results.innerHTML = "Highlighted result "+(ICEcoder.findResult+1)+" of "+ICEcoder.results.length+" results"; - lineCount=0; - for (var i=0;iICEcoder.results.length- -1&&(ICEcoder.findResult=0);e.innerHTML="Highlighted result "+(ICEcoder.findResult+1)+" of "+ICEcoder.results.length+" results";for(d=b=0;d'}},replaceInFile:function(a,b,c){top.ICEcoder.serverQueue("add","lib/file-control.php?action=replaceText&fileRef="+a.replace(/\//g,"|")+"&find="+b+"&replace="+c);top.ICEcoder.serverMessage("Replacing text in
"+a)},getNestLocation:function(a){var b,c,d;if(b=ICEcoder.getcMInstance()){c=b.getValue();b=b.getTokenAt(b.getCursor()).state;if("undefined"!=typeof b.curState&& -"undefined"!=typeof b.curState.htmlState)for(ICEcoder.htmlTagArray=[],d=b.curState.htmlState.context;d;d=d.prev)"undefined"!=typeof d.tagName&&ICEcoder.htmlTagArray.unshift(d.tagName);ICEcoder.tagString=ICEcoder.htmlTagArray[ICEcoder.htmlTagArray.length-1];"JavaScript"==ICEcoder.caretLocType&&(ICEcoder.tagString="script");if(a&&!top.ICEcoder.dontUpdateNest&&(ICEcoder.nestDisplay.innerHTML="","undefined"!=typeof ICEcoder.openFiles[ICEcoder.selectedTab-1]&&(a=ICEcoder.openFiles[ICEcoder.selectedTab- -1],0>["js","coffee","css","less"].indexOf(a.split(".")[1])&&-1==c.indexOf("include(")&&-1==c.indexOf("include_once(")&&(-1'), -ICEcoder.nestDisplay.innerHTML+="'+ICEcoder.htmlTagArray[a]+"",ICEcoder.nestDisplay.innerHTML+=a':"
'; -"undefined"!=typeof b.curState&&0'+(b.curState.tagName?b.curState.tagName:"content")+"")}}},updateNestingIndicator:function(){var a,b,c;a=ICEcoder.getcMInstance();b=!0;c=ICEcoder.openFiles[ICEcoder.selectedTab-1];a&&(c&&-1==["js","coffee","css","less"].indexOf(c.split(".")[1]))&&(b="error"!=a.getTokenAt({line:a.lineCount(), -ch:a.lineInfo(a.lineCount()-1).text.length}).className?!0:!1);ICEcoder.nestValid.style.background=b?"#0b0":"#f00";ICEcoder.nestValid.title=b?"Nesting OK":"Nesting Broken"},getCaretPosition:function(){var a,b,c,d;a=ICEcoder.getcMInstance();b=a.getValue();c=a.getCursor().line;a=a.getCursor().ch;for(var e=d=0;ea.lastIndexOf("\x3c/script>")&&"Unknown"==b?b="JavaScript":a.lastIndexOf("a.lastIndexOf("?>")&&"Unknown"==b?b="PHP":a.lastIndexOf("<%")>a.lastIndexOf("%>")&&"Unknown"==b?b="Ruby":a.lastIndexOf("<")>a.lastIndexOf(">")&&"Unknown"==b?b="HTML":"Unknown"==b&&(b="Content");(a=ICEcoder.openFiles[ICEcoder.selectedTab-1])&&(0c||19c||40c||36c||123=a||10<=a))&&top.ICEcoder.mouseX>parseInt(top.ICEcoder.files.style.width,10)&&(top.ICEcoder.tabDragMouseX=top.ICEcoder.mouseX-parseInt(top.ICEcoder.files.style.width,10)-top.ICEcoder.tabDragMouseXStart,top.ICEcoder.tabDragMove());if(top.ICEcoder.ready&&(a=window.innerWidth?window.innerHeight:document.body.clientHeight, -top.ICEcoder.mouseDown||(top.ICEcoder.draggingFilesW=!1),a=!ICEcoder.draggingTab&&(top.ICEcoder.mouseX>top.ICEcoder.filesW-7&&top.ICEcoder.mouseXICEcoder.results.length-1&&(ICEcoder.findResult=0);e.innerHTML= +"Highlighted result "+(ICEcoder.findResult+1)+" of "+ICEcoder.results.length+" results";e=f.getSearchCursor(a,f.getCursor(),!0);e.findNext();e.from()||(e=f.getSearchCursor(a,{line:0,ch:0},!0),e.findNext());f.setSelection(e.from(),e.to());f.focus();top.ICEcoder.findMode=!0}return!0}e.innerHTML="No results";return!1}""!=a&&c&&(filesQS=e=f="","and"==document.findAndReplace.connector.value&&(f="&replace="+d),0<=document.findAndReplace.target.value.indexOf("file")&&(e="&target="+document.findAndReplace.target.value.replace(/ /g, +"-")),"selected files"==document.findAndReplace.target.value&&(filesQS="&selectedFiles="+top.ICEcoder.selectedFiles.join(":")),a=a.replace(/\'/g,"'"),a!=encodeURIComponent(a)?a="ICEcoder:"+encodeURIComponent(a):a,top.ICEcoder.showHide("show",top.get("loadingMask")),top.get("mediaContainer").innerHTML='')},replaceInFile:function(a,b,c){top.ICEcoder.serverQueue("add","lib/file-control.php?action=replaceText&fileRef="+ +a.replace(/\//g,"|")+"&find="+b+"&replace="+c);top.ICEcoder.serverMessage("Replacing text in
"+a)},getNestLocation:function(a){var b,c,d;if(b=ICEcoder.getcMInstance()){c=b.getValue();b=b.getTokenAt(b.getCursor()).state;if("undefined"!=typeof b.curState&&"undefined"!=typeof b.curState.htmlState)for(ICEcoder.htmlTagArray=[],d=b.curState.htmlState.context;d;d=d.prev)"undefined"!=typeof d.tagName&&ICEcoder.htmlTagArray.unshift(d.tagName);ICEcoder.tagString=ICEcoder.htmlTagArray[ICEcoder.htmlTagArray.length- +1];"JavaScript"==ICEcoder.caretLocType&&(ICEcoder.tagString="script");if(a&&!top.ICEcoder.dontUpdateNest&&(ICEcoder.nestDisplay.innerHTML="","undefined"!=typeof ICEcoder.openFiles[ICEcoder.selectedTab-1]&&(a=ICEcoder.openFiles[ICEcoder.selectedTab-1],0>["js","coffee","css","less"].indexOf(a.split(".")[1])&&-1==c.indexOf("include(")&&-1==c.indexOf("include_once(")&&(-1'),ICEcoder.nestDisplay.innerHTML+="'+ +ICEcoder.htmlTagArray[a]+"",ICEcoder.nestDisplay.innerHTML+=a':"
';"undefined"!=typeof b.curState&& +0'+(b.curState.tagName?b.curState.tagName:"content")+"")}}},updateNestingIndicator:function(){var a,b,c;a=ICEcoder.getcMInstance();b=!0;c=ICEcoder.openFiles[ICEcoder.selectedTab-1];a&&(c&&-1==["js","coffee","css","less"].indexOf(c.split(".")[1]))&&(b="error"!=a.getTokenAt({line:a.lineCount(),ch:a.lineInfo(a.lineCount()- +1).text.length}).className?!0:!1);ICEcoder.nestValid.style.background=b?"#0b0":"#f00";ICEcoder.nestValid.title=b?"Nesting OK":"Nesting Broken"},getCaretPosition:function(){var a,b,c,d;a=ICEcoder.getcMInstance();b=a.getValue();c=a.getCursor().line;a=a.getCursor().ch;for(var e=d=0;ea.lastIndexOf("\x3c/script>")&&"Unknown"==b?b="JavaScript":a.lastIndexOf("a.lastIndexOf("?>")&&"Unknown"==b?b="PHP":a.lastIndexOf("<%")>a.lastIndexOf("%>")&&"Unknown"==b?b="Ruby":a.lastIndexOf("<")>a.lastIndexOf(">")&&"Unknown"==b?b="HTML":"Unknown"==b&&(b="Content");(a=ICEcoder.openFiles[ICEcoder.selectedTab-1])&&(0c||19c||40c||36c||123=a||10<=a))&&top.ICEcoder.mouseX>parseInt(top.ICEcoder.files.style.width,10)&&(top.ICEcoder.tabDragMouseX=top.ICEcoder.mouseX-parseInt(top.ICEcoder.files.style.width,10)-top.ICEcoder.tabDragMouseXStart,top.ICEcoder.tabDragMove());if(top.ICEcoder.ready&&(a=window.innerWidth?window.innerHeight:document.body.clientHeight,top.ICEcoder.mouseDown|| +(top.ICEcoder.draggingFilesW=!1),a=!ICEcoder.draggingTab&&(top.ICEcoder.mouseX>top.ICEcoder.filesW-7&&top.ICEcoder.mouseXd.index+d[0].length;);(b=top.get("content").contentWindow.document.getElementById("cssColor"))&&b.parentNode.removeChild(b); top.ICEcoder.codeAssist&&"CSS"==top.ICEcoder.caretLocType&&(b=top.document.createElement("div"),b.id="cssColor",b.style.position="absolute",b.style.display="block",b.style.width=b.style.height="20px",b.style.zIndex="1000",b.style.background=d?d[0]:"",b.style.cursor="pointer",b.onclick=function(){top.ICEcoder.showColorPicker(d[0])},""==b.style.backgroundColor&&(b.style.display="none"),top.get("header").appendChild(b),a.addWidget(a.getCursor(),top.get("cssColor"),!0))},showColorPicker:function(a){top.get("blackMask").style.visibility= "visible";top.get("mediaContainer").innerHTML='



';