local-notification.js 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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. var config = { actionGroupId: id, actions: actions };
  356. this._exec('actions', [0, id, config], callback, scope);
  357. };
  358. /**
  359. * Remove an group of actions by id.
  360. *
  361. * @param [ String ] id The Id of the group.
  362. * @param [ Function ] callback The function to be exec as the callback.
  363. * @param [ Object ] scope The callback function's scope.
  364. *
  365. * @return [ Void ]
  366. */
  367. exports.removeActions = function (id, callback, scope) {
  368. this._exec('actions', [1, id], callback, scope);
  369. };
  370. /**
  371. * Check if a group of actions is defined.
  372. *
  373. * @param [ String ] id The Id of the group.
  374. * @param [ Function ] callback The function to be exec as the callback.
  375. * @param [ Object ] scope The callback function's scope.
  376. *
  377. * @return [ Void ]
  378. */
  379. exports.hasActions = function (id, callback, scope) {
  380. this._exec('actions', [2, id], callback, scope);
  381. };
  382. /**
  383. * The (platform specific) default settings.
  384. *
  385. * @return [ Object ]
  386. */
  387. exports.getDefaults = function () {
  388. var map = Object.assign({}, this._defaults);
  389. for (var key in map) {
  390. if (Array.isArray(map[key])) {
  391. map[key] = Array.from(map[key]);
  392. } else
  393. if (Object.prototype.isPrototypeOf(map[key])) {
  394. map[key] = Object.assign({}, map[key]);
  395. }
  396. }
  397. return map;
  398. };
  399. /**
  400. * Overwrite default settings.
  401. *
  402. * @param [ Object ] newDefaults New default values.
  403. *
  404. * @return [ Void ]
  405. */
  406. exports.setDefaults = function (newDefaults) {
  407. Object.assign(this._defaults, newDefaults);
  408. };
  409. /**
  410. * Register callback for given event.
  411. *
  412. * @param [ String ] event The name of the event.
  413. * @param [ Function ] callback The function to be exec as callback.
  414. * @param [ Object ] scope The callback function's scope.
  415. *
  416. * @return [ Void ]
  417. */
  418. exports.on = function (event, callback, scope) {
  419. var type = typeof callback;
  420. if (type !== 'function' && type !== 'string')
  421. return;
  422. if (!this._listener[event]) {
  423. this._listener[event] = [];
  424. }
  425. var item = [callback, scope || window];
  426. this._listener[event].push(item);
  427. };
  428. /**
  429. * Unregister callback for given event.
  430. *
  431. * @param [ String ] event The name of the event.
  432. * @param [ Function ] callback The function to be exec as callback.
  433. *
  434. * @return [ Void ]
  435. */
  436. exports.un = function (event, callback) {
  437. var listener = this._listener[event];
  438. if (!listener)
  439. return;
  440. for (var i = 0; i < listener.length; i++) {
  441. var fn = listener[i][0];
  442. if (fn == callback) {
  443. listener.splice(i, 1);
  444. break;
  445. }
  446. }
  447. };
  448. /**
  449. * Fire the event with given arguments.
  450. *
  451. * @param [ String ] event The event's name.
  452. * @param [ *Array] args The callback's arguments.
  453. *
  454. * @return [ Void]
  455. */
  456. exports.fireEvent = function (event) {
  457. var args = Array.apply(null, arguments).slice(1),
  458. listener = this._listener[event];
  459. if (!listener)
  460. return;
  461. if (args[0] && typeof args[0].data === 'string') {
  462. args[0].data = JSON.parse(args[0].data);
  463. }
  464. for (var i = 0; i < listener.length; i++) {
  465. var fn = listener[i][0],
  466. scope = listener[i][1];
  467. if (typeof fn !== 'function') {
  468. fn = scope[fn];
  469. }
  470. fn.apply(scope, args);
  471. }
  472. };
  473. /**
  474. * Fire queued events once the device is ready and all listeners are registered.
  475. *
  476. * @return [ Void ]
  477. */
  478. exports.fireQueuedEvents = function() {
  479. exports._exec('ready');
  480. };
  481. /**
  482. * Merge custom properties with the default values.
  483. *
  484. * @param [ Object ] options Set of custom values.
  485. *
  486. * @retrun [ Object ]
  487. */
  488. exports._mergeWithDefaults = function (options) {
  489. var values = this.getDefaults();
  490. if (values.hasOwnProperty('sticky')) {
  491. options.sticky = this._getValueFor(options, 'sticky', 'ongoing');
  492. }
  493. if (options.sticky && options.autoClear !== true) {
  494. options.autoClear = false;
  495. }
  496. Object.assign(values, options);
  497. for (var key in values) {
  498. if (values[key] !== null) {
  499. options[key] = values[key];
  500. } else {
  501. delete options[key];
  502. }
  503. if (!this._defaults.hasOwnProperty(key)) {
  504. console.warn('Unknown property: ' + key);
  505. }
  506. }
  507. options.meta = {
  508. plugin: 'cordova-plugin-local-notification',
  509. version: '0.9-beta.3'
  510. };
  511. return options;
  512. };
  513. /**
  514. * Convert the passed values to their required type.
  515. *
  516. * @param [ Object ] options Properties to convert for.
  517. *
  518. * @return [ Object ] The converted property list
  519. */
  520. exports._convertProperties = function (options) {
  521. var parseToInt = function (prop, options) {
  522. if (isNaN(options[prop])) {
  523. console.warn(prop + ' is not a number: ' + options[prop]);
  524. return this._defaults[prop];
  525. } else {
  526. return Number(options[prop]);
  527. }
  528. };
  529. if (options.id) {
  530. options.id = parseToInt('id', options);
  531. }
  532. if (options.title) {
  533. options.title = options.title.toString();
  534. }
  535. if (options.badge) {
  536. options.badge = parseToInt('badge', options);
  537. }
  538. if (options.defaults) {
  539. options.defaults = parseToInt('defaults', options);
  540. }
  541. if (options.smallIcon && !options.smallIcon.match(/^res:/)) {
  542. console.warn('Property "smallIcon" must be of kind res://...');
  543. }
  544. if (typeof options.timeout === 'boolean') {
  545. options.timeout = options.timeout ? 3600000 : null;
  546. }
  547. if (options.timeout) {
  548. options.timeout = parseToInt('timeout', options);
  549. }
  550. options.data = JSON.stringify(options.data);
  551. this._convertPriority(options);
  552. this._convertTrigger(options);
  553. this._convertActions(options);
  554. this._convertProgressBar(options);
  555. return options;
  556. };
  557. /**
  558. * Convert the passed values for the priority to their required type.
  559. *
  560. * @param [ Map ] options Set of custom values.
  561. *
  562. * @return [ Map ] Interaction object with trigger spec.
  563. */
  564. exports._convertPriority = function (options) {
  565. var prio = options.priority || options.prio || 0;
  566. if (typeof prio === 'string') {
  567. prio = { min: -2, low: -1, high: 1, max: 2 }[prio] || 0;
  568. }
  569. if (options.foreground === true) {
  570. prio = Math.max(prio, 1);
  571. }
  572. if (options.foreground === false) {
  573. prio = Math.min(prio, 0);
  574. }
  575. options.priority = prio;
  576. return options;
  577. };
  578. /**
  579. * Convert the passed values to their required type, modifying them
  580. * directly for Android and passing the converted list back for iOS.
  581. *
  582. * @param [ Map ] options Set of custom values.
  583. *
  584. * @return [ Map ] Interaction object with category & actions.
  585. */
  586. exports._convertActions = function (options) {
  587. var actions = [];
  588. if (!options.actions || typeof options.actions === 'string')
  589. return options;
  590. for (var i = 0, len = options.actions.length; i < len; i++) {
  591. var action = options.actions[i];
  592. if (!action.id) {
  593. console.warn('Action with title ' + action.title + ' ' +
  594. 'has no id and will not be added.');
  595. continue;
  596. }
  597. action.id = action.id.toString();
  598. actions.push(action);
  599. }
  600. options.actions = actions;
  601. return options;
  602. };
  603. /**
  604. * Convert the passed values for the trigger to their required type.
  605. *
  606. * @param [ Map ] options Set of custom values.
  607. *
  608. * @return [ Map ] Interaction object with trigger spec.
  609. */
  610. exports._convertTrigger = function (options) {
  611. var trigger = options.trigger || {},
  612. date = this._getValueFor(trigger, 'at', 'firstAt', 'date');
  613. var dateToNum = function (date) {
  614. var num = typeof date == 'object' ? date.getTime() : date;
  615. return Math.round(num);
  616. };
  617. if (!options.trigger)
  618. return;
  619. if (!trigger.type) {
  620. trigger.type = trigger.center ? 'location' : 'calendar';
  621. }
  622. var isCal = trigger.type == 'calendar';
  623. if (isCal && !date) {
  624. date = this._getValueFor(options, 'at', 'firstAt', 'date');
  625. }
  626. if (isCal && !trigger.every && options.every) {
  627. trigger.every = options.every;
  628. }
  629. if (isCal && (trigger.in || trigger.every)) {
  630. date = null;
  631. }
  632. if (isCal && date) {
  633. trigger.at = dateToNum(date);
  634. }
  635. if (isCal && trigger.firstAt) {
  636. trigger.firstAt = dateToNum(trigger.firstAt);
  637. }
  638. if (isCal && trigger.before) {
  639. trigger.before = dateToNum(trigger.before);
  640. }
  641. if (isCal && trigger.after) {
  642. trigger.after = dateToNum(trigger.after);
  643. }
  644. if (!trigger.count && device.platform == 'windows') {
  645. trigger.count = trigger.every ? 5 : 1;
  646. }
  647. if (trigger.count && device.platform == 'iOS') {
  648. console.warn('trigger: { count: } is not supported on iOS.');
  649. }
  650. if (!isCal) {
  651. trigger.notifyOnEntry = !!trigger.notifyOnEntry;
  652. trigger.notifyOnExit = trigger.notifyOnExit === true;
  653. trigger.radius = trigger.radius || 5;
  654. trigger.single = !!trigger.single;
  655. }
  656. if (!isCal || trigger.at) {
  657. delete trigger.every;
  658. }
  659. delete options.every;
  660. delete options.at;
  661. delete options.firstAt;
  662. delete options.date;
  663. options.trigger = trigger;
  664. return options;
  665. };
  666. /**
  667. * Convert the passed values for the progressBar to their required type.
  668. *
  669. * @param [ Map ] options Set of custom values.
  670. *
  671. * @return [ Map ] Interaction object with trigger spec.
  672. */
  673. exports._convertProgressBar = function (options) {
  674. var isAndroid = device.platform == 'Android',
  675. cfg = options.progressBar;
  676. if (cfg === undefined)
  677. return;
  678. if (typeof cfg === 'boolean') {
  679. cfg = options.progressBar = { enabled: cfg };
  680. }
  681. if (typeof cfg.enabled !== 'boolean') {
  682. cfg.enabled = !!(cfg.value || cfg.maxValue || cfg.indeterminate !== null);
  683. }
  684. cfg.value = cfg.value || 0;
  685. if (isAndroid) {
  686. cfg.maxValue = cfg.maxValue || 100;
  687. cfg.indeterminate = !!cfg.indeterminate;
  688. }
  689. cfg.enabled = !!cfg.enabled;
  690. if (cfg.enabled && options.clock === true) {
  691. options.clock = 'chronometer';
  692. }
  693. return options;
  694. };
  695. /**
  696. * Create a callback function to get executed within a specific scope.
  697. *
  698. * @param [ Function ] fn The function to be exec as the callback.
  699. * @param [ Object ] scope The callback function's scope.
  700. *
  701. * @return [ Function ]
  702. */
  703. exports._createCallbackFn = function (fn, scope) {
  704. if (typeof fn != 'function')
  705. return;
  706. return function () {
  707. fn.apply(scope || this, arguments);
  708. };
  709. };
  710. /**
  711. * Convert the IDs to numbers.
  712. *
  713. * @param [ Array ] ids
  714. *
  715. * @return [ Array<Number> ]
  716. */
  717. exports._convertIds = function (ids) {
  718. var convertedIds = [];
  719. for (var i = 0, len = ids.length; i < len; i++) {
  720. convertedIds.push(Number(ids[i]));
  721. }
  722. return convertedIds;
  723. };
  724. /**
  725. * First found value for the given keys.
  726. *
  727. * @param [ Object ] options Object with key-value properties.
  728. * @param [ *Array<String> ] keys List of keys.
  729. *
  730. * @return [ Object ]
  731. */
  732. exports._getValueFor = function (options) {
  733. var keys = Array.apply(null, arguments).slice(1);
  734. for (var i = 0, key = keys[i], len = keys.length; i < len; key = keys[++i]) {
  735. if (options.hasOwnProperty(key)) {
  736. return options[key];
  737. }
  738. }
  739. return null;
  740. };
  741. /**
  742. * Convert a value to an array.
  743. *
  744. * @param [ Object ] obj Any kind of object.
  745. *
  746. * @return [ Array ] An array with the object as first item.
  747. */
  748. exports._toArray = function (obj) {
  749. return Array.isArray(obj) ? Array.from(obj) : [obj];
  750. };
  751. /**
  752. * Execute the native counterpart.
  753. *
  754. * @param [ String ] action The name of the action.
  755. * @param [ Array ] args Array of arguments.
  756. * @param [ Function] callback The callback function.
  757. * @param [ Object ] scope The scope for the function.
  758. *
  759. * @return [ Void ]
  760. */
  761. exports._exec = function (action, args, callback, scope) {
  762. var fn = this._createCallbackFn(callback, scope),
  763. params = [];
  764. if (Array.isArray(args)) {
  765. params = args;
  766. } else if (args) {
  767. params.push(args);
  768. }
  769. exec(fn, null, 'LocalNotification', action, params);
  770. };
  771. /**
  772. * Set the launch details if the app was launched by clicking on a toast.
  773. *
  774. * @return [ Void ]
  775. */
  776. exports._setLaunchDetails = function () {
  777. exports._exec('launch', null, function (details) {
  778. if (details) {
  779. exports.launchDetails = details;
  780. }
  781. });
  782. };
  783. // Polyfill for Object.assign
  784. if (typeof Object.assign != 'function') {
  785. Object.assign = function(target) {
  786. 'use strict';
  787. if (target == null) {
  788. throw new TypeError('Cannot convert undefined or null to object');
  789. }
  790. target = Object(target);
  791. for (var index = 1; index < arguments.length; index++) {
  792. var source = arguments[index];
  793. if (source != null) {
  794. for (var key in source) {
  795. if (Object.prototype.hasOwnProperty.call(source, key)) {
  796. target[key] = source[key];
  797. }
  798. }
  799. }
  800. }
  801. return target;
  802. };
  803. }
  804. // Polyfill for Array.from
  805. // Production steps of ECMA-262, Edition 6, 22.1.2.1
  806. // Reference: https://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.from
  807. if (!Array.from) {
  808. Array.from = (function () {
  809. var toStr = Object.prototype.toString;
  810. var isCallable = function (fn) {
  811. return typeof fn === 'function' || toStr.call(fn) === '[object Function]';
  812. };
  813. var toInteger = function (value) {
  814. var number = Number(value);
  815. if (isNaN(number)) { return 0; }
  816. if (number === 0 || !isFinite(number)) { return number; }
  817. return (number > 0 ? 1 : -1) * Math.floor(Math.abs(number));
  818. };
  819. var maxSafeInteger = Math.pow(2, 53) - 1;
  820. var toLength = function (value) {
  821. var len = toInteger(value);
  822. return Math.min(Math.max(len, 0), maxSafeInteger);
  823. };
  824. // The length property of the from method is 1.
  825. return function from(arrayLike/*, mapFn, thisArg */) {
  826. // 1. Let C be the this value.
  827. var C = this;
  828. // 2. Let items be ToObject(arrayLike).
  829. var items = Object(arrayLike);
  830. // 3. ReturnIfAbrupt(items).
  831. if (arrayLike == null) {
  832. throw new TypeError("Array.from requires an array-like object - not null or undefined");
  833. }
  834. // 4. If mapfn is undefined, then let mapping be false.
  835. var mapFn = arguments.length > 1 ? arguments[1] : void undefined;
  836. var T;
  837. if (typeof mapFn !== 'undefined') {
  838. // 5. else
  839. // 5. a If IsCallable(mapfn) is false, throw a TypeError exception.
  840. if (!isCallable(mapFn)) {
  841. throw new TypeError('Array.from: when provided, the second argument must be a function');
  842. }
  843. // 5. b. If thisArg was supplied, let T be thisArg; else let T be undefined.
  844. if (arguments.length > 2) {
  845. T = arguments[2];
  846. }
  847. }
  848. // 10. Let lenValue be Get(items, "length").
  849. // 11. Let len be ToLength(lenValue).
  850. var len = toLength(items.length);
  851. // 13. If IsConstructor(C) is true, then
  852. // 13. a. Let A be the result of calling the [[Construct]] internal method of C with an argument list containing the single item len.
  853. // 14. a. Else, Let A be ArrayCreate(len).
  854. var A = isCallable(C) ? Object(new C(len)) : new Array(len);
  855. // 16. Let k be 0.
  856. var k = 0;
  857. // 17. Repeat, while k < len… (also steps a - h)
  858. var kValue;
  859. while (k < len) {
  860. kValue = items[k];
  861. if (mapFn) {
  862. A[k] = typeof T === 'undefined' ? mapFn(kValue, k) : mapFn.call(T, kValue, k);
  863. } else {
  864. A[k] = kValue;
  865. }
  866. k += 1;
  867. }
  868. // 18. Let putStatus be Put(A, "length", len, true).
  869. A.length = len;
  870. // 20. Return A.
  871. return A;
  872. };
  873. }());
  874. }
  875. // Called after 'deviceready' event
  876. channel.deviceready.subscribe(function () {
  877. if (!window.skipLocalNotificationReady) {
  878. exports.fireQueuedEvents();
  879. }
  880. });
  881. // Called before 'deviceready' event
  882. channel.onCordovaReady.subscribe(function () {
  883. channel.onCordovaInfoReady.subscribe(function () {
  884. exports._setLaunchDetails();
  885. });
  886. });