Resources/js/d/prefs.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
D = Ti.API.get( 'D' );
0
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     3
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     4
$( window ).ready( function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     5
	new PrefsRouter( $('#content') );
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     6
});
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     7
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     8
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     9
/* ---------------------------------------------- */
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    10
/* Router for the various Pref sections */
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    11
/* ---------------------------------------------- */
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    12
PrefsRouter = can.Control({
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    13
	defaults: {
1
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    14
		menu: '#sections',
0
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    15
		default_route: 'app'
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    16
	}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    17
}, {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    18
	init: function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    19
		var self = this;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    20
		self.controls = [];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    21
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    22
		if ( window.location.hash == "" ) window.location.hash = "#" + self.options.default_route;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    23
	},
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    24
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    25
	// Remove controllers (and their children DOM elements) and reflect
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    26
	// the currently selected route.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    27
	//
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    28
	reset: function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    29
		var self = this;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    30
		var current_route = can.route.attr( 'route' );
1
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    31
		var menu = $( self.options.menu );
0
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    32
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    33
		menu.find( 'a' ).removeClass( 'selected' );
1
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    34
		menu.find( 'a[href="#' + current_route + '"]' ).addClass( 'selected' );
0
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    35
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    36
		while ( control = self.controls.pop() ) control.destroy();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    37
	},
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    38
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    39
	"app route": function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    40
		this.reset();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    41
		this.controls.push( new PrefsAppWidget(this.element) );
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    42
	},
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    43
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    44
	"server route": function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    45
		this.reset();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    46
		this.controls.push( new PrefsServerWidget(this.element) );
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    47
	},
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    48
});
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    49
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    50
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    51
/* ---------------------------------------------- */
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    52
/* App behavior section */
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    53
/* ---------------------------------------------- */
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    54
PrefsAppWidget = can.Control({
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    55
	defaults: {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    56
		view: 'app://window/prefs/app.ejs'
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    57
	}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    58
}, {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    59
	init: function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    60
		var self = this;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    61
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    62
		// Get the current preference values.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    63
		//
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    64
		var bool_prefs = {
1
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    65
			hidesplash:      [ D.getBoolPref( 'hidesplash' ), 'Skip splash screen at startup' ],
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    66
			remember_window: [ D.getBoolPref( 'remember_window' ), 'Remember main window position' ],
0
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    67
		}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    68
1
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    69
		// Running from the builder env (or manually with --debug), include
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    70
		// developer options.
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    71
		//
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    72
		if ( Ti.App.getArguments()[0] == "--debug" )
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    73
			bool_prefs.devmode = [ D.getBoolPref( 'devmode' ), 'Developer mode' ];
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    74
0
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    75
		self.element.html(
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    76
			can.view( self.options.view, {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    77
				bool_prefs: bool_prefs
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    78
			})
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    79
		);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    80
	},
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
	'input click': function( ele, event ) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    83
		var pref = ele.data( 'field' );
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    84
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    85
		if ( ele.attr('type') == 'checkbox' ) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    86
			var bool = ele.is( ':checked' );
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    87
			D.setBoolPref( pref, ele.is(':checked') );
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    88
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    89
			if ( pref == 'devmode' && bool ) D.window.main.showInspector( true );
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    90
		}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    91
	},
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    92
});
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    93
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    94
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    95
/* ---------------------------------------------- */
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    96
/* Server section */
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    97
/* ---------------------------------------------- */
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    98
PrefsServerWidget = can.Control({
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    99
	defaults: {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   100
		view: 'app://window/prefs/servers.ejs'
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
}, {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   103
	init: function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   104
		var self = this;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   105
		var server = D.getPref( 'server_uri' );
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   106
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   107
		self.element.html(
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   108
			can.view( self.options.view, {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   109
				server: server
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   110
			})
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   111
		);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   112
	},
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   113
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   114
	// Update the server URI preference.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   115
	//
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   116
	updateServer: function( uri ) {
1
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   117
		if ( uri.length == 0 ) return;
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   118
		D.tf.attr( 'uri', uri );
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   119
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   120
		if ( D.tf.version ) {
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   121
			D.setPref( 'server_uri', uri );
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   122
			console.log( 'Updated server_uri to ' + uri + '.' );
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   123
			$( '#test .info' ).html( D.tf.version );
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   124
			$( '#test .icon' ).html( '&#xf15d;' );
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   125
		}
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   126
		else {
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   127
			$( '#test .info' ).empty();
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   128
			$( '#test .icon' ).html( '&#xf15b;' );
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   129
		}
0
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   130
	},
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   131
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   132
	// Hitting the return key on input fields defer to the 'blur' event,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   133
	// which in turn fires the 'change' event.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   134
	//
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   135
	"#server keypress": function( ele, event ) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   136
		if ( event.which == 13 ) ele.trigger( 'blur' );
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   137
	},
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   138
	'#server change': function( ele, event ) {
1
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   139
		var self = this;
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   140
		self.updateServer( ele.val() );
0
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   141
	},
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   142
	'#server keyup': function( ele, event ) {
1
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   143
		var self = this;
0
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   144
		D.delay( function() {
1
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   145
			self.updateServer( ele.val() );
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   146
		}, 1000, true );
b3419d05eabb Checkpoint.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   147
	}
0
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   148
});
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   149