Commits

ryanackley committed baf481e

initial implementation of saving config to localstorage and file urls

Comments (0)

Files changed (8)

AtlassianPluginProject.js

 ProjectTypes.push(
     {
 		name: 'Atlassian Plugin',
+		key: 'atlassian.plugin',
 		locationType : 'local',
 		createProject : function(root, callback){
 			var project = new AtlassianPluginProject();

BackgroundMsgSupport.js

 		chrome.extension.sendRequest({key: 'watchDirectory', path : fullPath}, function(response){
 			callback(response);
 		});
+	},
+	loadProject : function(type, path, callback){
+		chrome.extension.sendRequest({key: 'loadProject', type: type, path: path}, function(response){
+			callback(response);
+		});
 	}
 }
 		this.projectUrls = {};
 	}
 }
+
+FileUrlProjectFactory = {
+	createProject : function(dir, callback){
+		callback(new FileUrlProject());
+	}
+}
 }
 
 ProjectManager.prototype = {
+    _initProject : function(tabId, path, projectType, sendResponse){
+    	var self = this;
+    	var projectsByTab = this.projectsByTab;
+    	this.cleanUp(tabId);
+    	window.requestFileSystem(window.PERMANENT, 5*1024*1024*1024, function(fs){
+			fs.root.getDirectory(path, {create:false}, function(dir){
+				projectType.createProject(dir,function(project, error){
+					self.fsRoot = fs.root;
+					project.matchedResourceMap = {};
+					projectsByTab[tabId] = project;
+					sendResponse({path:path, error:error});
+				});
+			});
+		});
+    },
 	launchFileSelect : function(tabId, typeIndex, sendResponse){
-		var projectsByTab = this.projectsByTab;
 		var self = this;
 		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){
-							self.fsRoot = fs.root;
-							project.matchedResourceMap = {};
-							projectsByTab[tabId] = project;
-							sendResponse({path:path, error:error});
-						});
-					});
-				});
+				self._initProject(tabId, path, projectType, sendResponse);
 			}
 		});
 	},
 			sendResponse();
 		}
 	},
-	
+	loadProject : function(tabId, projectTypeKey, path, sendResponse){
+		if (projectTypeKey == 'fileUrl'){
+			this._initProject(tabId, path, FileUrlProjectFactory, sendResponse);
+		}
+		else{		
+			for (var i = 0; i < ProjectTypes.length; i++){
+				if (ProjectTypes[i].key === projectTypeKey){
+					this._initProject(tabId, path, ProjectTypes[i], sendResponse);
+					break;
+				}
+			}
+		}
+	},
 	resetProject : function(tabId, sendResponse){
 		var currentProject = this.projectsByTab[tabId];
 		currentProject.resetUrls();
     	</script>
     	<script src="file_utils.js"></script>
 		<script src="AtlassianPluginProject.js"></script>
+		<script src="FileUrlProject.js"></script>
 		<script src="zepto1.0rc1.js"></script>
 		<script src="RecentUpdateHandler.js"></script>
 		<script src="FileWatcher.js"></script>
 				else if (request.key == 'watchDirectory'){
 					projectManager.watchDirectory(sender.tab.id, request.path);
 				}
+				else if (request.key == 'loadProject'){
+					projectManager.loadProject(sender.tab.id, request.type, request.path, sendResponse);
+				}
 				else{
 					sendResponse({});
 				}
 <html>
-<body>
+<head>
+<script src="zepto1.0rc1.js"></script>
+<script src="BackgroundMsgSupport.js"></script>
+<script src="Logger.js"></script>
+<script src="RecentUpdateHandler.js"></script>
+<script src="ResourceChecker.js"></script>
 <script src="devtools.js"></script>
+</head>
+<body>
 </body>
 </html>
-chrome.devtools.panels.create('Local Project', 'icon.png', 'editorpanel.html');
+// Most of the logic happens in devtools.html since it loads right away
+var projectState = {};
+var inspectedLocation;
+
+var loadProject = function(type, path){
+	checkResources();
+	registerNavListener();
+	backgroundMsgSupport.watchDirectory(path, function(){});
+	
+	projectState = {type: type, path: path};
+	if (type != 'fileUrl'){
+		localStorage[inspectedLocation.origin] = JSON.stringify(projectState);
+	}
+};
+
+var checkLocation = function(){
+	chrome.devtools.inspectedWindow.eval('this.location', function(location, isException){
+		if (inspectedLocation && location.origin === inspectedLocation.origin){
+			return;
+		}
+		inspectedLocation = location;
+		// file protocol is a special case
+		var type,path;
+		if (location.protocol == 'file:'){
+			var pathElements = location.pathname.split("/");
+			type = 'fileUrl';
+			path = '';
+			for (var i = 1; i < pathElements.length - 1; i++){
+				path = path + '/' + pathElements[i];
+			}
+					
+		}
+		else{
+			var projectStateStr = localStorage[location.origin];
+			if (projectStateStr){
+				var temp = JSON.parse(projectStateStr);
+				if (temp.type != 'fileUrl'){
+					type = temp.type;
+					path = temp.path;
+				}
+			}
+		}
+		if (type && path && projectState.path != path){
+			backgroundMsgSupport.loadProject(type, path, function(){
+				loadProject(type, path);
+			});
+		}
+	});
+}
+checkLocation();
+
+chrome.devtools.panels.create('Local Project', 'icon.png', 'editorpanel.html', function(panel){
+	var windowInjector = function(panelWindow){
+		window.editorPanelWindow = panelWindow;
+		panelWindow.devtoolsWindow = window;
+		panelWindow.initUI();
+		panel.onShown.removeListener(windowInjector);
+	};
+	panel.onShown.addListener(windowInjector);
+});
 	<script src="zepto1.0rc1.js"></script>
 	<script src="BackgroundMsgSupport.js"></script>
 	<script src="Logger.js"></script>
-	<script src="RecentUpdateHandler.js"></script>
-	<script src="ResourceChecker.js"></script>
 	<script>
-		$(document).ready(function(){
-			console.log('ready');
+		
+		var checkProjectState = function(){
+			var projectState = window.devtoolsWindow.projectState,
+				disabled = 'false';
+			if (projectState.type == 'fileUrl'){
+				disabled = 'true';
+			}
+			$('#project-type').attr('disabled', disabled);
+			$('#native-browse').attr('disabled', disabled);
+		}
+		
+		var initUI = function(){
+			checkProjectState();
 			var typeSelect = document.getElementById('project-type');
 			
 			backgroundMsgSupport.getProjectTypes(function(projectTypes){
 				if (projectType.locationType = 'local'){
 					var fileSelect = document.getElementById('file-select');
 					fileSelect.style.display = '';
-					
-					var $fileSelect = $(fileSelect);
-					$fileSelect.off('file-change');
-					$fileSelect.on('file-change', function(e, filePath){
-						requestFs(function(fs){
-							fs.root.getDirectory(filePath,{create:false}, function(dir){
-								projectType.load(dir);
-							});
-						});
-					});
 				}
 			});
 			$('#native-browse').on('click', function(e){
 							logError(result.error);
 						}
 						else{
-							checkResources();
-							registerNavListener();
-							backgroundMsgSupport.watchDirectory(result.path, function(){});
+							var projectType = projectTypes[index];
+							window.devtoolsWindow.loadProject(projectType.key, result.path);
 						}
 					}
 				});
 			});
-		});
+		};
 	</script>
 </head>
 <body>