]> git.mxchange.org Git - friendica.git/blob - view/js/acl.js
Merge pull request #7565 from MrPetovan/bug/7406-neutralize-enter-in-autocomplete
[friendica.git] / view / js / acl.js
1 // @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPLv3-or-later
2 function ACL(backend_url, preset, automention, is_mobile){
3
4         this.url = backend_url;
5         this.automention = automention;
6         this.is_mobile = is_mobile;
7
8
9         this.kp_timer = null;
10
11         if (preset == undefined) {
12                 preset = [];
13         }
14         this.allow_cid = (preset[0] || []);
15         this.allow_gid = (preset[1] || []);
16         this.deny_cid  = (preset[2] || []);
17         this.deny_gid  = (preset[3] || []);
18         this.group_uids = [];
19         this.forumCache = null;
20
21         if (this.is_mobile) {
22                 this.nw = 1;
23         } else {
24                 this.nw = 4;
25         }
26
27
28         this.list_content = $("#acl-list-content");
29         this.item_tpl = unescape($(".acl-list-item[rel=acl-template]").html());
30         this.showall = $("#acl-showall");
31
32         if (preset.length==0) {
33                 this.showall.addClass("selected");
34         }
35
36         /*events*/
37         this.showall.click(this.on_showall.bind(this));
38         $(document).on("click", ".acl-button-show", this.on_button_show.bind(this));
39         $(document).on("click", ".acl-button-hide", this.on_button_hide.bind(this));
40         $("#acl-search").keypress(this.on_search.bind(this));
41         $("#acl-wrapper").parents("form").submit(this.on_submit.bind(this));
42
43         /* add/remove mentions  */
44         this.element = $("#profile-jot-text");
45         this.htmlelm = this.element.get()[0];
46 }
47
48 ACL.prototype.remove_mention = function(id) {
49         if (!this.automention) {
50                 return;
51         }
52         var nick = this.data[id].nick;
53         var addr = this.data[id].addr;
54
55         if (addr != "") {
56                 var searchText = "!" + addr + " ";
57         } else {
58                 var searchText = "!" + nick + "+" + id + " ";
59         }
60
61         var start = this.element.val().indexOf(searchText);
62         if (start < 0) {
63                 return;
64         }
65         var end = start + searchText.length;
66         this.element.setSelection(start, end).replaceSelectedText('').collapseSelection(false);
67 };
68
69 ACL.prototype.add_mention = function(id) {
70         if (!this.automention) {
71                 return;
72         }
73         var nick = this.data[id].nick;
74         var addr = this.data[id].addr;
75
76         if (addr != "") {
77                 var searchText = "!" + addr + " ";
78         } else {
79                 var searchText = "!" + nick + "+" + id + " ";
80         }
81
82         if (this.element.val().indexOf( searchText) >= 0 ) {
83                 return;
84         }
85         this.element.val(searchText + this.element.val()).trigger('change');
86 }
87
88 ACL.prototype.on_submit = function(){
89         var aclfields = $("#acl-fields").html("");
90         $(this.allow_gid).each(function(i,v){
91                 aclfields.append("<input type='hidden' name='group_allow[]' value='"+v+"'>");
92         });
93         $(this.allow_cid).each(function(i,v){
94                 aclfields.append("<input type='hidden' name='contact_allow[]' value='"+v+"'>");
95         });
96         $(this.deny_gid).each(function(i,v){
97                 aclfields.append("<input type='hidden' name='group_deny[]' value='"+v+"'>");
98         });
99         $(this.deny_cid).each(function(i,v){
100                 aclfields.append("<input type='hidden' name='contact_deny[]' value='"+v+"'>");
101         });
102 };
103
104 ACL.prototype.search = function(){
105         var srcstr = $("#acl-search").val();
106         this.list_content.html("");
107         this.get(0,100, srcstr);
108 };
109
110 ACL.prototype.on_search = function(event){
111         if (this.kp_timer) clearTimeout(this.kp_timer);
112
113         // Triggers an immediate search while preventing form submission
114         if (event.key === 'Enter') {
115                 this.search();
116                 event.preventDefault();
117         } else {
118                 this.kp_timer = setTimeout( this.search.bind(this), 500);
119         }
120 };
121
122 ACL.prototype.on_showall = function(event){
123         event.preventDefault()
124         event.stopPropagation();
125
126         if (this.showall.hasClass("selected")){
127                 return false;
128         }
129         this.showall.addClass("selected");
130
131         this.allow_cid = [];
132         this.allow_gid = [];
133         this.deny_cid  = [];
134         this.deny_gid  = [];
135
136         this.update_view();
137
138         return false;
139 };
140
141 ACL.prototype.on_button_show = function(event){
142         event.preventDefault()
143         event.stopImmediatePropagation()
144         event.stopPropagation();
145
146         this.set_allow($(event.target).parent().attr('id'));
147
148         return false;
149 };
150
151 ACL.prototype.on_button_hide = function(event){
152         event.preventDefault()
153         event.stopImmediatePropagation()
154         event.stopPropagation();
155
156         this.set_deny($(event.target).parent().attr('id'));
157
158         return false;
159 };
160
161 ACL.prototype.set_allow = function(itemid) {
162         type = itemid[0];
163         id   = parseInt(itemid.substr(1));
164
165         switch (type){
166                 case "g":
167                         if (this.allow_gid.indexOf(id) < 0) {
168                                 this.allow_gid.push(id);
169                         }else {
170                                 this.allow_gid.remove(id);
171                         }
172                         if (this.deny_gid.indexOf(id) >= 0) {
173                                 this.deny_gid.remove(id);
174                         }
175                         break;
176                 case "c":
177                         if (this.allow_cid.indexOf(id) < 0){
178                                 this.allow_cid.push(id);
179                                 if (this.data[id].forum == "1") {
180                                         // If we have select already a forum,
181                                         // we need to remove the old one (because friendica does
182                                         // allow only one forum as receiver).
183                                         if (this.forumCache !== null && this.forumCache !== id) {
184                                                 this.deselectCid(this.forumCache);
185                                         }
186                                         // Update the forum cache.
187                                         this.forumCache = id;
188                                         this.add_mention(id);
189                                 }
190                         } else {
191                                 this.allow_cid.remove(id);
192                                 if (this.data[id].forum == "1") {
193                                         this.remove_mention(id);
194                                 }
195                         }
196                         if (this.deny_cid.indexOf(id) >=0 ) {
197                                 this.deny_cid.remove(id);
198                         }
199                         break;
200         }
201         this.update_view();
202 };
203
204 ACL.prototype.set_deny = function(itemid){
205         type = itemid[0];
206         id     = parseInt(itemid.substr(1));
207
208         switch(type){
209                 case "g":
210                         if (this.deny_gid.indexOf(id)<0){
211                                 this.deny_gid.push(id)
212                         } else {
213                                 this.deny_gid.remove(id);
214                         }
215                         if (this.allow_gid.indexOf(id)>=0) this.allow_gid.remove(id);
216                         break;
217                 case "c":
218                         if (this.data[id].forum=="1") this.remove_mention(id);
219                         if (this.deny_cid.indexOf(id)<0){
220                                 this.deny_cid.push(id)
221                         } else {
222                                 this.deny_cid.remove(id);
223                         }
224                         if (this.allow_cid.indexOf(id)>=0) this.allow_cid.remove(id);
225                         break;
226         }
227         this.update_view();
228 };
229
230 ACL.prototype.is_show_all = function() {
231         return (this.allow_gid.length==0 && this.allow_cid.length==0 &&
232                 this.deny_gid.length==0 && this.deny_cid.length==0);
233 };
234
235 ACL.prototype.update_view = function () {
236         if (this.is_show_all()) {
237                 this.showall.addClass("selected");
238                 /* jot acl */
239                 $('#jot-perms-icon').removeClass('lock').addClass('unlock');
240                 $('#jot-public').show();
241                 $('.profile-jot-net input[type=checkbox]').each(function() {
242                         // Restores checkbox state if it had been saved
243                         if ($(this).attr('data-checked') !== undefined) {
244                                 $(this).prop('checked', $(this).attr('data-checked') === 'true');
245                         }
246                 });
247
248                 $('.profile-jot-net input').attr('disabled', false);
249                 if (typeof editor != 'undefined' && editor != false) {
250                         $('#profile-jot-desc').html(ispublic);
251                 }
252         } else {
253                 this.showall.removeClass("selected");
254                 /* jot acl */
255                 $('#jot-perms-icon').removeClass('unlock').addClass('lock');
256                 $('#jot-public').hide();
257                 $('.profile-jot-net input[type=checkbox]').each(function() {
258                         // Saves current checkbox state
259                         $(this)
260                                 .attr('data-checked', $(this).prop('checked'))
261                                 .prop('checked', false);
262                 });
263                 $('.profile-jot-net input').attr('disabled', 'disabled');
264                 $('#profile-jot-desc').html('&nbsp;');
265         }
266
267         $("#acl-list-content .acl-list-item").each(function (index, element) {
268                 $(this).removeClass("groupshow grouphide");
269
270                 itemid = $(element).attr('id');
271                 type = itemid[0];
272                 id       = parseInt(itemid.substr(1));
273
274                 btshow = $(element).children(".acl-button-show").removeClass("selected");
275                 bthide = $(element).children(".acl-button-hide").removeClass("selected");
276
277                 switch (type) {
278                         case "g":
279                                 var uclass = "";
280                                 if (this.allow_gid.indexOf(id) >= 0) {
281                                         btshow.addClass("selected");
282                                         bthide.removeClass("selected");
283                                         uclass = "groupshow";
284                                 }
285                                 if (this.deny_gid.indexOf(id) >= 0) {
286                                         btshow.removeClass("selected");
287                                         bthide.addClass("selected");
288                                         uclass = "grouphide";
289                                 }
290
291                                 $(this.group_uids[id]).each(function (i, v) {
292                                         if (uclass == "grouphide")
293                                                 $("#c" + v).removeClass("groupshow");
294                                         if (uclass != "") {
295                                                 var cls = $("#c" + v).attr('class');
296                                                 if (cls == undefined)
297                                                         return true;
298                                                 var hiding = cls.indexOf('grouphide');
299                                                 if (hiding == -1)
300                                                         $("#c" + v).addClass(uclass);
301                                         }
302                                 });
303
304                                 break;
305                         case "c":
306                                 if (this.allow_cid.indexOf(id) >= 0) {
307                                         btshow.addClass("selected");
308                                         bthide.removeClass("selected");
309                                 }
310                                 if (this.deny_cid.indexOf(id) >= 0) {
311                                         btshow.removeClass("selected");
312                                         bthide.addClass("selected");
313                                 }
314                 }
315
316         }.bind(this));
317
318 };
319
320 ACL.prototype.get = function(start,count, search){
321         var postdata = {
322                 start:start,
323                 count:count,
324                 search:search,
325         }
326
327         $.ajax({
328                 type:'POST',
329                 url: this.url,
330                 data: postdata,
331                 dataType: 'json',
332                 success:this.populate.bind(this)
333         });
334 };
335
336 ACL.prototype.populate = function(data){
337         var height = Math.ceil(data.tot / this.nw) * 42;
338         this.list_content.height(height);
339         this.data = {};
340         $(data.items).each(function(index, item) {
341                 if (item.separator != undefined) {
342                         html = "<hr class='clear'>";
343                 } else {
344                         html = "<div class='acl-list-item {4} {5} type{2}' title='{6}' id='{2}{3}'>"+this.item_tpl+"</div>";
345                         html = html.format(item.photo, item.name, item.type, item.id, (item.forum=='1'?'forum':''), item.network, item.link);
346                         if (item.uids != undefined) {
347                                 this.group_uids[item.id] = item.uids;
348                         }
349                 }
350                 this.list_content.append(html);
351                 this.data[item.id] = item;
352         }.bind(this));
353         $(".acl-list-item img[data-src]", this.list_content).each(function(i, el){
354                 // Add src attribute for images with a data-src attribute
355                 $(el).attr('src', $(el).data("src"));
356         });
357
358         this.update_view();
359 };
360
361 /**
362  * @brief Deselect previous selected contact.
363  *
364  * @param {int} id The contact ID.
365  * @returns {void}
366  */
367 ACL.prototype.deselectCid = function(id) {
368         if (this.allow_cid.indexOf(id) >= 0) {
369                 this.allow_cid.remove(id);
370         }
371         if (this.deny_cid.indexOf(id) >=0 ) {
372                 this.deny_cid.remove(id);
373         }
374         this.remove_mention(id);
375 };
376 // @license-end