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