added license hints to the Frio javascript files
[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 {
106         $("#mod-cmnt-wrap-" + id).show();
107         closeMenu("comment-fake-form-" + id);
108         openMenu("item-comments-" + id);
109         $("#comment-edit-text-" + id)
110                 .putCursorAtEnd()
111                 .addClass("comment-edit-text-full")
112                 .removeClass("comment-edit-text-empty");
113
114         return true;
115 }
116
117 function commentClose(obj, id)
118 {
119         if (obj.value === '' || obj.value === obj.dataset.default) {
120                 $("#comment-edit-text-" + id)
121                         .removeClass("comment-edit-text-full")
122                         .addClass("comment-edit-text-empty");
123                 $("#mod-cmnt-wrap-" + id).hide();
124                 openMenu("comment-fake-form-" + id);
125                 closeMenu("item-comments-" + id);
126                 return true;
127         }
128         return false;
129 }
130
131 function showHideCommentBox(id) {
132         var $el = $('#comment-edit-form-' + id);
133         if ($el.is(':visible')) {
134                 $el.hide();
135         } else {
136                 $el.show();
137         }
138 }
139
140 function commentOpenUI(obj, id) {
141         closeMenu("comment-fake-form-" + id);
142         openMenu("item-comments-" + id);
143         $("#comment-edit-text-" + id)
144                 .putCursorAtEnd()
145                 .addClass("comment-edit-text-full").removeClass("comment-edit-text-empty")
146                 .attr('tabindex', '9');         // Choose an arbitrary tab index that's greater than what we're using in jot (3 of them)
147         $("#comment-edit-submit-" + id).attr('tabindex', '10'); // The submit button gets tabindex + 1
148         // initialize autosize for this comment
149         autosize($("#comment-edit-text-" + id + ".text-autosize"));
150 }
151
152 function commentCloseUI(obj, id) {
153         if (obj.value === '' || obj.value === obj.dataset.default) {
154                 $("#comment-edit-text-" + id)
155                         .removeClass("comment-edit-text-full").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 /**
202  * Hide and removes an item element from the DOM after the deletion url is
203  * successful, restore it else.
204  *
205  * @param {string} url The item removal URL
206  * @param {string} elementId The DOM id of the item element
207  * @returns {undefined}
208  */
209 function dropItem(url, elementId) {
210         var confirm = confirmDelete();
211
212         if (confirm) {
213                 $('body').css('cursor', 'wait');
214
215                 var $el = $(document.getElementById(elementId));
216
217                 $el.fadeTo('fast', 0.33, function () {
218                         $.get(url).then(function() {
219                                 $el.remove();
220                         }).fail(function() {
221                                 // @todo Show related error message
222                                 $el.show();
223                         }).always(function() {
224                                 $('body').css('cursor', 'auto');
225                         });
226                 });
227         }
228 }
229 // @license-end