All files / boundless/packages/boundless-utils-web-notification index.js

95.35% Statements 41/43
90% Branches 27/30
100% Functions 12/12
95.12% Lines 39/41
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 1271x                         1x 21x   1x 1x 1x             2x 2x 2x 1x     2x           6x 6x       6x 6x   3x     1x     2x                                                                             14x 14x 1x 13x 1x 12x 1x 11x 1x 10x 1x 9x 1x 8x 1x 7x 1x     6x   4x                   4x 2x        
export const errors = {
    DISABLED: 'webNotification: web notifications are currently disabled by user settings.',
    NOT_AVAILABLE: 'webNotification: web notifications are not supported on this platform.',
    CONFIG_TYPE: 'webNotification: passed a non-object as configuration.',
    CONFIG_MISSING: 'webNotification: no configuration was passed.',
    BODY_TYPE: 'webNotification: `body` must be a string.',
    BODY_MISSING: 'webNotification: `body` was omitted from the configuration object.',
    HEADER_TYPE: 'webNotification: `header` must be a string.',
    HEADER_MISSING: 'webNotification: `header` was omitted from the configuration object.',
    ICON_TYPE: 'webNotification: `icon` must be a URL string.',
    ONCLICK_TYPE: 'webNotification: `onClick` must be a function.',
};
 
const isFunction = (x) => typeof x === 'function';
const isString = (x) => typeof x === 'string';
 
const NotificationAPI = (function detectSupport() {
    Eif (window.Notification) {
        return window.Notification;
    }
 
    return false;
})();
 
function requestPermission() {
    return new Promise((resolve, reject) => {
        NotificationAPI.requestPermission(function requestReceiver(status) {
            if (status === 'granted') {
                resolve();
            }
 
            reject(errors.DISABLED);
        });
    });
}
 
function checkPermission() {
    return new Promise((resolve, reject) => {
        Iif (!NotificationAPI) {
            return reject(errors.NOT_AVAILABLE);
        }
 
        Eif ('permission' in NotificationAPI) {
            switch (NotificationAPI.permission) {
            case 'granted':
                return resolve();
 
            case 'denied':
                return reject(errors.DISABLED);
            }
 
            requestPermission().then(resolve, reject);
 
        }
    });
}
 
/**
 * __Trigger native toasts in supporting browsers.__
 *
 * > Support for web notifications is [available in all major desktop browsers](http://caniuse.com/#feat=notifications),
 *   except IE (February 2017).
 *
 * This module is not a React component, but a utility. The "close" functionality of web notifications was removed in a platform
 * spec update, so it's no longer possible to have a true lifecycle.
 *
 * ```js
 * import webNotification from 'boundless-utils-web-notification';
 *
 * webNotification({body: 'Some text to be displayed...'});
 * ```
 *
 * The utility works by providing an object with the following properties:
 *
 * - __body__ `String`
 *   up to two lines are displayed in the notification (based on the current browser implementations)
 *
 * - __header__ `String`
 *   the bolded title displayed at the top of the notification
 *
 * - __icon__ `HTMLString`
 *   (optional) the URL of a picture or icon to be displayed with the notification (looks best if square)
 *
 * - __onClick__ `Function`
 *   (optional) add arbitrary functionality when the notification is clicked
 *
 * This will return a `Promise`. Resolution means the notification was created correctly (returns the `Notification`,
 * and rejection will return a relevant error description string.
 */
export default function webNotification(config) {
    return new Promise((resolve, reject) => {
        if (config === undefined) {
            return reject(errors.CONFIG_MISSING);
        } else if (Object.prototype.toString.call(config) !== '[object Object]') {
            return reject(errors.CONFIG_TYPE);
        } else if (config.body === undefined) {
            return reject(errors.BODY_MISSING);
        } else if (isString(config.body) === false) {
            return reject(errors.BODY_TYPE);
        } else if (config.header === undefined) {
            return reject(errors.HEADER_MISSING);
        } else if (isString(config.header) === false) {
            return reject(errors.HEADER_TYPE);
        } else if (config.icon !== undefined && isString(config.icon) === false) {
            return reject(errors.ICON_TYPE);
        } else if (config.onClick !== undefined && isFunction(config.onClick) === false) {
            return reject(errors.ONCLICK_TYPE);
        }
 
        checkPermission().then(
            function spawnWebNotification() {
                const notification = new NotificationAPI(config.header, {
                    body: config.body,
                    icon: config.icon,
                });
 
                /* istanbul ignore next */
                if (config.onClick) {
                    notification.addEventListener('click', config.onClick);
                }
 
                resolve(notification);
            }, (error) => reject(error)
        );
    });
}