]> git.mxchange.org Git - friendica.git/blob - view/theme/frio/js/textedit.js
Merge pull request #6577 from rabuzarus/20190129_-_jot_atachment_preview
[friendica.git] / view / theme / frio / js / textedit.js
1 /*
2  * @brief The file contains functions for text editing and commenting
3  */
4
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() {
8                 // Cache references
9                 var $el = $(this),
10                         el = this;
11
12                 // Only focus if input isn't already
13                 if (!$el.is(":focus")) {
14                         $el.focus();
15                 }
16
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;
21
22                         // Timeout seems to be required for Blink
23                         setTimeout(function() {
24                                 el.setSelectionRange(len, len);
25                         }, 1);
26                 } else {
27                         // As a fallback, replace the contents with itself
28                         // Doesn't work in Chrome, but Chrome supports setSelectionRange
29                         $el.val($el.val());
30                 }
31
32                 // Scroll to the bottom, in case we're in a tall textarea
33                 // (Necessary for Firefox and Chrome)
34                 this.scrollTop = 999999;
35         });
36 };
37
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);
44                 });
45         }
46 }
47
48 function addCommentText(data, id) {
49         // get the textfield
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));
58 }
59
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);
68                 });
69         }
70 }
71
72 function commentLinkDropper(event) {
73         var linkFound = event.dataTransfer.types.contains("text/uri-list");
74         if (linkFound) {
75                 event.preventDefault();
76         }
77 }
78
79
80 function insertFormatting(BBcode, id) {
81         var tmpStr = $("#comment-edit-text-" + id).val();
82         if (tmpStr == '') {
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);
86         }
87
88         textarea = document.getElementById("comment-edit-text-" + id);
89         if (document.selection) {
90                 textarea.focus();
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);
97         }
98
99         $(textarea).trigger('change');
100
101         return true;
102 }
103
104 function insertFormattingToPost(BBcode) {
105         textarea = document.getElementById("profile-jot-text");
106         if (document.selection) {
107                 textarea.focus();
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);
114         }
115
116         $(textarea).trigger('change');
117
118         return true;
119 }
120
121 function showThread(id) {
122         $("#collapsed-comments-" + id).show()
123         $("#collapsed-comments-" + id + " .collapsed-comments").show()
124 }
125 function hideThread(id) {
126         $("#collapsed-comments-" + id).hide()
127         $("#collapsed-comments-" + id + " .collapsed-comments").hide()
128 }
129
130 function cmtBbOpen(id) {
131         $("#comment-edit-bb-" + id).show();
132 }
133 function cmtBbClose(id) {
134         $("#comment-edit-bb-" + id).hide();
135 }
136
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);
144         return true;
145 }
146
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);
153                 return true;
154         }
155         return false;
156 }
157
158 function showHideCommentBox(id) {
159         if ($('#comment-edit-form-' + id).is(':visible')) {
160                 $('#comment-edit-form-' + id).hide();
161         } else {
162                 $('#comment-edit-form-' + id).show();
163         }
164 }
165
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"));
175 }
176
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"));
185         }
186 }
187
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"));
193         }
194 }
195
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"));
201         }
202 }
203
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);
210                 return true;
211         }
212         return false;
213 }
214
215 function commentInsert(obj, id) {
216         var tmpStr = $("#comment-edit-text-" + id).val();
217         if (tmpStr == '') {
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);
221         }
222         var ins = $(obj).html();
223         ins = ins.replace('&lt;', '<');
224         ins = ins.replace('&gt;', '>');
225         ins = ins.replace('&amp;', '&');
226         ins = ins.replace('&quot;', '"');
227         $("#comment-edit-text-" + id).val(tmpStr + ins);
228 }
229
230 function qCommentInsert(obj, id) {
231         var tmpStr = $("#comment-edit-text-" + id).val();
232         if (tmpStr == '') {
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);
236         }
237         var ins = $(obj).val();
238         ins = ins.replace('&lt;', '<');
239         ins = ins.replace('&gt;', '>');
240         ins = ins.replace('&amp;', '&');
241         ins = ins.replace('&quot;', '"');
242         $("#comment-edit-text-" + id).val(tmpStr + ins);
243         $(obj).val('');
244 }
245
246 function confirmDelete() {
247         return confirm(aStr.delitem);
248 }
249
250 /**
251  * Hide and removes an item element from the DOM after the deletion url is
252  * successful, restore it else.
253  *
254  * @param {string} url The item removal URL
255  * @param {string} elementId The DOM id of the item element
256  * @returns {undefined}
257  */
258 function dropItem(url, elementId) {
259         var confirm = confirmDelete();
260
261         if (confirm) {
262                 $('body').css('cursor', 'wait');
263
264                 var $el = $(document.getElementById(elementId));
265
266                 $el.fadeTo('fast', 0.33, function () {
267                         $.get(url).then(function() {
268                                 $el.remove();
269                         }).fail(function() {
270                                 // @todo Show related error message
271                                 $el.show();
272                         }).always(function() {
273                                 $('body').css('cursor', 'auto');
274                         });
275                 });
276         }
277 }