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