1 // @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPLv3-or-later
3 * The file contains functions for text editing and commenting
6 // Lifted from https://css-tricks.com/snippets/jquery/move-cursor-to-end-of-textarea-or-input/
7 jQuery.fn.putCursorAtEnd = function() {
8 return this.each(function() {
13 // Only focus if input isn't already
14 if (!$el.is(":focus")) {
18 // If this function exists... (IE 9+)
19 if (el.setSelectionRange) {
20 // Double the length because Opera is inconsistent about whether a carriage return is one character or two.
21 var len = $el.val().length * 2;
23 // Timeout seems to be required for Blink
24 setTimeout(function() {
25 el.setSelectionRange(len, len);
28 // As a fallback, replace the contents with itself
29 // Doesn't work in Chrome, but Chrome supports setSelectionRange
33 // Scroll to the bottom, in case we're in a tall textarea
34 // (Necessary for Firefox and Chrome)
35 this.scrollTop = 999999;
39 function commentGetLink(id, prompttext) {
40 reply = prompt(prompttext);
41 if(reply && reply.length) {
42 reply = bin2hex(reply);
43 $.get('parse_url?noAttachment=1&binurl=' + reply, function(data) {
44 addCommentText(data, id);
49 function addCommentText(data, id) {
51 var textfield = document.getElementById("comment-edit-text-" + id);
52 // check if the textfield does have the default-value
53 commentOpenUI(textfield, id);
54 // save already existent content
55 var currentText = $("#comment-edit-text-" + id).val();
56 //insert the data as new value
57 textfield.value = currentText + data;
58 autosize.update($("#comment-edit-text-" + id));
61 function commentLinkDrop(event, id) {
62 var reply = event.dataTransfer.getData("text/uri-list");
63 event.target.textContent = reply;
64 event.preventDefault();
65 if (reply && reply.length) {
66 reply = bin2hex(reply);
67 $.get('parse_url?noAttachment=1&binurl=' + reply, function(data) {
68 addCommentText(data, id);
73 function commentLinkDropper(event) {
74 var linkFound = event.dataTransfer.types.contains("text/uri-list");
76 event.preventDefault();
80 function insertFormattingToPost(BBCode) {
81 textarea = document.getElementById("profile-jot-text");
83 insertBBCodeInTextarea(BBCode, textarea);
88 function showThread(id) {
89 $("#collapsed-comments-" + id).show()
90 $("#collapsed-comments-" + id + " .collapsed-comments").show()
92 function hideThread(id) {
93 $("#collapsed-comments-" + id).hide()
94 $("#collapsed-comments-" + id + " .collapsed-comments").hide()
97 function cmtBbOpen(id) {
98 $("#comment-edit-bb-" + id).show();
100 function cmtBbClose(id) {
101 $("#comment-edit-bb-" + id).hide();
104 function commentExpand(id)
106 $("#mod-cmnt-wrap-" + id).show();
107 closeMenu("comment-fake-form-" + id);
108 openMenu("item-comments-" + id);
109 $("#comment-edit-text-" + id)
111 .addClass("comment-edit-text-full")
112 .removeClass("comment-edit-text-empty");
117 function commentClose(obj, id)
119 if (obj.value === '' || obj.value === obj.dataset.default) {
120 $("#comment-edit-text-" + id)
121 .removeClass("comment-edit-text-full")
122 .addClass("comment-edit-text-empty");
123 $("#mod-cmnt-wrap-" + id).hide();
124 openMenu("comment-fake-form-" + id);
125 closeMenu("item-comments-" + id);
131 function showHideCommentBox(id) {
132 var $el = $('#comment-edit-form-' + id);
133 if ($el.is(':visible')) {
140 function commentOpenUI(obj, id) {
141 closeMenu("comment-fake-form-" + id);
142 openMenu("item-comments-" + id);
143 $("#comment-edit-text-" + id)
145 .addClass("comment-edit-text-full").removeClass("comment-edit-text-empty")
146 .attr('tabindex', '9'); // Choose an arbitrary tab index that's greater than what we're using in jot (3 of them)
147 $("#comment-edit-submit-" + id).attr('tabindex', '10'); // The submit button gets tabindex + 1
148 // initialize autosize for this comment
149 autosize($("#comment-edit-text-" + id + ".text-autosize"));
152 function commentCloseUI(obj, id) {
153 if (obj.value === '' || obj.value === obj.dataset.default) {
154 $("#comment-edit-text-" + id)
155 .removeClass("comment-edit-text-full").addClass("comment-edit-text-empty")
156 .removeAttr('tabindex');
157 $("#comment-edit-submit-" + id).removeAttr('tabindex');
158 openMenu("comment-fake-form-" + id);
159 closeMenu("item-comments-" + id);
160 // destroy the automatic textarea resizing
161 autosize.destroy($("#comment-edit-text-" + id + ".text-autosize"));
165 function jotTextOpenUI(obj) {
166 if (obj.value === '' || obj.value === obj.dataset.default) {
167 var $el = $(".modal-body #profile-jot-text");
168 $el.addClass("profile-jot-text-full").removeClass("profile-jot-text-empty");
169 // initiale autosize for the jot
174 function jotTextCloseUI(obj) {
175 if (obj.value === '' || obj.value === obj.dataset.default) {
176 var $el = $(".modal-body #profile-jot-text");
177 $el.removeClass("profile-jot-text-full").addClass("profile-jot-text-empty");
178 // destroy the automatic textarea resizing
179 autosize.destroy($el);
183 function commentOpen(obj, id) {
184 if (obj.value === '' || obj.value === obj.dataset.default) {
185 $("#comment-edit-text-" + id)
187 .addClass("comment-edit-text-full")
188 .removeClass("comment-edit-text-empty");
189 $("#mod-cmnt-wrap-" + id).show();
190 closeMenu("comment-fake-form-" + id);
191 openMenu("item-comments-" + id);
197 function confirmDelete() {
198 return confirm(aStr.delitem);
202 * Hide and removes an item element from the DOM after the deletion url is
203 * successful, restore it else.
205 * @param {string} url The item removal URL
206 * @param {string} elementId The DOM id of the item element
207 * @returns {undefined}
209 function dropItem(url, elementId) {
210 var confirm = confirmDelete();
213 $('body').css('cursor', 'wait');
215 var $el = $(document.getElementById(elementId));
217 $el.fadeTo('fast', 0.33, function () {
218 $.get(url).then(function() {
221 // @todo Show related error message
223 }).always(function() {
224 $('body').css('cursor', 'auto');