]> git.mxchange.org Git - friendica.git/blob - view/js/acl.js
Merge pull request #6942 from annando/worker-fast-commands
[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         this.kp_timer = setTimeout( this.search.bind(this), 1000);
113 };
114
115 ACL.prototype.on_showall = function(event){
116         event.preventDefault()
117         event.stopPropagation();
118
119         if (this.showall.hasClass("selected")){
120                 return false;
121         }
122         this.showall.addClass("selected");
123
124         this.allow_cid = [];
125         this.allow_gid = [];
126         this.deny_cid  = [];
127         this.deny_gid  = [];
128
129         this.update_view();
130
131         return false;
132 };
133
134 ACL.prototype.on_button_show = function(event){
135         event.preventDefault()
136         event.stopImmediatePropagation()
137         event.stopPropagation();
138
139         this.set_allow($(event.target).parent().attr('id'));
140
141         return false;
142 };
143
144 ACL.prototype.on_button_hide = function(event){
145         event.preventDefault()
146         event.stopImmediatePropagation()
147         event.stopPropagation();
148
149         this.set_deny($(event.target).parent().attr('id'));
150
151         return false;
152 };
153
154 ACL.prototype.set_allow = function(itemid) {
155         type = itemid[0];
156         id   = parseInt(itemid.substr(1));
157
158         switch (type){
159                 case "g":
160                         if (this.allow_gid.indexOf(id) < 0) {
161                                 this.allow_gid.push(id);
162                         }else {
163                                 this.allow_gid.remove(id);
164                         }
165                         if (this.deny_gid.indexOf(id) >= 0) {
166                                 this.deny_gid.remove(id);
167                         }
168                         break;
169                 case "c":
170                         if (this.allow_cid.indexOf(id) < 0){
171                                 this.allow_cid.push(id);
172                                 if (this.data[id].forum == "1") {
173                                         // If we have select already a forum,
174                                         // we need to remove the old one (because friendica does
175                                         // allow only one forum as receiver).
176                                         if (this.forumCache !== null && this.forumCache !== id) {
177                                                 this.deselectCid(this.forumCache);
178                                         }
179                                         // Update the forum cache.
180                                         this.forumCache = id;
181                                         this.add_mention(id);
182                                 }
183                         } else {
184                                 this.allow_cid.remove(id);
185                                 if (this.data[id].forum == "1") {
186                                         this.remove_mention(id);
187                                 }
188                         }
189                         if (this.deny_cid.indexOf(id) >=0 ) {
190                                 this.deny_cid.remove(id);
191                         }
192                         break;
193         }
194         this.update_view();
195 };
196
197 ACL.prototype.set_deny = function(itemid){
198         type = itemid[0];
199         id     = parseInt(itemid.substr(1));
200
201         switch(type){
202                 case "g":
203                         if (this.deny_gid.indexOf(id)<0){
204                                 this.deny_gid.push(id)
205                         } else {
206                                 this.deny_gid.remove(id);
207                         }
208                         if (this.allow_gid.indexOf(id)>=0) this.allow_gid.remove(id);
209                         break;
210                 case "c":
211                         if (this.data[id].forum=="1") this.remove_mention(id);
212                         if (this.deny_cid.indexOf(id)<0){
213                                 this.deny_cid.push(id)
214                         } else {
215                                 this.deny_cid.remove(id);
216                         }
217                         if (this.allow_cid.indexOf(id)>=0) this.allow_cid.remove(id);
218                         break;
219         }
220         this.update_view();
221 };
222
223 ACL.prototype.is_show_all = function() {
224         return (this.allow_gid.length==0 && this.allow_cid.length==0 &&
225                 this.deny_gid.length==0 && this.deny_cid.length==0);
226 };
227
228 ACL.prototype.update_view = function () {
229         if (this.is_show_all()) {
230                 this.showall.addClass("selected");
231                 /* jot acl */
232                 $('#jot-perms-icon').removeClass('lock').addClass('unlock');
233                 $('#jot-public').show();
234                 $('.profile-jot-net input[type=checkbox]').each(function() {
235                         // Restores checkbox state if it had been saved
236                         if ($(this).attr('data-checked') !== undefined) {
237                                 $(this).prop('checked', $(this).attr('data-checked') === 'true');
238                         }
239                 });
240
241                 $('.profile-jot-net input').attr('disabled', false);
242                 if (typeof editor != 'undefined' && editor != false) {
243                         $('#profile-jot-desc').html(ispublic);
244                 }
245         } else {
246                 this.showall.removeClass("selected");
247                 /* jot acl */
248                 $('#jot-perms-icon').removeClass('unlock').addClass('lock');
249                 $('#jot-public').hide();
250                 $('.profile-jot-net input[type=checkbox]').each(function() {
251                         // Saves current checkbox state
252                         $(this)
253                                 .attr('data-checked', $(this).prop('checked'))
254                                 .prop('checked', false);
255                 });
256                 $('.profile-jot-net input').attr('disabled', 'disabled');
257                 $('#profile-jot-desc').html('&nbsp;');
258         }
259
260         $("#acl-list-content .acl-list-item").each(function (index, element) {
261                 $(this).removeClass("groupshow grouphide");
262
263                 itemid = $(element).attr('id');
264                 type = itemid[0];
265                 id       = parseInt(itemid.substr(1));
266
267                 btshow = $(element).children(".acl-button-show").removeClass("selected");
268                 bthide = $(element).children(".acl-button-hide").removeClass("selected");
269
270                 switch (type) {
271                         case "g":
272                                 var uclass = "";
273                                 if (this.allow_gid.indexOf(id) >= 0) {
274                                         btshow.addClass("selected");
275                                         bthide.removeClass("selected");
276                                         uclass = "groupshow";
277                                 }
278                                 if (this.deny_gid.indexOf(id) >= 0) {
279                                         btshow.removeClass("selected");
280                                         bthide.addClass("selected");
281                                         uclass = "grouphide";
282                                 }
283
284                                 $(this.group_uids[id]).each(function (i, v) {
285                                         if (uclass == "grouphide")
286                                                 $("#c" + v).removeClass("groupshow");
287                                         if (uclass != "") {
288                                                 var cls = $("#c" + v).attr('class');
289                                                 if (cls == undefined)
290                                                         return true;
291                                                 var hiding = cls.indexOf('grouphide');
292                                                 if (hiding == -1)
293                                                         $("#c" + v).addClass(uclass);
294                                         }
295                                 });
296
297                                 break;
298                         case "c":
299                                 if (this.allow_cid.indexOf(id) >= 0) {
300                                         btshow.addClass("selected");
301                                         bthide.removeClass("selected");
302                                 }
303                                 if (this.deny_cid.indexOf(id) >= 0) {
304                                         btshow.removeClass("selected");
305                                         bthide.addClass("selected");
306                                 }
307                 }
308
309         }.bind(this));
310
311 };
312
313 ACL.prototype.get = function(start,count, search){
314         var postdata = {
315                 start:start,
316                 count:count,
317                 search:search,
318         }
319
320         $.ajax({
321                 type:'POST',
322                 url: this.url,
323                 data: postdata,
324                 dataType: 'json',
325                 success:this.populate.bind(this)
326         });
327 };
328
329 ACL.prototype.populate = function(data){
330         var height = Math.ceil(data.tot / this.nw) * 42;
331         this.list_content.height(height);
332         this.data = {};
333         $(data.items).each(function(index, item) {
334                 if (item.separator != undefined) {
335                         html = "<hr class='clear'>";
336                 } else {
337                         html = "<div class='acl-list-item {4} {5} type{2}' title='{6}' id='{2}{3}'>"+this.item_tpl+"</div>";
338                         html = html.format(item.photo, item.name, item.type, item.id, (item.forum=='1'?'forum':''), item.network, item.link);
339                         if (item.uids != undefined) {
340                                 this.group_uids[item.id] = item.uids;
341                         }
342                 }
343                 this.list_content.append(html);
344                 this.data[item.id] = item;
345         }.bind(this));
346         $(".acl-list-item img[data-src]", this.list_content).each(function(i, el){
347                 // Add src attribute for images with a data-src attribute
348                 $(el).attr('src', $(el).data("src"));
349         });
350
351         this.update_view();
352 };
353
354 /**
355  * @brief Deselect previous selected contact.
356  *
357  * @param {int} id The contact ID.
358  * @returns {void}
359  */
360 ACL.prototype.deselectCid = function(id) {
361         if (this.allow_cid.indexOf(id) >= 0) {
362                 this.allow_cid.remove(id);
363         }
364         if (this.deny_cid.indexOf(id) >=0 ) {
365                 this.deny_cid.remove(id);
366         }
367         this.remove_mention(id);
368 };
369 // @license-end