]> git.mxchange.org Git - friendica.git/blob - view/templates/item/compose-footer.tpl
8a854238a17fc51624da31cd1df64a94f105579e
[friendica.git] / view / templates / item / compose-footer.tpl
1 <script type="text/javascript">
2         function updateLocationButtonDisplay(location_button, location_input)
3         {
4                 location_button.classList.remove('btn-primary');
5                 if (location_input.value) {
6                         location_button.disabled = false;
7                         location_button.classList.add('btn-primary');
8                         location_button.title = location_button.dataset.titleClear;
9                 } else if (!"geolocation" in navigator) {
10                         location_button.disabled = true;
11                         location_button.title = location_button.dataset.titleUnavailable;
12                 } else if (location_button.disabled) {
13                         location_button.title = location_button.dataset.titleDisabled;
14                 } else {
15                         location_button.title = location_button.dataset.titleSet;
16                 }
17         }
18
19         $(function() {
20                 // Jot attachment live preview.
21                 let $textarea = $('#comment-edit-text-0');
22                 $textarea.linkPreview();
23                 $textarea.keyup(function(){
24                         var textlen = $(this).val().length;
25                         $('#character-counter').text(textlen);
26                 });
27                 $textarea.editor_autocomplete(baseurl+"/acl");
28                 $textarea.bbco_autocomplete('bbcode');
29
30                 let $acl_allow_input = $('#acl_allow');
31                 let $group_allow_input = $('[name=group_allow]');
32                 let $contact_allow_input = $('[name=contact_allow]');
33                 let $acl_deny_input = $('#acl_deny');
34                 let $group_deny_input = $('[name=group_deny]');
35                 let $contact_deny_input = $('[name=contact_deny]');
36
37                 // Visibility accordion
38
39                 // Prevents open panel to collapse
40                 // @see https://stackoverflow.com/a/43593116
41                 $('[data-toggle="collapse"]').click(function(e) {
42                         target = $(this).attr('href');
43                         if ($(target).hasClass('in')) {
44                                 e.preventDefault(); // to stop the page jump to the anchor target.
45                                 e.stopPropagation()
46                         }
47                 });
48
49                 $('#visibility-public-panel').on('show.bs.collapse', function() {
50                         $('#visibility-public').prop('checked', true);
51                         $group_allow_input.prop('disabled', true);
52                         $contact_allow_input.prop('disabled', true);
53                         $group_deny_input.prop('disabled', true);
54                         $contact_deny_input.prop('disabled', true);
55                 });
56
57                 $('#visibility-custom-panel').on('show.bs.collapse', function() {
58                         $('#visibility-custom').prop('checked', true);
59                         $group_allow_input.prop('disabled', false);
60                         $contact_allow_input.prop('disabled', false);
61                         $group_deny_input.prop('disabled', false);
62                         $contact_deny_input.prop('disabled', false);
63                 });
64
65                 if (document.querySelector('input[name="visibility"]:checked').value === 'custom') {
66                         $('#visibility-custom-panel').collapse({parent: '#visibility-accordion'});
67                 }
68
69                 // Custom visibility tags inputs
70
71                 let acl_groups = new Bloodhound({
72                         local: {{$acl_groups|@json_encode nofilter}},
73                         identify: function(obj) { return obj.id; },
74                         datumTokenizer: Bloodhound.tokenizers.obj.whitespace(['name']),
75                         queryTokenizer: Bloodhound.tokenizers.whitespace,
76                 });
77                 let acl_contacts = new Bloodhound({
78                         local: {{$acl_contacts|@json_encode nofilter}},
79                         identify: function(obj) { return obj.id; },
80                         datumTokenizer: Bloodhound.tokenizers.obj.whitespace(['name', 'addr']),
81                         queryTokenizer: Bloodhound.tokenizers.whitespace,
82                 });
83                 let acl = new Bloodhound({
84                         local: {{$acl|@json_encode nofilter}},
85                         identify: function(obj) { return obj.id; },
86                         datumTokenizer: Bloodhound.tokenizers.obj.whitespace(['name', 'addr']),
87                         queryTokenizer: Bloodhound.tokenizers.whitespace,
88                 });
89                 acl.initialize();
90
91                 let suggestionTemplate = function (item) {
92                         return '<div><img src="' + item.micro + '" alt="" style="float: left; width: auto; height: 2.8em; margin-right: 0.5em;"> <strong>' + item.name + '</strong><br /><em>' + item.addr + '</em></div>';
93                 };
94
95                 $acl_allow_input.tagsinput({
96                         confirmKeys: [13, 44],
97                         cancelConfirmKeysOnEmpty: true,
98                         freeInput: false,
99                         tagClass: function(item) {
100                                 switch (item.type) {
101                                         case 'group'   : return 'label label-primary';
102                                         case 'contact'  :
103                                         default:
104                                                 return 'label label-info';
105                                 }
106                         },
107                         itemValue: 'id',
108                         itemText: 'name',
109                         itemThumb: 'micro',
110                         itemTitle: function(item) {
111                                 return item.addr;
112                         },
113                         typeaheadjs: {
114                                 name: 'contacts',
115                                 displayKey: 'name',
116                                 templates: {
117                                         suggestion: suggestionTemplate
118                                 },
119                                 source: acl.ttAdapter()
120                         }
121                 });
122
123                 $acl_deny_input
124                 .tagsinput({
125                         confirmKeys: [13, 44],
126                         freeInput: false,
127                         tagClass: function(item) {
128                                 switch (item.type) {
129                                         case 'group'   : return 'label label-primary';
130                                         case 'contact'  :
131                                         default:
132                                                 return 'label label-info';
133                                 }
134                         },
135                         itemValue: 'id',
136                         itemText: 'name',
137                         itemThumb: 'micro',
138                         itemTitle: function(item) {
139                                 return item.addr;
140                         },
141                         typeaheadjs: {
142                                 name: 'contacts',
143                                 displayKey: 'name',
144                                 templates: {
145                                         suggestion: suggestionTemplate
146                                 },
147                                 source: acl.ttAdapter()
148                         }
149                 });
150
151                 // Import existing ACL into the tags input fields.
152
153                 $group_allow_input.val().split(',').forEach(function (val) {
154                         $acl_allow_input.tagsinput('add', acl_groups.get(val)[0]);
155                 });
156                 $contact_allow_input.val().split(',').forEach(function (val) {
157                         $acl_allow_input.tagsinput('add', acl_contacts.get(val)[0]);
158                 });
159                 $group_deny_input.val().split(',').forEach(function (val) {
160                         $acl_deny_input.tagsinput('add', acl_groups.get(val)[0]);
161                 });
162                 $contact_deny_input.val().split(',').forEach(function (val) {
163                         $acl_deny_input.tagsinput('add', acl_contacts.get(val)[0]);
164                 });
165
166                 // Anti-duplicate callback + acl fields value generation
167
168                 $acl_allow_input.on('itemAdded', function (event) {
169                         // Removes duplicate in the opposite acl box
170                         $acl_deny_input.tagsinput('remove', event.item);
171
172                         // Update the real acl field
173                         $group_allow_input.val('');
174                         $contact_allow_input.val('');
175                         [].forEach.call($acl_allow_input.tagsinput('items'), function (item) {
176                                 if (item.type === 'group') {
177                                         $group_allow_input.val($group_allow_input.val() + '<' + item.id + '>');
178                                 } else {
179                                         $contact_allow_input.val($contact_allow_input.val() + '<' + item.id + '>');
180                                 }
181                         });
182                 });
183
184                 $acl_deny_input.on('itemAdded', function (event) {
185                         // Removes duplicate in the opposite acl box
186                         $acl_allow_input.tagsinput('remove', event.item);
187
188                         // Update the real acl field
189                         $group_deny_input.val('');
190                         $contact_deny_input.val('');
191                         [].forEach.call($acl_deny_input.tagsinput('items'), function (item) {
192                                 if (item.type === 'group') {
193                                         $group_deny_input.val($group_allow_input.val() + '<' + item.id + '>');
194                                 } else {
195                                         $contact_deny_input.val($contact_allow_input.val() + '<' + item.id + '>');
196                                 }
197                         });
198                 });
199
200                 let location_button = document.getElementById('profile-location');
201                 let location_input = document.getElementById('jot-location');
202
203                 updateLocationButtonDisplay(location_button, location_input);
204
205                 location_input.addEventListener('change', function () {
206                         updateLocationButtonDisplay(location_button, location_input);
207                 });
208                 location_input.addEventListener('keyup', function () {
209                         updateLocationButtonDisplay(location_button, location_input);
210                 });
211
212                 location_button.addEventListener('click', function() {
213                         if (location_input.value) {
214                                 location_input.value = '';
215                                 updateLocationButtonDisplay(location_button, location_input);
216                         } else if ("geolocation" in navigator) {
217                                 navigator.geolocation.getCurrentPosition(function(position) {
218                                         location_input.value = position.coords.latitude + ', ' + position.coords.longitude;
219                                         updateLocationButtonDisplay(location_button, location_input);
220                                 }, function (error) {
221                                         location_button.disabled = true;
222                                         updateLocationButtonDisplay(location_button, location_input);
223                                 });
224                         }
225                 });
226         })
227 </script>