2 * @brief The file contains functions for text editing and commenting
5 // Lifted from https://css-tricks.com/snippets/jquery/move-cursor-to-end-of-textarea-or-input/
6 jQuery.fn.putCursorAtEnd = function() {
7 return this.each(function() {
12 // Only focus if input isn't already
13 if (!$el.is(":focus")) {
17 // If this function exists... (IE 9+)
18 if (el.setSelectionRange) {
19 // Double the length because Opera is inconsistent about whether a carriage return is one character or two.
20 var len = $el.val().length * 2;
22 // Timeout seems to be required for Blink
23 setTimeout(function() {
24 el.setSelectionRange(len, len);
27 // As a fallback, replace the contents with itself
28 // Doesn't work in Chrome, but Chrome supports setSelectionRange
32 // Scroll to the bottom, in case we're in a tall textarea
33 // (Necessary for Firefox and Chrome)
34 this.scrollTop = 999999;
38 function commentGetLink(id, prompttext) {
39 reply = prompt(prompttext);
40 if(reply && reply.length) {
41 reply = bin2hex(reply);
42 $.get('parse_url?noAttachment=1&binurl=' + reply, function(data) {
43 addCommentText(data, id);
48 function addCommentText(data, id) {
50 var textfield = document.getElementById("comment-edit-text-" + id);
51 // check if the textfield does have the default-value
52 commentOpenUI(textfield, id);
53 // save already existent content
54 var currentText = $("#comment-edit-text-" + id).val();
55 //insert the data as new value
56 textfield.value = currentText + data;
57 autosize.update($("#comment-edit-text-" + id));
60 function commentLinkDrop(event, id) {
61 var reply = event.dataTransfer.getData("text/uri-list");
62 event.target.textContent = reply;
63 event.preventDefault();
64 if (reply && reply.length) {
65 reply = bin2hex(reply);
66 $.get('parse_url?noAttachment=1&binurl=' + reply, function(data) {
67 addCommentText(data, id);
72 function commentLinkDropper(event) {
73 var linkFound = event.dataTransfer.types.contains("text/uri-list");
75 event.preventDefault();
79 function insertFormattingToPost(BBCode) {
80 textarea = document.getElementById("profile-jot-text");
82 insertBBCodeInTextarea(BBCode, textarea);
87 function showThread(id) {
88 $("#collapsed-comments-" + id).show()
89 $("#collapsed-comments-" + id + " .collapsed-comments").show()
91 function hideThread(id) {
92 $("#collapsed-comments-" + id).hide()
93 $("#collapsed-comments-" + id + " .collapsed-comments").hide()
96 function cmtBbOpen(id) {
97 $("#comment-edit-bb-" + id).show();
99 function cmtBbClose(id) {
100 $("#comment-edit-bb-" + id).hide();
103 function commentExpand(id)
105 $("#mod-cmnt-wrap-" + id).show();
106 closeMenu("comment-fake-form-" + id);
107 openMenu("item-comments-" + id);
108 $("#comment-edit-text-" + id)
110 .addClass("comment-edit-text-full")
111 .removeClass("comment-edit-text-empty");
116 function commentClose(obj, id)
118 if (obj.value === '' || obj.value === obj.dataset.default) {
119 $("#comment-edit-text-" + id)
120 .removeClass("comment-edit-text-full")
121 .addClass("comment-edit-text-empty");
122 $("#mod-cmnt-wrap-" + id).hide();
123 openMenu("comment-fake-form-" + id);
124 closeMenu("item-comments-" + id);
130 function showHideCommentBox(id) {
131 var $el = $('#comment-edit-form-' + id);
132 if ($el.is(':visible')) {
139 function commentOpenUI(obj, id) {
140 closeMenu("comment-fake-form-" + id);
141 openMenu("item-comments-" + id);
142 $("#comment-edit-text-" + id)
144 .addClass("comment-edit-text-full").removeClass("comment-edit-text-empty")
145 .attr('tabindex', '9'); // Choose an arbitrary tab index that's greater than what we're using in jot (3 of them)
146 $("#comment-edit-submit-" + id).attr('tabindex', '10'); // The submit button gets tabindex + 1
147 // initialize autosize for this comment
148 autosize($("#comment-edit-text-" + id + ".text-autosize"));
151 function commentCloseUI(obj, id) {
152 if (obj.value === '' || obj.value === obj.dataset.default) {
153 $("#comment-edit-text-" + id)
154 .removeClass("comment-edit-text-full").addClass("comment-edit-text-empty")
155 .removeAttr('tabindex');
156 $("#comment-edit-submit-" + id).removeAttr('tabindex');
157 openMenu("comment-fake-form-" + id);
158 closeMenu("item-comments-" + id);
159 // destroy the automatic textarea resizing
160 autosize.destroy($("#comment-edit-text-" + id + ".text-autosize"));
164 function jotTextOpenUI(obj) {
165 if (obj.value === '' || obj.value === obj.dataset.default) {
166 var $el = $(".modal-body #profile-jot-text");
167 $el.addClass("profile-jot-text-full").removeClass("profile-jot-text-empty");
168 // initiale autosize for the jot
173 function jotTextCloseUI(obj) {
174 if (obj.value === '' || obj.value === obj.dataset.default) {
175 var $el = $(".modal-body #profile-jot-text");
176 $el.removeClass("profile-jot-text-full").addClass("profile-jot-text-empty");
177 // destroy the automatic textarea resizing
178 autosize.destroy($el);
182 function commentOpen(obj, id) {
183 if (obj.value === '' || obj.value === obj.dataset.default) {
184 $("#comment-edit-text-" + id)
186 .addClass("comment-edit-text-full")
187 .removeClass("comment-edit-text-empty");
188 $("#mod-cmnt-wrap-" + id).show();
189 closeMenu("comment-fake-form-" + id);
190 openMenu("item-comments-" + id);
196 function confirmDelete() {
197 return confirm(aStr.delitem);
201 * Hide and removes an item element from the DOM after the deletion url is
202 * successful, restore it else.
204 * @param {string} url The item removal URL
205 * @param {string} elementId The DOM id of the item element
206 * @returns {undefined}
208 function dropItem(url, elementId) {
209 var confirm = confirmDelete();
212 $('body').css('cursor', 'wait');
214 var $el = $(document.getElementById(elementId));
216 $el.fadeTo('fast', 0.33, function () {
217 $.get(url).then(function() {
220 // @todo Show related error message
222 }).always(function() {
223 $('body').css('cursor', 'auto');