forked from cory/tildefriends
		
	Show service processes differently on the index. Expose manifests to package list. Add a work in process library of shared user interface code.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3193 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
		| @@ -4,16 +4,25 @@ core.register("onSessionBegin", index); | ||||
| core.register("onSessionEnd", index); | ||||
|  | ||||
| function index() { | ||||
| 	Promise.all([core.getPackages(), core.getUsers()]).then(function(values) { | ||||
| 	return Promise.all([core.getPackages(), core.getUsers()]).then(function(values) { | ||||
| 		let packages = values[0]; | ||||
| 		let users = values[1]; | ||||
| 		let usersByApp = {}; | ||||
| 		let servicesByApp = {}; | ||||
| 		for (let i in users) { | ||||
| 			let user = users[i]; | ||||
| 			if (!usersByApp["/~" + user.packageOwner + "/" + user.packageName]) { | ||||
| 				usersByApp["/~" + user.packageOwner + "/" + user.packageName] = []; | ||||
| 			let key = "/~" + user.packageOwner + "/" + user.packageName; | ||||
| 			if (user.key.substring(0, "service_".length) == "service_") { | ||||
| 				if (!servicesByApp[key]) { | ||||
| 					servicesByApp[key] = []; | ||||
| 				} | ||||
| 				servicesByApp[key].push(user); | ||||
| 			} else { | ||||
| 				if (!usersByApp[key]) { | ||||
| 					usersByApp[key] = []; | ||||
| 				} | ||||
| 				usersByApp[key].push(user.name); | ||||
| 			} | ||||
| 			usersByApp["/~" + user.packageOwner + "/" + user.packageName].push(user.name); | ||||
| 		} | ||||
|  | ||||
| 		terminal.cork(); | ||||
| @@ -23,23 +32,32 @@ function index() { | ||||
| 			return Math.sign(x.owner.localeCompare(y.owner)) * 10 + Math.sign(x.name.localeCompare(y.name)) * 1; | ||||
| 		}).forEach(function(app) { | ||||
| 			let users = usersByApp["/~" + app.owner + "/" + app.name]; | ||||
| 			let services = servicesByApp["/~" + app.owner + "/" + app.name]; | ||||
| 			let message = []; | ||||
| 			if (users) { | ||||
| 			if (users || services) { | ||||
| 				message.push(" ["); | ||||
| 				let counts = {}; | ||||
| 				for (let i = 0; i < users.length; i++) { | ||||
| 					counts[users[i]] = (counts[users[i]] || 0) + 1; | ||||
| 				if (users) { | ||||
| 					let counts = {}; | ||||
| 					for (let i = 0; i < users.length; i++) { | ||||
| 						counts[users[i]] = (counts[users[i]] || 0) + 1; | ||||
| 					} | ||||
| 					let names = Object.keys(counts).sort(); | ||||
| 					for (let i = 0; i < names.length; i++) { | ||||
| 						var name = names[i]; | ||||
| 						if (message.length > 1) { | ||||
| 							message.push(", "); | ||||
| 						} | ||||
| 						message.push({class: "orange", value: name}); | ||||
| 						if (counts[name] > 1) { | ||||
| 							message.push({class: "base01", value: "(x" + counts[name] + ")"}); | ||||
| 						} | ||||
| 					} | ||||
| 				} | ||||
| 				let names = Object.keys(counts).sort(); | ||||
| 				for (let i = 0; i < names.length; i++) { | ||||
| 					var name = names[i]; | ||||
| 					if (message.length > 1) { | ||||
| 				if (services) { | ||||
| 					if (users) { | ||||
| 						message.push(", "); | ||||
| 					} | ||||
| 					message.push({class: "orange", value: name}); | ||||
| 					if (counts[name] > 1) { | ||||
| 						message.push({class: "base01", value: "(x" + counts[name] + ")"}); | ||||
| 					} | ||||
| 					message.push("⚒".repeat(services.length)); | ||||
| 				} | ||||
| 				message.push("]"); | ||||
| 			} | ||||
| @@ -52,4 +70,6 @@ function index() { | ||||
| 	}); | ||||
| } | ||||
|  | ||||
| index(); | ||||
| index().catch(function(error) { | ||||
| 	terminal.print("ERROR:", error); | ||||
| }); | ||||
							
								
								
									
										91
									
								
								packages/cory/ui/ui.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										91
									
								
								packages/cory/ui/ui.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,91 @@ | ||||
| "use strict"; | ||||
|  | ||||
| function fileList(title, prefix, editCallback) { | ||||
| 	terminal.setEcho(false); | ||||
| 	terminal.clear(); | ||||
| 	terminal.print(title); | ||||
| 	if (core.user.credentials.permissions.authenticated) { | ||||
| 		terminal.print({command: "new"}); | ||||
| 	} | ||||
|  | ||||
| 	let makeSaveCallback = function(oldName, oldValue) { | ||||
| 		return function(newName, newValue) { | ||||
| 			print(newName, " ", newValue); | ||||
| 			return database.set(prefix + newName, newValue); | ||||
| 		} | ||||
| 	} | ||||
| 	let backCallback = function() { | ||||
| 		return fileList(title, prefix, editCallback); | ||||
| 	} | ||||
|  | ||||
| 	return database.getAll().then(function(entries) { | ||||
| 		terminal.readLine().then(function(input) { | ||||
| 			if (input == "new") { | ||||
| 				editCallback("untitled", "", makeSaveCallback("untitled", ""), backCallback); | ||||
| 			} else if (input.substring(0, "open:".length) == "open:") { | ||||
| 				var title = input.substring("open:".length + prefix.length); | ||||
| 				database.get(prefix + title).then(function(contents) { | ||||
| 					editCallback(title, contents, makeSaveCallback(title, contents), backCallback); | ||||
| 				}); | ||||
| 			} else if (input == "home") { | ||||
| 				filelist(title, prefix, editCallback); | ||||
| 			} else if (input.substring(0, "delete:".length) == "delete:") { | ||||
| 				terminal.clear(); | ||||
| 				var title = input.substring(7); | ||||
| 				terminal.print("Are you sure you want to delete '", title.substring(prefix.length), "'?"); | ||||
| 				terminal.print({command: "confirmDelete:" + title, value: "delete it"}); | ||||
| 				terminal.print({command: "home", value: "cancel"}); | ||||
| 				terminal.readLine().then(function(input) { | ||||
| 					if (input == "home") { | ||||
| 						backCallback(); | ||||
| 					} else if (input.substring(0, "confirmDelete:".length) == "confirmDelete:") { | ||||
| 						var title = input.substring("confirmDelete:".length); | ||||
| 						return database.remove(title).then(backCallback); | ||||
| 					} else { | ||||
| 						backCallback(); | ||||
| 					} | ||||
| 				}); | ||||
| 			} | ||||
| 		}); | ||||
| 		for (var i = 0; i < entries.length; i++) { | ||||
| 			if (entries[i].substring(0, prefix.length) == prefix) { | ||||
| 				if (core.user.credentials.permissions.authenticated) { | ||||
| 					terminal.print( | ||||
| 						"* ", | ||||
| 						{style: "font-weight: bold", value: {command: "open:" + entries[i], value: entries[i].substring(prefix.length)}}, | ||||
| 						" (", | ||||
| 						{command: "delete:" + entries[i], value: "x"}, | ||||
| 						")"); | ||||
| 				} else { | ||||
| 					terminal.print( | ||||
| 						"* ", | ||||
| 						{style: "font-weight: bold", value: {command: "open:" + entries[i], value: entries[i].substring(prefix.length)}}); | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
| 	}); | ||||
| } | ||||
|  | ||||
| function testEdit(name, value, save, back) { | ||||
| 	terminal.clear(); | ||||
| 	terminal.print("testEdit ", name, " ", value); | ||||
| 	terminal.print({command: "++"}); | ||||
| 	terminal.print({command: "--"}); | ||||
| 	terminal.print({command: "back"}); | ||||
| 	terminal.readLine().then(function(command) { | ||||
| 		if (command == "back") { | ||||
| 			terminal.print("calling back"); | ||||
| 			back(); | ||||
| 		} else if (command == "++") { | ||||
| 			save(name, (parseInt(value || "0") + 1).toString()).then(back); | ||||
| 		} else if (command == "--") { | ||||
| 			save(name, (parseInt(value || "0") - 1).toString()).then(back); | ||||
| 		} | ||||
| 	}); | ||||
| } | ||||
|  | ||||
| if (imports.terminal) { | ||||
| 	fileList("Test File List", "fileList_", testEdit); | ||||
| } | ||||
|  | ||||
| exports.fileList = fileList; | ||||
		Reference in New Issue
	
	Block a user