014f6504fdbda0bfd47638bf00cf5586723b0b72
[friendica.git/.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("parse_url?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("parse_url?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 /**
206  * Hide and removes an item element from the DOM after the deletion url is
207  * successful, restore it else.
208  *
209  * @param {string} url The item removal URL
210  * @param {string} elementId The DOM id of the item element
211  * @returns {undefined}
212  */
213 function dropItem(url, elementId) {
214         if (confirmDelete()) {
215                 $("body").css("cursor", "wait");
216
217                 var $el = $(document.getElementById(elementId));
218
219                 $el.fadeTo('fast', 0.33, function () {
220                         $.get(url).then(function() {
221                                 $el.remove();
222                         }).fail(function() {
223                                 // @todo Show related error message
224                                 $el.show();
225                         }).always(function() {
226                                 $("body").css('cursor', 'auto');
227                         });
228                 });
229         }
230 }
231
232 /**
233  * Blocks an author and hide and removes an item element from the DOM after the block is
234  * successful, restore it else.
235  *
236  * @param {string} url The item removal URL
237  * @param {string} elementId The DOM id of the item element
238  * @returns {undefined}
239  */
240 function blockAuthor(url, elementId) {
241         if (confirmBlock()) {
242                 $("body").css("cursor", "wait");
243
244                 var $el = $(document.getElementById(elementId));
245
246                 $el.fadeTo("fast", 0.33, function () {
247                         $.get(url)
248                                 .then(function () {
249                                         $el.remove();
250                                 })
251                                 .fail(function () {
252                                         // @todo Show related error message
253                                         $el.show();
254                                 })
255                                 .always(function () {
256                                         $("body").css("cursor", "auto");
257                                 });
258                 });
259         }
260 }
261 // @license-end