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.
125 lines
3.6 KiB
125 lines
3.6 KiB
2 years ago
|
layui.define(['table', 'jquery', 'element'], function (exports) {
|
||
|
"use strict";
|
||
|
|
||
|
var MOD_NAME = 'message',
|
||
|
$ = layui.jquery,
|
||
|
element = layui.element;
|
||
|
|
||
|
var message = function (opt) {
|
||
|
this.option = opt;
|
||
|
};
|
||
|
|
||
|
message.prototype.render = function (opt) {
|
||
|
//默认配置值
|
||
|
var option = {
|
||
|
elem: opt.elem,
|
||
|
url: opt.url ? opt.url : false,
|
||
|
height: opt.height,
|
||
|
data: opt.data
|
||
|
}
|
||
|
if (option.url != false) {
|
||
|
option.data = getData(option.url);
|
||
|
var notice = createHtml(option);
|
||
|
$(option.elem).html(notice);
|
||
|
var targetNode = document.querySelector(option.elem + ' .pear-notice')
|
||
|
var mutationObserver = new MutationObserver(function (mutationsList, observer) {
|
||
|
if (getComputedStyle(targetNode).display !== 'none') {
|
||
|
var rect = targetNode.getBoundingClientRect();
|
||
|
//是否超出右侧屏幕
|
||
|
if (rect.right > $(window).width()) {
|
||
|
var elemRight = document.querySelector(option.elem).getBoundingClientRect().right;
|
||
|
var offsetRight = 20;
|
||
|
targetNode.style.right = elemRight - $(window).width() + offsetRight + 'px';
|
||
|
targetNode.style.left = 'unset';
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
mutationObserver.observe(targetNode, {
|
||
|
attributes: true,
|
||
|
childList: false,
|
||
|
subtree: false,
|
||
|
attributeOldValue: false,
|
||
|
attributeFilter: ['class']
|
||
|
});
|
||
|
}
|
||
|
setTimeout(function () {
|
||
|
element.init();
|
||
|
$(opt.elem + " li").click(function (e) {
|
||
|
$(this).siblings().removeClass('pear-this');
|
||
|
$(this).addClass('pear-this');
|
||
|
})
|
||
|
}, 300);
|
||
|
return new message(option);
|
||
|
}
|
||
|
|
||
|
message.prototype.click = function (callback) {
|
||
|
$("*[notice-id]").click(function (event) {
|
||
|
event.preventDefault();
|
||
|
var id = $(this).attr("notice-id");
|
||
|
var title = $(this).attr("notice-title");
|
||
|
var context = $(this).attr("notice-context");
|
||
|
var form = $(this).attr("notice-form");
|
||
|
callback(id, title, context, form);
|
||
|
})
|
||
|
}
|
||
|
|
||
|
/** 刷 新 消 息 */
|
||
|
message.prototype.reload = function () {
|
||
|
|
||
|
}
|
||
|
|
||
|
/** 同 步 请 求 获 取 数 据 */
|
||
|
function getData(url) {
|
||
|
$.ajaxSettings.async = false;
|
||
|
var data = null;
|
||
|
$.get(url, function (result) {
|
||
|
data = result;
|
||
|
});
|
||
|
$.ajaxSettings.async = true;
|
||
|
return data;
|
||
|
}
|
||
|
|
||
|
function createHtml(option) {
|
||
|
|
||
|
var count = 0;
|
||
|
var noticeTitle = '<ul class="layui-tab-title">';
|
||
|
var noticeContent = '<div class="layui-tab-content" style="height:' + option.height + ';overflow-x: hidden;padding:0px;">';
|
||
|
|
||
|
|
||
|
// 根据 data 便利数据
|
||
|
$.each(option.data, function (i, item) {
|
||
|
|
||
|
if (i === 0) {
|
||
|
noticeTitle += '<li class="pear-this">' + item.title + '</li>';
|
||
|
noticeContent += '<div class="layui-tab-item layui-show">';
|
||
|
} else {
|
||
|
noticeTitle += '<li>' + item.title + '</li>';
|
||
|
noticeContent += '<div class="layui-tab-item">';
|
||
|
}
|
||
|
|
||
|
$.each(item.children, function (i, note) {
|
||
|
noticeContent += '<div class="pear-notice-item" notice-form="' + note.form + '" notice-context="' + note.context +
|
||
|
'" notice-title="' + note.title + '" notice-id="' + note.id + '">' +
|
||
|
'<img src="' + note.avatar + '"/>' +
|
||
|
'<div style="display:inline-block;">' + note.title + '</div>' +
|
||
|
'<div class="pear-notice-end">' + note.time + '</div>' +
|
||
|
'</div>';
|
||
|
})
|
||
|
noticeContent += '</div>';
|
||
|
})
|
||
|
|
||
|
var notice = '<li class="layui-nav-item" lay-unselect="">' +
|
||
|
'<a href="#" class="notice layui-icon layui-icon-notice"><span class="layui-badge-dot"></div></a>' +
|
||
|
'<div class="layui-nav-child layui-tab pear-notice" style="margin-top: 0px;left: -200px;padding:0px;">';
|
||
|
|
||
|
noticeTitle += '</ul>';
|
||
|
noticeContent += '</div>';
|
||
|
notice += noticeTitle;
|
||
|
notice += noticeContent;
|
||
|
notice += '</div></li>';
|
||
|
return notice;
|
||
|
}
|
||
|
|
||
|
exports(MOD_NAME, new message());
|
||
|
})
|