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.
 
 
 
 
 
 

115 lines
3.5 KiB

  1. frappe.provide("frappe.ui.notifications");
  2. frappe.ui.notifications = {
  3. config: {
  4. "ToDo": { label: __("To Do") },
  5. "Chat": { label: __("Chat"), route: "chat"},
  6. "Event": { label: __("Calendar"), route: "List/Event/Calendar" },
  7. "Email": { label: __("Email"), route: "List/Communication/Inbox" },
  8. "Likes": { label: __("Likes"),
  9. click: function() {
  10. frappe.route_options = { show_likes: true };
  11. if (frappe.get_route()[0]=="activity") {
  12. frappe.pages['activity'].page.list.refresh();
  13. } else {
  14. frappe.set_route("activity");
  15. }
  16. }
  17. },
  18. },
  19. update_notifications: function() {
  20. this.total = 0;
  21. this.dropdown = $("#dropdown-notification").empty();
  22. this.boot_info = frappe.boot.notification_info;
  23. let defaults = ["Comment", "ToDo", "Event"];
  24. this.get_counts(this.boot_info.open_count_doctype, 1, defaults);
  25. this.get_counts(this.boot_info.open_count_other, 1);
  26. // Target counts are stored for docs per doctype
  27. let targets = { doctypes : {} }, map = this.boot_info.targets;
  28. Object.keys(map).map(doctype => {
  29. Object.keys(map[doctype]).map(doc => {
  30. targets[doc] = map[doctype][doc];
  31. targets.doctypes[doc] = doctype;
  32. });
  33. });
  34. this.get_counts(targets, 1, null, ["doctypes"], true);
  35. this.get_counts(this.boot_info.open_count_doctype,
  36. 0, null, defaults);
  37. this.bind_list();
  38. // switch colour on the navbar and disable if no notifications
  39. $(".navbar-new-comments")
  40. .html(this.total > 20 ? '20+' : this.total)
  41. .toggleClass("navbar-new-comments-true", this.total ? true : false)
  42. .parent().toggleClass("disabled", this.total ? false : true);
  43. },
  44. get_counts: function(map, divide, keys, excluded = [], target = false) {
  45. let empty_map = 1;
  46. keys = keys ? keys
  47. : Object.keys(map).sort().filter(e => !excluded.includes(e));
  48. keys.map(key => {
  49. let doc_dt = (map.doctypes) ? map.doctypes[key] : undefined;
  50. if(map[key] > 0 || target) {
  51. this.add_notification(key, map[key], doc_dt, target);
  52. empty_map = 0;
  53. }
  54. });
  55. if(divide && !empty_map) {
  56. this.dropdown.append($('<li class="divider"></li>'));
  57. }
  58. },
  59. add_notification: function(name, value, doc_dt, target = false) {
  60. let label = this.config[name] ? this.config[name].label : name;
  61. let $list_item = !target
  62. ? $(`<li><a class="badge-hover" data-doctype="${name}">${label}
  63. <span class="badge pull-right">${value}</span>
  64. </a></li>`)
  65. : $(`<li><a class="progress-small" data-doctype="${doc_dt}"
  66. data-doc="${name}"><span class="dropdown-item-label">${label}<span>
  67. <div class="progress-chart"><div class="progress">
  68. <div class="progress-bar" style="width: ${value}%"></div>
  69. </div></div>
  70. </a></li>`);
  71. this.dropdown.append($list_item);
  72. if(!target) this.total += value;
  73. },
  74. bind_list: function() {
  75. var me = this;
  76. $("#dropdown-notification a").on("click", function() {
  77. var doctype = $(this).attr("data-doctype");
  78. var doc = $(this).attr("data-doc");
  79. if(!doc) {
  80. var config = me.config[doctype] || {};
  81. if (config.route) {
  82. frappe.set_route(config.route);
  83. } else if (config.click) {
  84. config.click();
  85. } else {
  86. frappe.ui.notifications.show_open_count_list(doctype);
  87. }
  88. } else {
  89. frappe.set_route("Form", doctype, doc);
  90. }
  91. });
  92. },
  93. show_open_count_list: function(doctype) {
  94. let filters = this.boot_info.conditions[doctype];
  95. if(filters && $.isPlainObject(filters)) {
  96. frappe.route_options = filters;
  97. }
  98. let route = frappe.get_route();
  99. if(route[0]==="List" && route[1]===doctype) {
  100. frappe.pages["List/" + doctype].list_view.refresh();
  101. } else {
  102. frappe.set_route("List", doctype);
  103. }
  104. },
  105. }