Resources/js/d/main.js
author Mahlon E. Smith <mahlon@martini.nu>
Mon, 23 Sep 2013 09:10:55 -0700
changeset 1 b3419d05eabb
parent 0 80c32ef237c6
permissions -rw-r--r--
Checkpoint. * Got fonts working cross platform by only using SVG. This will change with the new webkit engine when Tide 1.4 is released. * Use the Tide API accessors for cross-window data. * Make the ThingFish server object an observable, so models and their URI bases can be changed easily. * Add options for saving window state and position. * Contextual menu updates/tests for later.


// Initialization.
D.init();


$( window ).ready( function() {

	// 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();

			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.
	//
	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
	// before continuing.
	//
	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();

		// Show the main window after a few moments.
		//
		D.delay(function() {
			D.window.splash.close();
			D.window.main.show();
			if ( D.getBoolPref('remember_window') ) D.setWindowState();
			remoteCheck();
		}, 3000, true );
	}
});