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