]> git.mxchange.org Git - friendica.git/blob - js/acl.js
Vier - mobile friendly ACL window and form fields
[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);
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.showall.removeClass("selected");
128         $(this).siblings(".acl-button-hide").removeClass("selected");
129         $(this).toggleClass("selected");*/
130
131         this.set_allow($(this).parent().attr('id'));
132
133         return false;
134 }
135 ACL.prototype.on_button_hide = function(event){
136         event.preventDefault()
137         event.stopImmediatePropagation()
138         event.stopPropagation();
139
140         /*this.showall.removeClass("selected");
141         $(this).siblings(".acl-button-show").removeClass("selected");
142         $(this).toggleClass("selected");*/
143
144         this.set_deny($(this).parent().attr('id'));
145
146         return false;
147 }
148
149 ACL.prototype.set_allow = function(itemid){
150         type = itemid[0];
151         id     = parseInt(itemid.substr(1));
152         
153         switch(type){
154                 case "g":
155                         if (this.allow_gid.indexOf(id)<0){
156                                 this.allow_gid.push(id)
157                         }else {
158                                 this.allow_gid.remove(id);
159                         }
160                         if (this.deny_gid.indexOf(id)>=0) this.deny_gid.remove(id);
161                         break;
162                 case "c":
163                         if (this.allow_cid.indexOf(id)<0){
164                                 this.allow_cid.push(id)
165                                 if (this.data[id].forum=="1") this.add_mention(id);
166                         } else {
167                                 this.allow_cid.remove(id);
168                                 if (this.data[id].forum=="1") this.remove_mention(id);
169                         }
170                         if (this.deny_cid.indexOf(id)>=0) this.deny_cid.remove(id);                     
171                         break;
172         }
173         this.update_view();
174 }
175
176 ACL.prototype.set_deny = function(itemid){
177         type = itemid[0];
178         id     = parseInt(itemid.substr(1));
179         
180         switch(type){
181                 case "g":
182                         if (this.deny_gid.indexOf(id)<0){
183                                 this.deny_gid.push(id)
184                         } else {
185                                 this.deny_gid.remove(id);
186                         }
187                         if (this.allow_gid.indexOf(id)>=0) this.allow_gid.remove(id);
188                         break;
189                 case "c":
190                         if (this.data[id].forum=="1") this.remove_mention(id);
191                         if (this.deny_cid.indexOf(id)<0){
192                                 this.deny_cid.push(id)
193                         } else {
194                                 this.deny_cid.remove(id);
195                         }
196                         if (this.allow_cid.indexOf(id)>=0) this.allow_cid.remove(id);
197                         break;
198         }
199         this.update_view();
200 }
201
202 ACL.prototype.is_show_all = function() {
203         return (this.allow_gid.length==0 && this.allow_cid.length==0 &&
204                 this.deny_gid.length==0 && this.deny_cid.length==0);
205 }
206
207 ACL.prototype.update_view = function(){
208         if (this.is_show_all()){
209                         this.showall.addClass("selected");
210                         /* jot acl */
211                                 $('#jot-perms-icon').removeClass('lock').addClass('unlock');
212                                 $('#jot-public').show();
213                                 $('.profile-jot-net input').attr('disabled', false);                    
214                                 if(typeof editor != 'undefined' && editor != false) {
215                                         $('#profile-jot-desc').html(ispublic);
216                                 }
217                         
218         } else {
219                         this.showall.removeClass("selected");
220                         /* jot acl */
221                                 $('#jot-perms-icon').removeClass('unlock').addClass('lock');
222                                 $('#jot-public').hide();
223                                 $('.profile-jot-net input').attr('disabled', 'disabled');                       
224                                 $('#profile-jot-desc').html('&nbsp;');
225         }
226         $("#acl-list-content .acl-list-item").each(function(){
227                 $(this).removeClass("groupshow grouphide");
228         });
229         
230         $("#acl-list-content .acl-list-item").each(function(index, element){
231                 itemid = $(element).attr('id');
232                 type = itemid[0];
233                 id       = parseInt(itemid.substr(1));
234                 
235                 btshow = $(element).children(".acl-button-show").removeClass("selected");
236                 bthide = $(element).children(".acl-button-hide").removeClass("selected");       
237                 
238                 switch(type){
239                         case "g":
240                                 var uclass = "";
241                                 if (this.allow_gid.indexOf(id)>=0){
242                                         btshow.addClass("selected");
243                                         bthide.removeClass("selected");
244                                         uclass="groupshow";
245                                 }
246                                 if (this.deny_gid.indexOf(id)>=0){
247                                         btshow.removeClass("selected");
248                                         bthide.addClass("selected");
249                                         uclass="grouphide";
250                                 }
251                                 
252                                 $(this.group_uids[id]).each(function(i,v) {
253                                         if(uclass == "grouphide")
254                                                 $("#c"+v).removeClass("groupshow");
255                                         if(uclass != "") {
256                                                 var cls = $("#c"+v).attr('class');
257                                                 if( cls == undefined)
258                                                         return true;
259                                                 var hiding = cls.indexOf('grouphide');
260                                                 if(hiding == -1)
261                                                         $("#c"+v).addClass(uclass);
262                                         }
263                                 });
264                                 
265                                 break;
266                         case "c":
267                                 if (this.allow_cid.indexOf(id)>=0){
268                                         btshow.addClass("selected");
269                                         bthide.removeClass("selected");
270                                 }
271                                 if (this.deny_cid.indexOf(id)>=0){
272                                         btshow.removeClass("selected");
273                                         bthide.addClass("selected");
274                                 }                       
275                 }
276                 
277         }.bind(this));
278         
279 }
280
281
282 ACL.prototype.get = function(start,count, search){
283         var postdata = {
284                 start:start,
285                 count:count,
286                 search:search,
287         }
288         
289         $.ajax({
290                 type:'POST',
291                 url: this.url,
292                 data: postdata,
293                 dataType: 'json',
294                 success:this.populate.bind(this)
295         });
296 }
297
298 ACL.prototype.populate = function(data){
299         var height = Math.ceil(data.tot / this.nw) * 42;
300         this.list_content.height(height);
301         this.data = {};
302         $(data.items).each(function(index, item){
303                 html = "<div class='acl-list-item {4} {5} type{2}' title='{6}' id='{2}{3}'>"+this.item_tpl+"</div>";
304                 html = html.format(item.photo, item.name, item.type, item.id, (item.forum=='1'?'forum':''), item.network, item.link);
305                 if (item.uids!=undefined) this.group_uids[item.id] = item.uids;
306                 //console.log(html);
307                 this.list_content.append(html);
308                 this.data[item.id] = item;
309         }.bind(this));
310         $(".acl-list-item img[data-src]", this.list_content).each(function(i, el){
311                 // Add src attribute for images with a data-src attribute
312                 $(el).attr('src', $(el).data("src"));
313         });
314         
315         this.update_view();
316 }
317