From 2dee0493fed060b617efb3b0c115a8cf7620d2a8 Mon Sep 17 00:00:00 2001 From: karim hamidou Date: Wed, 4 Jun 2014 16:58:39 +0200 Subject: [PATCH] fixed frontend email ordering bug --- src/front/app/js/app.js | 50 ++++++++------------------ src/front/app/js/controllers.js | 16 ++++----- src/front/app/js/services.js | 32 +++++++++++------ src/front/app/partials/baselayout.html | 2 +- 4 files changed, 43 insertions(+), 57 deletions(-) diff --git a/src/front/app/js/app.js b/src/front/app/js/app.js index 2320d08..b3964f8 100644 --- a/src/front/app/js/app.js +++ b/src/front/app/js/app.js @@ -3,47 +3,25 @@ // 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"); - } - } - }); -*/ }]); diff --git a/src/front/app/js/controllers.js b/src/front/app/js/controllers.js index 60f6a35..4088503 100644 --- a/src/front/app/js/controllers.js +++ b/src/front/app/js/controllers.js @@ -2,16 +2,14 @@ /* 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) { diff --git a/src/front/app/js/services.js b/src/front/app/js/services.js index 7fa3983..8a84a1f 100644 --- a/src/front/app/js/services.js +++ b/src/front/app/js/services.js @@ -3,25 +3,27 @@ /* 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: { @@ -38,7 +40,15 @@ factory('Emails', ['$resource', 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) { diff --git a/src/front/app/partials/baselayout.html b/src/front/app/partials/baselayout.html index 7010398..e8717dd 100644 --- a/src/front/app/partials/baselayout.html +++ b/src/front/app/partials/baselayout.html @@ -27,7 +27,7 @@
  • -
  • Inbox (4)
  • +
  • Inbox
  • {{inbox.name}}
  • -- 2.43.0