javascripts/app.js in robeaux-0.0.3 vs javascripts/app.js in robeaux-0.0.4

- old
+ new

@@ -1,20 +1,122 @@ // Routes var robeaux = angular.module("robeaux", ['ngRoute']); robeaux.config(["$routeProvider", function($routeProvider) { - $routeProvider.when("/robots", { + $routeProvider + + .when("/robots", { templateUrl: "/partials/robot-index.html", controller: RobotIndexCtrl - }).when("/robots/:robotId", { + }) + + .when("/robots/:robotId", { templateUrl: "/partials/robot-detail.html", controller: RobotDetailCtrl - }).otherwise({ + }) + + .when("/themes", { + templateUrl: "/partials/themes.html", + controller: ThemesCtrl + }) + + .otherwise({ redirectTo: "/robots" }); -}]) +}]); +robeaux.service("Themes", function() { + var service = {}; + var defaultThemes = { + "default": { custom: false, url: "/stylesheets/themes/default.css" }, + "dark": { custom: false, url: "/stylesheets/themes/dark.css" }, + "flat": { custom: false, url: "/stylesheets/themes/flat.css" } + }; + + var saveThemes = function() { + localStorage.setItem("themes", angular.toJson(service.themes)); + }; + + var loadThemes = function() { + if (localStorage.getItem("themes")) { + return angular.fromJson(localStorage.getItem("themes")); + } else { + return defaultThemes; + } + }; + + var getActiveTheme = function() { + return localStorage.getItem("activeTheme") || "default"; + }; + + service.themes = loadThemes(); + + service.activeTheme = getActiveTheme(); + + service.saveThemes = saveThemes; + + service.current = function() { + return service.themes[service.activeTheme]; + }; + + service.list = function() { + return Object.keys(service.themes); + }; + + service.customThemes = function() { + var themes = {}; + + for (var name in service.themes) { + if (service.themes[name].custom) { + themes[name] = service.themes[name]; + } + } + + return themes; + }; + + service.setActiveTheme = function() { + localStorage.setItem("activeTheme", service.activeTheme); + } + + service.newTheme = function(name) { + if (service.themes[name] || name === '' || name === undefined) { + return false; + } + + service.themes[name] = {css: "", custom: true}; + return service.themes[name]; + }; + + return service; +}); + // Controllers +var ThemesCtrl = function($scope, Themes) { + $scope.themes = Themes; + + $scope.selectTheme = function(name) { + $scope.selectedTheme = Themes.themes[name]; + $scope.selectedThemeName = name; + }; + + $scope.deleteTheme = function(name) { + delete Themes.themes[name]; + $scope.selectedTheme = null; + $scope.selectedThemeName = null; + }; + + $scope.newTheme = function(name) { + if (Themes.newTheme(name)) { $scope.newThemeName = ''; } + } +} + +var NavigationCtrl = function($scope, $location) { + $scope.active = function(path) { + return ($location.path().substring(1).split("/")[0] || "robots") === path; + } +} + var RobotIndexCtrl = function($scope, $http, $location, $route) { $http.get('/robots').success(function(data) { $scope.robots = data; });