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();
80 function insertFormatting(BBcode, id) {
81 var tmpStr = $("#comment-edit-text-" + id).val();
83 $("#comment-edit-text-" + id).addClass("comment-edit-text-full");
84 $("#comment-edit-text-" + id).removeClass("comment-edit-text-empty");
85 closeMenu("comment-fake-form-" + id);
86 openMenu("item-comments-" + id);
89 textarea = document.getElementById("comment-edit-text-" + id);
90 if (document.selection) {
92 selected = document.selection.createRange();
93 selected.text = "[" + BBcode + "]" + selected.text + "[/" + BBcode + "]";
94 } else if (textarea.selectionStart || textarea.selectionStart == "0") {
95 var start = textarea.selectionStart;
96 var end = textarea.selectionEnd;
97 textarea.value = textarea.value.substring(0, start) + "[" + BBcode + "]" + textarea.value.substring(start, end) + "[/" + BBcode + "]" + textarea.value.substring(end, textarea.value.length);
100 $(textarea).trigger('change');
105 function insertFormattingToPost(BBcode) {
106 textarea = document.getElementById("profile-jot-text");
107 if (document.selection) {
109 selected = document.selection.createRange();
110 selected.text = "[" + BBcode + "]" + selected.text + "[/" + BBcode + "]";
111 } else if (textarea.selectionStart || textarea.selectionStart == "0") {
112 var start = textarea.selectionStart;
113 var end = textarea.selectionEnd;
114 textarea.value = textarea.value.substring(0, start) + "[" + BBcode + "]" + textarea.value.substring(start, end) + "[/" + BBcode + "]" + textarea.value.substring(end, textarea.value.length);
117 $(textarea).trigger('change');
122 function showThread(id) {
123 $("#collapsed-comments-" + id).show()
124 $("#collapsed-comments-" + id + " .collapsed-comments").show()
126 function hideThread(id) {
127 $("#collapsed-comments-" + id).hide()
128 $("#collapsed-comments-" + id + " .collapsed-comments").hide()
131 function cmtBbOpen(id) {
132 $("#comment-edit-bb-" + id).show();
134 function cmtBbClose(id) {
135 $("#comment-edit-bb-" + id).hide();
138 function commentExpand(id)
140 $("#mod-cmnt-wrap-" + id).show();
141 closeMenu("comment-fake-form-" + id);
142 openMenu("item-comments-" + id);
143 $("#comment-edit-text-" + id)
145 .addClass("comment-edit-text-full")
146 .removeClass("comment-edit-text-empty");
151 function commentClose(obj, id)
153 if (obj.value === '' || obj.value === obj.dataset.default) {
154 $("#comment-edit-text-" + id)
155 .removeClass("comment-edit-text-full")
156 .addClass("comment-edit-text-empty");
157 $("#mod-cmnt-wrap-" + id).hide();
158 openMenu("comment-fake-form-" + id);
159 closeMenu("item-comments-" + id);
165 function showHideCommentBox(id) {
166 var $el = $('#comment-edit-form-' + id);
167 if ($el.is(':visible')) {
174 function commentOpenUI(obj, id) {
175 closeMenu("comment-fake-form-" + id);
176 openMenu("item-comments-" + id);
177 $("#comment-edit-text-" + id)
179 .addClass("comment-edit-text-full").removeClass("comment-edit-text-empty")
180 .attr('tabindex', '9'); // Choose an arbitrary tab index that's greater than what we're using in jot (3 of them)
181 $("#comment-edit-submit-" + id).attr('tabindex', '10'); // The submit button gets tabindex + 1
182 // initialize autosize for this comment
183 autosize($("#comment-edit-text-" + id + ".text-autosize"));
186 function commentCloseUI(obj, id) {
187 if (obj.value === '' || obj.value === obj.dataset.default) {
188 $("#comment-edit-text-" + id)
189 .removeClass("comment-edit-text-full").addClass("comment-edit-text-empty")
190 .removeAttr('tabindex');
191 $("#comment-edit-submit-" + id).removeAttr('tabindex');
192 openMenu("comment-fake-form-" + id);
193 closeMenu("item-comments-" + id);
194 // destroy the automatic textarea resizing
195 autosize.destroy($("#comment-edit-text-" + id + ".text-autosize"));
199 function jotTextOpenUI(obj) {
200 if (obj.value === '' || obj.value === obj.dataset.default) {
201 var $el = $(".modal-body #profile-jot-text");
202 $el.addClass("profile-jot-text-full").removeClass("profile-jot-text-empty");
203 // initiale autosize for the jot
208 function jotTextCloseUI(obj) {
209 if (obj.value === '' || obj.value === obj.dataset.default) {
210 var $el = $(".modal-body #profile-jot-text");
211 $el.removeClass("profile-jot-text-full").addClass("profile-jot-text-empty");
212 // destroy the automatic textarea resizing
213 autosize.destroy($el);
217 function commentOpen(obj, id) {
218 if (obj.value === '' || obj.value === obj.dataset.default) {
219 $("#comment-edit-text-" + id)
221 .addClass("comment-edit-text-full")
222 .removeClass("comment-edit-text-empty");
223 $("#mod-cmnt-wrap-" + id).show();
224 closeMenu("comment-fake-form-" + id);
225 openMenu("item-comments-" + id);
231 function confirmDelete() {
232 return confirm(aStr.delitem);
236 * Hide and removes an item element from the DOM after the deletion url is
237 * successful, restore it else.
239 * @param {string} url The item removal URL
240 * @param {string} elementId The DOM id of the item element
241 * @returns {undefined}
243 function dropItem(url, elementId) {
244 var confirm = confirmDelete();
247 $('body').css('cursor', 'wait');
249 var $el = $(document.getElementById(elementId));
251 $el.fadeTo('fast', 0.33, function () {
252 $.get(url).then(function() {
255 // @todo Show related error message
257 }).always(function() {
258 $('body').css('cursor', 'auto');