Resources/js/d/dascyllus.js
changeset 1 b3419d05eabb
parent 0 80c32ef237c6
--- a/Resources/js/d/dascyllus.js	Mon Sep 02 02:22:21 2013 -0700
+++ b/Resources/js/d/dascyllus.js	Mon Sep 23 09:10:55 2013 -0700
@@ -10,6 +10,7 @@
 	version:      Ti.API.Application.getVersion(),
 	ti_version:   Ti.getVersion(),
 	architecture: Ti.Platform.getArchitecture(),
+	copyright:    Ti.App.getCopyright(),
 
 	// Declared windows, for easy referencing
 	window: {},
@@ -20,11 +21,95 @@
 	// Persistent storage DB handle.
 	db: null,
 
-	// The current TF server we'll be connected to;
+	// The primary main tray menu.
+	menu: null,
+
+	// The current TF server we're connected to.
+	//
 	//     uri: http://localhost:8080/
 	//     version: Thingfish 0.5.0 (build ...)
 	//
-	tf: null,
+	tf: new can.Observe({
+		uri: null,
+		version: null
+	}),
+
+
+	// Actions to perform at startup.
+	//
+	init: function() {
+		var D = this;
+
+		D.initDB();
+		D.window.main = Ti.UI.getMainWindow();
+		D.initMenu();
+
+		// Thingfish server object event handlers.
+		//
+		D.tf.bind( 'uri', D.initTF );
+		var server_uri = D.getPref( 'server_uri' );
+		if ( server_uri ) D.tf.attr( 'uri', server_uri );
+
+		// Main window event handlers.
+		//
+		D.window.main.addEventListener( 'moved', function() { D.saveWindowState(); });
+		D.window.main.addEventListener( 'resized', function() {
+			D.delay( function() {
+				D.saveWindowState();
+			},
+			500, true );
+		});
+		$( document ).contextmenu( function() { D.window.main.setContextMenu( D.menu ); });
+
+		// Each Tide window is an environment unto itself.  There is a global
+		// key/val namespace, though.  Make the 'D' namespace accessible to other windows here.
+		Ti.API.set( 'D', D );
+	},
+
+
+	// Determine if we should be running in developer mode.
+	// This requires that the user has previously enabled it in preferences,
+	// and the --debug command line option was passed.
+	//
+	devmode: function() {
+		return Ti.App.getArguments()[0] == "--debug" && this.getBoolPref( 'devmode' );
+	},
+
+
+	// Event handler for changing the Thingfish server URI.
+	// Sanity check and instantiate appropriate Model(s).
+	//
+	initTF: function( event, uri, old_uri ) {
+		var serverOk = function( data, status, xhr ) {
+			var info = xhr.getResponseHeader( 'x-thingfish' );
+			if ( ! info ) {
+				D.tf.attr( 'version', null );
+				D.notify( 'Unable to connect.', 'Not a Thingfish server?' );
+				return;
+			}
+
+			D.tf.attr( 'version', data.version )
+			D.notify( 'Connected.', info );
+
+			// FIXME: models?
+		};
+
+		try {
+			$.ajax({
+				url:      uri + '/serverinfo',
+				async:    false,
+				success:  serverOk,
+				error:    function( xhr, status, err ) {
+					D.tf.attr( 'version', null );
+					D.notify( 'Unable to connect.', err ? err : 'Network error.' );
+				},
+				dataType: 'json'
+			});
+		}
+		catch( err ) {
+			console.log( err );
+		};
+	},
 
 
 	// Open a handle to the local database, initializing it
@@ -147,45 +232,111 @@
 	},
 
 
-	// Attempt a connection to a remote Thingfish server, and
-	// populate the D.tf variable.
+	// Record the main window position and state.
 	//
