From 2912e351f5e8fab162d0ee738fe595fe6a682ab5 Mon Sep 17 00:00:00 2001 From: Matt Pass Date: Wed, 25 Sep 2013 10:06:47 +0100 Subject: [PATCH] savedPoints plus function removed and improved savedPoints now replaces changedContent It's role is also different - it contains the integer change num from CM on load/save/revert etc. We can compare this against the current change num provided by CM (changeGeneration) to see if content has changed Removed redoChangedContent function. This was only called once (by editor.php) on all keyup's and we used ropey key detection to set changedContent. As we're no longer storing changed doc values, we no longer need this, and as redoTabHighlight is already called on CM change (much more efficient BTW), we now don't need this function at all getcMInstance can now accept a tab number, 'new' string or nothing (both those last 2 use selectedTab) Wrap fMIconVis setting in an if condition so we don't try to set it when doc isn't there yet All instances of changedContent now savedPoints where it still remains --- lib/ice-coder.js | 51 +++++++++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 29 deletions(-) diff --git a/lib/ice-coder.js b/lib/ice-coder.js index 8b75e88..111cb0c 100644 --- a/lib/ice-coder.js +++ b/lib/ice-coder.js @@ -15,7 +15,7 @@ var ICEcoder = { minFilesW: 14, // Min width of files pane maxFilesW: 250, // Max width of files pane selectedTab: 0, // The tab that's currently selected - changedContent: [], // Binary array to indicate which tabs have changed + savedPoints: [], // Ints array to indicate save points for docs canSwitchTabs: true, // Stops switching of tabs when trying to close openFiles: [], // Array of open file URLs openFileMDTs: [], // Array of open file modification datetimes @@ -196,6 +196,7 @@ var ICEcoder = { // Then set the content in the editor & clear the history cM.setValue(content); cM.clearHistory(); + top.ICEcoder.savedPoints[top.ICEcoder.selectedTab-1] = cM.changeGeneration(); }, // Move current line up/down @@ -1344,32 +1345,21 @@ var ICEcoder = { ICEcoder.caretLocType = caretLocType; }, - // Alter array indicating which files have changed - redoChangedContent: function(evt) { - var cM, key; - - cM = ICEcoder.getcMInstance(); - key = evt.keyCode ? evt.keyCode : evt.which ? evt.which : evt.charCode; - // Exclude a few keys... - // Escape (27), Caps Lock (20), Shift, CTRL, Alt, Pause/Break (16-19), Left, Up, Right, Down (37-40), Num Lock, Scroll Lock (144-145), - // Insert, Delete (45,46), Page Up, Page Down, End, Home (33-36), Left Win Key, Right Win Key (91-92), F1-F12 (112-123) - if (!evt.ctrlKey && !top.ICEcoder.cmdKey && key!=27 && key!=20 && (key<16||key>19) && (key<37||key>40) && (key!=144||key!=145) && (key!=45||key!=46) && (key<33||key>36) && (key!=91||key!=92) && (key<112||key>123)) { - ICEcoder.changedContent[ICEcoder.selectedTab-1] = cM.historySize().undo > 0 ? 1 : 0; - ICEcoder.redoTabHighlight(ICEcoder.selectedTab); - } - }, - // Show & hide target element showHide: function(doVis,elem) { elem.style.visibility = doVis=="show" ? 'visible' : 'hidden'; }, - // Determine the CodeMirror instance we're using - getcMInstance: function(newTab) { + getcMInstance: function(tab) { return top.ICEcoder.content.contentWindow[ - newTab=="new"||(newTab!="new" && ICEcoder.openFiles.length>0) + // target specific tab + !isNaN(tab) + ? 'cM'+ICEcoder.cMInstances[tab-1] + // new tab or selected tab + : tab=="new"||(tab!="new" && ICEcoder.openFiles.length>0) ? 'cM'+ICEcoder.cMInstances[ICEcoder.selectedTab-1] + // fallback to first tab : 'cM1' ]; }, @@ -1951,7 +1941,7 @@ var ICEcoder = { top.ICEcoder.selectedTab=top.ICEcoder.openFiles.length; // Add a new value ready to indicate if this content has been changed - top.ICEcoder.changedContent.push(0); + top.ICEcoder.savedPoints.push(0); top.ICEcoder.setPreviousFiles(); }, @@ -1990,11 +1980,12 @@ var ICEcoder = { redoTabHighlight: function(selectedTab) { var tColor, fileLink; - for(var i=1;i<=ICEcoder.changedContent.length;i++) { + for(var i=1;i<=ICEcoder.savedPoints.length;i++) { if (top.get('tab'+i).childNodes[0]) { - top.get('tab'+i).childNodes[0].childNodes[0].style.backgroundColor = ICEcoder.changedContent[i-1]==1 + top.get('tab'+i).childNodes[0].childNodes[0].style.backgroundColor = ICEcoder.savedPoints[i-1]!=top.ICEcoder.getcMInstance(i).changeGeneration() ? "#b00" : "transparent"; } + tColor = i==selectedTab ? top.ICEcoder.tabFGselected : top.ICEcoder.tabFGnormalTab; 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,"|")); @@ -2006,7 +1997,9 @@ var ICEcoder = { top.get('tab'+i).style.color = tColor; top.get('tab'+i).style.background = i==selectedTab ? top.ICEcoder.tabBGcurrent : top.ICEcoder.tabBGopen; } - top.ICEcoder.fMIconVis('fMSave',ICEcoder.changedContent[selectedTab-1]==1 ? 1 : 0.3); + if (top.ICEcoder.getcMInstance(selectedTab)) { + top.ICEcoder.fMIconVis('fMSave',ICEcoder.savedPoints[selectedTab-1]!=top.ICEcoder.getcMInstance(selectedTab).changeGeneration() ? 1 : 0.3); + } }, // Close the tab upon request @@ -2018,7 +2011,7 @@ var ICEcoder = { cM = ICEcoder.getcMInstance(); okToRemove = true; - if (!dontAsk && ICEcoder.changedContent[closeTabNum-1]==1) { + if (!dontAsk && ICEcoder.savedPoints[closeTabNum-1]!=top.ICEcoder.getcMInstance(closeTabNum).changeGeneration()) { okToRemove = top.ICEcoder.ask('You have made changes.\n\nAre you sure you want to close without saving?'); } @@ -2057,7 +2050,7 @@ var ICEcoder = { ICEcoder.switchTab(ICEcoder.selectedTab); } // Highlight the selected tab after splicing the change state out of the array - top.ICEcoder.changedContent.splice(closeTabNum-1,1); + top.ICEcoder.savedPoints.splice(closeTabNum-1,1); top.ICEcoder.redoTabHighlight(ICEcoder.selectedTab); // Update the nesting indicator @@ -2202,11 +2195,11 @@ var ICEcoder = { // Sort tabs into new order sortTabs: function(newOrder) { - var a, b, changedContent = [], openFiles = [], openFileMDTs = [], cMInstances = [], selectedTabWillBe; + var a, b, savedPoints = [], openFiles = [], openFileMDTs = [], cMInstances = [], selectedTabWillBe; // Setup an array of our actual arrays and the blank ones - a = [ICEcoder.changedContent, ICEcoder.openFiles, ICEcoder.openFileMDTs, ICEcoder.cMInstances]; - b = [changedContent, openFiles, openFileMDTs, cMInstances]; + a = [ICEcoder.savedPoints, ICEcoder.openFiles, ICEcoder.openFileMDTs, ICEcoder.cMInstances]; + b = [savedPoints, openFiles, openFileMDTs, cMInstances]; // Push the new order values into array b then set into array a for (var i=0;i