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