]> git.mxchange.org Git - friendica.git/blob - view/theme/frio/js/textedit.js
Add a comment
[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                 closeMenu("comment-fake-form-" + id);
86                 openMenu("item-comments-" + id);
87         }
88
89         textarea = document.getElementById("comment-edit-text-" + id);
90         if (document.selection) {
91                 textarea.focus();
92                 selected = document.selection.createRange();
93                 selected.text = "[" + BBcode + "]" + selected.text + "[/" + BBcode + "]";
94         } else if (textarea.selectionStart || textarea.selectionStart == "0") {
95                 var start = textarea.selectionStart;
96                 var end = textarea.selectionEnd;
97                 textarea.value = textarea.value.substring(0, start) + "[" + BBcode + "]" + textarea.value.substring(start, end) + "[/" + BBcode + "]" + textarea.value.substring(end, textarea.value.length);
98         }
99
100         $(textarea).trigger('change');
101
102         return true;
103 }
104
105 function insertFormattingToPost(BBcode) {
106         textarea = document.getElementById("profile-jot-text");
107         if (document.selection) {
108                 textarea.focus();
109                 selected = document.selection.createRange();
110                 selected.text = "[" + BBcode + "]" + selected.text + "[/" + BBcode + "]";
111         } else if (textarea.selectionStart || textarea.selectionStart == "0") {
112                 var start = textarea.selectionStart;
113                 var end = textarea.selectionEnd;
114                 textarea.value = textarea.value.substring(0, start) + "[" + BBcode + "]" + textarea.value.substring(start, end) + "[/" + BBcode + "]" + textarea.value.substring(end, textarea.value.length);
115         }
116
117         $(textarea).trigger('change');
118
119         return true;
120 }
121
122 function showThread(id) {
123         $("#collapsed-comments-" + id).show()
124         $("#collapsed-comments-" + id + " .collapsed-comments").show()
125 }
126 function hideThread(id) {
127         $("#collapsed-comments-" + id).hide()
128         $("#collapsed-comments-" + id + " .collapsed-comments").hide()
129 }
130
131 function cmtBbOpen(id) {
132         $("#comment-edit-bb-" + id).show();
133 }
134 function cmtBbClose(id) {
135         $("#comment-edit-bb-" + id).hide();
136 }
137
138 function commentExpand(id)
139 {
140         $("#mod-cmnt-wrap-" + id).show();
141         closeMenu("comment-fake-form-" + id);
142         openMenu("item-comments-" + id);
143         $("#comment-edit-text-" + id)
144                 .putCursorAtEnd()
145                 .addClass("comment-edit-text-full")
146                 .removeClass("comment-edit-text-empty");
147
148         return true;
149 }
150
151 function commentClose(obj, id)
152 {
153         if (obj.value === '' || obj.value === obj.dataset.default) {
154                 $("#comment-edit-text-" + id)
155                         .removeClass("comment-edit-text-full")
156                         .addClass("comment-edit-text-empty");
157                 $("#mod-cmnt-wrap-" + id).hide();
158                 openMenu("comment-fake-form-" + id);
159                 closeMenu("item-comments-" + id);
160                 return true;
161         }
162         return false;
163 }
164
165 function showHideCommentBox(id) {
166         var $el = $('#comment-edit-form-' + id);
167         if ($el.is(':visible')) {
168                 $el.hide();
169         } else {
170                 $el.show();
171         }
172 }
173
174 function commentOpenUI(obj, id) {
175         closeMenu("comment-fake-form-" + id);
176         openMenu("item-comments-" + id);
177         $("#comment-edit-text-" + id)
178                 .putCursorAtEnd()
179                 .addClass("comment-edit-text-full").removeClass("comment-edit-text-empty")
180                 .attr('tabindex', '9');         // Choose an arbitrary tab index that's greater than what we're using in jot (3 of them)
181         $("#comment-edit-submit-" + id).attr('tabindex', '10'); // The submit button gets tabindex + 1
182         // initialize autosize for this comment
183         autosize($("#comment-edit-text-" + id + ".text-autosize"));
184 }
185
186 function commentCloseUI(obj, id) {
187         if (obj.value === '' || obj.value === obj.dataset.default) {
188                 $("#comment-edit-text-" + id)
189                         .removeClass("comment-edit-text-full").addClass("comment-edit-text-empty")
190                         .removeAttr('tabindex');
191                 $("#comment-edit-submit-" + id).removeAttr('tabindex');
192                 openMenu("comment-fake-form-" + id);
193                 closeMenu("item-comments-" + id);
194                 // destroy the automatic textarea resizing
195                 autosize.destroy($("#comment-edit-text-" + id + ".text-autosize"));
196         }
197 }
198
199 function jotTextOpenUI(obj) {
200         if (obj.value === '' || obj.value === obj.dataset.default) {
201                 var $el = $(".modal-body #profile-jot-text");
202                 $el.addClass("profile-jot-text-full").removeClass("profile-jot-text-empty");
203                 // initiale autosize for the jot
204                 autosize($el);
205         }
206 }
207
208 function jotTextCloseUI(obj) {
209         if (obj.value === '' || obj.value === obj.dataset.default) {
210                 var $el = $(".modal-body #profile-jot-text");
211                 $el.removeClass("profile-jot-text-full").addClass("profile-jot-text-empty");
212                 // destroy the automatic textarea resizing
213                 autosize.destroy($el);
214         }
215 }
216
217 function commentOpen(obj, id) {
218         if (obj.value === '' || obj.value === obj.dataset.default) {
219                 $("#comment-edit-text-" + id)
220                         .putCursorAtEnd()
221                         .addClass("comment-edit-text-full")
222                         .removeClass("comment-edit-text-empty");
223                 $("#mod-cmnt-wrap-" + id).show();
224                 closeMenu("comment-fake-form-" + id);
225                 openMenu("item-comments-" + id);
226                 return true;
227         }
228         return false;
229 }
230
231 function confirmDelete() {
232         return confirm(aStr.delitem);
233 }
234
235 /**
236  * Hide and removes an item element from the DOM after the deletion url is
237  * successful, restore it else.
238  *
239  * @param {string} url The item removal URL
240  * @param {string} elementId The DOM id of the item element
241  * @returns {undefined}
242  */
243 function dropItem(url, elementId) {
244         var confirm = confirmDelete();
245
246         if (confirm) {
247                 $('body').css('cursor', 'wait');
248
249                 var $el = $(document.getElementById(elementId));
250
251                 $el.fadeTo('fast', 0.33, function () {
252                         $.get(url).then(function() {
253                                 $el.remove();
254                         }).fail(function() {
255                                 // @todo Show related error message
256                                 $el.show();
257                         }).always(function() {
258                                 $('body').css('cursor', 'auto');
259                         });
260                 });
261         }
262 }