]> git.mxchange.org Git - friendica.git/blob - view/theme/frio/js/textedit.js
Merge pull request #8131 from nupplaphil/task/cleanup_lock
[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 function insertFormattingToPost(BBCode) {
80         textarea = document.getElementById("profile-jot-text");
81
82         insertBBCodeInTextarea(BBCode, textarea);
83
84         return true;
85 }
86
87 function showThread(id) {
88         $("#collapsed-comments-" + id).show()
89         $("#collapsed-comments-" + id + " .collapsed-comments").show()
90 }
91 function hideThread(id) {
92         $("#collapsed-comments-" + id).hide()
93         $("#collapsed-comments-" + id + " .collapsed-comments").hide()
94 }
95
96 function cmtBbOpen(id) {
97         $("#comment-edit-bb-" + id).show();
98 }
99 function cmtBbClose(id) {
100         $("#comment-edit-bb-" + id).hide();
101 }
102
103 function commentExpand(id)
104 {
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 {
118         if (obj.value === '' || obj.value === obj.dataset.default) {
119                 $("#comment-edit-text-" + id)
120                         .removeClass("comment-edit-text-full")
121                         .addClass("comment-edit-text-empty");
122                 $("#mod-cmnt-wrap-" + id).hide();
123                 openMenu("comment-fake-form-" + id);
124                 closeMenu("item-comments-" + id);
125                 return true;
126         }
127         return false;
128 }
129
130 function showHideCommentBox(id) {
131         var $el = $('#comment-edit-form-' + id);
132         if ($el.is(':visible')) {
133                 $el.hide();
134         } else {
135                 $el.show();
136         }
137 }
138
139 function commentOpenUI(obj, id) {
140         closeMenu("comment-fake-form-" + id);
141         openMenu("item-comments-" + id);
142         $("#comment-edit-text-" + id)
143                 .putCursorAtEnd()
144                 .addClass("comment-edit-text-full").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").addClass("comment-edit-text-empty")
155                         .removeAttr('tabindex');
156                 $("#comment-edit-submit-" + id).removeAttr('tabindex');
157                 openMenu("comment-fake-form-" + id);
158                 closeMenu("item-comments-" + id);
159                 // destroy the automatic textarea resizing
160                 autosize.destroy($("#comment-edit-text-" + id + ".text-autosize"));
161         }
162 }
163
164 function jotTextOpenUI(obj) {
165         if (obj.value === '' || obj.value === obj.dataset.default) {
166                 var $el = $(".modal-body #profile-jot-text");
167                 $el.addClass("profile-jot-text-full").removeClass("profile-jot-text-empty");
168                 // initiale autosize for the jot
169                 autosize($el);
170         }
171 }
172
173 function jotTextCloseUI(obj) {
174         if (obj.value === '' || obj.value === obj.dataset.default) {
175                 var $el = $(".modal-body #profile-jot-text");
176                 $el.removeClass("profile-jot-text-full").addClass("profile-jot-text-empty");
177                 // destroy the automatic textarea resizing
178                 autosize.destroy($el);
179         }
180 }
181
182 function commentOpen(obj, id) {
183         if (obj.value === '' || obj.value === obj.dataset.default) {
184                 $("#comment-edit-text-" + id)
185                         .putCursorAtEnd()
186                         .addClass("comment-edit-text-full")
187                         .removeClass("comment-edit-text-empty");
188                 $("#mod-cmnt-wrap-" + id).show();
189                 closeMenu("comment-fake-form-" + id);
190                 openMenu("item-comments-" + id);
191                 return true;
192         }
193         return false;
194 }
195
196 function confirmDelete() {
197         return confirm(aStr.delitem);
198 }
199
200 /**
201  * Hide and removes an item element from the DOM after the deletion url is
202  * successful, restore it else.
203  *
204  * @param {string} url The item removal URL
205  * @param {string} elementId The DOM id of the item element
206  * @returns {undefined}
207  */
208 function dropItem(url, elementId) {
209         var confirm = confirmDelete();
210
211         if (confirm) {
212                 $('body').css('cursor', 'wait');
213
214                 var $el = $(document.getElementById(elementId));
215
216                 $el.fadeTo('fast', 0.33, function () {
217                         $.get(url).then(function() {
218                                 $el.remove();
219                         }).fail(function() {
220                                 // @todo Show related error message
221                                 $el.show();
222                         }).always(function() {
223                                 $('body').css('cursor', 'auto');
224                         });
225                 });
226         }
227 }