1 // @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPLv3-or-later
2 function ACL(backend_url, preset, automention, is_mobile){
4 this.url = backend_url;
5 this.automention = automention;
6 this.is_mobile = is_mobile;
11 if (preset == undefined) {
14 this.allow_cid = (preset[0] || []);
15 this.allow_gid = (preset[1] || []);
16 this.deny_cid = (preset[2] || []);
17 this.deny_gid = (preset[3] || []);
19 this.forumCache = null;
28 this.list_content = $("#acl-list-content");
29 this.item_tpl = unescape($(".acl-list-item[rel=acl-template]").html());
30 this.showall = $("#acl-showall");
32 if (preset.length==0) {
33 this.showall.addClass("selected");
37 this.showall.click(this.on_showall.bind(this));
38 $(document).on("click", ".acl-button-show", this.on_button_show.bind(this));
39 $(document).on("click", ".acl-button-hide", this.on_button_hide.bind(this));
40 $("#acl-search").keypress(this.on_search.bind(this));
41 $("#acl-wrapper").parents("form").submit(this.on_submit.bind(this));
43 /* add/remove mentions */
44 this.element = $("#profile-jot-text");
45 this.htmlelm = this.element.get()[0];
48 ACL.prototype.remove_mention = function(id) {
49 if (!this.automention) {
52 var nick = this.data[id].nick;
53 var addr = this.data[id].addr;
56 var searchText = "!" + addr + " ";
58 var searchText = "!" + nick + "+" + id + " ";
61 var start = this.element.val().indexOf(searchText);
65 var end = start + searchText.length;
66 this.element.setSelection(start, end).replaceSelectedText('').collapseSelection(false);
69 ACL.prototype.add_mention = function(id) {
70 if (!this.automention) {
73 var nick = this.data[id].nick;
74 var addr = this.data[id].addr;
77 var searchText = "!" + addr + " ";
79 var searchText = "!" + nick + "+" + id + " ";
82 if (this.element.val().indexOf( searchText) >= 0 ) {
85 this.element.val(searchText + this.element.val()).trigger('change');
88 ACL.prototype.on_submit = function(){
89 var aclfields = $("#acl-fields").html("");
90 $(this.allow_gid).each(function(i,v){
91 aclfields.append("<input type='hidden' name='group_allow[]' value='"+v+"'>");
93 $(this.allow_cid).each(function(i,v){
94 aclfields.append("<input type='hidden' name='contact_allow[]' value='"+v+"'>");
96 $(this.deny_gid).each(function(i,v){
97 aclfields.append("<input type='hidden' name='group_deny[]' value='"+v+"'>");
99 $(this.deny_cid).each(function(i,v){
100 aclfields.append("<input type='hidden' name='contact_deny[]' value='"+v+"'>");
104 ACL.prototype.search = function(){
105 var srcstr = $("#acl-search").val();
106 this.list_content.html("");
107 this.get(0,100, srcstr);
110 ACL.prototype.on_search = function(event){
111 if (this.kp_timer) clearTimeout(this.kp_timer);
112 this.kp_timer = setTimeout( this.search.bind(this), 1000);
115 ACL.prototype.on_showall = function(event){
116 event.preventDefault()
117 event.stopPropagation();
119 if (this.showall.hasClass("selected")){
122 this.showall.addClass("selected");
134 ACL.prototype.on_button_show = function(event){
135 event.preventDefault()
136 event.stopImmediatePropagation()
137 event.stopPropagation();
139 this.set_allow($(event.target).parent().attr('id'));
144 ACL.prototype.on_button_hide = function(event){
145 event.preventDefault()
146 event.stopImmediatePropagation()
147 event.stopPropagation();
149 this.set_deny($(event.target).parent().attr('id'));
154 ACL.prototype.set_allow = function(itemid) {
156 id = parseInt(itemid.substr(1));
160 if (this.allow_gid.indexOf(id) < 0) {
161 this.allow_gid.push(id);
163 this.allow_gid.remove(id);
165 if (this.deny_gid.indexOf(id) >= 0) {
166 this.deny_gid.remove(id);
170 if (this.allow_cid.indexOf(id) < 0){
171 this.allow_cid.push(id);
172 if (this.data[id].forum == "1") {
173 // If we have select already a forum,
174 // we need to remove the old one (because friendica does
175 // allow only one forum as receiver).
176 if (this.forumCache !== null && this.forumCache !== id) {
177 this.deselectCid(this.forumCache);
179 // Update the forum cache.
180 this.forumCache = id;
181 this.add_mention(id);
184 this.allow_cid.remove(id);
185 if (this.data[id].forum == "1") {
186 this.remove_mention(id);
189 if (this.deny_cid.indexOf(id) >=0 ) {
190 this.deny_cid.remove(id);
197 ACL.prototype.set_deny = function(itemid){
199 id = parseInt(itemid.substr(1));
203 if (this.deny_gid.indexOf(id)<0){
204 this.deny_gid.push(id)
206 this.deny_gid.remove(id);
208 if (this.allow_gid.indexOf(id)>=0) this.allow_gid.remove(id);
211 if (this.data[id].forum=="1") this.remove_mention(id);
212 if (this.deny_cid.indexOf(id)<0){
213 this.deny_cid.push(id)
215 this.deny_cid.remove(id);
217 if (this.allow_cid.indexOf(id)>=0) this.allow_cid.remove(id);
223 ACL.prototype.is_show_all = function() {
224 return (this.allow_gid.length==0 && this.allow_cid.length==0 &&
225 this.deny_gid.length==0 && this.deny_cid.length==0);
228 ACL.prototype.update_view = function(){
229 if (this.is_show_all()){
230 this.showall.addClass("selected");
232 $('#jot-perms-icon').removeClass('lock').addClass('unlock');
233 $('#jot-public').show();
234 $('.profile-jot-net input').attr('disabled', false);
235 if(typeof editor != 'undefined' && editor != false) {
236 $('#profile-jot-desc').html(ispublic);
240 this.showall.removeClass("selected");
242 $('#jot-perms-icon').removeClass('unlock').addClass('lock');
243 $('#jot-public').hide();
244 $('.profile-jot-net input').attr('disabled', 'disabled');
245 $('#profile-jot-desc').html(' ');
247 $("#acl-list-content .acl-list-item").each(function(){
248 $(this).removeClass("groupshow grouphide");
251 $("#acl-list-content .acl-list-item").each(function(index, element){
252 itemid = $(element).attr('id');
254 id = parseInt(itemid.substr(1));
256 btshow = $(element).children(".acl-button-show").removeClass("selected");
257 bthide = $(element).children(".acl-button-hide").removeClass("selected");
262 if (this.allow_gid.indexOf(id)>=0){
263 btshow.addClass("selected");
264 bthide.removeClass("selected");
267 if (this.deny_gid.indexOf(id)>=0){
268 btshow.removeClass("selected");
269 bthide.addClass("selected");
273 $(this.group_uids[id]).each(function(i,v) {
274 if(uclass == "grouphide")
275 $("#c"+v).removeClass("groupshow");
277 var cls = $("#c"+v).attr('class');
278 if( cls == undefined)
280 var hiding = cls.indexOf('grouphide');
282 $("#c"+v).addClass(uclass);
288 if (this.allow_cid.indexOf(id)>=0){
289 btshow.addClass("selected");
290 bthide.removeClass("selected");
292 if (this.deny_cid.indexOf(id)>=0){
293 btshow.removeClass("selected");
294 bthide.addClass("selected");
302 ACL.prototype.get = function(start,count, search){
314 success:this.populate.bind(this)
318 ACL.prototype.populate = function(data){
319 var height = Math.ceil(data.tot / this.nw) * 42;
320 this.list_content.height(height);
322 $(data.items).each(function(index, item) {
323 if (item.separator != undefined) {
324 html = "<hr class='clear'>";
326 html = "<div class='acl-list-item {4} {5} type{2}' title='{6}' id='{2}{3}'>"+this.item_tpl+"</div>";
327 html = html.format(item.photo, item.name, item.type, item.id, (item.forum=='1'?'forum':''), item.network, item.link);
328 if (item.uids != undefined) {
329 this.group_uids[item.id] = item.uids;
332 this.list_content.append(html);
333 this.data[item.id] = item;
335 $(".acl-list-item img[data-src]", this.list_content).each(function(i, el){
336 // Add src attribute for images with a data-src attribute
337 $(el).attr('src', $(el).data("src"));
344 * @brief Deselect previous selected contact.
346 * @param {int} id The contact ID.
349 ACL.prototype.deselectCid = function(id) {
350 if (this.allow_cid.indexOf(id) >= 0) {
351 this.allow_cid.remove(id);
353 if (this.deny_cid.indexOf(id) >=0 ) {
354 this.deny_cid.remove(id);
356 this.remove_mention(id);