local-notification.js 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. /*
  2. * Apache 2.0 License
  3. *
  4. * Copyright (c) Sebastian Katzer 2017
  5. *
  6. * This file contains Original Code and/or Modifications of Original Code
  7. * as defined in and that are subject to the Apache License
  8. * Version 2.0 (the 'License'). You may not use this file except in
  9. * compliance with the License. Please obtain a copy of the License at
  10. * http://opensource.org/licenses/Apache-2.0/ and read it before using this
  11. * file.
  12. *
  13. * The Original Code and all software distributed under the License are
  14. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  15. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  16. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  18. * Please see the License for the specific language governing rights and
  19. * limitations under the License.
  20. */
  21. var exec = require('cordova/exec'),
  22. channel = require('cordova/channel');
  23. // Defaults
  24. exports._defaults = {
  25. actions : [],
  26. attachments : [],
  27. autoClear : true,
  28. badge : null,
  29. channel : null,
  30. clock : true,
  31. color : null,
  32. data : null,
  33. defaults : 0,
  34. foreground : null,
  35. group : null,
  36. groupSummary : false,
  37. icon : null,
  38. id : 0,
  39. launch : true,
  40. led : true,
  41. lockscreen : true,
  42. mediaSession : null,
  43. number : 0,
  44. priority : 0,
  45. progressBar : false,
  46. silent : false,
  47. smallIcon : 'res://icon',
  48. sound : true,
  49. sticky : false,
  50. summary : null,
  51. text : '',
  52. timeoutAfter : false,
  53. title : '',
  54. trigger : { type : 'calendar' },
  55. vibrate : false,
  56. wakeup : true
  57. };
  58. // Event listener
  59. exports._listener = {};
  60. /**
  61. * Check permission to show notifications.
  62. *
  63. * @param [ Function ] callback The function to be exec as the callback.
  64. * @param [ Object ] scope The callback function's scope.
  65. *
  66. * @return [ Void ]
  67. */
  68. exports.hasPermission = function (callback, scope) {
  69. this._exec('check', null, callback, scope);
  70. };
  71. /**
  72. * Request permission to show notifications.
  73. *
  74. * @param [ Function ] callback The function to be exec as the callback.
  75. * @param [ Object ] scope The callback function's scope.
  76. *
  77. * @return [ Void ]
  78. */
  79. exports.requestPermission = function (callback, scope) {
  80. this._exec('request', null, callback, scope);
  81. };
  82. /**
  83. * Schedule notifications.
  84. *
  85. * @param [ Array ] notifications The notifications to schedule.
  86. * @param [ Function ] callback The function to be exec as the callback.
  87. * @param [ Object ] scope The callback function's scope.
  88. * @param [ Object ] args Optional flags how to schedule.
  89. *
  90. * @return [ Void ]
  91. */
  92. exports.schedule = function (msgs, callback, scope, args) {
  93. var fn = function (granted) {
  94. var toasts = this._toArray(msgs);
  95. if (!granted && callback) {
  96. callback.call(scope || this, false);
  97. return;
  98. }
  99. for (var i = 0, len = toasts.length; i < len; i++) {
  100. var toast = toasts[i];
  101. this._mergeWithDefaults(toast);
  102. this._convertProperties(toast);
  103. }
  104. this._exec('schedule', toasts, callback, scope);
  105. };
  106. if (args && args.skipPermission) {
  107. fn.call(this, true);
  108. } else {
  109. this.requestPermission(fn, this);
  110. }
  111. };
  112. /**
  113. * Schedule notifications.
  114. *
  115. * @param [ Array ] notifications The notifications to schedule.
  116. * @param [ Function ] callback The function to be exec as the callback.
  117. * @param [ Object ] scope The callback function's scope.
  118. * @param [ Object ] args Optional flags how to schedule.
  119. *
  120. * @return [ Void ]
  121. */
  122. exports.update = function (msgs, callback, scope, args) {
  123. var fn = function(granted) {
  124. var toasts = this._toArray(msgs);
  125. if (!granted && callback) {
  126. callback.call(scope || this, false);
  127. return;
  128. }
  129. for (var i = 0, len = toasts.length; i < len; i++) {
  130. this._convertProperties(toasts[i]);
  131. }
  132. this._exec('update', toasts, callback, scope);
  133. };
  134. if (args && args.skipPermission) {
  135. fn.call(this, true);
  136. } else {
  137. this.requestPermission(fn, this);
  138. }
  139. };
  140. /**
  141. * Clear the specified notifications by id.
  142. *
  143. * @param [ Array<Int> ] ids The IDs of the notifications.
  144. * @param [ Function ] callback The function to be exec as the callback.
  145. * @param [ Object ] scope The callback function's scope.
  146. *
  147. * @return [ Void ]
  148. */
  149. exports.clear = function (ids, callback, scope) {
  150. ids = this._toArray(ids);
  151. ids = this._convertIds(ids);
  152. this._exec('clear', ids, callback, scope);
  153. };
  154. /**
  155. * Clear all triggered notifications.
  156. *
  157. * @param [ Function ] callback The function to be exec as the callback.
  158. * @param [ Object ] scope The callback function's scope.
  159. *
  160. * @return [ Void ]
  161. */
  162. exports.clearAll = function (callback, scope) {
  163. this._exec('clearAll', null, callback, scope);
  164. };
  165. /**
  166. * Clear the specified notifications by id.
  167. *
  168. * @param [ Array<Int> ] ids The IDs of the notifications.
  169. * @param [ Function ] callback The function to be exec as the callback.
  170. * @param [ Object ] scope The callback function's scope.
  171. *
  172. * @return [ Void ]
  173. */
  174. exports.cancel = function (ids, callback, scope) {
  175. ids = this._toArray(ids);
  176. ids = this._convertIds(ids);
  177. this._exec('cancel', ids, callback, scope);
  178. };
  179. /**
  180. * Cancel all scheduled notifications.
  181. *
  182. * @param [ Function ] callback The function to be exec as the callback.
  183. * @param [ Object ] scope The callback function's scope.
  184. *
  185. * @return [ Void ]
  186. */
  187. exports.cancelAll = function (callback, scope) {
  188. this._exec('cancelAll', null, callback, scope);
  189. };
  190. /**
  191. * Check if a notification is present.
  192. *
  193. * @param [ Int ] id The ID of the notification.
  194. * @param [ Function ] callback The function to be exec as the callback.
  195. * @param [ Object ] scope The callback function's scope.
  196. *
  197. * @return [ Void ]
  198. */
  199. exports.isPresent = function (id, callback, scope) {
  200. var fn = this._createCallbackFn(callback, scope);
  201. this.getType(id, function (type) {
  202. fn(type != 'unknown');
  203. });
  204. };
  205. /**
  206. * Check if a notification is scheduled.
  207. *
  208. * @param [ Int ] id The ID of the notification.
  209. * @param [ Function ] callback The function to be exec as the callback.
  210. * @param [ Object ] scope The callback function's scope.
  211. *
  212. * @return [ Void ]
  213. */
  214. exports.isScheduled = function (id, callback, scope) {
  215. this.hasType(id, 'scheduled', callback, scope);
  216. };
  217. /**
  218. * Check if a notification was triggered.
  219. *
  220. * @param [ Int ] id The ID of the notification.
  221. * @param [ Function ] callback The function to be exec as the callback.
  222. * @param [ Object ] scope The callback function's scope.
  223. *
  224. * @return [ Void ]
  225. */
  226. exports.isTriggered = function (id, callback, scope) {
  227. this.hasType(id, 'triggered', callback, scope);
  228. };
  229. /**
  230. * Check if a notification has a given type.
  231. *
  232. * @param [ Int ] id The ID of the notification.
  233. * @param [ String ] type The type of the notification.
  234. * @param [ Function ] callback The function to be exec as the callback.
  235. * @param [ Object ] scope The callback function's scope.
  236. *
  237. * @return [ Void ]
  238. */
  239. exports.hasType = function (id, type, callback, scope) {
  240. var fn = this._createCallbackFn(callback, scope);
  241. this.getType(id, function (type2) {
  242. fn(type == type2);
  243. });
  244. };
  245. /**
  246. * Get the type (triggered, scheduled) for the notification.
  247. *
  248. * @param [ Int ] id The ID of the notification.
  249. * @param [ Function ] callback The function to be exec as the callback.
  250. * @param [ Object ] scope The callback function's scope.
  251. *
  252. * @return [ Void ]
  253. */
  254. exports.getType = function (id, callback, scope) {
  255. this._exec('type', id, callback, scope);
  256. };
  257. /**
  258. * List of all notification ids.
  259. *
  260. * @param [ Function ] callback The function to be exec as the callback.
  261. * @param [ Object ] scope The callback function's scope.
  262. *
  263. * @return [ Void ]
  264. */
  265. exports.getIds = function (callback, scope) {
  266. this._exec('ids', 0, callback, scope);
  267. };
  268. /**
  269. * List of all scheduled notification IDs.
  270. *
  271. * @param [ Function ] callback The function to be exec as the callback.
  272. * @param [ Object ] scope The callback function's scope.
  273. *
  274. * @return [ Void ]
  275. */
  276. exports.getScheduledIds = function (callback, scope) {
  277. this._exec('ids', 1, callback, scope);
  278. };
  279. /**
  280. * List of all triggered notification IDs.
  281. *
  282. * @param [ Function ] callback The function to be exec as the callback.
  283. * @param [ Object ] scope The callback function's scope.
  284. *
  285. * @return [ Void ]
  286. */
  287. exports.getTriggeredIds = function (callback, scope) {
  288. this._exec('ids', 2, callback, scope);
  289. };
  290. /**
  291. * List of local notifications specified by id.
  292. * If called without IDs, all notification will be returned.
  293. *
  294. * @param [ Array<Int> ] ids The IDs of the notifications.
  295. * @param [ Function ] callback The function to be exec as the callback.
  296. * @param [ Object ] scope The callback function's scope.
  297. *
  298. * @return [ Void ]
  299. */
  300. exports.get = function () {
  301. var args = Array.apply(null, arguments);
  302. if (typeof args[0] == 'function') {
  303. args.unshift([]);
  304. }
  305. var ids = args[0],
  306. callback = args[1],
  307. scope = args[2];
  308. if (!Array.isArray(ids)) {
  309. this._exec('notification', Number(ids), callback, scope);
  310. return;
  311. }
  312. ids = this._convertIds(ids);
  313. this._exec('notifications', [3, ids], callback, scope);
  314. };
  315. /**
  316. * List for all notifications.
  317. *
  318. * @param [ Function ] callback The function to be exec as the callback.
  319. * @param [ Object ] scope The callback function's scope.
  320. *
  321. * @return [ Void ]
  322. */
  323. exports.getAll = function (callback, scope) {
  324. this._exec('notifications', 0, callback, scope);
  325. };
  326. /**
  327. * List of all scheduled notifications.
  328. *
  329. * @param [ Function ] callback The function to be exec as the callback.
  330. * @param [ Object ] scope The callback function's scope.
  331. */
  332. exports.getScheduled = function (callback, scope) {
  333. this._exec('notifications', 1, callback, scope);
  334. };
  335. /**
  336. * List of all triggered notifications.
  337. *
  338. * @param [ Function ] callback The function to be exec as the callback.
  339. * @param [ Object ] scope The callback function's scope.
  340. */
  341. exports.getTriggered = function (callback, scope) {
  342. this._exec('notifications', 2, callback, scope);
  343. };
  344. /**
  345. * Add an group of actions by id.
  346. *
  347. * @param [ String ] id The Id of the group.
  348. * @param [ Array] actions The action config settings.
  349. * @param [ Function ] callback The function to be exec as the callback.
  350. * @param [ Object ] scope The callback function's scope.
  351. *
  352. * @return [ Void ]
  353. */
  354. exports.addActions = function (id, actions, callback, scope) {
  355. this._exec('actions', [0, id, actions], callback, scope);
  356. };
  357. /**
  358. * Remove an group of actions by id.
  359. *
  360. * @param [ String ] id The Id of the group.
  361. * @param [ Function ] callback The function to be exec as the callback.
  362. * @param [ Object ] scope The callback function's scope.
  363. *
  364. * @return [ Void ]
  365. */
  366. exports.removeActions = function (id, callback, scope) {
  367. this._exec('actions', [1, id], callback, scope);
  368. };
  369. /**
  370. * Check if a group of actions is defined.
  371. *
  372. * @param [ String ] id The Id of the group.
  373. * @param [ Function ] callback The function to be exec as the callback.
  374. * @param [ Object ] scope The callback function's scope.
  375. *
  376. * @return [ Void ]
  377. */
  378. exports.hasActions = function (id, callback, scope) {
  379. this._exec('actions', [2, id], callback, scope);
  380. };
  381. /**
  382. * The (platform specific) default settings.
  383. *
  384. * @return [ Object ]
  385. */
  386. exports.getDefaults = function () {
  387. var map = Object.assign({}, this._defaults);
  388. for (var key in map) {
  389. if (Array.isArray(map[key])) {
  390. map[key] = Array.from(map[key]);
  391. } else
  392. if (Object.prototype.isPrototypeOf(map[key])) {
  393. map[key] = Object.assign({}, map[key]);
  394. }
  395. }
  396. return map;
  397. };
  398. /**
  399. * Overwrite default settings.
  400. *
  401. * @param [ Object ] newDefaults New default values.
  402. *
  403. * @return [ Void ]
  404. */
  405. exports.setDefaults = function (newDefaults) {
  406. Object.assign(this._defaults, newDefaults);
  407. };
  408. /**
  409. * Register callback for given event.
  410. *
  411. * @param [ String ] event The name of the event.
  412. * @param [ Function ] callback The function to be exec as callback.
  413. * @param [ Object ] scope The callback function's scope.
  414. *
  415. * @return [ Void ]
  416. */
  417. exports.on = function (event, callback, scope) {
  418. var type = typeof callback;
  419. if (type !== 'function' && type !== 'string')
  420. return;
  421. if (!this._listener[event]) {
  422. this._listener[event] = [];
  423. }
  424. var item = [callback, scope || window];
  425. this._listener[event].push(item);
  426. };
  427. /**
  428. * Unregister callback for given event.
  429. *
  430. * @param [ String ] event The name of the event.
  431. * @param [ Function ] callback The function to be exec as callback.
  432. *
  433. * @return [ Void ]
  434. */
  435. exports.un = function (event, callback) {
  436. var listener = this._listener[event];
  437. if (!listener)
  438. return;
  439. for (var i = 0; i < listener.length; i++) {
  440. var fn = listener[i][0];
  441. if (fn == callback) {
  442. listener.splice(i, 1);
  443. break;
  444. }
  445. }
  446. };
  447. /**
  448. * Fire the event with given arguments.
  449. *
  450. * @param [ String ] event The event's name.
  451. * @param [ *Array] args The callback's arguments.
  452. *
  453. * @return [ Void]
  454. */
  455. exports.fireEvent = function (event) {
  456. var args = Array.apply(null, arguments).slice(1),
  457. listener = this._listener[event];
  458. if (!listener)
  459. return;
  460. if (args[0] && typeof args[0].data === 'string') {
  461. args[0].data = JSON.parse(args[0].data);
  462. }
  463. for (var i = 0; i < listener.length; i++) {
  464. var fn = listener[i][0],
  465. scope = listener[i][1];
  466. if (typeof fn !== 'function') {
  467. fn = scope[fn];
  468. }
  469. fn.apply(scope, args);
  470. }
  471. };
  472. /**
  473. * Fire queued events once the device is ready and all listeners are registered.
  474. *
  475. * @return [ Void ]
  476. */
  477. exports.fireQueuedEvents = function() {
  478. exports._exec('ready');
  479. };
  480. /**
  481. * Merge custom properties with the default values.
  482. *
  483. * @param [ Object ] options Set of custom values.
  484. *
  485. * @retrun [ Object ]
  486. */
  487. exports._mergeWithDefaults = function (options) {
  488. var values = this.getDefaults();
  489. if (values.hasOwnProperty('sticky')) {
  490. options.sticky = this._getValueFor(options, 'sticky', 'ongoing');
  491. }
  492. if (options.sticky && options.autoClear !== true) {
  493. options.autoClear = false;
  494. }
  495. Object.assign(values, options);
  496. for (var key in values) {
  497. if (values[key] !== null) {
  498. options[key] = values[key];
  499. } else {
  500. delete options[key];
  501. }
  502. if (!this._defaults.hasOwnProperty(key)) {
  503. console.warn('Unknown property: ' + key);
  504. }
  505. }
  506. options.meta = {
  507. plugin: 'cordova-plugin-local-notification',
  508. version: '0.9-beta.3'
  509. };
  510. return options;
  511. };
  512. /**
  513. * Convert the passed values to their required type.
  514. *
  515. * @param [ Object ] options Properties to convert for.
  516. *
  517. * @return [ Object ] The converted property list
  518. */
  519. exports._convertProperties = function (options) {
  520. var parseToInt = function (prop, options) {
  521. if (isNaN(options[prop])) {
  522. console.warn(prop + ' is not a number: ' + options[prop]);
  523. return this._defaults[prop];
  524. } else {
  525. return Number(options[prop]);
  526. }
  527. };
  528. if (options.id) {
  529. options.id = parseToInt('id', options);
  530. }
  531. if (options.title) {
  532. options.title = options.title.toString();
  533. }
  534. if (options.badge) {
  535. options.badge = parseToInt('badge', options);
  536. }
  537. if (options.defaults) {
  538. options.defaults = parseToInt('defaults', options);
  539. }
  540. if (options.smallIcon && !options.smallIcon.match(/^res:/)) {
  541. console.warn('Property "smallIcon" must be of kind res://...');
  542. }
  543. if (typeof options.timeoutAfter === 'boolean') {
  544. options.timeoutAfter = options.timeoutAfter ? 3600000 : null;
  545. }
  546. if (options.timeoutAfter) {
  547. options.timeoutAfter = parseToInt('timeoutAfter', options);
  548. }
  549. options.data = JSON.stringify(options.data);
  550. this._convertPriority(options);
  551. this._convertTrigger(options);
  552. this._convertActions(options);
  553. this._convertProgressBar(options);
  554. return options;
  555. };
  556. /**
  557. * Convert the passed values for the priority to their required type.
  558. *
  559. * @param [ Map ] options Set of custom values.
  560. *
  561. * @return [ Map ] Interaction object with trigger spec.
  562. */
  563. exports._convertPriority = function (options) {
  564. var prio = options.priority || options.prio || 0;
  565. if (typeof prio === 'string') {
  566. prio = { min: -2, low: -1, high: 1, max: 2 }[prio] || 0;
  567. }
  568. if (options.foreground === true) {
  569. prio = Math.max(prio, 1);
  570. }
  571. if (options.foreground === false) {
  572. prio = Math.min(prio, 0);
  573. }
  574. options.priority = prio;
  575. return options;
  576. };
  577. /**
  578. * Convert the passed values to their required type, modifying them
  579. * directly for Android and passing the converted list back for iOS.
  580. *
  581. * @param [ Map ] options Set of custom values.
  582. *
  583. * @return [ Map ] Interaction object with category & actions.
  584. */
  585. exports._convertActions = function (options) {
  586. var actions = [];
  587. if (!options.actions || typeof options.actions === 'string')
  588. return options;
  589. for (var i = 0, len = options.actions.length; i < len; i++) {
  590. var action = options.actions[i];
  591. if (!action.id) {
  592. console.warn('Action with title ' + action.title + ' ' +
  593. 'has no id and will not be added.');
  594. continue;
  595. }
  596. action.id = action.id.toString();
  597. actions.push(action);
  598. }
  599. options.actions = actions;
  600. return options;
  601. };
  602. /**
  603. * Convert the passed values for the trigger to their required type.
  604. *
  605. * @param [ Map ] options Set of custom values.
  606. *
  607. * @return [ Map ] Interaction object with trigger spec.
  608. */
  609. exports._convertTrigger = function (options) {
  610. var trigger = options.trigger || {},
  611. date = this._getValueFor(trigger, 'at', 'firstAt', 'date');
  612. var dateToNum = function (date) {
  613. var num = typeof date == 'object' ? date.getTime() : date;
  614. return Math.round(num);
  615. };
  616. if (!options.trigger)
  617. return;
  618. if (!trigger.type) {
  619. trigger.type = trigger.center ? 'location' : 'calendar';
  620. }
  621. var isCal = trigger.type == 'calendar';
  622. if (isCal && !date) {
  623. date = this._getValueFor(options, 'at', 'firstAt', 'date');
  624. }
  625. if (isCal && !trigger.every && options.every) {
  626. trigger.every = options.every;
  627. }
  628. if (isCal && (trigger.in || trigger.every)) {
  629. date = null;
  630. }
  631. if (isCal && date) {
  632. trigger.at = dateToNum(date);
  633. }
  634. if (isCal && trigger.firstAt) {
  635. trigger.firstAt = dateToNum(trigger.firstAt);
  636. }
  637. if (isCal && trigger.before) {
  638. trigger.before = dateToNum(trigger.before);
  639. }
  640. if (isCal && trigger.after) {
  641. trigger.after = dateToNum(trigger.after);
  642. }
  643. if (!trigger.count && device.platform == 'windows') {
  644. trigger.count = trigger.every ? 5 : 1;
  645. }
  646. if (trigger.count && device.platform == 'iOS') {
  647. console.warn('trigger: { count: } is not supported on iOS.');
  648. }
  649. if (!isCal) {
  650. trigger.notifyOnEntry = !!trigger.notifyOnEntry;
  651. trigger.notifyOnExit = trigger.notifyOnExit === true;
  652. trigger.radius = trigger.radius || 5;
  653. trigger.single = !!trigger.single;
  654. }
  655. if (!isCal || trigger.at) {
  656. delete trigger.every;
  657. }
  658. delete options.every;
  659. delete options.at;
  660. delete options.firstAt;
  661. delete options.date;
  662. options.trigger = trigger;
  663. return options;
  664. };
  665. /**
  666. * Convert the passed values for the progressBar to their required type.
  667. *
  668. * @param [ Map ] options Set of custom values.
  669. *
  670. * @return [ Map ] Interaction object with trigger spec.
  671. */
  672. exports._convertProgressBar = function (options) {
  673. var isAndroid = device.platform == 'Android',
  674. cfg = options.progressBar;
  675. if (cfg === undefined)
  676. return;
  677. if (typeof cfg === 'boolean') {
  678. cfg = options.progressBar = { enabled: cfg };
  679. }
  680. if (typeof cfg.enabled !== 'boolean') {
  681. cfg.enabled = !!(cfg.value || cfg.maxValue || cfg.indeterminate !== null);
  682. }
  683. cfg.value = cfg.value || 0;
  684. if (isAndroid) {
  685. cfg.maxValue = cfg.maxValue || 100;
  686. cfg.indeterminate = !!cfg.indeterminate;
  687. }
  688. cfg.enabled = !!cfg.enabled;
  689. if (cfg.enabled && options.clock === true) {
  690. options.clock = 'chronometer';
  691. }
  692. return options;
  693. };
  694. /**
  695. * Create a callback function to get executed within a specific scope.
  696. *
  697. * @param [ Function ] fn The function to be exec as the callback.
  698. * @param [ Object ] scope The callback function's scope.
  699. *
  700. * @return [ Function ]
  701. */
  702. exports._createCallbackFn = function (fn, scope) {
  703. if (typeof fn != 'function')
  704. return;
  705. return function () {
  706. fn.apply(scope || this, arguments);
  707. };
  708. };
  709. /**
  710. * Convert the IDs to numbers.
  711. *
  712. * @param [ Array ] ids
  713. *
  714. * @return [ Array<Number> ]
  715. */
  716. exports._convertIds = function (ids) {
  717. var convertedIds = [];
  718. for (var i = 0, len = ids.length; i < len; i++) {
  719. convertedIds.push(Number(ids[i]));
  720. }
  721. return convertedIds;
  722. };
  723. /**
  724. * First found value for the given keys.
  725. *
  726. * @param [ Object ] options Object with key-value properties.
  727. * @param [ *Array<String> ] keys List of keys.
  728. *
  729. * @return [ Object ]
  730. */
  731. exports._getValueFor = function (options) {
  732. var keys = Array.apply(null, arguments).slice(1);
  733. for (var i = 0, key = keys[i], len = keys.length; i < len; key = keys[++i]) {
  734. if (options.hasOwnProperty(key)) {
  735. return options[key];
  736. }
  737. }
  738. return null;
  739. };
  740. /**
  741. * Convert a value to an array.
  742. *
  743. * @param [ Object ] obj Any kind of object.
  744. *
  745. * @return [ Array ] An array with the object as first item.
  746. */
  747. exports._toArray = function (obj) {
  748. return Array.isArray(obj) ? Array.from(obj) : [obj];
  749. };
  750. /**
  751. * Execute the native counterpart.
  752. *
  753. * @param [ String ] action The name of the action.
  754. * @param [ Array ] args Array of arguments.
  755. * @param [ Function] callback The callback function.
  756. * @param [ Object ] scope The scope for the function.
  757. *
  758. * @return [ Void ]
  759. */
  760. exports._exec = function (action, args, callback, scope) {
  761. var fn = this._createCallbackFn(callback, scope),
  762. params = [];
  763. if (Array.isArray(args)) {
  764. params = args;
  765. } else if (args) {
  766. params.push(args);
  767. }
  768. exec(fn, null, 'LocalNotification', action, params);
  769. };
  770. /**
  771. * Set the launch details if the app was launched by clicking on a toast.
  772. *
  773. * @return [ Void ]
  774. */
  775. exports._setLaunchDetails = function () {
  776. exports._exec('launch', null, function (details) {
  777. if (details) {
  778. exports.launchDetails = details;
  779. }
  780. });
  781. };
  782. // Polyfill for Object.assign
  783. if (typeof Object.assign != 'function') {
  784. Object.assign = function(target) {
  785. 'use strict';
  786. if (target == null) {
  787. throw new TypeError('Cannot convert undefined or null to object');
  788. }
  789. target = Object(target);
  790. for (var index = 1; index < arguments.length; index++) {
  791. var source = arguments[index];
  792. if (source != null) {
  793. for (var key in source) {
  794. if (Object.prototype.hasOwnProperty.call(source, key)) {
  795. target[key] = source[key];
  796. }
  797. }
  798. }
  799. }
  800. return target;
  801. };
  802. }
  803. // Polyfill for Array.from
  804. // Production steps of ECMA-262, Edition 6, 22.1.2.1
  805. // Reference: https://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.from
  806. if (!Array.from) {
  807. Array.from = (function () {
  808. var toStr = Object.prototype.toString;
  809. var isCallable = function (fn) {
  810. return typeof fn === 'function' || toStr.call(fn) === '[object Function]';
  811. };
  812. var toInteger = function (value) {
  813. var number = Number(value);
  814. if (isNaN(number)) { return 0; }
  815. if (number === 0 || !isFinite(number)) { return number; }
  816. return (number > 0 ? 1 : -1) * Math.floor(Math.abs(number));
  817. };
  818. var maxSafeInteger = Math.pow(2, 53) - 1;
  819. var toLength = function (value) {
  820. var len = toInteger(value);
  821. return Math.min(Math.max(len, 0), maxSafeInteger);
  822. };
  823. // The length property of the from method is 1.
  824. return function from(arrayLike/*, mapFn, thisArg */) {
  825. // 1. Let C be the this value.
  826. var C = this;
  827. // 2. Let items be ToObject(arrayLike).
  828. var items = Object(arrayLike);
  829. // 3. ReturnIfAbrupt(items).
  830. if (arrayLike == null) {
  831. throw new TypeError("Array.from requires an array-like object - not null or undefined");
  832. }
  833. // 4. If mapfn is undefined, then let mapping be false.
  834. var mapFn = arguments.length > 1 ? arguments[1] : void undefined;
  835. var T;
  836. if (typeof mapFn !== 'undefined') {
  837. // 5. else
  838. // 5. a If IsCallable(mapfn) is false, throw a TypeError exception.
  839. if (!isCallable(mapFn)) {
  840. throw new TypeError('Array.from: when provided, the second argument must be a function');
  841. }
  842. // 5. b. If thisArg was supplied, let T be thisArg; else let T be undefined.
  843. if (arguments.length > 2) {
  844. T = arguments[2];
  845. }
  846. }
  847. // 10. Let lenValue be Get(items, "length").
  848. // 11. Let len be ToLength(lenValue).
  849. var len = toLength(items.length);
  850. // 13. If IsConstructor(C) is true, then
  851. // 13. a. Let A be the result of calling the [[Construct]] internal method of C with an argument list containing the single item len.
  852. // 14. a. Else, Let A be ArrayCreate(len).
  853. var A = isCallable(C) ? Object(new C(len)) : new Array(len);
  854. // 16. Let k be 0.
  855. var k = 0;
  856. // 17. Repeat, while k < len… (also steps a - h)
  857. var kValue;
  858. while (k < len) {
  859. kValue = items[k];
  860. if (mapFn) {
  861. A[k] = typeof T === 'undefined' ? mapFn(kValue, k) : mapFn.call(T, kValue, k);
  862. } else {
  863. A[k] = kValue;
  864. }
  865. k += 1;
  866. }
  867. // 18. Let putStatus be Put(A, "length", len, true).
  868. A.length = len;
  869. // 20. Return A.
  870. return A;
  871. };
  872. }());
  873. }
  874. // Called after 'deviceready' event
  875. channel.deviceready.subscribe(function () {
  876. if (!window.skipLocalNotificationReady) {
  877. exports.fireQueuedEvents();
  878. }
  879. });
  880. // Called before 'deviceready' event
  881. channel.onCordovaReady.subscribe(function () {
  882. channel.onCordovaInfoReady.subscribe(function () {
  883. exports._setLaunchDetails();
  884. });
  885. });