Commits

ryanackley committed da94a40

Refactored tab management, currently tracking down some bugs

  • Participants
  • Parent commits f3c2b69

Comments (0)

Files changed (3)

File ProjectManager.js

+ProjectManager = function(){
+	this.projectsByTab = {};
+	this.watchersByTab = {};
+}
+
+ProjectManager.prototype = {
+	launchFileSelect : function(tabId, typeIndex, sendResponse){
+		var projectsByTab = this.projectsByTab;
+		nativeFileSupport.launchFileSelect(function(path){
+			if (path && path.length){
+				var projectType = ProjectTypes[typeIndex];
+				window.requestFileSystem(window.PERMANENT, 5*1024*1024*1024, function(fs){
+					fs.root.getDirectory(path, {create:false}, function(dir){
+						projectType.createProject(dir,function(project, error){
+							currentProject = project;
+							projectsByTab[tabId] = currentProject;
+							sendResponse({path:path, error:error});
+						});
+					});
+				});
+			}
+		});
+	},
+	
+	checkResources : function(tabId, urls, sendResponse){
+		var currentProject = this.projectsByTab[tabId];
+		var retVal = [];
+		if (currentProject){
+			for (var i = 0; i < urls.length; i++){
+				if (urls[i].type == 'script' || urls[i].type == 'stylesheet'){
+					retVal.push(currentProject.isProjectUrl(urls[i].url));
+				}
+				else{
+					retVal.push(false);
+				}
+			}
+		}
+		sendResponse(retVal);
+	},
+	
+	checkResourceContent : function(tabId, url, content, sendResponse){
+		var currentProject = this.projectsByTab[tabId];
+		if (currentProject){
+			currentProject.matchContents(url, content, function(success, msg){
+				sendResponse({success:success, msg:msg});
+			});
+		}
+	},
+	
+	updateResource: function(tabId, url, content, sendResponse){
+		var currentProject = this.projectsByTab[tabId];
+		var currentWatcher = this.watchersByTab[tabId];
+		currentProject.commitResource(url, content, currentWatcher, sendResponse);
+	},
+	
+	resetProject : function(tabId, sendResponse){
+		var currentProject = this.projectsByTab[tabId];
+		currentProject.resetUrls();
+		sendResponse();
+	},
+	watchDirectory : function(tabId, path){
+		if (!this.watchersByTab[tabId]){
+			var currentProject = this.projectsByTab[tabId];
+			this.watchersByTab[tabId] = new FileWatcher(tabId, currentProject, path);
+		}
+	},
+	cleanUp : function(tabId){
+		if (this.projectsByTab[tabId])
+			delete this.projectsByTab[tabId];
+		  			
+		var watcher = this.watchersByTab[tabId];
+		if (watcher){
+			watcher.stopWatching();
+			delete this.watchersByTab[tabId];	
+		}
+	}
+}

File ResourceChecker.js

 var registerNavListener = function(){
 	if (!navListenerRegistered){
 		chrome.devtools.network.onNavigated.addListener(function(){
-			console.log('page navigated');
 			backgroundMsgSupport.pageChanged(function(){
 				checkResources();
 			});
 		});
 		chrome.devtools.inspectedWindow.onResourceAdded.addListener(function(resource){
-			console.log('resource added');
+			resourceCache.push(resource);
 			matchResourcesWithProject([resource]);
 		});
 	}

File background.html

 		<script src="zepto1.0rc1.js"></script>
 		<script src="RecentUpdateHandler.js"></script>
 		<script src="FileWatcher.js"></script>
+		<script src="ProjectManager.js"></script>
     	<embed type="application/x-npapifileioforchrome" id="pluginId" style="position:absolute; top:0px;left:-10000px;width:0px;height:0px;">
 		<script>
 		  window.nativeFileSupport = document.getElementById("pluginId");
 		</script>
 		<script src="FauxFilesystem.js"></script>
 		<script>
-		  
+		  var projectManager = new ProjectManager();
 		  chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
-		  		var currentProject = projectsByTab[sender.tab.id];
+		  		
 				if (request.key == 'ProjectTypes'){
 					//var cleanResponse = [];
 					//for (var i = 0; i < ProjectTypes.length){
 					//}
 					sendResponse(ProjectTypes);
 				}
-				else if (request.key == 'launchFileSelect')
-				{
-					nativeFileSupport.launchFileSelect(function(path){
-						if (path && path.length){
-							var projectType = ProjectTypes[request.index];
-							window.requestFileSystem(window.PERMANENT, 5*1024*1024*1024, function(fs){
-								fs.root.getDirectory(path, {create:false}, function(dir){
-									projectType.createProject(dir,function(project, error){
-										currentProject = project;
-										projectsByTab[sender.tab.id] = currentProject;
-										sendResponse({path:path, error:error});
-									});
-								});
-							});
-						}
-					});
+				else if (request.key == 'launchFileSelect'){
+					projectManager.launchFileSelect(sender.tab.id, request.index, sendResponse);
 				}
 				else if (request.key == 'checkResources'){
-					var urls = request.resources;
-					var retVal = [];
-					if (currentProject){
-						for (var i = 0; i < urls.length; i++){
-							if (urls[i].type == 'script' || urls[i].type == 'stylesheet'){
-								retVal.push(currentProject.isProjectUrl(urls[i].url));
-							}
-							else{
-								retVal.push(false);
-							}
-						}
-					}
-					sendResponse(retVal);
+					projectManager.checkResources(sender.tab.id, request.resources, sendResponse);
 				}
 				else if (request.key == 'checkResourceContent'){
-					var resources = request.content;
-					if (currentProject){
-						currentProject.matchContents(request.url, request.content, function(success, msg){
-							sendResponse({success:success, msg:msg});
-						});
-					}
+					projectManager.checkResourceContent(sender.tab.id, request.url, request.content, sendResponse);
 				}
 				else if (request.key == 'updateResource'){
-					var currentWatcher = watchersByTab[sender.tab.id];
-					currentProject.commitResource(request.url, request.content, currentWatcher,function(){
-						sendResponse();
-					});
+				    projectManager.updateResource(sender.tab.id, request.url, request.content, sendResponse);
 				}
 				else if (request.key == 'pageChanged'){
-					currentProject.resetUrls();
-					sendResponse();
+					projectManager.resetProject(sender.tab.id, sendResponse);
 				}
 				else if (request.key == 'watchDirectory'){
-					if (!watchersByTab[sender.tab.id]){
-						watchersByTab[sender.tab.id] = new FileWatcher(sender.tab.id, currentProject, request.path);
-					}
+					projectManager.watchDirectory(sender.tab.id, request.path);
 				}
 				else{
 					sendResponse({});
 		  });
 		  
 		  chrome.tabs.onRemoved.addListener(function(tabId, removeInfo) {
-		  		if (projectsByTab[tabId])
-		  			delete projectsByTab[tabId];
-		  			
-		  		var watcher = watchersByTab[tabId];
-		  		if (watcher){
-		  			watcher.stopWatching();
-		  			delete watchersByTab[tabId];	
-		  		}
+		  		projectManager.cleanUp(tabId);
 		  });
 		</script>