diff --git a/src/app/auth/resolver/session_resolver.js b/src/app/auth/resolver/session_resolver.js index 5f514beb..55d23742 100644 --- a/src/app/auth/resolver/session_resolver.js +++ b/src/app/auth/resolver/session_resolver.js @@ -80,6 +80,15 @@ angular.module('sb.auth').constant('SessionResolver', } return deferred.promise; + }, + + /** + * This resolver ensures that the currentUser has been resolved + * before the route resolves. + */ + requireCurrentUser: function ($q, $log, CurrentUser) { + $log.debug('Resolving current user...'); + return CurrentUser.resolve(); } }; })()); diff --git a/src/app/profile/controller/profile_preferences_controller.js b/src/app/profile/controller/profile_preferences_controller.js new file mode 100644 index 00000000..1699fdc5 --- /dev/null +++ b/src/app/profile/controller/profile_preferences_controller.js @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2014 Hewlett-Packard Development Company, L.P. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +/** + * Preferences controller for our user profile. Allows explicit editing of + * individual preferences. + */ +angular.module('sb.profile').controller('ProfilePreferencesController', + function ($scope, Preference) { + 'use strict'; + + $scope.pageSize = Preference.$get('page_size'); + + $scope.save = function () { + Preference.$set('page_size', $scope.pageSize); + $scope.message = 'Preferences Saved!'; + }; + }); \ No newline at end of file diff --git a/src/app/profile/module.js b/src/app/profile/module.js new file mode 100644 index 00000000..12903c46 --- /dev/null +++ b/src/app/profile/module.js @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2014 Hewlett-Packard Development Company, L.P. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +/** + * The Storyboard root application module. + * + * This module contains the entire, standalone application for the Storyboard + * ticket tracking web client. + * + * @author Michael Krotscheck + */ +angular.module('sb.profile', + ['sb.services', 'sb.templates', 'sb.auth', 'ui.router', 'ui.bootstrap'] + ) + .config(function ($stateProvider, SessionResolver, $urlRouterProvider) { + 'use strict'; + + // URL Defaults. + $urlRouterProvider.when('/profile', '/profile/preferences'); + + // Declare the states for this module. + $stateProvider + .state('profile', { + abstract: true, + template: '
', + url: '/profile', + resolve: { + isLoggedIn: SessionResolver.requireLoggedIn, + currentUser: SessionResolver.requireCurrentUser + } + }) + .state('profile.preferences', { + url: '/preferences', + templateUrl: 'app/templates/profile/preferences.html', + controller: 'ProfilePreferencesController' + }); + }); \ No newline at end of file diff --git a/src/app/services/resource/preference.js b/src/app/services/resource/preference.js new file mode 100644 index 00000000..ac093e6d --- /dev/null +++ b/src/app/services/resource/preference.js @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2014 Hewlett-Packard Development Company, L.P. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +/** + * A simple preferences service, backed by localStorage. + */ +angular.module('sb.services').provider('Preference', + function () { + 'use strict'; + + /** + * Our preference defaults. We're using underscore naming here in + * anticipation of these keys living on the python side of things. + */ + var defaults = { }; + + /** + * Preference name key generator. Basically it's poor man's + * namespacing. + */ + function preferenceName(key) { + return 'pref_' + key; + } + + /** + * Each module can manually declare its own preferences that it would + * like to keep track of, as well as set a default. During the config() + * phase, inject the Preference Provider and call 'addPreference()' to + * do so. An example is available at the bottom of this file. + */ + this.addPreference = function (preferenceName, preferenceDefault) { + defaults[preferenceName] = preferenceDefault; + }; + + /** + * The actual preference implementation. + */ + function Preference($log, localStorageService) { + /** + * Get a preference. + */ + this.$get = function (key) { + + // Is this a valid preference? + if (!defaults.hasOwnProperty(key)) { + $log.warn('Attempt to get unregistered preference: ' + + key); + return null; + } + + var value = localStorageService.get(preferenceName(key)); + + // If the value is unset, and we have a default, set and use + // that. + if (value === null && defaults.hasOwnProperty(key)) { + var defaultValue = defaults[key]; + this.$set(key, defaultValue); + return defaultValue; + } + + return value; + }; + + /** + * Set a preference. + */ + this.$set = function (key, value) { + + // Is this a valid preference? + if (!defaults.hasOwnProperty(key)) { + $log.warn('Attempt to set unregistered preference: ' + + key); + return null; + } + + return localStorageService.set(preferenceName(key), value); + }; + } + + /** + * Factory getter - returns a configured instance of preference + * provider, as needed. + */ + this.$get = function ($log, localStorageService) { + return new Preference($log, localStorageService); + }; + }) + .config(function (PreferenceProvider) { + 'use strict'; + + // WARNING: In all modules OTHER than the services module, this config + // block can appear anywhere as long as this module is listed as a + // dependency. In the services module, the config() block must appear + // AFTER the provider block. For more information, + // @see https://github.com/angular/angular.js/issues/6723 + + // Let our preference provider know about page_size. + PreferenceProvider.addPreference('page_size', 10); + + }) +; \ No newline at end of file diff --git a/src/app/storyboard/module.js b/src/app/storyboard/module.js index cc1441a6..0119082d 100644 --- a/src/app/storyboard/module.js +++ b/src/app/storyboard/module.js @@ -24,7 +24,7 @@ */ angular.module('storyboard', [ 'sb.services', 'sb.templates', 'sb.pages', 'sb.projects', 'sb.auth', - 'sb.story', 'ui.router', 'ui.bootstrap'] + 'sb.story', 'sb.profile', 'ui.router', 'ui.bootstrap'] ) .config(function ($provide, $stateProvider, $urlRouterProvider, $locationProvider, $httpProvider) { diff --git a/src/app/templates/header.html b/src/app/templates/header.html index 6153e035..8db69780 100644 --- a/src/app/templates/header.html +++ b/src/app/templates/header.html @@ -64,12 +64,17 @@