pushjs.pas 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. {
  2. This file is part of the Pas2JS run time library.
  3. Copyright (C) 2019 Silvio Clecio (silvioprog) and
  4. Fernando Baroni (frbaroni).
  5. Pascal mapping for PushJS: https://pushjs.org
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. { Compact and cross-browser solution for Notifications API. }
  13. unit PushJS;
  14. {$MODE OBJFPC}
  15. {$MODESWITCH EXTERNALCLASS}
  16. interface
  17. uses
  18. JS;
  19. { TODO:
  20. - Plugins: https://pushjs.org/docs/plugins }
  21. type
  22. { TPushFunction }
  23. TPushFunction = reference to procedure;
  24. { TPushOptions }
  25. TPushOptions = class external name 'Object'
  26. // The body text of the notification
  27. body: string;
  28. // Data to pass to ServiceWorker notifications
  29. data: JSValue;
  30. // Can be either the URL to an icon image or an array containing 16x16 and 32x32 pixel icon images (see above).
  31. icon: string;
  32. // A relative URL path to navigate to when the user clicks on the notification on mobile (e.g. if you want users
  33. // to navigate to your page http://example.com/page, then the relative URL is just page). If the page is already
  34. // open in the background, then the browser window will automatically become focused. Requires the serviceWorker.js
  35. // file to be present on your server to work.
  36. link: string;
  37. // When set to true, the notification will not close unless the user manually closes or clicks on it
  38. requireInteraction: Boolean;
  39. // Unique tag used to identify the notification. Can be used to later close the notification manually.
  40. tag: string;
  41. // Time in milliseconds until the notification closes automatically
  42. timeout: Integer;
  43. // An array of durations for a mobile device receiving the notification to vibrate. For example, [200, 100] would
  44. // vibrate first for 200 milliseconds, pause, then continue for 100 milliseconds. For more information, see below.
  45. // Available in Mobile Chrome only.
  46. vibrate: Boolean;
  47. // Specifies whether the notification should be silent, i.e. no sounds or vibrations should be issued, regardless
  48. // of the device settings.
  49. // Supported only in Chrome 43.0 or higher.
  50. silent: Boolean;
  51. // Callback to execute when the notification is clicked
  52. onClick: TPushFunction;
  53. // Callback to execute when if the notification throws an error
  54. onError: TPushFunction;
  55. // Creates new push options
  56. constructor new;
  57. end;
  58. { TPushFallbackPayload }
  59. TPushFallbackPayload = class external name 'Object'
  60. // Notification title
  61. title: string;
  62. // Notification body
  63. body: string;
  64. // Notification tag
  65. tag: string;
  66. // Notification icon
  67. icon: string;
  68. end;
  69. { TPushParamsFallback }
  70. TPushParamsFallback = reference to procedure(payload: TPushFallbackPayload);
  71. { TPushParams }
  72. TPushParams = class external name 'Object'
  73. // Sets a custom service worker script
  74. serviceWorker: string;
  75. // Code that executes on browsers with no notification support
  76. // "payload" is an object containing the
  77. // title, body, tag, and icon of the notification
  78. fallback: TPushParamsFallback;
  79. end;
  80. { TPushPermission }
  81. TPushPermission = class external name 'Object'
  82. private
  83. FDEFAULT: string; external name 'DEFAULT';
  84. FGRANTED: string; external name 'GRANTED';
  85. FDENIED: string; external name 'DENIED';
  86. public
  87. // Requests permission for desktop notifications
  88. procedure request(
  89. // Function to execute once permission is granted
  90. onGranted: TPushFunction;
  91. // Function to execute once permission is denied
  92. onDenied: TPushFunction);
  93. // Returns whether Push has been granted permission to run
  94. function has: Boolean;
  95. // Gets the permission level
  96. function get: string;
  97. // 'default'
  98. property DEFAULT: string read FDEFAULT;
  99. // 'granted'
  100. property GRANTED: string read FGRANTED;
  101. // 'denied'
  102. property DENIED: string read FDENIED;
  103. end;
  104. { TPush }
  105. TPush = class external name 'Push' (TJSPromise)
  106. private class var
  107. FPermission: TPushPermission; external name 'Permission';
  108. public
  109. // Creates and displays a new notification
  110. class function create(
  111. // Notification title
  112. const title: string): TPush; overload; external name 'create';
  113. // Creates and displays a new notification
  114. class function create(
  115. // Notification title
  116. const title: string;
  117. // Notification options
  118. params: TPushOptions): TPush; overload; external name 'create';
  119. // Returns the notification count
  120. class function count: Integer;
  121. // Closes a notification with the given tag
  122. // Return boolean denoting success
  123. class function close(
  124. // Tag of the notification to close
  125. const tag: string): Boolean;
  126. // Clears all notifications
  127. // Return boolean denoting whether the clear was successful in closing all notifications
  128. class function clear: Boolean;
  129. // Denotes whether Push is supported in the current browser
  130. class function supported: Boolean;
  131. // Modifies settings or returns all settings if no parameter passed
  132. class procedure config(params: TPushParams);
  133. // Permission object
  134. class property Permission: TPushPermission read FPermission;
  135. end;
  136. implementation
  137. end.