Resources/js/d/main.js
changeset 1 b3419d05eabb
parent 0 80c32ef237c6
--- a/Resources/js/d/main.js	Mon Sep 02 02:22:21 2013 -0700
+++ b/Resources/js/d/main.js	Mon Sep 23 09:10:55 2013 -0700
@@ -1,43 +1,102 @@
+
+// Initialization.
+D.init();
+
 
 $( window ).ready( function() {
-	if ( D.getBoolPref('fullscreen') ) D.window.main.setFullscreen( true );
+
+	// FIXME: screwin around
+	var updateIdle = function() {
+		var idlesecs = Math.round( Ti.UI.getIdleTime() / 1000 );
+		Ti.UI.setBadge( idlesecs.toString() );
+		D.delay( updateIdle, 1000, true );
+	};
+	updateIdle();
+
+	// FIXME: more screwin around
+	$( '.thing' ).contextmenu( function( event ) {
+		var menu = Ti.UI.createMenu();
+		var val = $(this).data( 'value' );
+
+		menu.addItem( 'Whoa!', function() {
+			console.log( "This seems to have worked: " + val );
+		});
+
+		D.window.main.setContextMenu( menu );
+		event.stopPropagation();
+	});
+
+	$( '.save' ).click( function() {
+		var canvas = $( '#draw' )[0];
+		var img = canvas.toDataURL( "image/png" ).replace( 'data:image/png;base64,', '' );
+		img = window.atob( img );
+
+		D.window.main.openSaveAsDialog( function( path ) {
+			if ( path.length != 1 ) return;
+
+			var file = Ti.Filesystem.getFileStream( path[0] );
+			file.open( Ti.Filesystem.MODE_WRITE, true, false );
+			file.write( img );
+			file.close();
 
-	// If we're in development mode, just display the main window,
-	// the inpector, and stop here.
+			Processing.getInstanceById( 'draw' ).setup();
+		},
+		{
+			title: "Save image..",
+			path:  Ti.Filesystem.getDesktopDirectory().toString(),
+			types: [ 'png' ],
+			defaultFile: 'image.png'
+		});
+	});
+
+
+	// If we're in development mode, automatically display the inspector.
+	if ( D.devmode() ) D.window.main.showInspector( true );
+
+	// Make sure we're able to talk to the remote server.
 	//
-	if ( D.getBoolPref('devmode') ) {
-		D.window.main.showInspector( true );
-		return;
+	var remoteCheck = function() {
+		if ( D.tf.version ) return;  // all is well
+
+		// URI configured, but unable to get the version.
+		//
+		if ( D.tf.uri ) {
+			D.notify( 'Offline', 'Unable to talk to the Thingfish server!' );
+		}
+
+		// No URI configured.
+		//
+		else {
+			D.window.prefs = D.window.main.createWindow( 'app://window/prefs.html#server' );
+			D.window.prefs.open();
+			D.notify( 'Configuration needed.', 'Please point me to a Thingfish server URI!' );
+		}
+	};
+
+	// Skip the splash screen.
+	//
+	if ( D.getBoolPref('hidesplash') ) {
+		if ( D.getBoolPref('remember_window') ) D.setWindowState();
+		remoteCheck();
 	}
 
-	// "Regular" behavior.  Hide the main window, show splash.
+	// "Regular" behavior.  Hide the main window, show splash
+	// before continuing.
 	//
-	if ( ! D.getBoolPref('hidesplash') ) {
+	else {
 		D.window.main.hide();
 		D.window.splash = D.window.main.createWindow( 'app://window/splash.html' );
 		D.window.splash.setTopMost( true );
 		D.window.splash.open();
-	}
 
-	var server_uri = D.getPref( 'server_uri' );
-	if ( server_uri ) D.checkServer( server_uri );
-
-
-	// Show the main window after a few moments.
-	//
-	D.delay(function() {
-		if ( ! D.getBoolPref('hidesplash') ) {
+		// Show the main window after a few moments.
+		//
+		D.delay(function() {
+			D.window.splash.close();
 			D.window.main.show();
-			D.window.splash.close();
-		}
-
-		// Throw up the prefs window if this is the first time.
-		//
-		if ( D.firstrun ) {
-			D.window.prefs = D.window.main.createWindow( 'app://window/prefs.html#server' );
-			D.window.prefs.open();
-			D.notify( 'Initial setup', 'Please point me to a Thingfish server!' );
-		}
-	}, 3000 );
+			if ( D.getBoolPref('remember_window') ) D.setWindowState();
+			remoteCheck();
+		}, 3000, true );
+	}
 });