~uriboni/+junk/artchive

« back to all changes in this revision

Viewing changes to public/js/controllers.js

  • Committer: Ugo Riboni
  • Date: 2015-08-24 00:46:09 UTC
  • Revision ID: ugo.riboni@canonical.com-20150824004609-vud9svllkj6wcawl
Initial commit. Shows list of years and list of projects per year

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
var app = angular.module('artchivistApp', ['ui.bootstrap'])
 
2
.filter('format', function() {
 
3
  return function(input, format, placeholder) {
 
4
        if (input) {
 
5
                if (placeholder === undefined) placeholder = "##"
 
6
                else placeholder = String(placeholder)
 
7
                return String(format).replace(placeholder, String(input))
 
8
        } else return ""
 
9
  };
 
10
})
 
11
 
 
12
function handleResponse(data, $scope) {
 
13
        console.log(data)
 
14
        $scope.login_display = false
 
15
        $scope.error_display = false
 
16
        if (data.error) {
 
17
                if (data.error == "need_login") $scope.login_display = true
 
18
                else {
 
19
                        $scope.error_display = true
 
20
                        $scope.error_data = data.data
 
21
                }
 
22
                return null
 
23
        } return data.Response
 
24
}
 
25
 
 
26
function parseKeyVal(data) {
 
27
        return _.reduce(data.split("\n"), function(map, line) {
 
28
                line = line.split("=")
 
29
                if (line.length < 2) return map;
 
30
 
 
31
                map[line[0].trim()] = line[1].trim()
 
32
                return map
 
33
        }, {})
 
34
}
 
35
 
 
36
app.controller('ArchiveCtrl', function ($scope, $http, $timeout) {
 
37
        $scope.currentYear = ''
 
38
        $scope.currentProject = ''
 
39
 
 
40
        $scope.$watch('currentYear', function(value, previous) {
 
41
                if (value != previous) $scope.loadYearProjects()
 
42
        })
 
43
 
 
44
        $scope.$watch('currentProject', function(value, previous) {
 
45
                console.log("project changed", value, previous)
 
46
                if (value != previous) $scope.loadProject()
 
47
        })
 
48
 
 
49
        $scope.yearListLoading = true
 
50
        $http.get('/years').success(function(data) {
 
51
                $scope.yearListLoading = false
 
52
                var res = handleResponse(data, $scope)
 
53
                if (!res) return;
 
54
 
 
55
                var groups = _.groupBy(res.Album, function(album) {
 
56
                        return album.NiceName.indexOf('Info-') == 0 ? "info" : "photo"
 
57
                })
 
58
                var all = _.indexBy(_.map(groups.info, function(info) {
 
59
                        var item = parseKeyVal(info.Description)
 
60
                        item.year = parseInt(info.NiceName.replace(/^Info-/,''), 10)
 
61
                        item.key = _.last(info.Uri.split("/"))
 
62
                        return item
 
63
                }), 'year')
 
64
                _.each(groups.photo, function(album) {
 
65
                        var year = parseInt(album.NiceName.replace(/^Nowhere-/,''), 10)
 
66
                        if (all[year]) all[year].album = album
 
67
                })
 
68
 
 
69
                $scope.years = _.sortBy(_.toArray(all), "year").reverse()
 
70
                $scope.currentYear = _.first($scope.years).year
 
71
                if ($scope.currentYear) $scope.loadYearProjects()
 
72
        })
 
73
 
 
74
        $scope.loadYearProjects = function() {
 
75
                $scope.yearLoading = true
 
76
                console.log("Loading projects for " + $scope.currentYear)
 
77
                var album = _.find($scope.years, function(year) { return year.year == $scope.currentYear })
 
78
                $http.get('/projects/' + album.key).success(function(data) {
 
79
                        $scope.yearLoading = false
 
80
                        var res = handleResponse(data, $scope)
 
81
                        if (!res) return;
 
82
 
 
83
                        $scope.projects = _.map(res.AlbumImage, function(image) {
 
84
                                var item = parseKeyVal(image.Caption)
 
85
                                item.thumbnail = image.ThumbnailUrl
 
86
                                item.key = image.ImageKey
 
87
                                return item
 
88
                        })
 
89
            })
 
90
        }
 
91
 
 
92
        $scope.loadProject = function() {
 
93
                $scope.projectLoading = true
 
94
 
 
95
        }
 
96
});
 
97