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 openMenu("comment-edit-submit-wrapper-" + id);
88 textarea = document.getElementById("comment-edit-text-" + id);
89 if (document.selection) {
91 selected = document.selection.createRange();
92 selected.text = "[" + BBcode + "]" + selected.text + "[/" + BBcode + "]";
93 } else if (textarea.selectionStart || textarea.selectionStart == "0") {
94 var start = textarea.selectionStart;
95 var end = textarea.selectionEnd;
96 textarea.value = textarea.value.substring(0, start) + "[" + BBcode + "]" + textarea.value.substring(start, end) + "[/" + BBcode + "]" + textarea.value.substring(end, textarea.value.length);
99 $(textarea).trigger('change');
104 function insertFormattingToPost(BBcode) {
105 textarea = document.getElementById("profile-jot-text");
106 if (document.selection) {
108 selected = document.selection.createRange();
109 selected.text = "[" + BBcode + "]" + selected.text + "[/" + BBcode + "]";
110 } else if (textarea.selectionStart || textarea.selectionStart == "0") {
111 var start = textarea.selectionStart;
112 var end = textarea.selectionEnd;
113 textarea.value = textarea.value.substring(0, start) + "[" + BBcode + "]" + textarea.value.substring(start, end) + "[/" + BBcode + "]" + textarea.value.substring(end, textarea.value.length);
116 $(textarea).trigger('change');
121 function showThread(id) {
122 $("#collapsed-comments-" + id).show()
123 $("#collapsed-comments-" + id + " .collapsed-comments").show()
125 function hideThread(id) {
126 $("#collapsed-comments-" + id).hide()
127 $("#collapsed-comments-" + id + " .collapsed-comments").hide()
130 function cmtBbOpen(id) {
131 $("#comment-edit-bb-" + id).show();
133 function cmtBbClose(id) {
134 $("#comment-edit-bb-" + id).hide();
137 function commentExpand(id) {
138 $("#comment-edit-text-" + id).putCursorAtEnd();
139 $("#comment-edit-text-" + id).addClass("comment-edit-text-full");
140 $("#comment-edit-text-" + id).removeClass("comment-edit-text-empty");
141 $("#comment-edit-text-" + id).focus();
142 $("#mod-cmnt-wrap-" + id).show();
143 openMenu("comment-edit-submit-wrapper-" + id);
147 function commentClose(obj, id) {
148 if (obj.value == '') {
149 $("#comment-edit-text-" + id).removeClass("comment-edit-text-full");
150 $("#comment-edit-text-" + id).addClass("comment-edit-text-empty");
151 $("#mod-cmnt-wrap-" + id).hide();
152 closeMenu("comment-edit-submit-wrapper-" + id);
158 function showHideCommentBox(id) {
159 if ($('#comment-edit-form-' + id).is(':visible')) {
160 $('#comment-edit-form-' + id).hide();
162 $('#comment-edit-form-' + id).show();
166 function commentOpenUI(obj, id) {
167 $("#comment-edit-text-" + id).addClass("comment-edit-text-full").removeClass("comment-edit-text-empty");
168 // Choose an arbitrary tab index that's greater than what we're using in jot (3 of them)
169 // The submit button gets tabindex + 1
170 $("#comment-edit-text-" + id).attr('tabindex', '9');
171 $("#comment-edit-submit-" + id).attr('tabindex', '10');
172 $("#comment-edit-submit-wrapper-" + id).show();
173 // initialize autosize for this comment
174 autosize($("#comment-edit-text-" + id + ".text-autosize"));
177 function commentCloseUI(obj, id) {
178 if (obj.value === '') {
179 $("#comment-edit-text-" + id).removeClass("comment-edit-text-full").addClass("comment-edit-text-empty");
180 $("#comment-edit-text-" + id).removeAttr('tabindex');
181 $("#comment-edit-submit-" + id).removeAttr('tabindex');
182 $("#comment-edit-submit-wrapper-" + id).hide();
183 // destroy the automatic textarea resizing
184 autosize.destroy($("#comment-edit-text-" + id + ".text-autosize"));
188 function jotTextOpenUI(obj) {
189 if (obj.value == '') {
190 $(".modal-body #profile-jot-text").addClass("profile-jot-text-full").removeClass("profile-jot-text-empty");
191 // initiale autosize for the jot
192 autosize($(".modal-body #profile-jot-text"));
196 function jotTextCloseUI(obj) {
197 if (obj.value === '') {
198 $(".modal-body #profile-jot-text").removeClass("profile-jot-text-full").addClass("profile-jot-text-empty");
199 // destroy the automatic textarea resizing
200 autosize.destroy($(".modal-body #profile-jot-text"));
204 function commentOpen(obj, id) {
205 if (obj.value == '') {
206 $("#comment-edit-text-" + id).addClass("comment-edit-text-full");
207 $("#comment-edit-text-" + id).removeClass("comment-edit-text-empty");
208 $("#mod-cmnt-wrap-" + id).show();
209 openMenu("comment-edit-submit-wrapper-" + id);
215 function commentInsert(obj, id) {
216 var tmpStr = $("#comment-edit-text-" + id).val();
218 $("#comment-edit-text-" + id).addClass("comment-edit-text-full");
219 $("#comment-edit-text-" + id).removeClass("comment-edit-text-empty");
220 openMenu("comment-edit-submit-wrapper-" + id);
222 var ins = $(obj).html();
223 ins = ins.replace('<', '<');
224 ins = ins.replace('>', '>');
225 ins = ins.replace('&', '&');
226 ins = ins.replace('"', '"');
227 $("#comment-edit-text-" + id).val(tmpStr + ins);
230 function qCommentInsert(obj, id) {
231 var tmpStr = $("#comment-edit-text-" + id).val();
233 $("#comment-edit-text-" + id).addClass("comment-edit-text-full");
234 $("#comment-edit-text-" + id).removeClass("comment-edit-text-empty");
235 openMenu("comment-edit-submit-wrapper-" + id);
237 var ins = $(obj).val();
238 ins = ins.replace('<', '<');
239 ins = ins.replace('>', '>');
240 ins = ins.replace('&', '&');
241 ins = ins.replace('"', '"');
242 $("#comment-edit-text-" + id).val(tmpStr + ins);
246 function confirmDelete() {
247 return confirm(aStr.delitem);
251 * Hide and removes an item element from the DOM after the deletion url is
252 * successful, restore it else.
254 * @param {string} url The item removal URL
255 * @param {string} elementId The DOM id of the item element
256 * @returns {undefined}
258 function dropItem(url, elementId) {
259 var confirm = confirmDelete();
262 $('body').css('cursor', 'wait');
264 var $el = $(document.getElementById(elementId));
266 $el.fadeTo('fast', 0.33, function () {
267 $.get(url).then(function() {
270 // @todo Show related error message
272 }).always(function() {
273 $('body').css('cursor', 'auto');