You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
2.0KB

  1. /**
  2. * Copyright 2013 IBM Corp.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. **/
  16. RED.notify = (function() {
  17. var currentNotifications = [];
  18. var c = 0;
  19. return function(msg,type,fixed,timeout) {
  20. if (currentNotifications.length > 4) {
  21. var ll = currentNotifications.length;
  22. for (var i = 0;ll > 4 && i<currentNotifications.length;i+=1) {
  23. var notifiction = currentNotifications[i];
  24. if (!notifiction.fixed) {
  25. window.clearTimeout(notifiction.timeoutid);
  26. notifiction.close();
  27. ll -= 1;
  28. }
  29. }
  30. }
  31. var n = document.createElement("div");
  32. n.id="red-notification-"+c;
  33. n.className = "alert";
  34. n.fixed = fixed;
  35. if (type) {
  36. n.className = "alert alert-"+type;
  37. }
  38. n.style.display = "none";
  39. n.innerHTML = msg;
  40. $("#notifications").append(n);
  41. $(n).slideDown(300);
  42. n.close = (function() {
  43. var nn = n;
  44. return function() {
  45. currentNotifications.splice(currentNotifications.indexOf(nn),1);
  46. $(nn).slideUp(300, function() {
  47. nn.parentNode.removeChild(nn);
  48. });
  49. };
  50. })();
  51. if (!fixed) {
  52. n.timeoutid = window.setTimeout(n.close,timeout||3000);
  53. }
  54. currentNotifications.push(n);
  55. c+=1;
  56. return n;
  57. }
  58. })();