define('jira/viewissue/comment/comment-form', [ 'jira/util/formatter', 'jira/util/events', 'jira/util/events/types', 'jira/ajs/ajax/smart-ajax', 'jira/issue', 'jira/dialog/form-dialog', 'wrm/context-path', 'jira/jquery/deferred', 'jquery' ], function( formatter, Events, Types, SmartAjax, IssueApi, FormDialog, wrmContextPath, Deferred, $ ) { var contextPath = wrmContextPath(); var commentForm = { /** * Cancels a comment. This means clearing the text area, resetting the * dirty state for the closes form, and collapsing the comment box. * * If comment preview mode is enabled, this function disables it before * attempting to clear the comment textarea. */ setCaretAtEndOfCommentField: function () { var $field = this.getField(); var field = $field[0]; var length; if ($field.length) { length = $field.val().length; $field.scrollTop($field.attr("scrollHeight")); if (field.setSelectionRange && length > 0) { field.setSelectionRange(length, length); } } }, /** * When we submit disable all the fields to avoid mods and double submit */ disable: function () { this.getForm().find("textarea").attr("readonly", "readonly"); this.getForm().find("input[type=submit]").attr("disabled", "disabled"); }, /** * Check if value has changed * * @return {Boolean} */ isDirty: function () { var field = this.getField(); if (field.length) { return field[0].value !== field[0].defaultValue; } return false; }, /** * Construct a dirty comment warning if the comment form is dirty. * * @returns {string|undefined} A dirty comment warning or undefined. */ handleBrowseAway: function () { // If the form isn't dirty, no point continuing. if (!commentForm.isDirty()) {return;} // If the form isn't visible, then don't show a dirty warning. This // is particularly important for the issue search single-page-app. var form = commentForm.getForm(); var isVisible = form.length && form.is(":visible"); if (isVisible) { return formatter.I18n.getText("common.forms.dirty.comment"); } }, isVisibilityAvailble: function() { return this.getForm().find("#commentLevel :selected").val() != 'none'; }, setSubmitState: function () { if ($.trim(this.getField().val()).length > 0 && this.isVisibilityAvailble()) { this.getForm().find("#issue-comment-add-submit").removeAttr("disabled"); } else { this.getForm().find("#issue-comment-add-submit").attr("disabled", "disabled"); } }, /** * Enables the comment form */ enable: function () { this.getForm().find("textarea").removeAttr("readonly"); this.getForm().find("input[type=submit]").removeAttr("disabled"); }, /** * Get comment visibility permission. * * @return {Object} -- null if no value has been selected */ getCommentVisibility: function() { var visibility = this.getForm().find("#commentLevel :selected").val(); if (visibility) { var split_v = visibility.split(':'); return { "type" : split_v[0], "value" : this.getForm().find("#commentLevel :selected").text() }; } return null; }, /** * Submits comments via ajax, used in kickass * @returns {$.Deferred} */ ajaxSubmit: function () { var $loading = $(''); var issueId = IssueApi.getIssueId(); var issueKey = IssueApi.getIssueKey(); var restURL = contextPath + "/rest/api/2/issue/" + issueKey + "/comment"; //build rest request var newComment = { // set line ending to CRLF for consistency with other comment methods // for example: add comment in new tab (midlde click), add comment in edit issue, or edit comment - they all use CRLF "body":this.getField()[0].value.replace(/\r?\n/g, "\r\n") }; var visibility = this.getCommentVisibility(); if (visibility) { newComment["visibility"] = visibility; } var updateDone = Deferred(); var result = $.ajax({ url:restURL, type:"POST", contentType:"application/json", data:JSON.stringify(newComment), success:function (data) { Events.trigger(Types.UNLOCK_PANEL_REFRESHING, ["addcommentmodule"]); Events.trigger(Types.REFRESH_ISSUE_PAGE, [issueId, { complete:function () { //highlight comment, set anchor var newCommentId = "comment-" + data.id; //Do not append the hash to the url (to let browser scroll the element into view) //Appending the hash will add a new history point and as of 6.0 it will be incompatible, //as back/forward button will be navigation between issues, instead of the states in the current issue. //Instead manually scroll the element into view $("#" + newCommentId).scrollIntoView({ marginBottom: 200, marginTop: 200 }); //remove the focusing from any other comments var $focusedTabs = $("#issue_actions_container > .issue-data-block.focused"); $focusedTabs.removeClass("focused"); var $newfocusedTab = $("#" + newCommentId); //assume only one focused comment $newfocusedTab.addClass("focused"); $loading.remove(); // Re-enable the comment form after successful complete in preparation for future use. commentForm.enable(); // Signal the update was successful updateDone.resolve(); } }]); }, error: function (xhr) { function buildErrorDialog(errorMessage) { var errorContent = '