From d67c25aab5e71cfa79c1d9d8fe7dc3221020590a Mon Sep 17 00:00:00 2001 From: Matt Pass Date: Sun, 29 Jul 2012 13:51:39 +0100 Subject: [PATCH] Ternary shortening & other optimisation Ternary statements shortened where possible Account now has it's width set too when setting layout fmLock now moved into another DIV container, adjust marginLeft value No need to add last argument to substr as we want up to the end Few hex codes shortened --- lib/coder.js | 161 ++++++++++++++++++++++----------------------------- 1 file changed, 69 insertions(+), 92 deletions(-) diff --git a/lib/coder.js b/lib/coder.js index 97d2011..725acd3 100644 --- a/lib/coder.js +++ b/lib/coder.js @@ -73,15 +73,15 @@ var ICEcoder = { var winW, winH, headerH, footerH, accountH, tabsBarH, findBarH, cMCSS; // Determin width & height available - window.innerWidth ? winW = window.innerWidth : winW = document.body.clientWidth; - window.innerHeight ? winH = window.innerHeight : winH = document.body.clientHeight; + winW = window.innerWidth ? window.innerWidth : document.body.clientWidth; + winH = window.innerHeight ? window.innerHeight : document.body.clientHeight; // Apply sizes to various elements of the page headerH = 40, footerH = 30, accountH = 50, tabsBarH = 21, findBarH = 28; this.header.style.width = this.tabsBar.style.width = this.findBar.style.width = winW + "px"; - this.files.style.width = this.accountLogin.style.width = this.editor.style.left = this.filesW + "px"; + this.files.style.width = this.account.style.width = this.accountLogin.style.width = this.editor.style.left = this.filesW + "px"; this.account.style.height = this.accountH + "px"; - this.fmLock.style.marginLeft = (this.filesW-27) + "px"; + this.fmLock.style.marginLeft = (this.filesW-42) + "px"; this.filesFrame.style.height = (winH-headerH-accountH-footerH) + "px"; // If we need to set the editor sizes @@ -91,13 +91,12 @@ var ICEcoder = { // Resize the CodeMirror instances to match the window size cMCSS = ICEcoder.content.contentWindow.document.styleSheets[2]; - cMCSS.rules ? strCSS = 'rules' : strCSS = 'cssRules'; + strCSS = cMCSS.rules ? 'rules' : 'cssRules'; for(var i=0;i")[0].split("\n")[0]; if (tagStart=="script"||tagStart=="?php"||tagStart=="?"||tagStart=="%") {ICEcoder.codeBlock=true} - if (tagStart!="") {ICEcoder.tagStart = tagStart} + if (tagStart!="") {ICEcoder.tagStart=tagStart} }; // If we find a > tag and we're within a tag or codeblock @@ -162,12 +161,12 @@ var ICEcoder = { if (canDoTheEndTag) { // Get this end tag name tagEnd=nestCheck.substr(0,i); - tagEndJS=tagEnd.substr(tagEnd.lastIndexOf('<'),tagEnd.length); - 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); + tagEndJS=tagEnd.substr(tagEnd.lastIndexOf('<')); + tagEnd=tagEnd.substr(tagEnd.lastIndexOf('<')+1); + tagEnd=tagEnd.substr(tagEnd.lastIndexOf(' ')+1); + tagEnd=tagEnd.substr(tagEnd.lastIndexOf('\t')+1); + tagEnd=tagEnd.substr(tagEnd.lastIndexOf('\n')+1); + tagEnd=tagEnd.substr(tagEnd.lastIndexOf(';')+1); if (!ICEcoder.codeBlock) { // OK, we can do something further as we're not in a code block @@ -295,9 +294,9 @@ var ICEcoder = { key = evt.keyCode ? evt.keyCode : evt.which ? evt.which : evt.charCode; - if (key==17) {top.ICEcoder.ctrlKeyDown = false; top.ICEcoder.stickyTabMaybe = false;} - if (key==16) {top.ICEcoder.shiftKeyDown = false;} - if (key==46) {top.ICEcoder.delKeyDown = false;} + if (key==17) {top.ICEcoder.ctrlKeyDown = false; top.ICEcoder.stickyTabMaybe = false} + if (key==16) {top.ICEcoder.shiftKeyDown = false} + if (key==46) {top.ICEcoder.delKeyDown = false} }, // Set the width of the file manager on demand @@ -329,13 +328,8 @@ var ICEcoder = { if (top.document.body.style.cursor == "w-resize") { // If our mouse is down and we're within a 250-400px range if (top.ICEcoder.mouseDown) { - if (top.ICEcoder.mouseX >=250 && top.ICEcoder.mouseX <= 400) { - top.ICEcoder.filesW = top.ICEcoder.maxFilesW = top.ICEcoder.mouseX; - } else if (top.ICEcoder.mouseX <250) { - top.ICEcoder.filesW = top.ICEcoder.maxFilesW = 250; - } else { - top.ICEcoder.filesW = top.ICEcoder.maxFilesW = 400; - } + top.ICEcoder.filesW = top.ICEcoder.maxFilesW = top.ICEcoder.mouseX >=250 && top.ICEcoder.mouseX <= 400 + ? top.ICEcoder.mouseX : top.ICEcoder.mouseX <250 ? 250 : 400; // Set various widths based on the new width top.ICEcoder.files.style.width = top.ICEcoder.account.style.width = top.ICEcoder.filesFrame.style.width = top.ICEcoder.filesW + "px"; top.ICEcoder.setLayout(); @@ -394,13 +388,13 @@ var ICEcoder = { i==selectedTab ? tColor = "#000" : tColor = "#fff"; if ("undefined" != typeof top.ICEcoder.openFiles[i-1] && top.ICEcoder.openFiles[i-1] != "/[NEW]") { fileLink = top.ICEcoder.filesFrame.contentWindow.document.getElementById(top.ICEcoder.openFiles[i-1].replace(/\//g,"|")); - if (fileLink) {i==selectedTab ? fileLink.style.backgroundColor = "#4499dd" : fileLink.style.backgroundColor = "rgba(255,255,255,0.15)";}; + if (fileLink) {i==selectedTab ? fileLink.style.backgroundColor = "#49d" : fileLink.style.backgroundColor = "rgba(255,255,255,0.15)";}; } document.getElementById('tab'+i).style.color = tColor; i==selectedTab ? bgVPos = -22 : bgVPos = 0; document.getElementById('tab'+i).style.backgroundPosition = "0 "+bgVPos+"px"; } - ICEcoder.changedContent[selectedTab-1]==1 ? top.ICEcoder.fMIconVis('fMSave',1) : top.ICEcoder.fMIconVis('fMSave',0.3); + top.ICEcoder.fMIconVis('fMSave',ICEcoder.changedContent[selectedTab-1]==1 ? 1 : 0.3); }, // Starts a new file by setting a few vars & creating a new cM instance @@ -459,20 +453,19 @@ var ICEcoder = { // Indicate if the nesting structure of the code is OK updateNestingIndicator: function () { - var cM, fileName; + var cM, fileName, nestOK; cM = ICEcoder.getcMInstance(); fileName = ICEcoder.openFiles[ICEcoder.selectedTab-1]; ICEcoder.caretPos=cM.getValue().length; ICEcoder.getNestLocation(); + nestOK = false; // Nesting is OK if at the end of the file we have no nests left, or it's a JS, Ruby or CSS file if (ICEcoder.htmlTagArray.length==0||fileName.indexOf(".js")>0||fileName.indexOf(".coffee")>0||fileName.indexOf(".rb")>0||fileName.indexOf(".css")>0||fileName.indexOf(".less")>0) { - ICEcoder.nestValid.style.backgroundColor="#0b0"; - ICEcoder.nestValid.innerHTML = "Nesting OK"; - } else { - ICEcoder.nestValid.style.backgroundColor="#f00"; - ICEcoder.nestValid.innerHTML = "Nesting Broken"; + nestOK = true; } + ICEcoder.nestValid.style.backgroundColor = nestOK ? "#0b0" : "#f00"; + ICEcoder.nestValid.innerHTML = nestOK ? "Nesting OK" : "Nesting Broken"; }, // Get the caret position on demand @@ -524,7 +517,7 @@ var ICEcoder = { ICEcoder.caretLocType = caretLocType; // If we're in a JS, CoffeeScript PHP or Ruby code block, add that to the nest display - if (caretLocType=="JavaScript"||caretLocType=="CoffeeScript"||caretLocType=="PHP"||caretLocType=="Ruby") { + if (["JavaScript","CoffeeScript","PHP","Ruby"].indexOf(caretLocType)) { ICEcoder.nestDisplay.innerHTML += " > " + caretLocType; } }, @@ -609,7 +602,7 @@ var ICEcoder = { if (node && node.tagName=="UL") { var d=(node.style.display=="none"); node.style.display=(d) ? "block" : "none"; - this.className=(d) ? this.parentNode.className="pft-directory dirOpen" : this.parentNode.className="pft-directory"; + this.parentNode.className=this.className=(d) ? "pft-directory dirOpen" : "pft-directory"; } return false; } @@ -687,19 +680,15 @@ var ICEcoder = { } } // Adjust the file & replace select values depending on if we have files selected - if (!top.ICEcoder.selectedFiles[0]) { - document.findAndReplace.target[2].innerHTML = "all files"; - document.findAndReplace.target[3].innerHTML = "all filenames"; - } else { - document.findAndReplace.target[2].innerHTML = "selected files"; - document.findAndReplace.target[3].innerHTML = "selected filenames"; - } + document.findAndReplace.target[2].innerHTML = !top.ICEcoder.selectedFiles[0] ? "all files" : "selected files"; + document.findAndReplace.target[3].innerHTML = !top.ICEcoder.selectedFiles[0] ? "all filenames" : "selected filenames"; + // Finally, show or grey out the relevant file manager icons - top.ICEcoder.selectedFiles.length == 1 ? top.ICEcoder.fMIconVis('fMOpen',1) : top.ICEcoder.fMIconVis('fMOpen',0.3); - top.ICEcoder.selectedFiles.length == 1 && top.ICEcoder.thisFileFolderType == "folder" ? top.ICEcoder.fMIconVis('fMNewFile',1) : top.ICEcoder.fMIconVis('fMNewFile',0.3); - top.ICEcoder.selectedFiles.length == 1 && top.ICEcoder.thisFileFolderType == "folder" ? top.ICEcoder.fMIconVis('fMNewFolder',1) : top.ICEcoder.fMIconVis('fMNewFolder',0.3); - top.ICEcoder.selectedFiles.length > 0 ? top.ICEcoder.fMIconVis('fMDelete',1) : top.ICEcoder.fMIconVis('fMDelete',0.3); - top.ICEcoder.selectedFiles.length == 1 ? top.ICEcoder.fMIconVis('fMRename',1) : top.ICEcoder.fMIconVis('fMRename',0.3); + top.ICEcoder.fMIconVis('fMOpen',top.ICEcoder.selectedFiles.length == 1 ? 1 : 0.3); + top.ICEcoder.fMIconVis('fMNewFile',top.ICEcoder.selectedFiles.length == 1 && top.ICEcoder.thisFileFolderType == "folder" ? 1 : 0.3); + top.ICEcoder.fMIconVis('fMNewFolder',top.ICEcoder.selectedFiles.length == 1 && top.ICEcoder.thisFileFolderType == "folder" ? 1 : 0.3); + top.ICEcoder.fMIconVis('fMDelete',top.ICEcoder.selectedFiles.length > 0 ? 1 : 0.3); + top.ICEcoder.fMIconVis('fMRename',top.ICEcoder.selectedFiles.length == 1 ? 1 : 0.3); // Hide the file menu incase it's showing top.document.getElementById('fileMenu').style.display = "none"; }, @@ -708,18 +697,18 @@ var ICEcoder = { selectDeselectFile: function(action,file) { var isOpen; - top.ICEcoder.openFiles.indexOf(file.id.replace(/\|/g,"/")) > -1 ? isOpen = true : isOpen = false; + isOpen = top.ICEcoder.openFiles.indexOf(file.id.replace(/\|/g,"/")) > -1 ? true : false; if (top.ICEcoder.openFiles[top.ICEcoder.selectedTab-1] == file.id.replace(/\|/g,"/")) { - file.style.backgroundColor="#4499dd"; + file.style.backgroundColor="#49d"; } else { if (action=="select") { file.style.backgroundColor="#888"; } else { - isOpen ? file.style.backgroundColor = "rgba(255,255,255,0.15)" : file.style.backgroundColor="transparent"; + file.style.backgroundColor = isOpen ? "rgba(255,255,255,0.15)" : "transparent"; } } - action == "select" ? file.style.color="#fff" : file.style.color="#eee"; + file.style.color= action=="select" ? "#fff" : "#eee"; }, // Create a new file (start & instant save) @@ -794,7 +783,7 @@ var ICEcoder = { saveFile: function(saveAs) { var saveType; - saveAs ? saveType = "saveAs" : saveType = "save"; + saveType = saveAs ? "saveAs" : "save"; top.ICEcoder.serverQueue("add","lib/file-control.php?action=save&file="+ICEcoder.openFiles[ICEcoder.selectedTab-1].replace(/\//g,"|")+"&fileMDT="+ICEcoder.openFileMDTs[ICEcoder.selectedTab-1]+"&saveType="+saveType); top.ICEcoder.serverMessage('Saving
'+ICEcoder.openFiles[ICEcoder.selectedTab-1]); @@ -859,10 +848,10 @@ var ICEcoder = { if (!foundFile) {top.ICEcoder.selectFileFolder()}; if ("undefined" != typeof top.ICEcoder.thisFileFolderLink && top.ICEcoder.thisFileFolderLink!="") { - top.ICEcoder.selectedFiles[0].indexOf(".")>-1 ? menuType = "file" : menuType = "folder"; + menuType = top.ICEcoder.selectedFiles[0].indexOf(".")>-1 ? "file" : "folder"; folderMenuItems = top.document.getElementById('folderMenuItems'); - menuType == "folder" && top.ICEcoder.selectedFiles.length == 1 ? folderMenuItems.style.display = "block" : folderMenuItems.style.display = "none"; - top.ICEcoder.selectedFiles.length > 1 ? singleFileMenuItems.style.display = "none" : singleFileMenuItems.style.display = "block"; + folderMenuItems.style.display = menuType == "folder" && top.ICEcoder.selectedFiles.length == 1 ? "block" : "none"; + singleFileMenuItems.style.display = top.ICEcoder.selectedFiles.length > 1 ? "none" : "block"; 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+10) + "px"; @@ -872,18 +861,16 @@ var ICEcoder = { // Show & hide target element showHide: function(doVis,elem) { - doVis=="show" ? elem.style.visibility='visible' : elem.style.visibility='hidden'; + elem.style.visibility = doVis=="show" ? 'visible' : 'hidden'; }, // Update find & replace options based on user selection findReplaceOptions: function() { - var rText, replace, rTarget; - - rText = document.getElementById('rText').style.display; - replace = document.getElementById('replace').style.display; - rTarget = document.getElementById('rTarget').style.display; - - document.findAndReplace.connector.value=="and" ? document.getElementById('rText').style.display = document.getElementById('replace').style.display = document.getElementById('rTarget').style.display = "inline-block" : document.getElementById('rText').style.display = document.getElementById('replace').style.display = document.getElementById('rTarget').style.display = "none"; + top.document.getElementById('rText').style.display = + top.document.getElementById('replace').style.display = + top.document.getElementById('rTarget').style.display = + document.findAndReplace.connector.value=="and" + ? "inline-block" : "none"; }, // Find & replace text according to user selections @@ -968,11 +955,9 @@ var ICEcoder = { startPos.ch = charCount; endPos = new Object(); endPos.line = lineCount; - if (document.findAndReplace.connector.value=="and" && document.findAndReplace.replaceAction.value=="replace all" && buttonClick) { - endPos.ch = charCount+replaceLen; - } else { - endPos.ch = charCount+findLen; - } + document.findAndReplace.connector.value=="and" && document.findAndReplace.replaceAction.value=="replace all" && buttonClick + ? endPos.ch = charCount+replaceLen + : endPos.ch = charCount+findLen; // Finally, highlight our selection cM = ICEcoder.getcMInstance(); @@ -1037,8 +1022,8 @@ var ICEcoder = { var lockIcon; lockIcon = top.document.getElementById('fmLock'); - ICEcoder.lockedNav ? ICEcoder.lockedNav = false : ICEcoder.lockedNav = true; - ICEcoder.lockedNav ? lockIcon.src="images/padlock.png" : lockIcon.src="images/padlock-disabled.png"; + ICEcoder.lockedNav = ICEcoder.lockedNav ? false : true; + lockIcon.style.backgroundPosition = ICEcoder.lockedNav ? "-64px -16px" : "-80px -16px"; }, // Determine the CodeMirror instance we're using on demand @@ -1127,17 +1112,11 @@ var ICEcoder = { // Get the mouse position on demand getMouseXY: function(e,area) { - var tempX, tempY, scrollTop, IE; + var tempX, tempY, scrollTop; - IE = !e.pageX ? true : false; - if (IE) { - top.ICEcoder.mouseX = e.clientX + document.body.scrollLeft; - top.ICEcoder.mouseY = e.clientY + document.body.scrollTop; - } else { - document.captureEvents(Event.MOUSEMOVE); - top.ICEcoder.mouseX = e.pageX; - top.ICEcoder.mouseY = e.pageY; - } + top.ICEcoder.mouseX = e.pageX ? e.pageX : e.clientX + document.body.scrollLeft; + top.ICEcoder.mouseY = e.pageY ? e.pageY : e.clientY + document.body.scrollTop; + if (area!="top") { top.ICEcoder.mouseY += 40 + 50; } @@ -1152,14 +1131,12 @@ var ICEcoder = { var winH, cursorName; if (top.ICEcoder.ready) { - window.innerWidth ? winH = window.innerHeight : winH = document.body.clientHeight; + winH = window.innerWidth ? window.innerHeight : document.body.clientHeight; if (!top.ICEcoder.mouseDown) {top.ICEcoder.draggingFilesW = false}; - if ((top.ICEcoder.mouseX > top.ICEcoder.filesW-7 && top.ICEcoder.mouseX < top.ICEcoder.filesW+7 && top.ICEcoder.mouseY > 40 && top.ICEcoder.mouseY < (winH-30)) || top.ICEcoder.draggingFilesW) { - cursorName = "w-resize"; - } else { - cursorName = "auto"; - } + cursorName = ((top.ICEcoder.mouseX > top.ICEcoder.filesW-7 && top.ICEcoder.mouseX < top.ICEcoder.filesW+7 && top.ICEcoder.mouseY > 40 && top.ICEcoder.mouseY < (winH-30)) || top.ICEcoder.draggingFilesW) + ? "w-resize" + : "auto"; if (top.ICEcoder.content.contentWindow.document && top.ICEcoder.filesFrame.contentWindow) { top.document.body.style.cursor = cursorName; if (top.ICEcoder.content.contentWindow.document.body) {top.ICEcoder.content.contentWindow.document.body.style.cursor = cursorName}; @@ -1263,7 +1240,7 @@ var ICEcoder = { var cM; cM = ICEcoder.getcMInstance(); - top.ICEcoder.codeAssist ? top.ICEcoder.codeAssist = false : top.ICEcoder.codeAssist = true; + top.ICEcoder.codeAssist = top.ICEcoder.codeAssist ? false : true; top.ICEcoder.cssColorPreview(); cM.focus(); }, @@ -1274,7 +1251,7 @@ var ICEcoder = { serverMessage = document.getElementById('serverMessage'); if (message) {serverMessage.innerHTML = message}; - message ? serverMessage.style.opacity = 1 : serverMessage.style.opacity = 0; + serverMessage.style.opacity = message ? 1 : 0; }, // Queue items up for processing in turn @@ -1554,8 +1531,8 @@ var ICEcoder = { } cMCSS = ICEcoder.content.contentWindow.document.styleSheets[2]; - cMCSS.rules ? strCSS = 'rules' : strCSS = 'cssRules'; - visibleTabs ? cMCSS[strCSS][5].style['content'] = '"\\21e5"' : cMCSS[strCSS][5].style['content'] = '" "'; + strCSS = cMCSS.rules ? 'rules' : 'cssRules'; + cMCSS[strCSS][5].style['content'] = visibleTabs ? '"\\21e5"' : '" "'; top.tabWidth = tabWidth; for (var i=0;i 1 ? dirText = "folders" : dirText = "folder"; - top.ICEcoder.fileCount > 1 ? fileText = "files" : fileText = "file"; + dirText = top.ICEcoder.dirCount > 1 ? "folders" : "folder"; + fileText = top.ICEcoder.fileCount > 1 ? "files" : "file"; // Change into kilobytes unitSize = Math.ceil(top.ICEcoder.fileBytes/1024); unitText = "kb"; @@ -1611,7 +1588,7 @@ var ICEcoder = { document.webkitIsFullScreen ? document.webkitCancelFullScreen() : document.body.webkitRequestFullScreen(); } - screenIcon.src.indexOf("images/full-screen.gif") > -1 ? screenIcon.src = "images/restored-screen.gif" : screenIcon.src = "images/full-screen.gif"; + screenIcon.src = screenIcon.src.indexOf("images/full-screen.gif") > -1 ? "images/restored-screen.gif" : "images/full-screen.gif"; },