message.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. layui.define(['table', 'jquery', 'element'], function (exports) {
  2. "use strict";
  3. var MOD_NAME = 'message',
  4. $ = layui.jquery,
  5. element = layui.element;
  6. var message = function (opt) {
  7. this.option = opt;
  8. };
  9. message.prototype.render = function (opt) {
  10. //默认配置值
  11. var option = {
  12. elem: opt.elem,
  13. url: opt.url ? opt.url : false,
  14. height: opt.height,
  15. data: opt.data
  16. }
  17. if (option.url != false) {
  18. option.data = getData(option.url);
  19. var notice = createHtml(option);
  20. $(option.elem).html(notice);
  21. var targetNode = document.querySelector(option.elem + ' .pear-notice')
  22. var mutationObserver = new MutationObserver(function (mutationsList, observer) {
  23. if (getComputedStyle(targetNode).display !== 'none') {
  24. var rect = targetNode.getBoundingClientRect();
  25. //是否超出右侧屏幕
  26. if (rect.right > $(window).width()) {
  27. var elemRight = document.querySelector(option.elem).getBoundingClientRect().right;
  28. var offsetRight = 20;
  29. targetNode.style.right = elemRight - $(window).width() + offsetRight + 'px';
  30. targetNode.style.left = 'unset';
  31. }
  32. }
  33. });
  34. mutationObserver.observe(targetNode, {
  35. attributes: true,
  36. childList: false,
  37. subtree: false,
  38. attributeOldValue: false,
  39. attributeFilter: ['class']
  40. });
  41. }
  42. setTimeout(function () {
  43. element.init();
  44. $(opt.elem + " li").click(function (e) {
  45. $(this).siblings().removeClass('pear-this');
  46. $(this).addClass('pear-this');
  47. })
  48. }, 300);
  49. return new message(option);
  50. }
  51. message.prototype.click = function (callback) {
  52. $("*[notice-id]").click(function (event) {
  53. event.preventDefault();
  54. var id = $(this).attr("notice-id");
  55. var title = $(this).attr("notice-title");
  56. var context = $(this).attr("notice-context");
  57. var form = $(this).attr("notice-form");
  58. callback(id, title, context, form);
  59. })
  60. }
  61. /** 刷 新 消 息 */
  62. message.prototype.reload = function () {
  63. }
  64. /** 同 步 请 求 获 取 数 据 */
  65. function getData(url) {
  66. $.ajaxSettings.async = false;
  67. var data = null;
  68. $.get(url, function (result) {
  69. data = result;
  70. });
  71. $.ajaxSettings.async = true;
  72. return data;
  73. }
  74. function createHtml(option) {
  75. var count = 0;
  76. var noticeTitle = '<ul class="layui-tab-title">';
  77. var noticeContent = '<div class="layui-tab-content" style="height:' + option.height + ';overflow-x: hidden;padding:0px;">';
  78. // 根据 data 便利数据
  79. $.each(option.data, function (i, item) {
  80. if (i === 0) {
  81. noticeTitle += '<li class="pear-this">' + item.title + '</li>';
  82. noticeContent += '<div class="layui-tab-item layui-show">';
  83. } else {
  84. noticeTitle += '<li>' + item.title + '</li>';
  85. noticeContent += '<div class="layui-tab-item">';
  86. }
  87. $.each(item.children, function (i, note) {
  88. noticeContent += '<div class="pear-notice-item" notice-form="' + note.form + '" notice-context="' + note.context +
  89. '" notice-title="' + note.title + '" notice-id="' + note.id + '">' ;
  90. if (note.avatar)
  91. noticeContent +='<img src="' + note.avatar + '"/>';
  92. noticeContent +='<div style="display:inline-block;">' + note.title + '</div>' +
  93. '<div class="pear-notice-end">' + note.time + '</div>' +
  94. '</div>';
  95. })
  96. // 空内容
  97. if(item.children.length==0){
  98. noticeContent +='<div class="pear-empty"><div class="pear-empty-image"><svg class="pear-empty-img-default" width="184" height="152" viewBox="0 0 184 152"><g fill="none" fill-rule="evenodd"><g transform="translate(24 31.67)"><ellipse class="pear-empty-img-default-ellipse" cx="67.797" cy="106.89" rx="67.797" ry="12.668"></ellipse><path class="pear-empty-img-default-path-1" d="M122.034 69.674L98.109 40.229c-1.148-1.386-2.826-2.225-4.593-2.225h-51.44c-1.766 0-3.444.839-4.592 2.225L13.56 69.674v15.383h108.475V69.674z"></path><path class="pear-empty-img-default-path-2" d="M101.537 86.214L80.63 61.102c-1.001-1.207-2.507-1.867-4.048-1.867H31.724c-1.54 0-3.047.66-4.048 1.867L6.769 86.214v13.792h94.768V86.214z" transform="translate(13.56)"></path><path class="pear-empty-img-default-path-3" d="M33.83 0h67.933a4 4 0 0 1 4 4v93.344a4 4 0 0 1-4 4H33.83a4 4 0 0 1-4-4V4a4 4 0 0 1 4-4z"></path><path class="pear-empty-img-default-path-4" d="M42.678 9.953h50.237a2 2 0 0 1 2 2V36.91a2 2 0 0 1-2 2H42.678a2 2 0 0 1-2-2V11.953a2 2 0 0 1 2-2zM42.94 49.767h49.713a2.262 2.262 0 1 1 0 4.524H42.94a2.262 2.262 0 0 1 0-4.524zM42.94 61.53h49.713a2.262 2.262 0 1 1 0 4.525H42.94a2.262 2.262 0 0 1 0-4.525zM121.813 105.032c-.775 3.071-3.497 5.36-6.735 5.36H20.515c-3.238 0-5.96-2.29-6.734-5.36a7.309 7.309 0 0 1-.222-1.79V69.675h26.318c2.907 0 5.25 2.448 5.25 5.42v.04c0 2.971 2.37 5.37 5.277 5.37h34.785c2.907 0 5.277-2.421 5.277-5.393V75.1c0-2.972 2.343-5.426 5.25-5.426h26.318v33.569c0 .617-.077 1.216-.221 1.789z"></path></g><path class="pear-empty-img-default-path-5" d="M149.121 33.292l-6.83 2.65a1 1 0 0 1-1.317-1.23l1.937-6.207c-2.589-2.944-4.109-6.534-4.109-10.408C138.802 8.102 148.92 0 161.402 0 173.881 0 184 8.102 184 18.097c0 9.995-10.118 18.097-22.599 18.097-4.528 0-8.744-1.066-12.28-2.902z"></path><g class="pear-empty-img-default-g" transform="translate(149.65 15.383)"><ellipse cx="20.654" cy="3.167" rx="2.849" ry="2.815"></ellipse><path d="M5.698 5.63H0L2.898.704zM9.259.704h4.985V5.63H9.259z"></path></g></g></svg></div><p class="pear-empty-description">暂无数据</p></div>';
  99. }
  100. noticeContent += '</div>';
  101. })
  102. var notice = '<li class="layui-nav-item" lay-unselect="">' +
  103. '<a href="#" class="notice layui-icon layui-icon-notice"><span class="layui-badge-dot"></div></a>' +
  104. '<div class="layui-nav-child layui-tab pear-notice" style="margin-top: 0px;left: -200px;padding:0px;">';
  105. noticeTitle += '</ul>';
  106. noticeContent += '</div>';
  107. notice += noticeTitle;
  108. notice += noticeContent;
  109. notice += '</div></li>';
  110. return notice;
  111. }
  112. exports(MOD_NAME, new message());
  113. })