Resources/js/d/prefs.js
author Mahlon E. Smith <mahlon@martini.nu>
Mon, 02 Sep 2013 02:22:21 -0700
changeset 0 80c32ef237c6
child 1 b3419d05eabb
permissions -rw-r--r--
Initial commit.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     1
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     2
$( window ).ready( function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     3
	new PrefsRouter( $('#content') );
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     4
});
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
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
/* Router for the various Pref sections */
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
PrefsRouter = can.Control({
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    11
	defaults: {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    12
		menu: $( '#sections' ),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    13
		default_route: 'app'
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    14
	}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    15
}, {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    16
	init: function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    17
		var self = this;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    18
		self.controls = [];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    19
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    20
		if ( window.location.hash == "" ) window.location.hash = "#" + self.options.default_route;
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
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    23
	// Remove controllers (and their children DOM elements) and reflect
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    24
	// the currently selected route.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    25
	//
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    26
	reset: function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    27
		var self = this;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    28
		var current_route = can.route.attr( 'route' );
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    29
		var menu = self.options.menu;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    30
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    31
		// FIXME
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    32
		menu.find( 'a' ).removeClass( 'selected' );
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    33
		console.log( menu.find( 'a' ) );
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    34
		console.log( menu.find( 'a[href="#' + current_route + '"]' ) );
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 = {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    65
			hidesplash: [ D.getBoolPref( 'hidesplash' ), 'Skip splash screen at startup' ],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    66
			fullscreen: [ D.getBoolPref( 'fullscreen' ), 'Go full screen by default' ],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    67
			devmode:    [ D.getBoolPref( 'devmode' ), 'Developer mode' ]
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    68
		}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    69
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    70
		self.element.html(
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    71
			can.view( self.options.view, {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    72
				bool_prefs: bool_prefs
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    73
			})
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    74
		);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    75
	},
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    76
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    77
	'input click': function( ele, event ) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    78
		var pref = ele.data( 'field' );
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
		if ( ele.attr('type') == 'checkbox' ) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    81
			var bool = ele.is( ':checked' );
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    82
			D.setBoolPref( pref, ele.is(':checked') );
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    83
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    84
			if ( pref == 'devmode' && bool ) D.window.main.showInspector( true );
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    85
		}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    86
	},
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    87
});
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
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
/* Server section */
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
PrefsServerWidget = can.Control({
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    94
	defaults: {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    95
		view: 'app://window/prefs/servers.ejs'
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    96
	}
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
	init: function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    99
		var self = this;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   100
		var server = D.getPref( 'server_uri' );
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
		self.element.html(
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   103
			can.view( self.options.view, {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   104
				server: server
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   105
			})
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
	},
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   108
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   109
	// Update the server URI preference.
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
	updateServer: function( uri ) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   112
		console.log( 'Updated server_uri to ' + uri + '.' );
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   113
		D.setPref( 'server_uri', uri );
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   114
		D.tf.uri = uri;
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
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   117
	// Hitting the return key on input fields defer to the 'blur' event,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   118
	// which in turn fires the 'change' event.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   119
	//
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   120
	"#server keypress": function( ele, event ) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   121
		if ( event.which == 13 ) ele.trigger( 'blur' );
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   122
	},
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   123
	'#server change': function( ele, event ) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   124
		this.updateServer( ele.val() );
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   125
	},
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   126
	'#server keyup': function( ele, event ) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   127
		D.delay( function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   128
			this.updateServer( ele.val() );
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   129
		}, 500, true );
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
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   133
	// Talk to the remote server, check to see if it's really
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   134
	// a ThingFish handler or not.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   135
	//
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   136
	'#test click': function( ele, event ) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   137
		var server = $( '#server' ).val();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   138
		D.checkServer( server );
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   139
	},
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   140
});
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   141