fleet-contractor/Site/js/usermessage.js
2024-03-26 15:56:31 +08:00

264 lines
11 KiB
JavaScript

if (typeof (UserMessage) != "function") {
UserMessage = function () {
var _this = this;
var REDUN = 2;
var ROW_HEIGHT = 129;
var MIN_LENGTH = 40;
var trunc = function (val) {
return (val > 0 ? Math.floor : Math.ceil)(val);
};
var vusermsg = new Vue({
el: '#divmsgs',
data: {
startIndex: 0,
bodyContentStyle: { top: null },
bodyContainerHeight: 0,
bodyContainerStyle: { height: null },
scrollTop: 0,
allmessages: []
},
computed: {
bodyClientRowCount: function () {
var height = document.getElementById('divmsgcontainer').clientHeight;
return trunc((height - 1) / ROW_HEIGHT) + 1;
},
innerMessages: function () {
var start = this.startIndex;
if (start < 0) {
start = 0;
}
if (this.allmessages == null || this.allmessages.length == 0)
return [];
if (this.allmessages.length < MIN_LENGTH) {
return this.allmessages.slice();
}
var end = this.bodyClientRowCount + start + (REDUN * 2) + 1;
if (end > this.allmessages.length) {
end = this.allmessages.length;
}
return this.allmessages.slice(start, end);
}
},
methods: {
reload: function (data) {
for (var i = 0; i < this.allmessages.length; i++) {
data.push(this.allmessages[i]);
}
this.allmessages = data;
var height = data && data.length * ROW_HEIGHT;
this.bodyContainerHeight = height;
this.bodyContainerStyle.height = height && (height + 'px');
if (data.length < MIN_LENGTH) {
this.startIndex = -1;
this.startIndex = 0;
this.bodyContentStyle.top = '0px';
} else {
var index = this.startIndex;
var lastIndex = data.length - this.bodyClientRowCount;
if (index > lastIndex) {
index = lastIndex;
}
this.startIndex = -1;
this.startIndex = index;
}
},
refresh: function () {
var index = this.startIndex;
this.startIndex = -1;
this.startIndex = index;
},
linkClick: function (m) {
if (m.Source == "WorkOrder")
openWorkOrder(m.SourceId);
},
readMessage: function (m, ev) {
var t = $(ev.target);
if (t.attr("type") == "checkbox")
return;
var msgids = [];
if (m) {
m.Readed = true;
msgids.push(m.Id);
}
else {
for (var i = 0; i < this.allmessages.length; i++) {
if (this.allmessages[i].Selected) {
this.allmessages[i].Readed = true;
msgids.push(this.allmessages[i].Id);
}
}
}
this.refresh();
if (msgids.length > 0)
_this.readUserMessages(msgids);
},
deleteMessage: function (m) {
var msgids = [];
var this_t = this;
if (m) {
msgids.push(m.Id);
showConfirm(GetTextByKey("P_MAIN_DOYOUWANTTODELETETHEMESSAGE", 'Do you want to delete the message?'), GetTextByKey("P_MAIN_DELETEMESSAGES", 'Delete Message'), function () {
var i = this_t.allmessages.indexOf(m);
this_t.allmessages.splice(i, 1);
_this.deleteUserMessages(msgids);
this_t.reload([]);
});
}
else {
for (var i = 0; i < this.allmessages.length; i++) {
if (this.allmessages[i].Selected) {
msgids.push(this.allmessages[i].Id);
}
}
if (msgids.length > 0) {
showConfirm(GetTextByKey("P_MAIN_DOYOUWANTTODELETESELECTEDMESSAGES", 'Do you want to delete selected message(s)?'), GetTextByKey("P_MAIN_DELETEMESSAGES", 'Delete Message'), function () {
for (var i = this_t.allmessages.length - 1; i >= 0; i--) {
if (this_t.allmessages[i].Selected) {
this_t.allmessages.splice(i, 1);
}
}
_this.deleteUserMessages(msgids);
this_t.reload([]);
});
}
}
},
closeMessage: function () {
$("#divmsgs").hide();
},
refreshMessage: function () {
_this.getUserMessages();
},
selectAll: function (ev) {
var t = $(ev.target);
var checked = t.prop("checked");
for (var i = 0; i < this.allmessages.length; i++) {
this.allmessages[i].Selected = checked;
}
this.refresh();
}
}
});
function onscrollMessages(e) {
if (vusermsg.allmessages == null || vusermsg.allmessages.length < MIN_LENGTH) {
return;
}
var top = e == null ? 0 : e.target.scrollTop;
top -= (top % ROW_HEIGHT) + (REDUN * ROW_HEIGHT);
if (top < 0) {
top = 0;
} else {
var bottomTop = vusermsg.bodyContainerHeight - ((vusermsg.bodyClientRowCount + (REDUN * 2) + 1) * ROW_HEIGHT);
if (top > bottomTop) {
top = bottomTop;
}
}
if (vusermsg.scrollTop !== top) {
vusermsg.scrollTop = top;
vusermsg.startIndex = top / ROW_HEIGHT;
vusermsg.bodyContentStyle.top = top && (top + 'px');
}
};
document.getElementById('divmsgcontainer').addEventListener('scroll', onscrollMessages, { passive: true });
$("#iconmessage").click(function () {
_this.getUserMessages();
$("#divmsgs").show();
});
function openWorkOrder(woid) {
var p = "jt=woe;woid=" + woid;
p = window.btoa(p);
window.open(_network.root + "Jump.aspx?p=" + p, "_blank");
}
var lastmsgid = 0;
var loadingusermsg = false;
this.getUserMessages = function () {
if (loadingusermsg) return;
loadingusermsg = false;
_network.commonpagequery(10, lastmsgid, function (data) {
loadingusermsg = false;
$("#iconmessage").show();
showUserMessages(data);
}, function (err) {
loadingusermsg = false;
}, true);
}
function showUserMessages(data) {
if (data && typeof data != "string" && data.length > 0) {
lastmsgid = data[0].Id;
for (var i = 0; i < data.length; i++) {
var msg = data[i];
msg.Selected = false;
if (msg.Source == "WorkOrder") {
msg.LinkText = msg.Title;
}
if (msg.Message.toLowerCase().indexOf("http://") >= 0 || msg.Message.toLowerCase().indexOf("https://") >= 0) {
msg.Message = formatUrl(msg.Message);
}
else {
msg.Message = htmlencode(msg.Message);
}
if (msg.Message.indexOf("\r\n") > -1) {
var reg = new RegExp("\r\n", "g");
msg.Message = msg.Message.replace(reg, "<br/>");
}
if (msg.Message.indexOf("\n") > -1) {
var reg = new RegExp("\n", "g");
msg.Message = msg.Message.replace(reg, "<br/>");
}
}
vusermsg.reload(data);
}
}
this.readUserMessages = function (msgids) {
_network.commonpagequery(11, JSON.stringify(msgids), function (data) {
_this.getUnreadCount();
}, function (err) {
}, true);
}
this.deleteUserMessages = function (msgids) {
_network.commonpagequery(12, JSON.stringify(msgids), function (data) {
_this.getUnreadCount();
}, function (err) {
}, true);
}
var unreadcount = 0;
this.getUnreadCount = function () {
_network.commonpagequery(13, '', function (data) {
$("#iconmessage").show();
if (!isNaN(data) && eval(data) > 0) {
var count = eval(data);
if (unreadcount != count && $("#divmsgs").is(":visible"))
_this.getUserMessages();
unreadcount = count;
if (unreadcount > 99)
$("#divunreadcount").text("99+");
else
$("#divunreadcount").text(unreadcount);
$("#divunreadcount").show();
}
else
$("#divunreadcount").hide();
}, function (err) {
}, true);
}
setInterval(function () {
_this.getUnreadCount();
//if ($("#divmsgs").is(":visible"))
// _this.getUserMessages();
}, 45000);
_this.getUnreadCount();
}
}