%PDF- %PDF-
| Direktori : /data/old/home/stash/stash/atlassian-stash/static/feature/comments/ |
| Current File : //data/old/home/stash/stash/atlassian-stash/static/feature/comments/comment-container-min.js |
define("feature/comments/comment-container",["jquery","memoir","underscore","util/dom-event","util/events","util/function","util/scroll","model/page-state","widget/aui/form","widget/confirm-dialog","widget/markup-editor","feature/comments/comment-collection","feature/comments/comment-model","feature/comments/comment-tips"],function(F,L,M,D,O,I,G,J,A,N,E,C,B,H){var K=450;return Backbone.View.extend({initialize:function(){M.bindAll(this,"onCommentEditorResize");this.anchor=this.anchor||this.options.anchor;this.rootCommentListSelector=this.rootCommentListSelector||this.options.rootCommentListSelector;this.context=this.options.context;this.pullRequest=this.options.pullRequest||J.getPullRequest();if(!this.collection){this.collection=new C([],{anchor:this.anchor})}this.initDeleteButtons();this.$el.imagesLoaded(this.onImagesLoaded.bind(this));O.trigger("stash.feature.comments.commentContainerAdded",null,this.$el);var P=300;this.updateDraftComment=M.debounce(this.updateDraftComment,P);this.deleteDraftComment=M.debounce(this.deleteDraftComment,P)},events:{"submit form":"onFormSubmit","click a.times":"onDateClicked","click .cancel":"onCancelClicked","click .reply":"onReplyClicked","click .edit":"onEditClicked","keydown form":"onFormKeydown","input textarea":"onTextareaInput"},initDeleteButtons:function(P){this.createDeleteDialog().attachTo(".delete",null,this.el)},createDeleteDialog:function(){var P=this;var Q=new N({id:"delete-repository-dialog",titleText:AJS.I18n.getText("stash.web.comment.delete.title"),titleClass:"warning-header",panelContent:"<p>"+AJS.I18n.getText("stash.web.comment.delete.confirm")+"</p>",submitText:AJS.I18n.getText("stash.web.button.delete"),submitToHref:false});Q.addConfirmListener(function(T,R,S){S();P.deleteComment(R.closest(".comment"))});return Q},onFormSubmit:function(P){P.preventDefault();P.stopPropagation();this.submitCommentForm(F(P.target))},onDateClicked:function(Q){Q.preventDefault();Q.stopPropagation();F(".comment.focused").removeClass("focused");var P=F(Q.target).closest("a");P.closest(".comment").addClass("focused");L.pushState(null,null,P.prop("href"))},onCancelClicked:function(P){P.preventDefault();P.stopPropagation();this.cancelCommentForm(F(P.target).closest("form"))},onReplyClicked:function(P){P.preventDefault();P.stopPropagation();this.openReplyForm(F(P.target).closest(".comment"))},onEditClicked:function(P){P.preventDefault();P.stopPropagation();this.openEditForm(F(P.target).closest(".comment"))},onImagesLoaded:function(P){if(F.contains(document.documentElement,P[0])){this.trigger("change")}},onFormKeydown:function(P){if(D.isCtrlish(P)&&P.which===F.ui.keyCode.ENTER){P.preventDefault();P.stopPropagation();F(P.target).closest("form").submit()}},onTextareaInput:function(P){if(F(P.target).closest(".comment-container").is(this.el)){this.updateDraftComment(P.target)}},updateDraftComment:function(Q){var P=this.getDraftCommentFromForm(F(Q).closest("form"));this.context&&this.context.saveDraftComment(P)},getDraftCommentFromForm:function(Q){var P=this.getCommentFormJSON(Q);if(P.anchor){delete P.anchor.commitRange}return F.extend({},P)},deleteDraftComment:function(P){this.context&&this.context.deleteDraftComment(P)},getRootCommentList:function(){var P=this.$(this.rootCommentListSelector);if(!P.length){P=this.$el}return P},render:function(){var P=stash.feature.comments(F.extend({comments:this.collection.toJSON()},this.anchor.toJSON()));this.$el.replaceWith(P);this.setElement(P[0])},_toJSON:function(Q,R){var T=parseInt(Q.parent().closest(".comment").attr("data-id"),10);var S=parseInt(Q.attr("data-id"),10);var P=parseInt(Q.attr("data-version"),10);return{id:!isNaN(S)?S:undefined,version:!isNaN(P)?P:undefined,text:R,anchor:this.anchor.toJSON(),parent:!isNaN(T)?{id:T}:undefined}},getCommentJSON:function(P){return this._toJSON(P,P.find("> .content > .message").attr("data-text"))},getCommentFormJSON:function(P){var Q=P.parent().is(".comment")?P.parent():P;return this._toJSON(Q,P.find("textarea").val())},enableDeletion:function(P){P.find("> .content > .actions > li > .delete").parent().removeClass("hidden")},disableDeletion:function(P){P.find("> .content > .actions > li > .delete").parent().addClass("hidden")},renderContentUpdate:function(P,Q){P.children(".content").replaceWith(stash.feature.commentContent({pullRequest:this.pullRequest&&this.pullRequest.toJSON(),comment:Q,hideDelete:!!P.find("> .replies > .comment").length}));P.attr("data-version",Q.version).data("version",Q.version);this.$el.imagesLoaded(this.onImagesLoaded.bind(this));this.trigger("change");O.trigger("stash.feature.comments.commentEdited",null,Q,P)},insertCommentIntoList:function(R,P,S){var Q=P.children(".comment:first");while(Q.length){if(parseInt(Q.data("id"),10)>S.id){break}Q=Q.next(".comment")}Q=Q.length?Q:P.children(".comment-form-container:last");if(Q.length){R.insertBefore(Q)}else{P.append(R)}},renderComment:function(U,V,R){var Q;if(R&&(Q=F('.comment[data-id="'+U.id+'"]')).length){return this.renderContentUpdate(Q,U)}U=F.extend({isNew:true},U);var T=V&&this.$("[data-id="+V+"]");if(T){this.disableDeletion(T)}var S=(T?T.children(".replies"):this.getRootCommentList());Q=F(stash.feature.comment({pullRequest:this.pullRequest&&this.pullRequest.toJSON(),numOfAncestors:S.parents(".comment").length,extraClasses:this.getExtraCommentClasses(),comment:U}));this.insertCommentIntoList(Q,S,U);var P=5000;setTimeout(M.bind(Q.removeClass,Q,"new"),P);G.scrollTo(Q);Q.hide().fadeIn("slow");this.$el.imagesLoaded(this.onImagesLoaded.bind(this));this.trigger("change");O.trigger("stash.feature.comments.commentAdded",null,U,Q)},getExtraCommentClasses:function(){return""},showErrorMessage:function(S,R){var Q=this;var P=Q.find(".error");if(!P.length){P=F('<div class="error"></div>');Q.find(".comment-form-footer").before(P)}P.text(R)},cancelCommentForm:function(P){this.closeCommentForm(P)},submitCommentForm:function(Q){if(A.isSubmissionPrevented(Q)){return }var P=this;var R=Q.find(".comment-submit-spinner");var U=this.getCommentFormJSON(Q);var S=U.id!=null;var T=S&&this.collection.get(U.id);var W=U.parent&&U.parent.id;var V=T?this.collection.get(U.id):new B();V.on("invalid",this.showErrorMessage,Q);if(V.set(F.extend(U,{avatarSize:stash.widget.avatarSizeInPx({size:"medium"})}),{validate:true})){if(!T){this.collection.push(V)}Q.addClass("submitting");R.spin("medium");A.preventSubmission(Q);V.save().done(function(X){X.createdDate=Math.min(X.createdDate,new Date().getTime());X.updatedDate=Math.min(X.updatedDate,new Date().getTime());P.closeCommentForm(Q,{doNotDestroy:true});P.renderComment(X,W,S);P.trigger("comment.saved")}).fail(function(){if(!T&&!S){P.collection.remove(U.id)}}).always(function(){R.spinStop();Q.removeClass("submitting");A.allowSubmission(Q)})}V.off("invalid",this.showErrorMessage)},deleteComment:function(S){var T=this.getCommentJSON(S);var U;if(this.collection.get(T)){U=this.collection.get(T.id)}else{U=new B(T);this.collection.push(U)}var P=S.find("> .content .delete"),R={h:P.height(),w:P.width()};P.height(R.h).width(R.w).css("vertical-align","middle").empty().spin("small");var Q=this;U.destroy({wait:true}).always(function(){P.spinStop()}).done(function(){var W=S.parent().closest(".comment");if(W.length){var V=S.siblings(".comment").length||S.siblings(".comment-form-container").find("textarea").val();if(!V){Q.enableDeletion(W)}}S.fadeOut(function(){S.remove();Q.onCommentDeleted();Q.trigger("change");O.trigger("stash.feature.comments.commentDeleted",null,T)})}).fail(function(){P.css("height","").css("width","").css("vertical-align","").text(AJS.I18n.getText("stash.web.comment.delete"))})},onCommentDeleted:function(){},onCommentEditorResize:F.noop,openEditForm:function(R){var S=this.getCommentJSON(R);var P=F(stash.feature.commentForm(F.extend({tips:this.$el.width()>K?H.tips:[],currentUser:J.getCurrentUser()&&J.getCurrentUser().toJSON()},S)));var Q=R.find("> .user-avatar, > .content");Q.remove();R.prepend(P).addClass("comment-form-container");P.data("originalContent",Q);this._bindMarkupEditor(P);this.focusCommentForm(P);this.trigger("change");O.trigger("stash.feature.comments.commentFormShown",null,P);return P},openReplyForm:function(Q){var P=Q.children(".replies");return this.openCommentForm(P,{location:"top"})},openNewCommentForm:function(){return this.openCommentForm(this.getRootCommentList(),{location:"bottom"})},openCommentForm:function(P,S){var T=S&&S.location==="top"?"prependTo":"appendTo";var Q=P.children(".comment-form-container").not(".comment");if(!Q.length){Q=F(stash.feature.commentFormListItem({tips:this.$el.width()>K?H.tips:[],currentUser:J.getCurrentUser()&&J.getCurrentUser().toJSON()}))[T](P);this._bindMarkupEditor(Q.find("form"))}var R=Q.find("form");this.focusCommentForm(R);this.trigger("change");O.trigger("stash.feature.comments.commentFormShown",null,R);return R},closeCommentForm:function(P){P.find(".error").remove();this.deleteDraftComment(this.getDraftCommentFromForm(P));this._unbindMarkupEditor(P);var Q=P.data("originalContent");var R=P.parent();if(Q){R.removeClass("comment-form-container");P.replaceWith(Q)}else{R.remove()}this.trigger("change");O.trigger("stash.feature.comments.commentFormHidden",null,P)},focusCommentForm:M.debounce(function(P){P.find("textarea").focus()},0),populateCommentFormFromDraft:function(Q,P){F(Q).find("textarea").val(P.text).addClass("restored").attr("title",AJS.I18n.getText("stash.web.comment.restored.draft.title")).trigger("input").one("click input",function(){F(this).removeClass("restored").removeAttr("title")})},getCommentElById:function(P){return this.$(".comment[data-id="+P+"]")},restoreDraftComment:function(P){var Q;if(P.id){var R=this.getCommentElById(P.id);if(R.length){if(parseInt(P.version,10)<parseInt(R.attr("data-version"),10)){this.context.deleteDraftComment(P);return true}Q=this.openEditForm(R)}}else{if(P.parent){var S=this.getCommentElById(P.parent.id);if(S.length){Q=this.openReplyForm(S)}}else{Q=this.openNewCommentForm()}}Q&&this.populateCommentFormFromDraft(Q,P);return !!Q},_bindMarkupEditor:function(P){E.bindTo(P).on("resize",this.onCommentEditorResize)},_unbindMarkupEditor:function(P){E.unbindFrom(P)},destroy:function(){this.$("form").each(I.thisToParam(this._unbindMarkupEditor.bind(this)))}})});