]> git.mxchange.org Git - friendica.git/blob - include/acl.js
Merge pull request #163 from fabrixxm/master
[friendica.git] / include / acl.js
1 function ACL(backend_url, preset){
2         that = this;
3         
4         that.url = backend_url;
5         
6         that.kp_timer = null;
7         
8         if (preset==undefined) preset = [];
9         that.allow_cid = (preset[0] || []);
10         that.allow_gid = (preset[1] || []);
11         that.deny_cid  = (preset[2] || []);
12         that.deny_gid  = (preset[3] || []);
13         that.group_uids = [];
14         that.nw = 4; //items per row. should be calulated from #acl-list.width
15         
16         that.list_content = $("#acl-list-content");
17         that.item_tpl = unescape($(".acl-list-item[rel=acl-template]").html());
18         that.showall = $("#acl-showall");
19
20         if (preset.length==0) that.showall.addClass("selected");
21         
22         /*events*/
23         that.showall.click(that.on_showall);
24         $(".acl-button-show").live('click', that.on_button_show);
25         $(".acl-button-hide").live('click', that.on_button_hide);
26         $("#acl-search").keypress(that.on_search);
27         $("#acl-wrapper").parents("form").submit(that.on_submit);
28         
29         /* startup! */
30         that.get(0,100);
31 }
32
33 ACL.prototype.on_submit = function(){
34         aclfileds = $("#acl-fields").html("");
35         $(that.allow_gid).each(function(i,v){
36                 aclfileds.append("<input type='hidden' name='group_allow[]' value='"+v+"'>");
37         });
38         $(that.allow_cid).each(function(i,v){
39                 aclfileds.append("<input type='hidden' name='contact_allow[]' value='"+v+"'>");
40         });
41         $(that.deny_gid).each(function(i,v){
42                 aclfileds.append("<input type='hidden' name='group_deny[]' value='"+v+"'>");
43         });
44         $(that.deny_cid).each(function(i,v){
45                 aclfileds.append("<input type='hidden' name='contact_deny[]' value='"+v+"'>");
46         });     
47 }
48
49 ACL.prototype.search = function(){
50         var srcstr = $("#acl-search").val();
51         that.list_content.html("");
52         that.get(0,100, srcstr);
53 }
54
55 ACL.prototype.on_search = function(event){
56         if (that.kp_timer) clearTimeout(that.kp_timer);
57         that.kp_timer = setTimeout( that.search, 1000);
58 }
59
60 ACL.prototype.on_showall = function(event){
61         event.stopPropagation();
62         if (that.showall.hasClass("selected")){
63                 return false;
64         }
65         that.showall.addClass("selected");
66         
67         that.allow_cid = [];
68         that.allow_gid = [];
69         that.deny_cid  = [];
70         that.deny_gid  = [];
71         
72         that.updateview();
73         
74         return false;
75 }
76
77 ACL.prototype.on_button_show = function(event){
78         event.stopPropagation();
79
80         /*that.showall.removeClass("selected");
81         $(this).siblings(".acl-button-hide").removeClass("selected");
82         $(this).toggleClass("selected");*/
83
84         that.set_allow($(this).parent().attr('id'));
85
86         return false;
87 }
88 ACL.prototype.on_button_hide = function(event){
89         event.stopPropagation();
90
91         /*that.showall.removeClass("selected");
92         $(this).siblings(".acl-button-show").removeClass("selected");
93         $(this).toggleClass("selected");*/
94
95         that.set_deny($(this).parent().attr('id'));
96
97         return false;
98 }
99
100 ACL.prototype.set_allow = function(itemid){
101         type = itemid[0];
102         id       = parseInt(itemid.substr(1));
103         switch(type){
104                 case "g":
105                         if (that.allow_gid.indexOf(id)<0){
106                                 that.allow_gid.push(id)
107                         }else {
108                                 that.allow_gid.remove(id);
109                         }
110                         if (that.deny_gid.indexOf(id)>=0) that.deny_gid.remove(id);
111                         break;
112                 case "c":
113                         if (that.allow_cid.indexOf(id)<0){
114                                 that.allow_cid.push(id)
115                         } else {
116                                 that.allow_cid.remove(id);
117                         }
118                         if (that.deny_cid.indexOf(id)>=0) that.deny_cid.remove(id);                     
119                         break;
120         }
121         that.updateview();
122 }
123
124 ACL.prototype.set_deny = function(itemid){
125         type = itemid[0];
126         id       = parseInt(itemid.substr(1));
127         switch(type){
128                 case "g":
129                         if (that.deny_gid.indexOf(id)<0){
130                                 that.deny_gid.push(id)
131                         } else {
132                                 that.deny_gid.remove(id);
133                         }
134                         if (that.allow_gid.indexOf(id)>=0) that.allow_gid.remove(id);
135                         break;
136                 case "c":
137                         if (that.deny_cid.indexOf(id)<0){
138                                 that.deny_cid.push(id)
139                         } else {
140                                 that.deny_cid.remove(id);
141                         }
142                         if (that.allow_cid.indexOf(id)>=0) that.allow_cid.remove(id);
143                         break;
144         }
145         that.updateview();
146 }
147
148 ACL.prototype.updateview = function(){
149         if (that.allow_gid.length==0 && that.allow_cid.length==0 &&
150                 that.deny_gid.length==0 && that.deny_cid.length==0){
151                         that.showall.addClass("selected");
152                         /* jot acl */
153                                 $('#jot-perms-icon').removeClass('lock').addClass('unlock');
154                                 $('#jot-public').show();
155                                 $('.profile-jot-net input').attr('disabled', false);                    
156                                 if(editor != false) {
157                                         $('#profile-jot-desc').html(ispublic);
158                                 }
159                         
160         } else {
161                         that.showall.removeClass("selected");
162                         /* jot acl */
163                                 $('#jot-perms-icon').removeClass('unlock').addClass('lock');
164                                 $('#jot-public').hide();
165                                 $('.profile-jot-net input').attr('disabled', 'disabled');                       
166                                 $('#profile-jot-desc').html('&nbsp;');
167         }
168         
169         $("#acl-list-content .acl-list-item").each(function(){
170                 itemid = $(this).attr('id');
171                 type = itemid[0];
172                 id       = parseInt(itemid.substr(1));
173                 
174                 btshow = $(this).children(".acl-button-show").removeClass("selected");
175                 bthide = $(this).children(".acl-button-hide").removeClass("selected");  
176                 
177                 switch(type){
178                         case "g":
179                                 var uclass = "";
180                                 if (that.allow_gid.indexOf(id)>=0){
181                                         btshow.addClass("selected");
182                                         bthide.removeClass("selected");
183                                         uclass="groupshow";
184                                 }
185                                 if (that.deny_gid.indexOf(id)>=0){
186                                         btshow.removeClass("selected");
187                                         bthide.addClass("selected");
188                                         uclass="grouphide";
189                                 }
190                                 
191                                 $(that.group_uids[id]).each(function(i,v){
192                                         $("#c"+v).removeClass("groupshow grouphide").addClass(uclass);
193                                 });
194                                 
195                                 break;
196                         case "c":
197                                 if (that.allow_cid.indexOf(id)>=0){
198                                         btshow.addClass("selected");
199                                         bthide.removeClass("selected");
200                                 }
201                                 if (that.deny_cid.indexOf(id)>=0){
202                                         btshow.removeClass("selected");
203                                         bthide.addClass("selected");
204                                 }                       
205                 }
206                 
207         });
208         
209 }
210
211
212 ACL.prototype.get = function(start,count, search){
213         var postdata = {
214                 start:start,
215                 count:count,
216                 search:search,
217         }
218         
219         $.ajax({
220                 type:'POST',
221                 url: that.url,
222                 data: postdata,
223                 dataType: 'json',
224                 success:that.populate
225         });
226 }
227
228 ACL.prototype.populate = function(data){
229         var height = Math.ceil(data.tot / that.nw) * 42;
230         that.list_content.height(height);
231         $(data.items).each(function(){
232                 html = "<div class='acl-list-item {4} {5}' id='{2}{3}'>"+that.item_tpl+"</div>";
233                 html = html.format( this.photo, this.name, this.type, this.id, '', this.network );
234                 if (this.uids!=undefined) that.group_uids[this.id] = this.uids;
235                 //console.log(html);
236                 that.list_content.append(html);
237         });
238         that.updateview();
239 }
240