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