# Notify Service (all positions) Links: [Home](../index.md) Ten [Notify](notify.md) managers at once -- one container per screen position, all created and appended to `document.body` when the service is built -- behind one object: `service.position('top', 'right')` returns the manager of that corner, and the service itself forwards `log()`, `info()`, `warn()`, `error()`, `add()`, ... to the **bottom left** manager. Written by SGApps.IO. ## Loading the Module ```js Application.require("elements/notify-service").then(function (NotifyService) { var notifications = NotifyService(); // builds and attaches the 10 containers notifications.info('Ready'); // -> bottom left notifications.position('top', 'right').warn('Disk almost full', '92 % used'); }); ``` **Dependency:** `elements/notify` (and, through it, `extensions/prototype`, `elements/popup`, `uri-load`). Build the service after `document.body` exists. ## API ### `NotifyService()` Returns the **service**, an `ApplicationPrototype`. On construction it creates one manager for each of these position combinations, calls `manager.position(...)` on it and appends `manager.node()` to `document.body`: | Key | `position()` arguments | Container attribute | |---|---|---| | `top` | `'top'` | `m_notify-position="top"` | | `bottom` | `'bottom'` | `"bottom"` | | `center_top` | `'top', 'center'` | `"center top"` | | `bottom_center` | `'bottom', 'center'` | `"bottom center"` | | `left_top` | `'top', 'left'` | `"left top"` | | `right_top` | `'top', 'right'` | `"right top"` | | `bottom_left` | `'bottom', 'left'` | `"bottom left"` -- the **default** | | `bottom_right` | `'bottom', 'right'` | `"bottom right"` | | `center_left` | `'left'` | `"center left"` | | `center_right` | `'right'` | `"center right"` | | Method | Returns | Description | |---|---|---| | `position(...names)` | manager | The manager for that position; the names are normalised like `Notify.position()` (opposites replaced, `'left'` / `'right'` alone completed with `'center'`, sorted). No argument returns the **bottom left** manager; a combination that is not in the table above (for example `'center'` alone) returns `undefined` | | `positionCombination(...names)` | string[] | The normalised, sorted list, e.g. `positionCombination('right', 'top')` -> `['right', 'top']` | | `positionName(...names)` | string | The list joined with `_`: `'right_top'` -- the key of the internal map | | `log()`, `info()`, `warn()`, `error()`, `debug()`, `add()`, `remove()`, `timer()`, `node()`, `alert()`, `confirm()`, `prompt()`, `popup()` | as in Notify | Every method of the bottom-left manager that the service does not define itself is proxied to it (`service.node()` is the bottom-left container) | Each manager's own `position()` method is replaced by the service's `position()`, so `service.position('top').position('bottom')` also returns the bottom manager -- **the containers cannot be moved** after construction; they stay where the service put them. ## Use Cases ### Different corners for different severities ```js Application.require("elements/notify-service").then(function (NotifyService) { var n = NotifyService(); var top = n.position('top', 'center'); var corner = n.position('bottom', 'right'); top.error('Connection lost', 'Retrying in 5 s ...'); // impossible to miss corner.log('Draft saved'); // discreet }); ``` ### One service shared by the whole application ```js // register it once ... App.require("elements/notify-service").then(function (NotifyService) { App.bind('notifications', (function (service) { return function () { return service; }; })(NotifyService())); }); // ... use it anywhere App.notifications().info('Order #1042 shipped'); App.notifications().position('top', 'right').add('warn', ['Session expires in 2 minutes'], function () { extendSession(); this.remove(); }, {}, false); ``` ## Notes - Ten `position: fixed` containers are added to `document.body` (empty and invisible until a row is added); build the service once per page. - `service.position()` returns the same manager object every time, so listeners attached with `on()` stay valid. - Live demo: [playground](playground.html#module=notify-service ':ignore :target=_blank').