]> git.mxchange.org Git - friendica.git/blob - view/theme/frio/js/textedit.js
Some more settings moved to the admin frontend
[friendica.git] / view / theme / frio / js / textedit.js
1 // @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPLv3-or-later
2 /*
3  * The file contains functions for text editing and commenting
4  */
5
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 () {
9                 // Cache references
10                 var $el = $(this),
11                         el = this;
12
13                 // Only focus if input isn't already
14                 if (!$el.is(":focus")) {
15                         $el.focus();
16                 }
17
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;
22
23                         // Timeout seems to be required for Blink
24                         setTimeout(function () {
25                                 el.setSelectionRange(len, len);
26                         }, 1);
27                 } else {
28                         // As a fallback, replace the contents with itself
29                         // Doesn't work in Chrome, but Chrome supports setSelectionRange
30                         $el.val($el.val());
31                 }
32
33                 // Scroll to the bottom, in case we're in a tall textarea
34                 // (Necessary for Firefox and Chrome)
35                 this.scrollTop = 999999;
36         });
37 };
38
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);
45                 });
46         }
47 }
48
49 function addCommentText(data, id) {
50         // get the textfield
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));
59 }
60
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);
69                 });
70         }
71 }
72
73 function commentLinkDropper(event) {
74         var linkFound = event.dataTransfer.types.contains("text/uri-list");
75         if (linkFound) {
76                 event.preventDefault();
77         }
78 }
79
80 function insertFormattingToPost(BBCode) {
81         textarea = document.getElementById("profile-jot-text");
82
83         insertBBCodeInTextarea(BBCode, textarea);
84
85         return true;
86 }
87
88 function showThread(id) {
89         $("#collapsed-comments-" + id).show();
90         $("#collapsed-comments-" + id + " .collapsed-comments").show();
91 }
92 function hideThread(id) {
93         $("#collapsed-comments-" + id).hide();
94         $("#collapsed-comments-" + id + " .collapsed-comments").hide();
95 }
96
97 function cmtBbOpen(id) {
98         $("#comment-edit-bb-" + id).show();
99 }
100 function cmtBbClose(id) {
101         $("#comment-edit-bb-" + id).hide();
102 }
103
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)
109                 .putCursorAtEnd()
110                 .addClass("comment-edit-text-full")
111                 .removeClass("comment-edit-text-empty");
112
113         return true;
114 }
115
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);
124                 return true;
125         }
126         return false;
127 }
128
129 function showHideCommentBox(id) {
130         var $el = $("#comment-edit-form-" + id);
131         if ($el.is(":visible")) {
132                 $el.hide();
133         } else {
134                 $el.show();
135         }
136 }
137
138 function commentOpenUI(obj, id) {
139         closeMenu("comment-fake-form-" + id);
140         openMenu("item-comments-" + id);
141         $("#comment-edit-text-" + id)
142                 .putCursorAtEnd()
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"));
149 }
150
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"));
162         }
163 }
164
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
170                 autosize($el);
171         }
172 }
173
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);
180         }
181 }
182
183 function commentOpen(obj, id) {
184         if (obj.value === "" || obj.value === obj.dataset.default) {
185                 $("#comment-edit-text-" + id)
186                         .putCursorAtEnd()
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);
192                 return true;
193         }
194         return false;
195 }
196
197 function confirmDelete() {
198         return confirm(aStr.delitem);
199 }
200
201 function confirmBlock() {
202         return confirm(aStr.blockAuthor);
203 }
204
205 function confirmIgnore() {
206         return confirm(aStr.ignoreAuthor);
207 }
208
209 function confirmCollapse() {
210         return confirm(aStr.collapseAuthor);
211 }
212
213 function confirmIgnoreServer() {
214         return confirm(aStr.ignoreServer + "\n" + aStr.ignoreServerDesc);
215 }
216
217 /**
218  * Hide and removes an item element from the DOM after the deletion url is
219  * successful, restore it else.
220  *
221  * @param {string} url The item removal URL
222  * @param {string} elementId The DOM id of the item element
223  * @returns {undefined}
224  */
225 function dropItem(url, elementId) {
226         if (confirmDelete()) {
227                 $("body").css("cursor", "wait");
228
229                 var $el = $(document.getElementById(elementId));
230
231                 $el.fadeTo('fast', 0.33, function () {
232                         $.get(url).then(function() {
233                                 $el.remove();
234                         }).fail(function() {
235                                 // @todo Show related error message
236                                 $el.show();
237                         }).always(function() {
238                                 $("body").css('cursor', 'auto');
239                         });
240                 });
241         }
242 }
243
244 /**
245  * Blocks an author and hide and removes an item element from the DOM after the block is
246  * successful, restore it else.
247  *
248  * @param {string} url The item removal URL
249  * @param {string} elementId The DOM id of the item element
250  * @returns {undefined}
251  */
252 function blockAuthor(url, elementId) {
253         if (confirmBlock()) {
254                 $("body").css("cursor", "wait");
255
256                 var $el = $(document.getElementById(elementId));
257
258                 $el.fadeTo("fast", 0.33, function () {
259                         $.get(url)
260                                 .then(function () {
261                                         $el.remove();
262                                 })
263                                 .fail(function () {
264                                         // @todo Show related error message
265                                         $el.show();
266                                 })
267                                 .always(function () {
268                                         $("body").css("cursor", "auto");
269                                 });
270                 });
271         }
272 }
273
274 /**
275  * Ignored an author and hide and removes an item element from the DOM after the block is
276  * successful, restore it else.
277  *
278  * @param {string} url The item removal URL
279  * @param {string} elementId The DOM id of the item element
280  * @returns {undefined}
281  */
282 function ignoreAuthor(url, elementId) {
283         if (confirmIgnore()) {
284                 $("body").css("cursor", "wait");
285
286                 var $el = $(document.getElementById(elementId));
287
288                 $el.fadeTo("fast", 0.33, function () {
289                         $.get(url)
290                                 .then(function () {
291                                         $el.remove();
292                                 })
293                                 .fail(function () {
294                                         // @todo Show related error message
295                                         $el.show();
296                                 })
297                                 .always(function () {
298                                         $("body").css("cursor", "auto");
299                                 });
300                 });
301         }
302 }
303
304 /**
305  * Collapse author posts
306  *
307  * @param {string} url The item collapse URL
308  * @param {string} elementId The DOM id of the item element
309  * @returns {undefined}
310  */
311 function collapseAuthor(url, elementId) {
312         if (confirmCollapse()) {
313                 $("body").css("cursor", "wait");
314
315                 var $el = $(document.getElementById(elementId));
316
317                 $el.fadeTo("fast", 0.33, function () {
318                         $.get(url)
319                                 .then(function () {
320                                         //$el.remove();
321                                 })
322                                 .fail(function () {
323                                         // @todo Show related error message
324                                         $el.show();
325                                 })
326                                 .always(function () {
327                                         $("body").css("cursor", "auto");
328                                 });
329                 });
330         }
331 }
332
333
334 /**
335  * Ignore author server
336  *
337  * @param {string} url The server ignore URL
338  * @param {string} elementId The DOM id of the item element
339  * @returns {undefined}
340  */
341 function ignoreServer(url, elementId) {
342         if (confirmIgnoreServer()) {
343                 $("body").css("cursor", "wait");
344
345                 var $el = $(document.getElementById(elementId));
346
347                 $el.fadeTo("fast", 0.33, function () {
348                         $.post(url)
349                                 .then(function () {
350                                         $el.remove();
351                                 })
352                                 .fail(function () {
353                                         // @todo Show related error message
354                                         $el.fadeTo("fast", 1);
355                                 })
356                                 .always(function () {
357                                         $("body").css("cursor", "auto");
358                                 });
359                 });
360         }
361 }
362 // @license-end