forked from mozilla-b2g/gaia
/
local.js
129 lines (103 loc) · 2.9 KB
/
local.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
Calendar.ns('Provider').Local = (function() {
const LOCAL_CALENDAR_ID = 'local-first';
function Local() {
Calendar.Provider.Abstract.apply(this, arguments);
this.events = this.app.store('Event');
this.busytimes = this.app.store('Busytime');
this.alarms = this.app.store('Alarm');
}
Local.calendarId = LOCAL_CALENDAR_ID;
/**
* Returns the details for the default calendars.
*/
Local.defaultCalendar = function() {
//XXX: Make async
var l10nId = 'calendar-local';
var list = {};
var name;
if ('mozL10n' in window.navigator) {
name = window.navigator.mozL10n.get(l10nId);
if (name === l10nId) {
name = null;
}
}
if (!name) {
name = 'Offline calendar';
}
return {
// XXX localize this name somewhere
name: name,
id: LOCAL_CALENDAR_ID,
color: '#D2642A'
};
};
Local.prototype = {
__proto__: Calendar.Provider.Abstract.prototype,
canExpandRecurringEvents: false,
getAccount: function(account, callback) {
callback(null, {});
},
findCalendars: function(account, callback) {
var list = {};
list[LOCAL_CALENDAR_ID] = Local.defaultCalendar();
callback(null, list);
},
syncEvents: function(account, calendar, cb) {
cb(null);
},
/**
* @return {Calendar.EventMutations.Create} mutation object.
*/
createEvent: function(event, callback) {
// most providers come with their own built in
// id system when creating a local event we need to generate
// our own UUID.
if (!event.remote.id) {
// TOOD: uuid is provided by ext/uuid.js
// if/when platform supports a safe
// random number generator (values never conflict)
// we can use that instead of uuid.js
event.remote.id = uuid();
}
var create = new Calendar.EventMutations.create({
event: event
});
create.commit(function(err) {
if (err) {
callback(err);
return;
}
callback(null, create.busytime, create.event);
});
return create;
},
deleteEvent: function(event, busytime, callback) {
if (typeof(busytime) === 'function') {
callback = busytime;
busytime = null;
}
this.app.store('Event').remove(event._id, callback);
},
/**
* @return {Calendar.EventMutations.Update} mutation object.
*/
updateEvent: function(event, busytime, callback) {
if (typeof(busytime) === 'function') {
callback = busytime;
busytime = null;
}
var update = Calendar.EventMutations.update({
event: event
});
update.commit(function(err) {
if (err) {
callback(err);
return;
}
callback(null, update.busytime, update.event);
});
return update;
}
};
return Local;
}());