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("parseurl?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("parseurl?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) {
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) {
117 if (obj.value === "" || obj.value === obj.dataset.default) {
118 $("#comment-edit-text-" + id)
119 .removeClass("comment-edit-text-full")
120 .addClass("comment-edit-text-empty");
121 $("#mod-cmnt-wrap-" + id).hide();
122 openMenu("comment-fake-form-" + id);
123 closeMenu("item-comments-" + id);
129 function showHideCommentBox(id) {
130 var $el = $("#comment-edit-form-" + id);
131 if ($el.is(":visible")) {
138 function commentOpenUI(obj, id) {
139 closeMenu("comment-fake-form-" + id);
140 openMenu("item-comments-" + id);
141 $("#comment-edit-text-" + id)
143 .addClass("comment-edit-text-full")
144 .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")
155 .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);
201 function confirmBlock() {
202 return confirm(aStr.blockAuthor);
206 * Hide and removes an item element from the DOM after the deletion url is
207 * successful, restore it else.
209 * @param {string} url The item removal URL
210 * @param {string} elementId The DOM id of the item element
211 * @returns {undefined}
213 function dropItem(url, elementId) {
214 if (confirmDelete()) {
215 $("body").css("cursor", "wait");
217 var $el = $(document.getElementById(elementId));
219 $el.fadeTo('fast', 0.33, function () {
220 $.get(url).then(function() {
223 // @todo Show related error message
225 }).always(function() {
226 $("body").css('cursor', 'auto');
233 * Blocks an author and hide and removes an item element from the DOM after the block is
234 * successful, restore it else.
236 * @param {string} url The item removal URL
237 * @param {string} elementId The DOM id of the item element
238 * @returns {undefined}
240 function blockAuthor(url, elementId) {
241 if (confirmBlock()) {
242 $("body").css("cursor", "wait");
244 var $el = $(document.getElementById(elementId));
246 $el.fadeTo("fast", 0.33, function () {
252 // @todo Show related error message
255 .always(function () {
256 $("body").css("cursor", "auto");