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