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.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
     1
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
     2
// Initialization.
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
     3
D.init();
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
     4
0
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     5
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     6
$( window ).ready( function() {
1
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
     7
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
     8
	// FIXME: screwin around
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
     9
	var updateIdle = function() {
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    10
		var idlesecs = Math.round( Ti.UI.getIdleTime() / 1000 );
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    11
		Ti.UI.setBadge( idlesecs.toString() );
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    12
		D.delay( updateIdle, 1000, true );
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    13
	};
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    14
	updateIdle();
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    15
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    16
	// FIXME: more screwin around
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    17
	$( '.thing' ).contextmenu( function( event ) {
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    18
		var menu = Ti.UI.createMenu();
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    19
		var val = $(this).data( 'value' );
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    20
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    21
		menu.addItem( 'Whoa!', function() {
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    22
			console.log( "This seems to have worked: " + val );
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    23
		});
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    24
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    25
		D.window.main.setContextMenu( menu );
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    26
		event.stopPropagation();
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    27
	});
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    28
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    29
	$( '.save' ).click( function() {
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    30
		var canvas = $( '#draw' )[0];
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    31
		var img = canvas.toDataURL( "image/png" ).replace( 'data:image/png;base64,', '' );
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    32
		img = window.atob( img );
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    33
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    34
		D.window.main.openSaveAsDialog( function( path ) {
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    35
			if ( path.length != 1 ) return;
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    36
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    37
			var file = Ti.Filesystem.getFileStream( path[0] );
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    38
			file.open( Ti.Filesystem.MODE_WRITE, true, false );
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    39
			file.write( img );
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    40
			file.close();
0
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    41
1
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    42
			Processing.getInstanceById( 'draw' ).setup();
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    43
		},
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    44
		{
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    45
			title: "Save image..",
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    46
			path:  Ti.Filesystem.getDesktopDirectory().toString(),
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    47
			types: [ 'png' ],
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    48
			defaultFile: 'image.png'
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    49
		});
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    50
	});
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    51
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    52
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    53
	// If we're in development mode, automatically display the inspector.
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    54
	if ( D.devmode() ) D.window.main.showInspector( true );
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    55
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    56
	// Make sure we're able to talk to the remote server.
0
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    57
	//
1
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    58
	var remoteCheck = function() {
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    59
		if ( D.tf.version ) return;  // all is well
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    60
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    61
		// URI configured, but unable to get the version.
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    62
		//
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    63
		if ( D.tf.uri ) {
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    64
			D.notify( 'Offline', 'Unable to talk to the Thingfish server!' );
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    65
		}
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    66
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    67
		// No URI configured.
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    68
		//
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    69
		else {
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    70
			D.window.prefs = D.window.main.createWindow( 'app://window/prefs.html#server' );
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    71
			D.window.prefs.open();
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    72
			D.notify( 'Configuration needed.', 'Please point me to a Thingfish server URI!' );
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    73
		}
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    74
	};
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    75
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    76
	// Skip the splash screen.
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    77
	//
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    78
	if ( D.getBoolPref('hidesplash') ) {
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    79
		if ( D.getBoolPref('remember_window') ) D.setWindowState();
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    80
		remoteCheck();
0
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    81
	}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    82
1
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    83
	// "Regular" behavior.  Hide the main window, show splash
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    84
	// before continuing.
0
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    85
	//
1
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    86
	else {
0
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    87
		D.window.main.hide();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    88
		D.window.splash = D.window.main.createWindow( 'app://window/splash.html' );
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    89
		D.window.splash.setTopMost( true );
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    90
		D.window.splash.open();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    91
1
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    92
		// Show the main window after a few moments.
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    93
		//
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    94
		D.delay(function() {
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    95
			D.window.splash.close();
0
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    96
			D.window.main.show();
1
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    97
			if ( D.getBoolPref('remember_window') ) D.setWindowState();
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    98
			remoteCheck();
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    99
		}, 3000, true );
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   100
	}
0
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   101
});
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   102