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