diff --git a/index.html b/index.html index f706d06..82e76c9 100644 --- a/index.html +++ b/index.html @@ -1,3 +1,7 @@ +
diff --git a/resources/js/app.js b/resources/js/app.js index 3755d7d..97c31b7 100644 --- a/resources/js/app.js +++ b/resources/js/app.js @@ -51,9 +51,22 @@ _ ) { "use strict"; - return { - init: function() { + var App = Backbone.Router.extend({ + routes: { + "playlist": "_routerActionPlaylist" + , "servers/:server": "_routerActionClient" + , "playlist/:item": "_routerActionPlaylistItem" + , ":server/items/:item": "_routerActionServerItem" + //"*actions": "defaultRoute" // matches http://example.com/#anything-here + } + + , initialize: function() { _.bindAll(this + , "_routerActionPlaylist" + , "_routerActionClient" + , "_routerActionItem" + , "_routerActionServerItem" + , "_serverInit" ); @@ -65,39 +78,12 @@ _ // Setup underscore templates with mustache styles _.templateSettings.interpolate = /\{\{(.+?)\}\}/g; - var Router = Backbone.Router.extend({ - routes: { - "playlist": "_routerActionPlaylist" - , "servers": "_routerActionClient" - , "items/:id": "_routerActionItem" - , ":server/items/:id": "_routerActionServerItem" - //"*actions": "defaultRoute" // matches http://example.com/#anything-here - } - }); - - this.Router = new Router(); - - Backbone.history.start({ - pushState: true - , root: "/" - }); - this.servers = new ServerCollection(); this.servers.on("add", function (server) { server.save(); }); - this.servers.on("client:unauthorized", function __fnAppEventClientUnauthorized(event) { - var client = event.shift() - ; - console.log(arguments); - }); - - this.loadingStates = 0; - this.loadingModal = $(tmplModalLoading); - this.loadingTimeout = null; - async.waterfall([ function __asyncAppFetchServerCollection(callback) { that.servers.fetch({ @@ -143,35 +129,62 @@ _ $(event.target).remove(); }); + Backbone.history.start({ + pushState: true + , root: "/" + }); + + // For now let's reset the state when we start up + that.navigate("", false); + + App.__super__.initialize.apply(that, that.routes); }); return this; } - , _routerActionPlaylist: function () { + , _routerActionPlaylist: function () { } - , _routerActionClient: function (hostname) { + , _routerActionClient: function (hostname) { + var server = this.servers.get(hostname) + , client = server.client + ; + + if (client) { + if (client.sate < 4) { + client.on("inited", function __fnAppEventClientInitited(_client) { + _client.fetchDatabases(_client.fetchDatabase); + }); + client.init(); + } else if (client.state === 4) { + client.fetchDatabases(client.fetchDatabase); + } + } } - , _routerActionItem: function (item) { + , _routerActionPlaylistItem: function (item) { } - , _routerActionServerItem: function (server, item) { + , _routerActionServerItem: function (server, item) { } - , _serverInit: function(server) { + , _serverInit: function(server) { var that = this ; + that.loadingStates = 0; + that.loadingModal = $(tmplModalLoading); + that.loadingTimeout = null; + + server.client.off("all"); + server.client.on("state", function __fnAppEventClientStateChange(client, state) { - console.log(state); if (state === ClientModel.defaults._states.loading) { - if (that.loadingStates === 0) { that.loadingTimeout = setTimeout(function __fnAppTimeoutLoadingStat() { that.loadingModal.modal(); @@ -193,13 +206,49 @@ _ server.client.on("unauthorized", function __fnAppEventClientUnauthorized(client, xhr) { var tmplHtml = _.template(tmplModalLogin)({ dmap_itemname: client.url() }) , $tmpl = $(tmplHtml) + , serverModel = that.servers.get(client.attributes.hostname) ; $tmpl.modal(); + + $tmpl.find("form").on("submit", function __fnAppEventModalLogin(event) { + var username = $(event.target).find("input[type='text']").val() + , password = $(event.target).find("input[type='password']").val() + , saveCredentials = $(event.target).find("input[type='checkbox']").is(':checked') + ; + + client.setAuth(username, password); + + if (saveCredentials) { + var newModel = (_.clone({}, serverModel.attributes, {username: username, password: password})); + console.log(newModel); + + serverModel.set("username", username, {silent: true}); + serverModel.set("password", password, {silent: true}); + + serverModel.save(newModel, {silence: true}); + } + + $tmpl.modal("hide"); + + setTimeout(client.init, 250); + + event.preventDefault(); + }); + + $tmpl.delegate("input[type='checkbox']", "click", function __fnEventModalRemember(event) { + if (event.target.checked === true) { + if (!confirm("Using this feature will save the login and password locally in clear text, are you sure?")) { + event.target.checked = false; + event.preventDefault(); + } + } + }); }); server.client.init(); } - }; + }); + return App; }); diff --git a/resources/js/main.js b/resources/js/main.js index 418217d..0dc3684 100644 --- a/resources/js/main.js +++ b/resources/js/main.js @@ -88,7 +88,7 @@ require( $.curCSS = $.css; $("body").ready(function __fnAppRequireLoader(){ - App.init(); + window.App = new App(); }); } ); \ No newline at end of file diff --git a/resources/js/models/client.js b/resources/js/models/client.js index a870a43..1862ca8 100644 --- a/resources/js/models/client.js +++ b/resources/js/models/client.js @@ -95,11 +95,11 @@ define([ this.setState("connecting"); - console.debug("Fetching content types"); + //console.debug("Fetching content types"); that.fetchContentTypes(function __fnClientCbFetchContentTypes(contentTypes) { - console.debug("Fetching server info"); + //console.debug("Fetching server info"); that.fetchServerInfo(function __fnClientCbFetchServerInfo(serverInfo) { - console.debug("Fetching login"); + //console.debug("Fetching login"); that.fetchLogin(function __fnClientCbFetchLogin(sessionInfo) { // TODO: Here we need to request /update for the database version //console.debug("Fetching databases info"); diff --git a/resources/js/views/sidebar.js b/resources/js/views/sidebar.js index ca80965..020b382 100644 --- a/resources/js/views/sidebar.js +++ b/resources/js/views/sidebar.js @@ -186,7 +186,7 @@ _ console.log(item.client.collections); } */ - Backbone.Router.prototype.navigate("servers/" + item.id, false); + Backbone.Router.prototype.navigate("servers/" + item.id, true); }); } diff --git a/resources/templates/app/modal-login.html b/resources/templates/app/modal-login.html index 8f9a595..844cc32 100644 --- a/resources/templates/app/modal-login.html +++ b/resources/templates/app/modal-login.html @@ -1,22 +1,22 @@ \ No newline at end of file