Calendar.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. "use strict";
  2. function Calendar() {
  3. }
  4. Calendar.prototype.getCreateCalendarOptions = function () {
  5. return {
  6. calendarName: null,
  7. calendarColor: null // optional, the OS will choose one if left empty, example: pass "#FF0000" for red
  8. };
  9. };
  10. Calendar.prototype.hasReadPermission = function (callback) {
  11. cordova.exec(callback, null, "Calendar", "hasReadPermission", []);
  12. };
  13. Calendar.prototype.requestReadPermission = function (callback) {
  14. cordova.exec(callback, null, "Calendar", "requestReadPermission", []);
  15. };
  16. Calendar.prototype.hasWritePermission = function (callback) {
  17. cordova.exec(callback, null, "Calendar", "hasWritePermission", []);
  18. };
  19. Calendar.prototype.requestWritePermission = function (callback) {
  20. cordova.exec(callback, null, "Calendar", "requestWritePermission", []);
  21. };
  22. Calendar.prototype.hasReadWritePermission = function (callback) {
  23. cordova.exec(callback, null, "Calendar", "hasReadWritePermission", []);
  24. };
  25. Calendar.prototype.requestReadWritePermission = function (callback) {
  26. cordova.exec(callback, null, "Calendar", "requestReadWritePermission", []);
  27. };
  28. Calendar.prototype.createCalendar = function (calendarNameOrOptionsObject, successCallback, errorCallback) {
  29. var options;
  30. if (typeof calendarNameOrOptionsObject == "string") {
  31. options = {
  32. "calendarName": calendarNameOrOptionsObject
  33. };
  34. } else {
  35. options = calendarNameOrOptionsObject;
  36. }
  37. // merge passed options with defaults
  38. var mergedOptions = Calendar.prototype.getCreateCalendarOptions();
  39. for (var val in options) {
  40. if (options.hasOwnProperty(val)) {
  41. mergedOptions[val] = options[val];
  42. }
  43. }
  44. cordova.exec(successCallback, errorCallback, "Calendar", "createCalendar", [mergedOptions]);
  45. };
  46. Calendar.prototype.deleteCalendar = function (calendarName, successCallback, errorCallback) {
  47. cordova.exec(successCallback, errorCallback, "Calendar", "deleteCalendar", [{
  48. "calendarName": calendarName
  49. }]);
  50. };
  51. Calendar.prototype.openCalendar = function (date, successCallback, errorCallback) {
  52. // default: today
  53. if (!(date instanceof Date)) {
  54. date = new Date();
  55. }
  56. cordova.exec(successCallback, errorCallback, "Calendar", "openCalendar", [{
  57. "date": date.getTime()
  58. }]);
  59. };
  60. Calendar.prototype.getCalendarOptions = function () {
  61. return {
  62. firstReminderMinutes: 60,
  63. secondReminderMinutes: null,
  64. recurrence: null, // options are: 'daily', 'weekly', 'monthly', 'yearly'
  65. recurrenceInterval: 1, // only used when recurrence is set
  66. recurrenceWeekstart: "MO",
  67. recurrenceByDay: null,
  68. recurrenceByMonthDay: null,
  69. recurrenceEndDate: null,
  70. recurrenceCount: null,
  71. calendarName: null,
  72. calendarId: null,
  73. url: null
  74. };
  75. };
  76. /**
  77. * This method can be used if you want more control over the event details.
  78. * Pass in an options object which you can easily override as follow:
  79. * var options = window.plugins.calendar.getCalendarOptions();
  80. * options.firstReminderMinutes = 150;
  81. */
  82. Calendar.prototype.createEventWithOptions = function (title, location, notes, startDate, endDate, options, successCallback, errorCallback) {
  83. if (!(startDate instanceof Date && endDate instanceof Date)) {
  84. errorCallback("startDate and endDate must be JavaScript Date Objects");
  85. return;
  86. }
  87. // merge passed options with defaults
  88. var mergedOptions = Calendar.prototype.getCalendarOptions();
  89. for (var val in options) {
  90. if (options.hasOwnProperty(val)) {
  91. mergedOptions[val] = options[val];
  92. }
  93. }
  94. if (options.recurrenceEndDate != null) {
  95. mergedOptions.recurrenceEndTime = options.recurrenceEndDate.getTime();
  96. }
  97. cordova.exec(successCallback, errorCallback, "Calendar", "createEventWithOptions", [{
  98. "title": title,
  99. "location": location,
  100. "notes": notes,
  101. "startTime": startDate instanceof Date ? startDate.getTime() : null,
  102. "endTime": endDate instanceof Date ? endDate.getTime() : null,
  103. "options": mergedOptions
  104. }]);
  105. };
  106. /**
  107. * @deprecated use createEventWithOptions instead
  108. */
  109. Calendar.prototype.createEventInNamedCalendar = function (title, location, notes, startDate, endDate, calendarName, successCallback, errorCallback) {
  110. Calendar.prototype.createEventWithOptions(title, location, notes, startDate, endDate, {calendarName:calendarName}, successCallback, errorCallback);
  111. };
  112. Calendar.prototype.createEvent = function (title, location, notes, startDate, endDate, successCallback, errorCallback) {
  113. Calendar.prototype.createEventWithOptions(title, location, notes, startDate, endDate, {}, successCallback, errorCallback);
  114. };
  115. Calendar.prototype.createEventInteractively = function (title, location, notes, startDate, endDate, successCallback, errorCallback) {
  116. Calendar.prototype.createEventInteractivelyWithOptions(title, location, notes, startDate, endDate, {}, successCallback, errorCallback);
  117. };
  118. Calendar.prototype.createEventInteractivelyWithOptions = function (title, location, notes, startDate, endDate, options, successCallback, errorCallback) {
  119. // merge passed options with defaults
  120. var mergedOptions = Calendar.prototype.getCalendarOptions();
  121. for (var val in options) {
  122. if (options.hasOwnProperty(val)) {
  123. mergedOptions[val] = options[val];
  124. }
  125. }
  126. if (options.recurrenceEndDate != null) {
  127. mergedOptions.recurrenceEndTime = options.recurrenceEndDate.getTime();
  128. }
  129. cordova.exec(successCallback, errorCallback, "Calendar", "createEventInteractively", [{
  130. "title": title,
  131. "location": location,
  132. "notes": notes,
  133. "startTime": startDate instanceof Date ? startDate.getTime() : null,
  134. "endTime": endDate instanceof Date ? endDate.getTime() : null,
  135. "options": mergedOptions
  136. }])
  137. };
  138. Calendar.prototype.findEventWithOptions = function (title, location, notes, startDate, endDate, options, successCallback, errorCallback) {
  139. // merge passed options with defaults
  140. var mergedOptions = Calendar.prototype.getCalendarOptions();
  141. for (var val in options) {
  142. if (options.hasOwnProperty(val)) {
  143. mergedOptions[val] = options[val];
  144. }
  145. }
  146. if (options.recurrenceEndDate != null) {
  147. mergedOptions.recurrenceEndTime = options.recurrenceEndDate.getTime();
  148. }
  149. cordova.exec(successCallback, errorCallback, "Calendar", "findEventWithOptions", [{
  150. "title": title,
  151. "location": location,
  152. "notes": notes,
  153. "startTime": startDate instanceof Date ? startDate.getTime() : null,
  154. "endTime": endDate instanceof Date ? endDate.getTime() : null,
  155. "options": mergedOptions
  156. }])
  157. };
  158. Calendar.prototype.findEvent = function (title, location, notes, startDate, endDate, successCallback, errorCallback) {
  159. Calendar.prototype.findEventWithOptions(title, location, notes, startDate, endDate, {}, successCallback, errorCallback);
  160. };
  161. Calendar.prototype.findAllEventsInNamedCalendar = function (calendarName, successCallback, errorCallback) {
  162. cordova.exec(successCallback, errorCallback, "Calendar", "findAllEventsInNamedCalendar", [{
  163. "calendarName": calendarName
  164. }]);
  165. };
  166. Calendar.prototype.deleteEvent = function (title, location, notes, startDate, endDate, successCallback, errorCallback) {
  167. if (!(startDate instanceof Date && endDate instanceof Date)) {
  168. errorCallback("startDate and endDate must be JavaScript Date Objects");
  169. }
  170. cordova.exec(successCallback, errorCallback, "Calendar", "deleteEvent", [{
  171. "title": title,
  172. "location": location,
  173. "notes": notes,
  174. "startTime": startDate instanceof Date ? startDate.getTime() : null,
  175. "endTime": endDate instanceof Date ? endDate.getTime() : null
  176. }])
  177. };
  178. Calendar.prototype.deleteEventFromNamedCalendar = function (title, location, notes, startDate, endDate, calendarName, successCallback, errorCallback) {
  179. cordova.exec(successCallback, errorCallback, "Calendar", "deleteEventFromNamedCalendar", [{
  180. "title": title,
  181. "location": location,
  182. "notes": notes,
  183. "startTime": startDate instanceof Date ? startDate.getTime() : null,
  184. "endTime": endDate instanceof Date ? endDate.getTime() : null,
  185. "calendarName": calendarName
  186. }])
  187. };
  188. Calendar.prototype.modifyEventWithOptions = function (title, location, notes, startDate, endDate, newTitle, newLocation, newNotes, newStartDate, newEndDate, options, newOptions, successCallback, errorCallback) {
  189. if (!(newStartDate instanceof Date && newEndDate instanceof Date)) {
  190. errorCallback("newStartDate and newEndDate must be JavaScript Date Objects");
  191. return;
  192. }
  193. // merge passed options with defaults
  194. var mergedOptions = Calendar.prototype.getCalendarOptions();
  195. for (var val in options) {
  196. if (options.hasOwnProperty(val)) {
  197. mergedOptions[val] = options[val];
  198. }
  199. }
  200. if (options.recurrenceEndDate != null) {
  201. mergedOptions.recurrenceEndTime = options.recurrenceEndDate.getTime();
  202. }
  203. // and also merge passed newOptions with defaults
  204. var newMergedOptions = Calendar.prototype.getCalendarOptions();
  205. for (var val2 in newOptions) {
  206. if (newOptions.hasOwnProperty(val2)) {
  207. newMergedOptions[val2] = newOptions[val2];
  208. }
  209. }
  210. if (newOptions.recurrenceEndDate != null) {
  211. newMergedOptions.recurrenceEndTime = newOptions.recurrenceEndDate.getTime();
  212. }
  213. cordova.exec(successCallback, errorCallback, "Calendar", "modifyEventWithOptions", [{
  214. "title": title,
  215. "location": location,
  216. "notes": notes,
  217. "startTime": startDate instanceof Date ? startDate.getTime() : null,
  218. "endTime": endDate instanceof Date ? endDate.getTime() : null,
  219. "newTitle": newTitle,
  220. "newLocation": newLocation,
  221. "newNotes": newNotes,
  222. "newStartTime": newStartDate instanceof Date ? newStartDate.getTime() : null,
  223. "newEndTime": newEndDate instanceof Date ? newEndDate.getTime() : null,
  224. "options": mergedOptions,
  225. "newOptions": newMergedOptions
  226. }])
  227. };
  228. Calendar.prototype.modifyEvent = function (title, location, notes, startDate, endDate, newTitle, newLocation, newNotes, newStartDate, newEndDate, successCallback, errorCallback) {
  229. Calendar.prototype.modifyEventWithOptions(title, location, notes, startDate, endDate, newTitle, newLocation, newNotes, newStartDate, newEndDate, {}, successCallback, errorCallback);
  230. };
  231. Calendar.prototype.modifyEventInNamedCalendar = function (title, location, notes, startDate, endDate, newTitle, newLocation, newNotes, newStartDate, newEndDate, calendarName, successCallback, errorCallback) {
  232. var options = Calendar.prototype.getCalendarOptions();
  233. options.calendarName = calendarName;
  234. Calendar.prototype.modifyEventWithOptions(title, location, notes, startDate, endDate, newTitle, newLocation, newNotes, newStartDate, newEndDate, options, successCallback, errorCallback);
  235. };
  236. Calendar.prototype.listEventsInRange = function (startDate, endDate, successCallback, errorCallback) {
  237. cordova.exec(successCallback, errorCallback, "Calendar", "listEventsInRange", [{
  238. "startTime": startDate instanceof Date ? startDate.getTime() : null,
  239. "endTime": endDate instanceof Date ? endDate.getTime() : null
  240. }])
  241. };
  242. Calendar.prototype.listCalendars = function (successCallback, errorCallback) {
  243. cordova.exec(successCallback, errorCallback, "Calendar", "listCalendars", []);
  244. };
  245. Calendar.install = function () {
  246. if (!window.plugins) {
  247. window.plugins = {};
  248. }
  249. window.plugins.calendar = new Calendar();
  250. return window.plugins.calendar;
  251. };
  252. cordova.addConstructor(Calendar.install);