|
(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.bindEvent();
self.addHtml();
}
function lgin() {
if($.cookie('userid') !="null") {
return true;
}
quickLog();
operatTab();
closeLog();
return false;
}
LeaveWord.prototype.addHtml=function() {
$("body").append('<textarea class="txtArea" id="tt" style="position:absolute;top:0;left:-999px;"></textarea>')
}
LeaveWord.prototype.init = function(selecter,obj) {
obj.rows=5;
var id = $.cookie('userid');
var self = this;
ajaxRequist("/ajax/leavemsg/subject",obj, "GET", function(data) {
if(!self.id) {
var strer='<div class="ifLogin">' +
'<form class="ifLoginOn clearfix">' +
'<div class="msgContbox">' +
'<textarea class="msgCont" placeholder="请输入您的留言内容..."></textarea>' +
'<span class="msgconNum"><em>0</em>/200</span>'+
'</div>'+
'<input type="button" class="frmconbtn btnModel floatR" disabled="" value="留言">' +
'</form>' +
'</div>' +
'<div class="form-item commentsArea">'+
'<ul class="commentList">' +
'</ul>' +
'<button class="frmconbtn btnCancel displayNone">查看更多留言</button>' +
'</div>'
var $strer=$(strer);
selecter.append(strer);
$strer.find("input").data("ouse",obj);
//self.LwordTotal()
(data.length>4)?$strer.find(".btnCancel").removeClass("displayNone").data("obj",{
sid:obj.sid,
sType:4,
time:data[data.length-1].createTime,
id:data[data.length-1].id
}):"";
}else{
(data.length>4)?selector.siblings("button").removeClass("displayNone").data("obj",{
sid:obj.sid,
sType:4,
time:data[data.length-1].createTime,
id:data[data.length-1].id
}):selector.siblings("button").addClass("displayNone");
}
if(data.length == 0) {
return;
}
if(selector.siblings()[0]=="button") {
self.topHtml($strer.find("ul"),data);
}else{
self.topHtml(selector,data);
}
})
}
LeaveWord.prototype.topHtml=function(selector,data) {
var self = this;
for(var i = 0; i < data.length; i++) {
var reply="";
if(data[i].reciver) {
reply = '<span style="margin:0px 10px;">回复 </span><a href="userInforShow.html?professorId='+data[i].reciver+'" class="userhref"><span class="h1Font messageName">张某某</span></a><em class="authiconNew" title="科袖认证专家"></em>'
}
var itemlist = '<li class="flexCenter">';
itemlist += '<a href class="userhref"> <div class="madiaHead useHead useHeadMsg"></div></a>';
itemlist += '<div class="madiaInfo">';
itemlist += '<p><a href="userInforShow.html?professorId='+data[i].sender+'" class="userhref"><span class="h1Font messageName">张某某</span></a><em class="authiconNew" title="科袖认证专家"></em>'+reply+'<span class="commenttime" style="float:right;">'+commenTime(data[i].createTime)+'</span></p>';
itemlist += '<p class="h2Font messageContent">'+data[i].cnt+'</p>';
itemlist += '<div class="operateSpan"><em class="noMy"><span class="dzthumb"></span><span class="thNum">赞0</span><span class="callBack">回复</span></em><span class="messageDel displayNone">删除</span></div>';
itemlist += '<div class="displayNone replyLeword"><textarea class="txtArea"></textarea><p class="pTxt"><span class="qCancel">取消</span><button class="hReply btnModel" disabled>回复</button></p></div></div></li>';
$itemlist = $(itemlist);
selector.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].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.topLw = function($this) {
var self = this;
if(!self.size.call($($this).siblings(".msgContbox").find("textarea")) ){
return;
}
ajaxRequist("/ajax/leavemsg", {
cnt: $($this).siblings(".msgContbox").find("textarea"),
refId: $($this).data("ouse").sid,
refType: $($this).data("ouse").stype,
sender: $.cookie('userid'),
uname: $.cookie('userName')
}, "POST", function(data) {
$($this).siblings(".msgContbox").find("textarea").val("");
ajaxRequist("/ajax/leavemsg/qo", {id:data.data},"GET",function(data) {
self.topHtml($($this).parents(".ifLogin").siblings(".commentsArea").find("ul"),[data])
})
})
}
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;
$(".ifLogin").on('click',"input",function() {
if(!lgin()) {
return;
}
self.topLw($(this));
});
$("#curAnswers").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);
break;
case "hReply btnModel":self.replyLword(target,obj)
break;
case "qCancel": $(target).parent().siblings("textarea").val("").parents(".replyLeword").addClass("displayNone").siblings(".operateSpan").removeClass("displayNone")
break;
case "frmconbtn btnCancel":
var $obj=$(this).data(obj);
self.init($(this).siblings("ul"),{sid: $obj.sid,stype: $obj.stype,time: $obj.time,id: $obj.id});
}
});
$("#curAnswerst").on("input",".msgCon",function(){
if($.trim($(this).val()).length>0) {
$(this).siblings(".msgconNum").removeAttr("disabled");
}else{
$(this).siblings(".msgconNum").attr("disabled","disabled");
}
})
$("#curAnswers").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");
}
})
}
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.id,
uid: $.cookie('userid'),
uname: $.cookie('userName')
}, "POST", function(data) {
$($th).parents(".pTxt").siblings("textarea").val("").parents(".replyLeword").addClass("displayNone").siblings(".operateSpan").removeClass("displayNone")
ajaxRequist("/ajax/leavemsg/qo", {id:data.data},"GET",function(data) {
self.topHtml($($th).parents("ul"),[data])
})
self.LwordTotal($th,lid);
})
}
LeaveWord.prototype.LwordDel = function($this, lid) {
var self = this;
ajaxRequist("/ajax/leavemsg/del", {
id: lid.id
}, "GET", function(data) {
$($this).parents("li").remove();
self.LwordTotal($this,lid);
})
}
LeaveWord.prototype.LwordTotal = function($th,lid) {
var self = this;
ajaxRequist("/ajax/leavemsg/count", {
sid: lid.sid,
stype: lid.stype
}, "get", function($data) {
$($th).parents(".list-qa").find(".pageview").text($data);
})
}
var module = {
lWord: new LeaveWord()
}
window.module = module;
})(window)
|