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