PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/startZine/meilishuo/meilishuo_interview_files/1305244809636_data/SNB_004.js

http://ownerpress.googlecode.com/
JavaScript | 1 lines | 1 code | 0 blank | 0 comment | 1 complexity | 5d7876bf4ad0453a7cdd5687a46a79ad MD5 | raw file
Possible License(s): Apache-2.0, AGPL-1.0, GPL-2.0, GPL-3.0, LGPL-2.1
  1. SNB.Models.Comment=Backbone.Model.extend({sync:function(a,b,c){switch(a){case"create":c.url=SNB.domain.apiBase+"/statuses/reply.json",c.type="post",c.data={id:b.get("statusId"),comment:b.get("comment"),forward:b.get("forward")},b.get("in_reply_to_comment_id")&&(c.data.cid=b.get("in_reply_to_comment_id"));break;case"delete":c.url=SNB.domain.apiBase+"/statuses/comment_destroy/"+b.get("id")+".json";break;case"update":c.url=SNB.domain.apiBase+"/comments/edit.json",c.data={text:b.get("text"),id:b.get("id")}}return $.ajax(c)},parse:function(a){var b=$.parseJSON(a),c=b.user,d=c.profile_image_url?c.profile_image_url.split(","):"",e={img:SNB.domain.photo+"/"+(d.length>3?d[3]:d.length==1?d[0]:"community/default/30x30.gif"),id:c.id,profile:c.profile,screen_name:c.screen_name,verified:c.verified,verified_description:c.verified_description},f=SNB.Util.parseTime(b.created_at);return b.user=e,b.createdAt=f,b},rtext:function(){return this.get("text")},head_img:function(){var a=this.get("user").profile_image_url?this.get("user").profile_image_url.split(","):"";return a.length>3?a[3]:a.length==1?a[0]:"community/default/30x30.gif"},screen_name:function(){return this.get("user").screen_name},profile:function(){return this.get("user").profile},verified:function(){return this.get("user").verified},verified_description:function(){return this.get("user").verified_description},created_time:function(){return SNB.Util.parseTime(this.get("created_at"))},isDelete:function(){return SNB.currentUser&&SNB.currentUser.id==this.get("user").id},isEdit:function(){return SNB.currentUser&&SNB.currentUser.id==this.get("user").id}}),SNB.Collections.Comments=Backbone.Collection.extend({model:SNB.Models.Comment,initialize:function(a,b){this.statusId=b.statusId,this.commentCount=b.commentCount,this.in_reply_to_comment_id=b.in_reply_to_comment_id,this.page=this.page?this.page:b.page?b.page:1,this.maxPage=this.maxPage?this.maxPage:b.maxPage?b.maxPage:1,this.url=SNB.domain.apiBase+"/statuses/commentlist.json?id="+b.statusId},updateCount:function(a){this.commentCount&&(this.commentCount.innerHTML=SNB.Util.updateCount(this.commentCount.innerHTML,a))},parse:function(a){var b=typeof a=="string"?$.parseJSON(a):a;return this.maxPage=b.maxPage,this.page=b.page,_.map(b.comments,function(a){var b=a.user,c=b.profile_image_url?b.profile_image_url.split(","):"",d={img:SNB.domain.photo+"/"+(c.length>3?c[3]:c.length==1?c[0]:"community/default/30x30.gif"),id:b.id,profile:b.profile,screen_name:b.screen_name,verified:b.verified,verified_description:b.verified_description},e=SNB.Util.parseTime(a.created_at);return a.user=d,a.createdAt=e,a}),b.comments}}),SNB.Templates.Comment='<div class="headpic"><a href="{{=it.user.profile}}"><img src="{{=it.user.img}}"></a></div><div class="content"><div class=\'comment\'><a href="{{=it.user.profile}}">{{=it.user.screen_name}}</a>{{ if (it.user.verified){ }}'+"<img width='15px' height='13px' title='{{=it.user.verified_description}}' src='"+SNB.domain["static"]+"/images/vipicon.png'/>"+"{{ } }}"+': <span class="cmt_con">{{=it.text}}</span>'+'<span class="time">(<span class="createAt" createdat="{{=it.created_at}}">{{=it.createdAt}}</span>)</span>'+"</div>"+'<div class="ops">'+"{{ if ((it.user.id == {{#def.uid}}) && it.canEdit) { }}"+'<a href="#" class="edit">\u4fee\u6539</a>'+"{{ } }}"+"{{ if (it.user.id == {{#def.uid}}) { }}"+'<a href="#" class="deleteComment">\u5220\u9664</a>'+"{{ } }}"+'<a href="#" class="reply">\u56de\u590d</a>'+"</div>"+"</div>"+"{{ if ((it.user.id == {{#def.uid}}) && it.canEdit) { }}"+'<div style="display:none" class="editarea">'+'<textarea class="comment_edit"></textarea>'+'<div class="ops">'+'<a href="#" class="comment_cancel">\u53d6\u6d88</a>'+'<input type="button" class="comment_save" value="\u4fdd\u5b58"/>'+"</div>"+"</div>"+"{{ } }}",SNB.Templates.commentFunc=doT.template(SNB.Templates.Comment,undefined,{uid:SNB.currentUser.id||0}),SNB.Views.Comment=Backbone.View.extend({initialize:function(a){this.editor=a.editor,_.bindAll(this,"render"),this.model.bind("change",this.render,this),this.model.bind("destroy",this.remove,this)},tagName:"li",events:{"click .edit":"edit","click .reply":"reply","click .deleteComment":"destroy","click .comment_cancel":"cancel","click .comment_save":"save"},render:function(){return $(this.el).html(SNB.Templates.commentFunc(this.model.toJSON())),this.$(".content").show(),this.$(".editarea").hide(),this},edit:function(a){a.preventDefault(),this.$(".content").hide();var b=SNB.Util.reparseContent(this.model.get("text").replace(/<br\/?>/g,"\n"));this.$(".editarea").show().find("textarea").val("").autoResize({minHeight:50,extraSpace:20}).insertString(b).trigger("keyup")},reply:function(a){a.preventDefault();if(!this.editor.attr("disabled")){var b=this,c="\u56de\u590d@"+b.model.get("user").screen_name+": ";b.editor.val(c).focus().setSelection(c.length,0),b.model.collection.in_reply_to_comment_id=b.model.id,b.model.get("statusId")&&(b.model.collection.statusId=b.model.get("statusId"),delete b.model.collection.in_reply_to_comment_id)}},destroy:function(a){var b=$("#dialog-delete-comment"),c=this;if($("#dialog-delete-comment").length===0){var d='<div id="dialog-delete-comment" class="dialog-wrapper"><div class="tipsdivcontent" style="text-align:center;"><p class="message" style="text-align:center;">\u786e\u5b9a\u5220\u9664\u5417\uff1f</p><div><input type="button" class="submit okButton" value="\u786e\u5b9a"/><input type="button" class="button cancelButton" value="\u53d6\u6d88"/></div></div></div>';$("body").append(d),b=$("#dialog-delete-comment")}b.find(".cancelButton").click(function(){b.dialog("close")}),b.find(".okButton").unbind("click").click(function(){b.dialog("close"),a.preventDefault(),c.model.collection.updateCount(-1),c.model.destroy(),c.remove()});var e=$(a.target);return b.dialog({modal:"true",minHeight:50,width:200,position:[e.offset().left-110,e.offset().top-$(document).scrollTop()-110]}).prev().hide(),!1},cancel:function(a){a.preventDefault(),this.$(".content").show(),this.$(".editarea").hide()},save:function(a){a.preventDefault();var b=this.$(".comment_edit").val().replace(/\n/g,"<br/>");this.model.save({text:b})}}),SNB.Templates.Comments='<div class="commentPostArea"><div class="commentTop"></div><div id="inputArea"><textarea class="commentEditor" ></textarea></div><p class="forbidden" style="display:none">\u7531\u4e8e\u5bf9\u65b9\u7684\u8bbe\u7f6e\uff0c\u60a8\u4e0d\u80fd\u8bc4\u8bba</p><span class="showFaceButton">&nbsp;</span><span class="addStock">&nbsp;</span><label><input type="checkbox" name="forward"/>\u540c\u65f6\u8f6c\u53d1\u5230\u6211\u7684\u9996\u9875 </label><input type="submit" class="commentSubmit button" value="\u53d1\u5e03"/><div class="fixit"></div></div><div class=\'loading\' style=\'display:none\'></div><ul class="commentList"></ul>',SNB.Views.Comments=Backbone.View.extend({initialize:function(a){this.collection.bind("reset",this.render,this),this.collection.bind("add",this.addComment,this),this.collection.commentTo=a.commentTo},events:{"click .commentSubmit":"submitComment","click .addStock":"addStock","click .showFaceButton":"showFace"},template:SNB.Templates.Comments,render:function(a){var b=this;if(!$(this.el).html()){$(this.el).append(this.template),this.$("textarea").autoResize({minHeight:50,extraSpace:30});var c=this.$("textarea"),b=this,d=this.submitComment;$.browser.msie&&c.live("keyup keydown mouseup",function(){$(this).data("start",$(this).getSelectionStart()),$(this).data("end",$(this).getSelectionEnd())}),function(){var a=!1;c.keydown(function(c){if(c.keyCode==17||c.keyCode==91)a=!0;if(a&&c.keyCode===13)return d.call(b,{}),!1}).keyup(function(b){if(b.keyCode==17||b.keyCode==91)a=!1})}()}(!a||this.collection&&$(this.collection.commentCount).text().match(/\d+/ig))&&this.$(".loading").show(),SNB.currentUser.isGuest||$.getJSON(SNB.domain.apiBase+"/statuses/comment/allow.json",{id:this.collection.statusId},function(a){a.allow||(b.$("textarea").attr("disabled","disabled"),b.$(".forbidden").show())});var e=$("<ul class='commentList'></ul>");this.collection.each(function(a){e.append((new SNB.Views.Comment({model:a,id:"comment_"+a.id,editor:b.$(".commentEditor").filter(":last")})).render().el)});var b=this;this.collection.maxPage>1&&e.append($(SNB.Pager(this.collection.page,this.collection.maxPage)).find("a").click(function(a){a.preventDefault();var c=$(this).attr("data-page");b.$(".commentList").html(""),b.$(".loading").show(),b.collection.fetch({data:{page:c}}),$("html, body").animate({scrollTop:b.$(".commentPostArea").offset().top},200)}).end());var f=this.collection.commentTo?"\u56de\u590d @"+this.collection.commentTo.user.screen_name+": ":"";this.$(".commentEditor").val(),f&&(this.collection.$textarea=this.$(".commentEditor").insertString(f)),this.$(".commentList").replaceWith(e),a!=1&&this.$(".loading").hide()},display:!0,toggle:function(a){this.display?($(this.el).hide(),this.display=!1):(this.$(".commentList").html(""),a||(this.$(".loading").show(),this.collection.fetch()),$(this.el).show(),this.display=!0)},addComment:function(a){this.$(".commentList").prepend((new SNB.Views.Comment({model:a})).render().el)},submitComment:function(a){var b=this;SNB.Util.checkLogin(function(){$.getJSON(SNB.domain.apiBase+"/statuses/comment/allow.json",{id:b.collection.statusId},function(a){if(a.allow){var c=b.$(".commentEditor").filter(":last"),d=c.val();if($.trim(d)==="")return alert("\u8bc4\u8bba\u4e0d\u80fd\u4e3a\u7a7a\uff01"),!1;c.val("").trigger("keyup");var e=b.$("input[name=forward]:checked").length>0?1:0;b.$(".commentSubmit").attr("disabled","disabled"),b.collection.create({statusId:b.collection.statusId,comment:d.replace(/\n/g,"<br/>"),in_reply_to_comment_id:b.collection.in_reply_to_comment_id,forward:e},{success:function(a){var c=b.$(".commentSubmit").attr("disabled");b.$(".commentSubmit").removeAttr("disabled"),b.collection.updateCount()},error:function(a){b.$(".commentSubmit").removeAttr("disabled"),alert("\u8bc4\u8bba\u5931\u8d25")}})}else alert("\u5bf9\u65b9\u4e0d\u5141\u8bb8\u8bc4\u8bba")})},a)},addStock:function(a){var b=$(this.el).find(".commentEditor").filter(":last"),c=[b.offset().left-$(window).scrollLeft(),b.offset().top-$(window).scrollTop()+b.height()];SNB.Util.insertStock(c,function(a){b.insertString("$"+a.name+"("+a.value+")$").focus()})},showFace:function(a){var b=$(this.el).find(".commentEditor").filter(":last");SNB.Util.emotion(a,function(a){b.insertString(a).focus()})}})