(function(window) {
function ajaxRequist(url, obj, type, fn) {
$.ajax({
url: url,
data: obj,
dataType: 'json', //服务器返回json格式数据
type: type, //支持'GET'和'POST'
traditional: true,
success: function(data) {
if(data.success) {
fn(data.data)
}
},
error: function(xhr, type, errorThrown) {
$.MsgBox.Alert('提示', '服务器请求失败');
}
});
}
var LeaveWord = function() {
var self = this;
self.addHtml();
self.bindEvent();
}
function lgin() {
if($.cookie('userid')) {
return true;
}
location.href = "login.html";
return false;
}
LeaveWord.prototype.addHtml = function() {
$("body").append('')
var str='
'
$("body").prepend(str);
}
LeaveWord.prototype.init = function(obj) {
var id = $.cookie('userid');
var self = this;
this.id=obj.id;
console.log(self)
ajaxRequist("/ajax/leavemsg/dialog", obj, "GET", function(data) {
if(data.length == 0) {
return;
}
$(".commentList").html("");
for(var i = 0; i < data.length; i++) {
var reply = "";
if(data[i].reciver) {
reply = '回复 张某某'
}
if(data[i].state =="0") {
data[i].cnt="该留言已被其本人删除。";
}
var itemlist = '';
itemlist += ' ';
itemlist += '';
itemlist += '
张某某' + reply + '
';
itemlist += '
' + data[i].cnt + '
';
itemlist += '
赞0回复删除
';
itemlist += '
';
$itemlist = $(itemlist);
$(".commentList").append($itemlist);
$itemlist.attr('data-obj', JSON.stringify(data[i]));
if(id == data[i].sender) {
$itemlist.find(".messageDel").removeClass("displayNone").end().find(".noMy").hide();
}
if(data[i].state =="0") {
$itemlist.find(".operateSpan").addClass("displayNone");
}
if(data[i].reciver) {
self.userInfo(data[i].sender, $itemlist, 0);
self.userInfo(data[i].reciver, $itemlist, 1)
} else {
self.userInfo(data[i].sender, $itemlist, 0);
}
if(data[i].agreeCount)
self.referThup(data[i].id, $itemlist, data[i].agreeCount);
}
})
}
LeaveWord.prototype.userInfo = function(uId, li, parNum) {
ajaxRequist("/ajax/professor/editBaseInfo/" + uId, {}, "GET", function($data) {
if(parNum == 0) {
if($data.hasHeadImage == 1) {
li.find(".useHead")[0].style.backgroundImage = "url(../images/head/" + $data.id + "_l.jpg" + ")";
}
li.find("textarea").attr("placeholder", "回复 " + $data.name + ":")
}
var userType = autho($data.authType, $data.orgAuth, $data.authStatus);
li.find(".messageName").eq(parNum).html($data.name).end().end().find(".authiconNew").eq(parNum).addClass(userType.sty).attr("title", userType.title);
})
}
LeaveWord.prototype.size = function() {
if(this.val().length > 200) {
$.MsgBox.Alert('提示', '留言不得超过200个字');
return false;
}
return true;
}
LeaveWord.prototype.autoGrow = function() {
document.getElementById("tt").style.width = this.scrollWidth + "px";
document.getElementById("tt").value = this.value;
this.style.height = document.getElementById("tt").scrollHeight + "px";
}
LeaveWord.prototype.bindEvent = function() {
var self = this;
$(".commentList").on("click", "li", function(e) {
if(!lgin()) {
return;
}
var obj = JSON.parse($(this).attr("data-obj")),
target = e.target;
switch(target.className) {
case "dzthumb":
self.thub.call(target, obj.id, obj.agreeCount);
break;
case "callBack":
$(target).parents(".operateSpan").addClass("displayNone").siblings(".replyLeword").removeClass("displayNone");
break;
case "messageDel":
self.LwordDel(target, obj.id);
break;
case "hReply btnModel":
self.replyLword(target, obj.id)
break;
case "qCancel":
$(target).parent().siblings("textarea").val("").parents(".replyLeword").addClass("displayNone").siblings(".operateSpan").removeClass("displayNone")
break;
}
});
$(".msgCont").on("input", function() {
if($.trim($(this).val()).length > 0) {
$("#meSendtt").removeAttr("disabled");
} else {
$("#meSendtt").attr("disabled", "disabled");
}
})
$(".commentList").on("input", "textarea", function(e) {
if($.trim($(this).val()).length > 0) {
$(this).siblings().find(".btnModel").removeAttr("disabled");
self.autoGrow.call(this);
} else {
$(this).siblings().find(".btnModel").attr("disabled", "disabled");
}
})
$("#workclose").click(function(){
$(".blackcover").addClass("displayNone").find("ul").html("");
})
}
LeaveWord.prototype.referThup = function(lid, li, num) {
ajaxRequist("/ajax/leavemsg/agree", {
id: lid,
uid: $.cookie('userid')
}, "GET", function(data) {
if(data) {
li.find(".thNum").html("已赞" + num).end().find(".dzthumb").addClass("dzthumbed").css("cursor", "auto");
}
})
}
LeaveWord.prototype.thub = function(lid, num) {
var self = this;
ajaxRequist("/ajax/leavemsg/agree", {
id: lid,
uid: $.cookie('userid'),
uname: $.cookie('userName')
}, "POST", function(data) {
$(self).addClass('dzthumbed').siblings(".thNum").html("已赞" + (num + 1)) // + (Number(self.getAttribute("data-num")) + 1);
})
}
LeaveWord.prototype.replyLword = function($th, lid) {
var self = this;
if(!self.size.call($($th).parents(".pTxt").siblings("textarea"))) {
return;
}
ajaxRequist("/ajax/leavemsg/reply", {
cnt: $($th).parents(".pTxt").siblings("textarea").val(),
id: lid,
uid: $.cookie('userid'),
uname: $.cookie('userName')
}, "POST", function(data) {
$($th).parents(".pTxt").siblings("textarea").val("").parents(".replyLeword").addClass("displayNone").siblings(".operateSpan").removeClass("displayNone")
self.init({
id: self.id
});
})
}
LeaveWord.prototype.LwordDel = function($this, lid) {
var self = this;
ajaxRequist("/ajax/leavemsg/del", {
id: lid
}, "GET", function(data) {
$($this).parents("li").remove();
})
}
var module = {
lWord: new LeaveWord()
}
window.module = module;
})(window)