-	checkServer: function( uri ) {
-		this.tf = null;
+	saveWindowState: function() {
+		var w      = this.window.main;
+		var x      = w.getX();
+		var y      = w.getY();
+		var width  = w.getWidth();
+		var height = w.getHeight();
+
+		var hiddenMenu     = this.menu.getItemAt( 2 ).getSubmenu().getItemAt( 0 );
+		var fullScreenMenu = this.menu.getItemAt( 2 ).getSubmenu().getItemAt( 1 );
+
+		var pos = this.getPref( 'window_pos' );
+		pos = Ti.JSON.parse( pos );
+
+		// first time
+		//
+		if ( ! pos ) {
+			pos =  {
+				x: x,
+				y: y,
+				width: width,
+				height: height,
+				fullscreen: false,
+				hidden: false
+			};
+		}
+
+		if ( w.isVisible() ) {
+			pos.hidden = false;
+			fullScreenMenu.enable();
+		}
+		else {
+			pos.hidden = true;
+			fullScreenMenu.disable();
+		}
 
-		var serverOk = function( data, status, xhr ) {
-			var info = xhr.getResponseHeader( 'x-thingfish' );
-			if ( info ) {
-				D.tf = {
-					uri: uri,
-					version: data.version
-				};
-				D.notify( 'Connected.', info );
+		if ( w.isFullScreen() ) {
+			pos.fullscreen = true;
+			hiddenMenu.disable();
+		}
+		else {
+			pos.fullscreen = false;
+			hiddenMenu.enable();
+		}
+
+		if ( ! pos.fullscreen ) {
+			pos.x      = x;
+			pos.y      = y;
+			pos.width  = width;
+			pos.height = height;
+		}
+
+		this.setPref( 'window_pos', Ti.JSON.stringify(pos) );
+	},
+
+
+	// Set the main window according to previously saved values.
+	//
+	setWindowState: function() {
+		var w   = this.window.main;
+		var pos = this.getPref( 'window_pos' );
+
+		var hiddenMenu     = this.menu.getItemAt( 2 ).getSubmenu().getItemAt( 0 );
+		var fullScreenMenu = this.menu.getItemAt( 2 ).getSubmenu().getItemAt( 1 );
+
+		pos = Ti.JSON.parse( pos );
+		if ( ! pos ) return;
+
+		if ( pos.fullscreen ) {
+			w.setFullscreen( true );
+			hiddenMenu.disable();
+		}
+		else {
+
+			console.log( "Moving to x:" + pos.x + ", y:" + pos.y );
+			console.log( "Sizing to w:" + pos.width + ", h:" + pos.height );
+			w.moveTo(  pos.x, pos.y );
+			w.setSize( pos.width, pos.height );
+
+			hiddenMenu.enable();
+
+			if ( pos.hidden ) {
+				w.hide();
+				fullScreenMenu.disable();
 			}
 			else {
-				D.notify( 'Unable to connect.', 'Not a Thingfish server?' );
+				w.show();
+				fullScreenMenu.enable();
 			}
-		};
+		}
 
-		$.ajax({
-			url:      uri,
-			sync:     false,
-			success:  serverOk,
-			error:    function( xhr, status, err ) {
-				D.notify( 'Unable to connect.', err );
-			},
-			dataType: 'json'
-		});
+		this.saveWindowState();
 	},
 
 
 	// Build the main menu.
 	//
-	setupMenu: function() {
-		var menu  = Ti.UI.createMenu();
-		var yep   = Ti.UI.createMenuItem( 'Yep' );
+	initMenu: function() {
+		this.menu = Ti.UI.createMenu();
+		var tray  = Ti.UI.addTray( 'app://img/tray_icon.png' );
 
-		yep.addItem( 'About', function() {
+		// windows
+		// ---------------------------------------------------------------
+		this.menu.addItem( 'About', function() {
 			if ( D.window.about && D.window.about.isVisible() ) {
 				D.window.about.focus();
 			}
@@ -196,7 +347,7 @@
 			}
 		});
 
-		yep.addItem( 'Preferences', function() {
+		this.menu.addItem( 'Preferences', function() {
 			if ( D.window.prefs && D.window.prefs.isVisible() ) {
 				D.window.prefs.focus();
 			}
@@ -206,12 +357,39 @@
 			}
 		});
 
-		yep.addItem( 'Toggle Full Screen', function() {
-			D.window.main.setFullscreen( ! D.window.main.isFullscreen() );
+		// view menu
+		// ---------------------------------------------------------------
+		var viewMenu = Ti.UI.createMenuItem( 'View' );
+
+		viewMenu.addItem( 'Toggle Hidden', function() {
+			if ( D.window.main.isVisible() ) {
+				D.window.main.hide();
+			}
+			else {
+				D.window.main.show();
+			}
+			D.saveWindowState();
 		});
 
-		menu.appendItem( yep );
-		Ti.UI.setMenu( menu );
+		viewMenu.addItem( 'Toggle Full Screen', function() {
+			D.window.main.setFullscreen( ! D.window.main.isFullscreen() );
+			D.saveWindowState();
+		});
+
+		this.menu.appendItem( viewMenu );
+
+		// other
+		// --------------------------------------------------------------
+		if ( this.getBoolPref('devmode') ) {
+			this.menu.addItem( 'Restart', function() {
+				Ti.App.restart();
+			});
+	   }
+
+		tray.setMenu( this.menu );
+		Ti.UI.setDockMenu( this.menu );
+		// D.window.main.setContextMenu( this.menu );
+		// Ti.UI.setMenu( menu );
 	},
 
 
@@ -219,38 +397,31 @@
 	### M O D E L S
 	######################################################################*/
 
-	// A Thingfish asset.
-	//
-	initModel: function() {
-		D.Asset = can.Model({
-			id     : 'oid',
-			findAll: 'GET '    + D.tf.uri + '/',
-			findOne: 'GET '    + D.tf.uri + '/{oid}',
-			create : 'POST '   + D.tf.uri + '/',
-			update : 'PUT '    + D.tf.uri + '/{oid}',
-			destroy: 'DELETE ' + D.tf.uri + '/{oid}',
+	// // A Thingfish asset.
+	// //
+	// initModel: function() {
+	//     D.Asset = can.Model({
+	//         id     : 'oid',
+	//         findAll: 'GET '    + D.tf.uri + '/',
+	//         findOne: 'GET '    + D.tf.uri + '/{oid}',
+	//         create : 'POST '   + D.tf.uri + '/',
+	//         update : 'PUT '    + D.tf.uri + '/{oid}',
+	//         destroy: 'DELETE ' + D.tf.uri + '/{oid}',
 
-			// TODO: other metadata hooks?  diff model?
-			metadata: 'GET ' + D.tf.uri + '/{oid}/metadata',
+	//         // TODO: other metadata hooks?  diff model?
+	//         metadata: 'GET ' + D.tf.uri + '/{oid}/metadata',
 
-		}, {}).
-			bind( 'created', function( ev, asset ) {
-				console.debug( "Created a new asset " + asset.id );
-			}).
-			bind( 'updated', function( ev, asset ) {
-				console.debug( "Updated asset " + asset.id );
-			}).
-			bind( 'destroyed', function( ev, asset ) {
-				console.debug( "Removed asset " + asset.id );
-			}
-		);
-	},
+	//     }, {}).
+	//         bind( 'created', function( ev, asset ) {
+	//             console.debug( "Created a new asset " + asset.id );
+	//         }).
+	//         bind( 'updated', function( ev, asset ) {
+	//             console.debug( "Updated asset " + asset.id );
+	//         }).
+	//         bind( 'destroyed', function( ev, asset ) {
+	//             console.debug( "Removed asset " + asset.id );
+	//         }
+	//     );
+	// },
 };
 
-
-// Initialization.
-//
-D.window.main = Ti.UI.getMainWindow();
-D.setupMenu();
-D.initDB();
-