/
services.js
263 lines (221 loc) · 7.92 KB
/
services.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
angular.module('starter.services', [])
/**
* A simple example service that returns some photo data.
*
* this service DOES save the information to parse
*/
.
factory('ParseImageService', function ($window) {
var _all = function () {
var query = new Parse.Query("ImageInfo");
query.descending("createdAt");
return query.find();
};
var _delete = function (_objectId) {
var query = new Parse.Query("ImageInfo");
return query.get(_objectId).then(function (_data) {
return _data.destroy();
});
};
var _get = function (_objectId) {
var query = new Parse.Query("ImageInfo");
//query.descending("gpa");
return query.get(_objectId);
};
/**
*
* @param _params
* @private
*/
var _save = function (_params) {
var ImageObject = Parse.Object.extend("ImageInfo");
if (_params.photo !== "") {
console.log("_params.photo " + _params.photo);
// create the parse file
var imageFile = new Parse.File("mypic.jpg", {base64: _params.photo});
// var imageFile = new Parse.File("mypic.jpg", _params.photo);
// save the parse file
return imageFile.save().then(function () {
_params.photo = null;
// create object to hold caption and file reference
var imageObject = new ImageObject();
// set object properties
imageObject.set("caption", _params.caption);
imageObject.set("picture", imageFile);
imageObject.set("thumbBase64", _params.thumbBase64);
imageObject.set("location", new Parse.GeoPoint(_params.coords.latitude, _params.coords.longitude));
// save object to parse backend
return imageObject.save();
}, function (error) {
console.log("Error");
console.log(error);
});
} else {
// create object to hold caption and file reference
var imageObject = new ImageObject();
// set object properties
imageObject.set("caption", _params.caption);
// save object to parse backend
return imageObject.save();
}
};
return {
all: _all,
save: _save,
get: _get,
delete: _delete,
/**
* get settings from local storage
* @returns {*}
*/
imageSettings: function () {
var savedData = $window.localStorage.getItem("application.image.props") || null;
return (savedData !== null ? JSON.parse(savedData) :
{ quality: 50, dimensions: 250, saveToAlbum: false});
},
/**
* save settings to local storage
* @param _settings
*/
saveImageSettings: function (_settings) {
$window.localStorage.setItem("application.image.props", JSON.stringify(_settings));
}
}
})
/**
* A simple example service that returns some photo data.
*
* this service does not save the information to parse, it saves
* it locally in the array imageServiceData
*/
.factory('ImageService', function ($window, $q, $timeout) {
// Might use a resource here that returns a JSON array
// Some fake testing data
var date = new Date();
var imageServiceData = [
{ id: 0, name: 'Scruff McGruff', timestamp: date},
{ id: 1, name: 'G.I. Joe', timestamp: date}
];
/**
* This is a helper model that is created to allow the application UI to work with the
* local datastore and the parse datastore without modifications
*
* @param _params
* @returns {{id: *, createdAt: *, get: get, set: set}}
* @constructor
*/
var ImageModel = function (_params) {
var _data = {};
/**
*
* @param _params
* @private
*/
this.set = function (_params) {
if (!_params) {
return;
}
console.log(_params.timestamp);
this.id = _params.id;
this.createdAt = _params.timestamp;
_data['createdAt'] = _params.timestamp;
_data.caption = _params.name;
_data.id = _params.id;
_data.picture = { _url: _params.data};
_data.location = _params.location
};
/**
*
* @param _key
* @returns {*}
* @private
*/
this.get = function (_key) {
return _data[_key]
};
this.set(_params);
/*
return {
'id': _data.id,
'createdAt': _data.name,
get: this._get,
set: this._set
};
*/
};
return {
/**
* get settings from local storage
* @returns {*}
*/
imageSettings: function () {
var savedData = $window.localStorage.getItem("application.image.props") || null;
return (savedData !== null ? JSON.parse(savedData) :
{ quality: 85, dimensions: 350, saveToAlbum: false});
},
/**
* save settings to local storage
* @param _settings
*/
saveImageSettings: function (_settings) {
$window.localStorage.setItem("application.image.props", JSON.stringify(_settings));
},
/**
*
* @returns {Array}
*/
all: function () {
var deferred = $q.defer();
var returnArray = [];
angular.forEach(imageServiceData, function (_value, _key) {
_value.data && console.log("length " + _value.data.length);
this.push(new ImageModel(_value));
}, returnArray);
$timeout(function () {
deferred.resolve(returnArray);
}, 1);
return deferred.promise;
},
/**
*
* @param friendId
* @returns {*|{id: number, name: string}}
*/
get: function (imageId) {
// Simple index lookup
var deferred = $q.defer();
$timeout(function () {
deferred.resolve(new ImageModel(imageServiceData[imageId]));
}, 1);
return deferred.promise;
},
/**
*
* @param _params.caption
* @param _params.photo
*/
save: function (_params) {
var deferred = $q.defer();
var id = imageServiceData.length;
$timeout(function () {
deferred.resolve(imageServiceData.push({
id: id,
name: _params.caption,
data: 'data:image/png;base64,' + _params.photo,
timestamp: new Date()
}));
}, 1);
return deferred.promise;
},
delete: function (_params) {
var deferred = $q.defer();
$timeout(function () {
imageServiceData.splice(_params.index, 1)
deferred.resolve();
}, 1);
return deferred.promise;
}
}
}
)
;