]> git.mxchange.org Git - friendica.git/blob - view/theme/frio/js/textedit.js
Merge branch 'develop' of https://github.com/friendica/friendica into develop
[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, prompttext) {
6         reply = prompt(prompttext);
7         if(reply && reply.length) {
8                 reply = bin2hex(reply);
9                 $.get('parse_url?noAttachment=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?noAttachment=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
47 function insertFormatting(BBcode, id) {
48         var tmpStr = $("#comment-edit-text-" + id).val();
49         if (tmpStr == '') {
50                 $("#comment-edit-text-" + id).addClass("comment-edit-text-full");
51                 $("#comment-edit-text-" + id).removeClass("comment-edit-text-empty");
52                 openMenu("comment-edit-submit-wrapper-" + id);
53         }
54
55         textarea = document.getElementById("comment-edit-text-" + id);
56         if (document.selection) {
57                 textarea.focus();
58                 selected = document.selection.createRange();
59                 selected.text = "[" + BBcode + "]" + selected.text + "[/" + BBcode + "]";
60         } else if (textarea.selectionStart || textarea.selectionStart == "0") {
61                 var start = textarea.selectionStart;
62                 var end = textarea.selectionEnd;
63                 textarea.value = textarea.value.substring(0, start) + "[" + BBcode + "]" + textarea.value.substring(start, end) + "[/" + BBcode + "]" + textarea.value.substring(end, textarea.value.length);
64         }
65
66         $(textarea).trigger('change');
67
68         return true;
69 }
70
71 function insertFormattingToPost(BBcode) {
72         textarea = document.getElementById("profile-jot-text");
73         if (document.selection) {
74                 textarea.focus();
75                 selected = document.selection.createRange();
76                 selected.text = "[" + BBcode + "]" + selected.text + "[/" + BBcode + "]";
77         } else if (textarea.selectionStart || textarea.selectionStart == "0") {
78                 var start = textarea.selectionStart;
79                 var end = textarea.selectionEnd;
80                 textarea.value = textarea.value.substring(0, start) + "[" + BBcode + "]" + textarea.value.substring(start, end) + "[/" + BBcode + "]" + textarea.value.substring(end, textarea.value.length);
81         }
82
83         $(textarea).trigger('change');
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         $("#comment-edit-text-" + id).value = '';
106         $("#comment-edit-text-" + id).addClass("comment-edit-text-full");
107         $("#comment-edit-text-" + id).removeClass("comment-edit-text-empty");
108         $("#comment-edit-text-" + id).focus();
109         $("#mod-cmnt-wrap-" + id).show();
110         openMenu("comment-edit-submit-wrapper-" + id);
111         return true;
112 }
113
114 function commentClose(obj, id) {
115         if (obj.value == '') {
116                 $("#comment-edit-text-" + id).removeClass("comment-edit-text-full");
117                 $("#comment-edit-text-" + id).addClass("comment-edit-text-empty");
118                 $("#mod-cmnt-wrap-" + id).hide();
119                 closeMenu("comment-edit-submit-wrapper-" + id);
120                 return true;
121         }
122         return false;
123 }
124
125 function showHideCommentBox(id) {
126         if ($('#comment-edit-form-' + id).is(':visible')) {
127                 $('#comment-edit-form-' + id).hide();
128         } else {
129                 $('#comment-edit-form-' + id).show();
130         }
131 }
132
133 function commentOpenUI(obj, id) {
134         $("#comment-edit-text-" + id).addClass("comment-edit-text-full").removeClass("comment-edit-text-empty");
135         // Choose an arbitrary tab index that's greater than what we're using in jot (3 of them)
136         // The submit button gets tabindex + 1
137         $("#comment-edit-text-" + id).attr('tabindex', '9');
138         $("#comment-edit-submit-" + id).attr('tabindex', '10');
139         $("#comment-edit-submit-wrapper-" + id).show();
140         // initialize autosize for this comment
141         autosize($("#comment-edit-text-" + id + ".text-autosize"));
142 }
143
144 function commentCloseUI(obj, id) {
145         if (obj.value === '') {
146                 $("#comment-edit-text-" + id).removeClass("comment-edit-text-full").addClass("comment-edit-text-empty");
147                 $("#comment-edit-text-" + id).removeAttr('tabindex');
148                 $("#comment-edit-submit-" + id).removeAttr('tabindex');
149                 $("#comment-edit-submit-wrapper-" + id).hide();
150                 // destroy the automatic textarea resizing
151                 autosize.destroy($("#comment-edit-text-" + id + ".text-autosize"));
152         }
153 }
154
155 function jotTextOpenUI(obj) {
156         if (obj.value == '') {
157                 $(".modal-body #profile-jot-text").addClass("profile-jot-text-full").removeClass("profile-jot-text-empty");
158                 // initiale autosize for the jot
159                 autosize($(".modal-body #profile-jot-text"));
160         }
161 }
162
163 function jotTextCloseUI(obj) {
164         if (obj.value === '') {
165                 $(".modal-body #profile-jot-text").removeClass("profile-jot-text-full").addClass("profile-jot-text-empty");
166                 // destroy the automatic textarea resizing
167                 autosize.destroy($(".modal-body #profile-jot-text"));
168         }
169 }
170
171 function commentOpen(obj, id) {
172         if (obj.value == '') {
173                 $("#comment-edit-text-" + id).addClass("comment-edit-text-full");
174                 $("#comment-edit-text-" + id).removeClass("comment-edit-text-empty");
175                 $("#mod-cmnt-wrap-" + id).show();
176                 openMenu("comment-edit-submit-wrapper-" + id);
177                 return true;
178         }
179         return false;
180 }
181
182 function commentInsert(obj, id) {
183         var tmpStr = $("#comment-edit-text-" + id).val();
184         if (tmpStr == '') {
185                 $("#comment-edit-text-" + id).addClass("comment-edit-text-full");
186                 $("#comment-edit-text-" + id).removeClass("comment-edit-text-empty");
187                 openMenu("comment-edit-submit-wrapper-" + id);
188         }
189         var ins = $(obj).html();
190         ins = ins.replace('&lt;', '<');
191         ins = ins.replace('&gt;', '>');
192         ins = ins.replace('&amp;', '&');
193         ins = ins.replace('&quot;', '"');
194         $("#comment-edit-text-" + id).val(tmpStr + ins);
195 }
196
197 function qCommentInsert(obj, id) {
198         var tmpStr = $("#comment-edit-text-" + id).val();
199         if (tmpStr == '') {
200                 $("#comment-edit-text-" + id).addClass("comment-edit-text-full");
201                 $("#comment-edit-text-" + id).removeClass("comment-edit-text-empty");
202                 openMenu("comment-edit-submit-wrapper-" + id);
203         }
204         var ins = $(obj).val();
205         ins = ins.replace('&lt;', '<');
206         ins = ins.replace('&gt;', '>');
207         ins = ins.replace('&amp;', '&');
208         ins = ins.replace('&quot;', '"');
209         $("#comment-edit-text-" + id).val(tmpStr + ins);
210         $(obj).val('');
211 }
212
213 function confirmDelete() {
214         return confirm(aStr.delitem);
215 }
216
217 /**
218  * Hide and removes an item element from the DOM after the deletion url is
219  * successful, restore it else.
220  *
221  * @param {string} url The item removal URL
222  * @param {string} elementId The DOM id of the item element
223  * @returns {undefined}
224  */
225 function dropItem(url, elementId) {
226         var confirm = confirmDelete();
227
228         if (confirm) {
229                 $('body').css('cursor', 'wait');
230
231                 var $el = $(document.getElementById(elementId));
232
233                 $el.fadeTo('fast', 0.33, function () {
234                         $.get(url).then(function() {
235                                 $el.remove();
236                         }).fail(function() {
237                                 // @todo Show related error message
238                                 $el.show();
239                         }).always(function() {
240                                 $('body').css('cursor', 'auto');
241                         });
242                 });
243         }
244 }