4ce8b869c317304732744702fe85593f28dd8801
[friendica.git/.git] / view / theme / frost-mobile / js / 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 = 2; //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         $(document).on("click", ".acl-button-show", that.on_button_show);
25         $(document).on("click", ".acl-button-hide", 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.preventDefault()
62         event.stopPropagation();
63         
64         if (that.showall.hasClass("selected")){
65                 return false;
66         }
67         that.showall.addClass("selected");
68         
69         that.allow_cid = [];
70         that.allow_gid = [];
71         that.deny_cid  = [];
72         that.deny_gid  = [];
73         
74         that.update_view();
75         
76         return false;
77 }
78
79 ACL.prototype.on_button_show = function(event){
80         event.preventDefault()
81         event.stopImmediatePropagation()
82         event.stopPropagation();
83
84         /*that.showall.removeClass("selected");
85         $(this).siblings(".acl-button-hide").removeClass("selected");
86         $(this).toggleClass("selected");*/
87
88         that.set_allow($(this).parent().attr('id'));
89
90         return false;
91 }
92 ACL.prototype.on_button_hide = function(event){
93         event.preventDefault()
94         event.stopImmediatePropagation()
95         event.stopPropagation();
96
97         /*that.showall.removeClass("selected");
98         $(this).siblings(".acl-button-show").removeClass("selected");
99         $(this).toggleClass("selected");*/
100
101         that.set_deny($(this).parent().attr('id'));
102
103         return false;
104 }
105
106 ACL.prototype.set_allow = function(itemid){
107         type = itemid[0];
108         id       = parseInt(itemid.substr(1));
109         switch(type){
110                 case "g":
111                         if (that.allow_gid.indexOf(id)<0){
112                                 that.allow_gid.push(id)
113                         }else {
114                                 that.allow_gid.remove(id);
115                         }
116                         if (that.deny_gid.indexOf(id)>=0) that.deny_gid.remove(id);
117                         break;
118                 case "c":
119                         if (that.allow_cid.indexOf(id)<0){
120                                 that.allow_cid.push(id)
121                         } else {
122                                 that.allow_cid.remove(id);
123                         }
124                         if (that.deny_cid.indexOf(id)>=0) that.deny_cid.remove(id);                     
125                         break;
126         }
127         that.update_view();
128 }
129
130 ACL.prototype.set_deny = function(itemid){
131         type = itemid[0];
132         id       = parseInt(itemid.substr(1));
133         switch(type){
134                 case "g":
135                         if (that.deny_gid.indexOf(id)<0){
136                                 that.deny_gid.push(id)
137                         } else {
138                                 that.deny_gid.remove(id);
139                         }
140                         if (that.allow_gid.indexOf(id)>=0) that.allow_gid.remove(id);
141                         break;
142                 case "c":
143                         if (that.deny_cid.indexOf(id)<0){
144                                 that.deny_cid.push(id)
145                         } else {
146                                 that.deny_cid.remove(id);
147                         }
148                         if (that.allow_cid.indexOf(id)>=0) that.allow_cid.remove(id);
149                         break;
150         }
151         that.update_view();
152 }
153
154 ACL.prototype.update_view = function(){
155         if (that.allow_gid.length==0 && that.allow_cid.length==0 &&
156                 that.deny_gid.length==0 && that.deny_cid.length==0){
157                         that.showall.addClass("selected");
158                         /* jot acl */
159                                 $('#jot-perms-icon').removeClass('lock').addClass('unlock');
160                                 $('#jot-public').show();
161                                 $('.profile-jot-net input').attr('disabled', false);                    
162                                 if(typeof editor != 'undefined' && editor != false) {
163                                         $('#profile-jot-desc').html(window.isPublic);
164                                 }
165                         
166         } else {
167                         that.showall.removeClass("selected");
168                         /* jot acl */
169                                 $('#jot-perms-icon').removeClass('unlock').addClass('lock');
170                                 $('#jot-public').hide();
171                                 $('.profile-jot-net input').attr('disabled', 'disabled');                       
172                                 $('#profile-jot-desc').html('&nbsp;');
173         }
174         $("#acl-list-content .acl-list-item").each(function(){
175                 $(this).removeClass("groupshow grouphide");
176         });
177         
178         $("#acl-list-content .acl-list-item").each(function(){
179                 itemid = $(this).attr('id');
180                 type = itemid[0];
181                 id       = parseInt(itemid.substr(1));
182                 
183                 btshow = $(this).children(".acl-button-show").removeClass("selected");
184                 bthide = $(this).children(".acl-button-hide").removeClass("selected");  
185                 
186                 switch(type){
187                         case "g":
188                                 var uclass = "";
189                                 if (that.allow_gid.indexOf(id)>=0){
190                                         btshow.addClass("selected");
191                                         bthide.removeClass("selected");
192                                         uclass="groupshow";
193                                 }
194                                 if (that.deny_gid.indexOf(id)>=0){
195                                         btshow.removeClass("selected");
196                                         bthide.addClass("selected");
197                                         uclass="grouphide";
198                                 }
199                                 
200                                 $(that.group_uids[id]).each(function(i,v) {
201                                         if(uclass == "grouphide")
202                                                 $("#c"+v).removeClass("groupshow");
203                                         if(uclass != "") {
204                                                 var cls = $("#c"+v).attr('class');
205                                                 if( cls == undefined)
206                                                         return true;
207                                                 var hiding = cls.indexOf('grouphide');
208                                                 if(hiding == -1)
209                                                         $("#c"+v).addClass(uclass);
210                                         }
211                                 });
212                                 
213                                 break;
214                         case "c":
215                                 if (that.allow_cid.indexOf(id)>=0){
216                                         btshow.addClass("selected");
217                                         bthide.removeClass("selected");
218                                 }
219                                 if (that.deny_cid.indexOf(id)>=0){
220                                         btshow.removeClass("selected");
221                                         bthide.addClass("selected");
222                                 }                       
223                 }
224                 
225         });
226         
227 }
228
229
230 ACL.prototype.get = function(start,count, search){
231         var postdata = {
232                 start:start,
233                 count:count,
234                 search:search,
235         }
236         
237         $.ajax({
238                 type:'POST',
239                 url: that.url,
240                 data: postdata,
241                 dataType: 'json',
242                 success:that.populate
243         });
244 }
245
246 ACL.prototype.populate = function(data){
247 /*      var height = Math.ceil(data.tot / that.nw) * 42;
248         that.list_content.height(height);*/
249         $(data.items).each(function(){
250                 html = "<div class='acl-list-item {4} {5}' title='{6}' id='{2}{3}'>"+that.item_tpl+"</div>";
251                 html = html.format( this.photo, this.name, this.type, this.id, '', this.network, this.link );
252                 if (this.uids!=undefined) that.group_uids[this.id] = this.uids;
253                 //console.log(html);
254                 that.list_content.append(html);
255         });
256         $(".acl-list-item img[data-src]", that.list_content).each(function(i, el){
257                 // Add src attribute for images with a data-src attribute
258                 $(el).attr('src', $(el).data("src"));
259         });
260         that.update_view();
261 }
262