]> git.mxchange.org Git - friendica.git/blob - view/theme/frio/js/textedit.js
f9f4ff37fc0d452f04255b3085403965e5965ae0
[friendica.git] / view / theme / frio / js / textedit.js
1 /*
2  * @brief The file contains functions for text editing and commenting
3  */
4
5 function commentGetLink(id) {
6     reply = prompt("Please enter a link URL:");
7     if(reply && reply.length) {
8         reply = bin2hex(reply);
9         $.get('parse_url?isComment=1&binurl=' + reply, function(data) {
10             addcommenttext(data, id);
11         });
12     }
13 }
14
15 function addcommenttext(data, id) {
16     // get the textfield
17     var textfield = document.getElementById("comment-edit-text-" + id);
18     // check if the textfield does have the default-value
19     commentOpenUI(textfield, id);
20     // save already existent content
21     var currentText = $("#comment-edit-text-" + id).val();
22     //insert the data as new value
23     textfield.value = currentText + data;
24     autosize.update($("#comment-edit-text-" + id));
25 }
26
27 function commentlinkdrop(event, id) {
28     var reply = event.dataTransfer.getData("text/uri-list");
29     event.target.textContent = reply;
30     event.preventDefault();
31     if(reply && reply.length) {
32         reply = bin2hex(reply);
33         $.get('parse_url?isComment=1&binurl=' + reply, function(data) {
34                         addcommenttext(data, id);
35         });
36     }
37 }
38
39 function commentlinkdropper(event) {
40     var linkFound = event.dataTransfer.types.contains("text/uri-list");
41     if(linkFound)
42         event.preventDefault();
43 }
44
45
46 function insertFormatting(BBcode, id) {
47         var tmpStr = $("#comment-edit-text-" + id).val();
48         if (tmpStr == '') {
49                 $("#comment-edit-text-" + id).addClass("comment-edit-text-full");
50                 $("#comment-edit-text-" + id).removeClass("comment-edit-text-empty");
51                 openMenu("comment-edit-submit-wrapper-" + id);
52         }
53
54         textarea = document.getElementById("comment-edit-text-" + id);
55         if (document.selection) {
56                 textarea.focus();
57                 selected = document.selection.createRange();
58                 selected.text = "[" + BBcode + "]" + selected.text + "[/" + BBcode + "]";
59         } else if (textarea.selectionStart || textarea.selectionStart == "0") {
60                 var start = textarea.selectionStart;
61                 var end = textarea.selectionEnd;
62                 textarea.value = textarea.value.substring(0, start) + "[" + BBcode + "]" + textarea.value.substring(start, end) + "[/" + BBcode + "]" + textarea.value.substring(end, textarea.value.length);
63         }
64
65         $(textarea).trigger('change');
66
67         return true;
68 }
69
70 function insertFormattingToPost(BBcode) {
71         textarea = document.getElementById("profile-jot-text");
72         if (document.selection) {
73                 textarea.focus();
74                 selected = document.selection.createRange();
75                 selected.text = "[" + BBcode + "]" + selected.text + "[/" + BBcode + "]";
76         } else if (textarea.selectionStart || textarea.selectionStart == "0") {
77                 var start = textarea.selectionStart;
78                 var end = textarea.selectionEnd;
79                 textarea.value = textarea.value.substring(0, start) + "[" + BBcode + "]" + textarea.value.substring(start, end) + "[/" + BBcode + "]" + textarea.value.substring(end, textarea.value.length);
80         }
81
82         $(textarea).trigger('change');
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         $("#comment-edit-text-" + id).value = '';
105         $("#comment-edit-text-" + id).addClass("comment-edit-text-full");
106         $("#comment-edit-text-" + id).removeClass("comment-edit-text-empty");
107         $("#comment-edit-text-" + id).focus();
108         $("#mod-cmnt-wrap-" + id).show();
109         openMenu("comment-edit-submit-wrapper-" + id);
110         return true;
111 }
112
113 function commentClose(obj, id) {
114         if (obj.value == '') {
115                 $("#comment-edit-text-" + id).removeClass("comment-edit-text-full");
116                 $("#comment-edit-text-" + id).addClass("comment-edit-text-empty");
117                 $("#mod-cmnt-wrap-" + id).hide();
118                 closeMenu("comment-edit-submit-wrapper-" + id);
119                 return true;
120         }
121         return false;
122 }
123
124 function showHideCommentBox(id) {
125         if ($('#comment-edit-form-' + id).is(':visible')) {
126                 $('#comment-edit-form-' + id).hide();
127         } else {
128                 $('#comment-edit-form-' + id).show();
129         }
130 }
131
132 function commentOpenUI(obj, id) {
133         $("#comment-edit-text-" + id).addClass("comment-edit-text-full").removeClass("comment-edit-text-empty");
134         // Choose an arbitrary tab index that's greater than what we're using in jot (3 of them)
135         // The submit button gets tabindex + 1
136         $("#comment-edit-text-" + id).attr('tabindex', '9');
137         $("#comment-edit-submit-" + id).attr('tabindex', '10');
138         $("#comment-edit-submit-wrapper-" + id).show();
139         // initialize autosize for this comment
140         autosize($("#comment-edit-text-" + id + ".text-autosize"));
141 }
142
143 function commentCloseUI(obj, id) {
144         if (obj.value === '') {
145                 $("#comment-edit-text-" + id).removeClass("comment-edit-text-full").addClass("comment-edit-text-empty");
146                 $("#comment-edit-text-" + id).removeAttr('tabindex');
147                 $("#comment-edit-submit-" + id).removeAttr('tabindex');
148                 $("#comment-edit-submit-wrapper-" + id).hide();
149                 // destroy the automatic textarea resizing
150                 autosize.destroy($("#comment-edit-text-" + id + ".text-autosize"));
151         }
152 }
153
154 function jotTextOpenUI(obj) {
155         if (obj.value == '') {
156                 $(".modal-body #profile-jot-text").addClass("profile-jot-text-full").removeClass("profile-jot-text-empty");
157                 // initiale autosize for the jot
158                 autosize($(".modal-body #profile-jot-text"));
159         }
160 }
161
162 function jotTextCloseUI(obj) {
163         if (obj.value === '') {
164                 $(".modal-body #profile-jot-text").removeClass("profile-jot-text-full").addClass("profile-jot-text-empty");
165                 // destroy the automatic textarea resizing
166                 autosize.destroy($(".modal-body #profile-jot-text"));
167         }
168 }
169
170 function commentOpen(obj, id) {
171         if (obj.value == '') {
172                 $("#comment-edit-text-" + id).addClass("comment-edit-text-full");
173                 $("#comment-edit-text-" + id).removeClass("comment-edit-text-empty");
174                 $("#mod-cmnt-wrap-" + id).show();
175                 openMenu("comment-edit-submit-wrapper-" + id);
176                 return true;
177         }
178         return false;
179 }
180
181 function commentInsert(obj, id) {
182         var tmpStr = $("#comment-edit-text-" + id).val();
183         if (tmpStr == '') {
184                 $("#comment-edit-text-" + id).addClass("comment-edit-text-full");
185                 $("#comment-edit-text-" + id).removeClass("comment-edit-text-empty");
186                 openMenu("comment-edit-submit-wrapper-" + id);
187         }
188         var ins = $(obj).html();
189         ins = ins.replace('&lt;', '<');
190         ins = ins.replace('&gt;', '>');
191         ins = ins.replace('&amp;', '&');
192         ins = ins.replace('&quot;', '"');
193         $("#comment-edit-text-" + id).val(tmpStr + ins);
194 }
195
196 function qCommentInsert(obj, id) {
197         var tmpStr = $("#comment-edit-text-" + id).val();
198         if (tmpStr == '') {
199                 $("#comment-edit-text-" + id).addClass("comment-edit-text-full");
200                 $("#comment-edit-text-" + id).removeClass("comment-edit-text-empty");
201                 openMenu("comment-edit-submit-wrapper-" + id);
202         }
203         var ins = $(obj).val();
204         ins = ins.replace('&lt;', '<');
205         ins = ins.replace('&gt;', '>');
206         ins = ins.replace('&amp;', '&');
207         ins = ins.replace('&quot;', '"');
208         $("#comment-edit-text-" + id).val(tmpStr + ins);
209         $(obj).val('');
210 }
211
212 function confirmDelete() {
213         return confirm(aStr.delitem);
214 }
215
216 /**
217  * Hide and removes an item element from the DOM after the deletion url is
218  * successful, restore it else.
219  *
220  * @param {string} url The item removal URL
221  * @param {string} elementId The DOM id of the item element
222  * @returns {undefined}
223  */
224 function dropItem(url, elementId) {
225         var confirm = confirmDelete();
226
227         if (confirm) {
228                 $('body').css('cursor', 'wait');
229
230                 var $el = $(document.getElementById(elementId));
231
232                 $el.fadeTo('fast', 0.33, function () {
233                         $.get(url).then(function() {
234                                 $el.remove();
235                         }).fail(function() {
236                                 // @todo Show related error message
237                                 $el.show();
238                         }).always(function() {
239                                 $('body').css('cursor', 'auto');
240                         });
241                 });
242         }
243 }