1
0
Fork 0
mirror of synced 2024-06-27 18:50:47 +12:00
appwrite/public/scripts/services/alerts.js

57 lines
1.6 KiB
JavaScript
Raw Normal View History

2019-05-09 18:54:39 +12:00
(function (window) {
"use strict";
2019-05-09 20:01:51 +12:00
window.ls.container.set('alerts', function (window) {
2019-08-08 08:35:20 +12:00
return {
list: [],
2019-12-17 09:30:14 +13:00
ids: 0,
2019-08-08 08:35:20 +12:00
counter: 0,
2019-12-17 09:30:14 +13:00
max: 5,
2019-08-08 08:35:20 +12:00
add: function (message, time) {
var scope = this;
2019-12-17 09:30:14 +13:00
message.id = scope.ids++;
2020-04-04 08:26:52 +13:00
message.remove = function () {
scope.remove(message.id);
};
2019-08-08 08:35:20 +12:00
2019-12-17 09:30:14 +13:00
scope.counter++;
2019-08-08 08:35:20 +12:00
scope.list.unshift(message);
2019-12-17 09:30:14 +13:00
if(scope.counter > scope.max) {
scope.list.pop();
scope.counter--;
}
2019-08-08 08:35:20 +12:00
if (time > 0) { // When 0 alert is unlimited in time
window.setTimeout(function (message) {
return function () {
scope.remove(message.id)
}
}(message), time);
}
2019-12-17 09:30:14 +13:00
2019-08-08 08:35:20 +12:00
return message.id;
},
remove: function (id) {
let scope = this;
for (let index = 0; index < scope.list.length; index++) {
let obj = scope.list[index];
if (obj.id === parseInt(id)) {
2019-12-17 09:30:14 +13:00
scope.counter--;
2019-08-08 08:35:20 +12:00
if (typeof obj.callback === "function") {
obj.callback();
}
scope.list.splice(index, 1);
2019-12-17 09:30:14 +13:00
2019-08-08 08:35:20 +12:00
};
}
2019-05-09 18:54:39 +12:00
}
};
2019-08-08 08:35:20 +12:00
}, true, true);
2019-05-09 18:54:39 +12:00
})(window);