1 line
16 KiB
JavaScript
1 line
16 KiB
JavaScript
var Page=function(e){this.currentCommentId="",this.tips=e};$.extend(Page.prototype,{insertEmotions:function(r){var l=this;void 0===r&&(r=""),$("#emotions"+r+" span").click(function(){var e=$("#comment"+r),t=l._getCursorEndPosition(e[0]),a=this.title+" ",s=e[0].value;if(s=s.substring(0,t)+a+s.substring(t,s.length),$("#comment"+r).val(s),$.browser.msie){t-=s.split("\n").length-1;var i=e[0].createTextRange();i.collapse(!0),i.moveStart("character",t+a.length),i.select()}else e[0].setSelectionRange(t+a.length,t+a.length)})},_getCursorEndPosition:function(e){if(e.focus(),e.setSelectionRange)return e.selectionEnd;if(document.selection){var t=0,a=document.selection.createRange(),s=document.body.createTextRange();for(s.moveToElementText(e),a.getBookmark(),t=0;s.compareEndPoints("StartToStart",a)<0&&0!==a.moveStart("character",-1);t++)"\n"===e.value.charAt(t)&&t++;return t}},validateComment:function(e){if("true"===latkeConfig.isLoggedIn)return!((a=$("#comment"+e).val().replace(/(^\s*)|(\s*$)/g,"")).length<2||500<a.length)||($("#commentErrorTip"+e).html(this.tips.commentContentCannotEmptyLabel),$("#comment"+e).focus(),$("#commentErrorTip"+e).show(),!1);var t=$("#commentName"+e).val().replace(/(^\s*)|(\s*$)/g,""),a=$("#comment"+e).val().replace(/(^\s*)|(\s*$)/g,"");if(t.length<2||20<t.length)$("#commentErrorTip"+e).html(this.tips.nameTooLongLabel),$("#commentName"+e).focus();else if(""===$("#commentEmail"+e).val().replace(/\s/g,""))$("#commentErrorTip"+e).html(this.tips.mailCannotEmptyLabel),$("#commentEmail"+e).focus();else if(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test($("#commentEmail"+e).val()))if(a.length<2||500<a.length)$("#commentErrorTip"+e).html(this.tips.commentContentCannotEmptyLabel),$("#comment"+e).focus();else{if(""!==$("#commentValidate"+e).val().replace(/\s/g,""))return!0;$("#commentErrorTip"+e).html(this.tips.captchaCannotEmptyLabel),$("#commentValidate"+e).focus()}else $("#commentErrorTip"+e).html(this.tips.mailInvalidLabel),$("#commentEmail"+e).focus();return $("#commentErrorTip"+e).show(),!1},replaceCommentsEm:function(e){for(var t=$(e),a=0;a<t.length;a++){var s=t[a].innerHTML;t[a].innerHTML=Util.replaceEmString(s)}},_initSyntaxHighlighter:function(e){for(var t=0;t<e.length;t++)switch(e[t]){case"groovy":e[t]="groovy\t\t\t\t"+latkeConfig.staticServePath+"/js/lib/SyntaxHighlighter/scripts/shBrushGroovy.js";break;case"java":e[t]="java\t\t\t\t"+latkeConfig.staticServePath+"/js/lib/SyntaxHighlighter/scripts/shBrushJava.js";break;case"php":e[t]="php\t\t\t\t"+latkeConfig.staticServePath+"/js/lib/SyntaxHighlighter/scripts/shBrushPhp.js";break;case"scala":e[t]="scala\t\t\t\t"+latkeConfig.staticServePath+"/js/lib/SyntaxHighlighter/scripts/shBrushScala.js";break;case"sql":e[t]="sql\t\t\t\t"+latkeConfig.staticServePath+"/js/lib/SyntaxHighlighter/scripts/shBrushSql.js";break;case"applescript":e[t]="applescript\t\t\t"+latkeConfig.staticServePath+"/js/lib/SyntaxHighlighter/scripts/shBrushAppleScript.js";break;case"as3":case"actionscript3":e[t]="actionscript3 as3 "+latkeConfig.staticServePath+"/js/lib/SyntaxHighlighter/scripts/shBrushAS3.js";break;case"bash":case"shell":e[t]="bash shell "+latkeConfig.staticServePath+"/js/lib/SyntaxHighlighter/scripts/shBrushBash.js";break;case"coldfusion":case"cf":e[t]="coldfusion cf\t\t\t"+latkeConfig.staticServePath+"/js/lib/SyntaxHighlighter/scripts/shBrushColdFusion.js";break;case"c#":case"c-sharp":case"csharp":e[t]="c# c-sharp csharp "+latkeConfig.staticServePath+"/js/lib/SyntaxHighlighter/scripts/shBrushCSharp.js";break;case"cpp":case"c":e[t]="cpp c\t\t\t\t"+latkeConfig.staticServePath+"/js/lib/SyntaxHighlighter/scripts/shBrushCpp.js";break;case"css":e[t]="css\t\t\t\t"+latkeConfig.staticServePath+"/js/lib/SyntaxHighlighter/scripts/shBrushCss.js";break;case"delphi":case"pascal":e[t]="delphi pascal\t\t\t"+latkeConfig.staticServePath+"/js/lib/SyntaxHighlighter/scripts/shBrushDelphi.js";break;case"diff":case"patch":case"pas":e[t]="diff patch pas\t\t\t"+latkeConfig.staticServePath+"/js/lib/SyntaxHighlighter/scripts/shBrushDiff.js";break;case"erl":case"erlang":e[t]="erl erlang "+latkeConfig.staticServePath+"/js/lib/SyntaxHighlighter/scripts/shBrushErlang.js";break;case"js":case"jscript":case"javascript":e[t]="js jscript javascript "+latkeConfig.staticServePath+"/js/lib/SyntaxHighlighter/scripts/shBrushJScript.js";break;case"jfx":case"javafx":e[t]="jfx javafx \t"+latkeConfig.staticServePath+"/js/lib/SyntaxHighlighter/scripts/shBrushJavaFX.js";break;case"perl":case"pl":e[t]="perl pl \t"+latkeConfig.staticServePath+"/js/lib/SyntaxHighlighter/scripts/shBrushPerl.js";break;case"plain":case"text":e[t]="text plain \t"+latkeConfig.staticServePath+"/js/lib/SyntaxHighlighter/scripts/shBrushPlain.js";break;case"ps":case"powershell":e[t]="ps powershell "+latkeConfig.staticServePath+"/js/lib/SyntaxHighlighter/scripts/shBrushPowerShell.js";break;case"py":case"python":e[t]="py python "+latkeConfig.staticServePath+"/js/lib/SyntaxHighlighter/scripts/shBrushPython.js";break;case"rails":case"ror":case"ruby":case"rb":e[t]="ruby rails ror rb \t"+latkeConfig.staticServePath+"/js/lib/SyntaxHighlighter/scripts/shBrushRuby.js";break;case"sass":case"scss":e[t]="sass scss \t"+latkeConfig.staticServePath+"/js/lib/SyntaxHighlighter/scripts/shBrushSass.js";break;case"vb":case"vbnet":e[t]="vb vbnet \t"+latkeConfig.staticServePath+"/js/lib/SyntaxHighlighter/scripts/shBrushVb.js";break;case"xml":case"xhtml":case"xslt":case"html":e[t]="xml xhtml xslt html "+latkeConfig.staticServePath+"/js/lib/SyntaxHighlighter/scripts/shBrushXml.js"}SyntaxHighlighter.autoloader.apply(null,e),SyntaxHighlighter.config.stripBrs=!0,SyntaxHighlighter.all()},_loadSyntaxHighlighter:function(e){var t=e||"shCoreEclipse",i=this;document.createStyleSheet?document.createStyleSheet(latkeConfig.staticServePath+"/js/lib/SyntaxHighlighter/styles/"+t+".css"):$("head").append($("<link rel='stylesheet' href='"+latkeConfig.staticServePath+"/js/lib/SyntaxHighlighter/styles/"+t+".css' type='text/css' charset='utf-8' />")),$.ajax({url:latkeConfig.staticServePath+"/js/lib/SyntaxHighlighter/scripts/shCore.js",dataType:"script",cache:!0,success:function(){var a=[],s=!1;$(".article-body pre, .code-highlight pre").each(function(){var e=this.className.split(";")[0],t=e.substr(7,e.length-1);-1<this.className.indexOf("html-script: true")&&"xml"!==t&&"xhtml"!==t&&"xslt"!==t&&"html"!=t&&(s=!0),a.push(t)}),s?$.ajax({url:latkeConfig.staticServePath+"/js/lib/SyntaxHighlighter/scripts/shBrushXml.js",dataType:"script",cache:!0,success:function(){i._initSyntaxHighlighter(a)}}):i._initSyntaxHighlighter(a)}})},parseLanguage:function(e){var t=!1,a=!1;return $(".article-body pre, .code-highlight pre").each(function(){-1<this.className.indexOf("brush")&&(a=!0),-1<this.className.indexOf("prettyprint")&&(t=!0)}),a?(this._loadSyntaxHighlighter(e&&e.SHTheme?e.SHTheme:void 0),!1):t?(document.createStyleSheet?document.createStyleSheet(latkeConfig.staticServePath+"/js/lib/google-code-prettify/prettify.css"):$("head").append($("<link rel='stylesheet' href='"+latkeConfig.staticServePath+"/js/lib/google-code-prettify/prettify.css'>")),$.ajax({url:latkeConfig.staticServePath+"/js/lib/google-code-prettify/prettify.js",dataType:"script",cache:!0,success:function(){prettyPrint()}}),!1):(document.createStyleSheet?document.createStyleSheet(latkeConfig.staticServePath+"/js/lib/highlight.js-9.6.0/styles/default.css"):$("head").append($("<link rel='stylesheet' href='"+latkeConfig.staticServePath+"/js/lib/highlight.js-9.6.0/styles/github.css'>")),void $.ajax({url:latkeConfig.staticServePath+"/js/lib/highlight.js-9.6.0/highlight.pack.js",dataType:"script",cache:!0,success:function(){hljs.initHighlighting.called=!1,hljs.initHighlighting()}}))},load:function(e){var t=this;t.insertEmotions(),t.parseLanguage(e&&e.language?e.language:void 0),$("#commentValidate").keypress(function(e){13===e.keyCode&&t.submitComment()}),$("#comment").keypress(function(e){13===e.keyCode&&e.ctrlKey&&t.submitComment()}),$("#captcha").click(function(){$(this).attr("src",latkeConfig.servePath+"/captcha.do?code="+Math.random())}),"true"!==latkeConfig.isLoggedIn&&($("#commentEmail").val(Cookie.readCookie("commentEmail")),$("#commentURL").val(Cookie.readCookie("commentURL")),$("#commentName").val(Cookie.readCookie("commentName")));try{JSON}catch(e){document.write('<script src="'+latkeConfig.staticServePath+'/js/lib/json2.js"><\/script>')}},loadRandomArticles:function(c){var o=this.tips.randomArticles1Label;$.ajax({url:latkeConfig.servePath+"/get-random-articles.do",type:"POST",success:function(e,t){var a=e.randomArticles;if(a&&0!==a.length){for(var s="",i=0;i<a.length;i++){var r=a[i],l=r.articleTitle;s+="<li><a rel='nofollow' title='"+l+"' href='"+latkeConfig.servePath+r.articlePermalink+"'>"+l+"</a></li>"}var n=(c||"<h4>"+o+"</h4>")+"<ul class='marginLeft12'>"+s+"</ul>";$("#randomArticles").append(n)}else $("#randomArticles").remove()}})},loadRelevantArticles:function(e,c){$.ajax({url:latkeConfig.servePath+"/article/id/"+e+"/relevant/articles",type:"GET",success:function(e,t){var a=e.relevantArticles;if(a&&0!==a.length){for(var s="",i=0;i<a.length;i++){var r=a[i],l=r.articleTitle;s+="<li><a rel='nofollow' title='"+l+"' href='"+latkeConfig.servePath+r.articlePermalink+"'>"+l+"</a></li>"}var n=c+"<ul class='marginLeft12'>"+s+"</ul>";$("#relevantArticles").append(n)}else $("#relevantArticles").remove()},error:function(){$("#relevantArticles").remove()}})},loadExternalRelevantArticles:function(e,c){var o=this.tips;try{$.ajax({url:"https://rhythm.b3log.org/get-articles-by-tags.do?tags="+e+"&blogHost="+o.blogHost+"&paginationPageSize="+o.externalRelevantArticlesDisplayCount,type:"GET",cache:!0,dataType:"jsonp",error:function(){$("#externalRelevantArticles").remove()},success:function(e,t){var a=e.articles;if(a&&0!==a.length){for(var s="",i=0;i<a.length;i++){var r=a[i],l=r.articleTitle;s+="<li><a rel='nofollow' title='"+l+"' target='_blank' href='"+r.articlePermalink+"'>"+l+"</a></li>"}var n=(c||"<h4>"+o.externalRelevantArticles1Label+"</h4>")+"<ul class='marginLeft12'>"+s+"</ul>";$("#externalRelevantArticles").append(n)}else $("#externalRelevantArticles").remove()}})}catch(e){}},submitComment:function(e,t,a){this.currentCommentId=e,t||(t="");var s=this,i=this.tips,r="article";if(void 0===i.externalRelevantArticlesDisplayCount&&(r="page"),this.validateComment(t)){$("#submitCommentButton"+t).attr("disabled","disabled"),$("#commentErrorTip"+t).show().html(this.tips.loadingLabel);var l={oId:i.oId,commentContent:$("#comment"+t).val().replace(/(^\s*)|(\s*$)/g,"")};"true"!==latkeConfig.isLoggedIn&&(l={oId:i.oId,commentContent:$("#comment"+t).val().replace(/(^\s*)|(\s*$)/g,""),commentEmail:$("#commentEmail"+t).val(),commentURL:Util.proessURL($("#commentURL"+t).val().replace(/(^\s*)|(\s*$)/g,"")),commentName:$("#commentName"+t).val().replace(/(^\s*)|(\s*$)/g,""),captcha:$("#commentValidate"+t).val()},Cookie.createCookie("commentName",l.commentName,365),Cookie.createCookie("commentEmail",l.commentEmail,365),Cookie.createCookie("commentURL",$("#commentURL"+t).val().replace(/(^\s*)|(\s*$)/g,""),365)),("Reply"===t||a)&&(l.commentOriginalCommentId=e),$.ajax({type:"POST",url:latkeConfig.servePath+"/add-"+r+"-comment.do",cache:!1,contentType:"application/json",data:JSON.stringify(l),success:function(e){if($("#submitCommentButton"+t).removeAttr("disabled"),!e.sc)return $("#commentErrorTip"+t).html(e.msg),$("#commentValidate"+t).val(""),$("#captcha"+t).click(),void("true"!==latkeConfig.isLoggedIn&&$("#captcha"+t).attr("src",latkeConfig.servePath+"/captcha.do?code="+Math.random()));$("#comment"+t).val(e.commentContent),$("#commentName"+t).val(e.commentName),e.replyNameHTML="","true"!==latkeConfig.isLoggedIn?($("#captcha"+t).attr("src",latkeConfig.servePath+"/captcha.do?code="+Math.random()),""===$("#commentURL"+t).val().replace(/\s/g,"")?e.replyNameHTML="<a>"+$("#commentName"+t).val()+"</a>":e.replyNameHTML='<a href="'+Util.proessURL($("#commentURL"+t).val())+'" target="_blank">'+$("#commentName"+t).val()+"</a>",e.userName=e.commentName):(e.replyNameHTML='<a href="'+window.location.host+'" target="_blank">'+latkeConfig.userName+"</a>",e.userName=latkeConfig.userName),"undefined"==typeof addComment?s.addCommentAjax(Util.replaceEmString(e.cmtTpl),t):s.addCommentAjax(addComment(e,t,a),t)}})}},addReplyForm:function(t,e,a){var s=this;if(t!==this.currentCommentId){$("#replyForm").remove(),"</div>"===(a=a||"")?$("#"+t).append(e+$("#commentForm").html()+a):$("#"+t).append(e+$("#commentForm").html()+"</table>"+a),$("#replyForm input, #replyForm textarea").each(function(){this.id=this.id+"Reply"}),$("#commentNameReply").val(Cookie.readCookie("commentName")),$("#commentEmailReply").val(Cookie.readCookie("commentEmail"));var i=$("#replyForm #commentURLLabel");1===i.length&&i.attr("id","commentURLLabelReply"),$("#commentURLReply").val(Cookie.readCookie("commentURL")),$("#replyForm #emotions").attr("id","emotionsReply"),this.insertEmotions("Reply"),$("#commentReply").unbind().keypress(function(e){13===e.keyCode&&e.ctrlKey&&(s.submitComment(t,"Reply"),e.preventDefault())}),$("#commentValidateReply").unbind().keypress(function(e){13===e.keyCode&&(s.submitComment(t,"Reply"),e.preventDefault())}),$("#replyForm #captcha").attr("id","captchaReply").attr("src",latkeConfig.servePath+"/captcha.do?"+(new Date).getTime()).click(function(){$(this).attr("src",latkeConfig.servePath+"/captcha.do?code="+Math.random())}),$("#replyForm #commentErrorTip").attr("id","commentErrorTipReply").html("").hide(),$("#replyForm #submitCommentButton").attr("id","submitCommentButtonReply"),$("#replyForm #submitCommentButtonReply").unbind("click").removeAttr("onclick").click(function(){s.submitComment(t,"Reply")}),""===$("#commentNameReply").val()?$("#commentNameReply").focus():""===$("#commentEmailReply").val()?$("#commentEmailReply").focus():$("#commentReply").focus(),this.currentCommentId=t}else""===$("#commentNameReply").val()?$("#commentNameReply").focus():""===$("#commentEmailReply").val()?$("#commentEmailReply").focus():$("#commentReply").focus()},hideComment:function(e){$("#commentRef"+e).hide()},showComment:function(e,t,a,s){var i=parseInt($(e).position().top);if(s&&(i=parseInt($(e).parents(s).position().top)),0<$("#commentRef"+t).length)$("#commentRef"+t).show().css("top",i+a+"px");else{var r=$("#"+t).clone();r.addClass("comment-body-ref").attr("id","commentRef"+t),r.find("#replyForm").remove(),$("#comments").append(r),$("#commentRef"+t).css("top",i+a+"px")}},addCommentAjax:function(e,t){0<$("#comments").children().length?$($("#comments").children()[0]).before(e):$("#comments").html(e),""===t?($("#commentErrorTip").html("").hide(),$("#comment").val(""),$("#commentValidate").val(""),$("#captcha").attr("src",latkeConfig.servePath+"/captcha.do?code="+Math.random())):$("#replyForm").remove(),window.location.hash="#comments"}}); |