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