message.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. }
  22. setTimeout(function() {
  23. element.init();
  24. $(opt.elem + " li").click(function(e) {
  25. $(this).siblings().removeClass('pear-this');
  26. $(this).addClass('pear-this');
  27. })
  28. }, 300);
  29. return new message(option);
  30. }
  31. message.prototype.click = function(callback) {
  32. $("*[notice-id]").click(function(event) {
  33. event.preventDefault();
  34. var id = $(this).attr("notice-id");
  35. var title = $(this).attr("notice-title");
  36. var context = $(this).attr("notice-context");
  37. var form = $(this).attr("notice-form");
  38. callback(id, title, context, form);
  39. })
  40. }
  41. /** 同 步 请 求 获 取 数 据 */
  42. function getData(url) {
  43. $.ajaxSettings.async = false;
  44. var data = null;
  45. $.get(url, function(result) {
  46. data = result;
  47. });
  48. $.ajaxSettings.async = true;
  49. return data;
  50. }
  51. function createHtml(option) {
  52. var count = 0;
  53. var noticeTitle = '<ul class="layui-tab-title">';
  54. var noticeContent = '<div class="layui-tab-content" style="height:' + option.height + ';overflow-x: hidden;">';
  55. // 根据 data 便利数据
  56. $.each(option.data, function(i, item) {
  57. if (i === 0) {
  58. noticeTitle += '<li class="pear-this">' + item.title + '</li>';
  59. noticeContent += '<div class="layui-tab-item layui-show">';
  60. } else {
  61. noticeTitle += '<li>' + item.title + '</li>';
  62. noticeContent += '<div class="layui-tab-item">';
  63. }
  64. $.each(item.children, function(i, note) {
  65. noticeContent += '<div class="pear-notice-item" notice-form="' + note.form + '" notice-context="' + note.context +
  66. '" notice-title="' + note.title + '" notice-id="' + note.id + '">' +
  67. '<img src="' + note.avatar + '"/>' +
  68. '<div style="display:inline-block;">' + note.title + '</div>'+
  69. '<div class="pear-notice-end">' + note.time + '</div>' +
  70. '</div>';
  71. })
  72. noticeContent += '</div>';
  73. })
  74. var notice = '<li class="layui-nav-item" lay-unselect="">' +
  75. // '<a href="#" class="notice layui-icon layui-icon-notice"><span class="layui-badge-dot"></div></a>' +
  76. '<div class="layui-nav-child layui-tab pear-notice" style="margin-top: 0px;;left: -200px;">';
  77. noticeTitle += '</ul>';
  78. noticeContent += '</div>';
  79. notice += noticeTitle;
  80. notice += noticeContent;
  81. notice += '</div></li>';
  82. return notice;
  83. }
  84. exports(MOD_NAME, new message());
  85. })