// Declare app level module which depends on filters, and services
angular.module('KiteMail', ['ngRoute', 'ngCookies', 'KiteMail.filters', 'KiteMail.services', 'KiteMail.directives', 'KiteMail.controllers']).
- config(['$routeProvider', '$httpProvider', function($routeProvider, $httpProvider) {
+ factory('errorInterceptor', ['$rootScope', '$q', '$location', function (scope, $q, $location) {
+ return {
+ response: function (response) {
+ if (response && response.status === 401) {
+ $location.path("/login");
+ return $q.reject(response);
+ }
+
+ return response || $q.when(response);
+ },
+ };
+ }]).config(['$routeProvider','$httpProvider', function($routeProvider, $httpProvider) {
+
$routeProvider.when('/mail', {templateUrl: 'partials/mailbox.html', controller: 'InboxesListController'});
$routeProvider.when('/login', {templateUrl: 'partials/login.html', controller: 'LoginController'});
$routeProvider.when('/thread/:id', {templateUrl: 'partials/thread.html', controller: 'ThreadController'});
$routeProvider.otherwise({redirectTo: '/login'});
- var interceptor = ['$rootScope', '$q', '$location', function (scope, $q, $location) {
- function success(response) {
- var status = response.status;
- // user is unauthorized.
- if (status == 401) {
- //window.location = "/account/login?redirectUrl=" + Base64.encode(document.URL);
- $location.path("/login");
- return;
- }
-
- return response;
- }
- function error(response) {
- // otherwise
- return $q.reject(response);
- }
- return function (promise) {
- return promise.then(success, error);
- }
- }];
- $httpProvider.responseInterceptors.push(interceptor);
+ $httpProvider.interceptors.push('errorInterceptor');
}]).run(['$rootScope', '$location', 'Auth', function($rootScope, $location, Auth) {
-/* $rootScope.$on( "$routeChangeStart", function(event, next, current) {
- console.log(Auth.loggedIn());
- if (!Auth.loggedIn()) {
- // no logged user, redirect to /login
- if (next.templateUrl != "partials/login.html") {
- $location.path("/login");
- }
- } else { // already loggedin
- if (next.templateUrl == "partials/login.html") {
- $location.path("/mail");
- }
- }
- });
-*/
}]);
/* Controllers */
-angular.module('KiteMail.controllers', ['KiteMail.services']).
- controller('InboxesListController', function($scope) {
- $scope.inboxes = [
- {"name": "Inbox", },
- {"name": "Trash", },
- {"name": "Spam", },];
-
-}).controller('MailsListController', ['$scope', 'Emails', 'Auth', function($scope, Emails, Auth) {
- console.log(Auth.username());
+angular.module('KiteMail.controllers', ['KiteMail.services'])
+.controller('InboxesListController', ['$scope', function($scope) {
+ $scope.inboxes = ["Inbox"];
+
+}]).controller('MailsListController', ['$scope', 'Emails', 'Auth', function($scope, Emails, Auth) {
+
$scope.threads = Emails.threads({username: Auth.username()});
+
}]).controller('ThreadController', ['$scope', '$routeParams', '$sce', 'Emails', 'Auth', function($scope, $routeParams, $sce, Emails, Auth) {
$scope.thread = Emails.thread({username: Auth.username(), "id": $routeParams.id});
$scope.trustHTML = function(html_code) {
/* Services */
angular.module('KiteMail.services', ['ngResource', 'ngCookies']).
-factory('Emails', ['$resource',
- function($resource) {
+factory('Emails', ['$resource', 'Utils',
+ function($resource, Utils) {
+ /* FIXME: maybe move this code to a response interceptor? */
+ var processThreads = function(data) {
+ var data = angular.fromJson(data);
+ return Utils.cleanDates(data);
+ };
+
var processThread = function(data) {
var data = angular.fromJson(data);
-
- // convert date string to js date objects
- for(var i = 0; i < data.length; i++) {
- data[i]["date"] = new Date(data[i]["date"]);
- }
-
+ data.messages = Utils.cleanDates(data.messages);
+ debugger;
return data;
- }
+ };
var res = $resource('/kite/:username/mail/:id', {}, {
threads: {
method:'GET',
params:{username: "@username"},
isArray:true,
- transformResponse: processThread,
+ transformResponse: processThreads,
},
thread: {
return {
date2array: function(date) {
return (date+'').split(' ');
- }
+ },
+
+ /* cleandates: transform every field named "date" to a js Date object */
+ cleanDates: function(data) {
+ for(var i = 0; i < data.length; i++) {
+ data[i]["date"] = new Date(data[i]["date"]);
+ }
+ return data;
+ },
};
}).factory('Auth', ['$cookies', '$http',
function($cookies, $http) {
<li>
<button class="btn btn-primary btn-compose">Compose</button>
</li>
- <li class="currentInbox">Inbox <span class=unreadMailCount">(4)</span></li>
+ <li class="currentInbox">Inbox <span class=unreadMailCount"></span></li>
<li ng-repeat="inbox in inboxes">{{inbox.name}}</li>
</ul>
</div>