-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathapp.js
239 lines (219 loc) · 6.27 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
'use strict';
angular.module('app', [])
.value('apiHost', 'http://localhost:8080')
.service('apiUrl', function(apiHost) {
return function(path) {
return apiHost + path;
}
})
.service('Api', function(apiUrl, $q, $http) {
return function(path) {
var url = apiUrl(path);
this.post = function(data) {
// CREATE
return $http.post(url, JSON.stringify(data));
};
this.get = function() {
// RETRIEVE
return $http.get(url);
};
this.put = function(data) {
// UPDATE
return $http.put(url, JSON.stringify(data));
};
this.delete = function() {
// DELETE
return $http.delete(url);
};
}
})
.service('Location', function($q) {
var getLocation = function(success, error) {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success);
} else {
error(new Error('Your browser does not support HTML5 location.'));
}
};
return function() {
this.get = function() {
var d = $q.defer();
getLocation(function(position){
d.resolve(position);
}, function(e){
d.reject(e);
});
return d.promise;
};
}
})
.service('objectArrayIndexOf', function() {
return function(arr, k, v) {
// usage: objectArrayIndexOf([{id:2},{id:1},{id:3}], 'id', 1) -> 1
for (var i = 0; i < arr.length; i++) {
if (arr[i][k] === v) return i;
}
return -1;
};
})
.directive("blob", function() {
return {
scope: {
blob: "="
},
link: function($scope, elem, attr) {
elem.bind("change", function(changeEvent) {
var reader = new FileReader();
reader.onload = function(loadEvent) {
$scope.$apply(function() {
$scope.blob = loadEvent.target.result;
});
}
reader.readAsBinaryString(changeEvent.target.files[0]);
});
}
}
})
.controller('GuestbookController', function($scope, $q, apiHost, Api, Location, objectArrayIndexOf) {
var EMPTY = {
user: {
name: null,
location: null
},
text: null,
image_ref: null
};
var locationCache = null;
var SEARCH_API = new Api('/search');
$scope.posts = [];
$scope.imagedata = null;
$scope.formdata = angular.copy(EMPTY);
$scope.apiHost = apiHost;
$scope.search = {
query_string: ''
};
var init = function() {
// try to get location from browser
var location = new Location();
location.get().then(function(pos) {
locationCache = [pos.coords.longitude, pos.coords.latitude];
$scope.formdata.user.location = angular.copy(locationCache);
}, function(e) {
console.warn(e);
window.alert(e.message);
});
// load existing posts
loadPosts();
};
var loadPosts = function() {
var api = new Api('/posts');
api.get().then(function(response) {
$scope.posts = response.data;
}, function(e) {
console.warn(e);
$scope.posts = [];
});
};
// reset input form but refill location from cache
var resetForm = function() {
$scope.formdata = angular.copy(EMPTY);
$scope.formdata.user.location = angular.copy(locationCache);
$scope.imagedata = null;
};
var uploadBlob = function(blob) {
var d = $q.defer();
var api = new Api('/images');
api.post({'blob': btoa(blob)}).then(function(response) {
d.resolve(response);
}, function(response) {
if (response.status === 409) {
d.resolve(response);
} else {
d.reject(response);
}
})
return d.promise;
};
var submitPost = function() {
var api = new Api('/posts');
api.post($scope.formdata).then(function(response) {
var posts = response.data;
for (var i=0; i<posts.length; i++) {
$scope.posts.unshift(posts[0]);
}
resetForm();
}, function(e) {
console.warn(e);
window.alert('Creating the post failed.');
})
};
var searchPosts = function() {
SEARCH_API.post($scope.search).then(function(response) {
$scope.posts = response.data;
}, function(e) {
console.warn(e);
$scope.posts = [];
});
};
// watch search input
$scope.$watch(function(scope) {
return scope.search.query_string;
}, function(newVal, oldVal) {
if (newVal === '') {
loadPosts();
} else if (newVal != oldVal) {
searchPosts();
}
});
// create new post
this.submitForm = function() {
// Not all geolocation APIs return values, some users may have disabled them.
// When there is no location detected, fill one in as a fallback.
// if (!$scope.formdata.user.location) return;
if (!$scope.formdata.user.location) $scope.formdata.user.location = [9.744417, 47.413417];
if ($scope.imagedata) {
uploadBlob($scope.imagedata).then(function(response) {
$scope.formdata.image_ref = response.data.digest;
submitPost();
}, function(e) {
console.warn(e);
window.alert('Image upload failed.');
});
} else {
submitPost();
}
};
// like an existing post
this.likePost = function(post) {
var api = new Api('/post/' + post.id + '/like');
api.put().then(function(response) {
post.like_count = response.data.like_count;
}, function(e) {
console.warn(e);
window.alert('Liking the post failed.');
});
};
// edit existing post
this.editPost = function(post) {
console.warn('editPost() is not implemented');
};
// delete existing post
this.deletePost = function(post) {
var idx = objectArrayIndexOf($scope.posts, 'id', post.id);
var api = new Api('/post/' + post.id);
api.delete().then(function(response) {
$scope.posts.splice(idx, 1);
}, function(e){
console.warn(e);
window.alert('Deleting the post failed.');
});
};
// clear search form
this.clearSearch = function() {
$scope.search = {
query_string: ''
};
};
// initialize controller
init();
});