1 function ACL(backend_url, preset, automention, is_mobile){
3 this.url = backend_url;
4 this.automention = automention;
5 this.is_mobile = is_mobile;
10 if (preset == undefined) {
13 this.allow_cid = (preset[0] || []);
14 this.allow_gid = (preset[1] || []);
15 this.deny_cid = (preset[2] || []);
16 this.deny_gid = (preset[3] || []);
18 this.forumCache = null;
27 this.list_content = $("#acl-list-content");
28 this.item_tpl = unescape($(".acl-list-item[rel=acl-template]").html());
29 this.showall = $("#acl-showall");
31 if (preset.length==0) {
32 this.showall.addClass("selected");
36 this.showall.click(this.on_showall.bind(this));
37 $(document).on("click", ".acl-button-show", this.on_button_show.bind(this));
38 $(document).on("click", ".acl-button-hide", this.on_button_hide.bind(this));
39 $("#acl-search").keypress(this.on_search.bind(this));
40 $("#acl-wrapper").parents("form").submit(this.on_submit.bind(this));
42 /* add/remove mentions */
43 this.element = $("#profile-jot-text");
44 this.htmlelm = this.element.get()[0];
50 ACL.prototype.remove_mention = function(id) {
51 if (!this.automention) {
54 var nick = this.data[id].nick;
55 var addr = this.data[id].addr;
58 var searchText = "!" + addr + " ";
60 var searchText = "!" + nick + "+" + id + " ";
63 var start = this.element.val().indexOf(searchText);
67 var end = start + searchText.length;
68 this.element.setSelection(start, end).replaceSelectedText('').collapseSelection(false);
71 ACL.prototype.add_mention = function(id) {
72 if (!this.automention) {
75 var nick = this.data[id].nick;
76 var addr = this.data[id].addr;
79 var searchText = "!" + addr + " ";
81 var searchText = "!" + nick + "+" + id + " ";
84 if (this.element.val().indexOf( searchText) >= 0 ) {
87 this.element.val(searchText + this.element.val()).trigger('change');
90 ACL.prototype.on_submit = function(){
91 var aclfields = $("#acl-fields").html("");
92 $(this.allow_gid).each(function(i,v){
93 aclfields.append("<input type='hidden' name='group_allow[]' value='"+v+"'>");
95 $(this.allow_cid).each(function(i,v){
96 aclfields.append("<input type='hidden' name='contact_allow[]' value='"+v+"'>");
98 $(this.deny_gid).each(function(i,v){
99 aclfields.append("<input type='hidden' name='group_deny[]' value='"+v+"'>");
101 $(this.deny_cid).each(function(i,v){
102 aclfields.append("<input type='hidden' name='contact_deny[]' value='"+v+"'>");
106 ACL.prototype.search = function(){
107 var srcstr = $("#acl-search").val();
108 this.list_content.html("");
109 this.get(0,100, srcstr);
112 ACL.prototype.on_search = function(event){
113 if (this.kp_timer) clearTimeout(this.kp_timer);
114 this.kp_timer = setTimeout( this.search.bind(this), 1000);
117 ACL.prototype.on_showall = function(event){
118 event.preventDefault()
119 event.stopPropagation();
121 if (this.showall.hasClass("selected")){
124 this.showall.addClass("selected");
136 ACL.prototype.on_button_show = function(event){
137 event.preventDefault()
138 event.stopImmediatePropagation()
139 event.stopPropagation();
141 this.set_allow($(event.target).parent().attr('id'));
146 ACL.prototype.on_button_hide = function(event){
147 event.preventDefault()
148 event.stopImmediatePropagation()
149 event.stopPropagation();
151 this.set_deny($(event.target).parent().attr('id'));
156 ACL.prototype.set_allow = function(itemid) {
158 id = parseInt(itemid.substr(1));
162 if (this.allow_gid.indexOf(id) < 0) {
163 this.allow_gid.push(id);
165 this.allow_gid.remove(id);
167 if (this.deny_gid.indexOf(id) >= 0) {
168 this.deny_gid.remove(id);
172 if (this.allow_cid.indexOf(id) < 0){
173 this.allow_cid.push(id);
174 if (this.data[id].forum == "1") {
175 // If we have select already a forum,
176 // we need to remove the old one (because friendica does
177 // allow only one forum as receiver).
178 if (this.forumCache !== null && this.forumCache !== id) {
179 this.deselectCid(this.forumCache);
181 // Update the forum cache.
182 this.forumCache = id;
183 this.add_mention(id);
186 this.allow_cid.remove(id);
187 if (this.data[id].forum == "1") {
188 this.remove_mention(id);
191 if (this.deny_cid.indexOf(id) >=0 ) {
192 this.deny_cid.remove(id);
199 ACL.prototype.set_deny = function(itemid){
201 id = parseInt(itemid.substr(1));
205 if (this.deny_gid.indexOf(id)<0){
206 this.deny_gid.push(id)
208 this.deny_gid.remove(id);
210 if (this.allow_gid.indexOf(id)>=0) this.allow_gid.remove(id);
213 if (this.data[id].forum=="1") this.remove_mention(id);
214 if (this.deny_cid.indexOf(id)<0){
215 this.deny_cid.push(id)
217 this.deny_cid.remove(id);
219 if (this.allow_cid.indexOf(id)>=0) this.allow_cid.remove(id);
225 ACL.prototype.is_show_all = function() {
226 return (this.allow_gid.length==0 && this.allow_cid.length==0 &&
227 this.deny_gid.length==0 && this.deny_cid.length==0);
230 ACL.prototype.update_view = function(){
231 if (this.is_show_all()){
232 this.showall.addClass("selected");
234 $('#jot-perms-icon').removeClass('lock').addClass('unlock');
235 $('#jot-public').show();
236 $('.profile-jot-net input').attr('disabled', false);
237 if(typeof editor != 'undefined' && editor != false) {
238 $('#profile-jot-desc').html(ispublic);
242 this.showall.removeClass("selected");
244 $('#jot-perms-icon').removeClass('unlock').addClass('lock');
245 $('#jot-public').hide();
246 $('.profile-jot-net input').attr('disabled', 'disabled');
247 $('#profile-jot-desc').html(' ');
249 $("#acl-list-content .acl-list-item").each(function(){
250 $(this).removeClass("groupshow grouphide");
253 $("#acl-list-content .acl-list-item").each(function(index, element){
254 itemid = $(element).attr('id');
256 id = parseInt(itemid.substr(1));
258 btshow = $(element).children(".acl-button-show").removeClass("selected");
259 bthide = $(element).children(".acl-button-hide").removeClass("selected");
264 if (this.allow_gid.indexOf(id)>=0){
265 btshow.addClass("selected");
266 bthide.removeClass("selected");
269 if (this.deny_gid.indexOf(id)>=0){
270 btshow.removeClass("selected");
271 bthide.addClass("selected");
275 $(this.group_uids[id]).each(function(i,v) {
276 if(uclass == "grouphide")
277 $("#c"+v).removeClass("groupshow");
279 var cls = $("#c"+v).attr('class');
280 if( cls == undefined)
282 var hiding = cls.indexOf('grouphide');
284 $("#c"+v).addClass(uclass);
290 if (this.allow_cid.indexOf(id)>=0){
291 btshow.addClass("selected");
292 bthide.removeClass("selected");
294 if (this.deny_cid.indexOf(id)>=0){
295 btshow.removeClass("selected");
296 bthide.addClass("selected");
304 ACL.prototype.get = function(start,count, search){
316 success:this.populate.bind(this)
320 ACL.prototype.populate = function(data){
321 var height = Math.ceil(data.tot / this.nw) * 42;
322 this.list_content.height(height);
324 $(data.items).each(function(index, item) {
325 if (item.separator != undefined) {
326 html = "<hr class='clear'>";
328 html = "<div class='acl-list-item {4} {5} type{2}' title='{6}' id='{2}{3}'>"+this.item_tpl+"</div>";
329 html = html.format(item.photo, item.name, item.type, item.id, (item.forum=='1'?'forum':''), item.network, item.link);
330 if (item.uids != undefined) {
331 this.group_uids[item.id] = item.uids;
334 this.list_content.append(html);
335 this.data[item.id] = item;
337 $(".acl-list-item img[data-src]", this.list_content).each(function(i, el){
338 // Add src attribute for images with a data-src attribute
339 $(el).attr('src', $(el).data("src"));
346 * @brief Deselect previous selected contact.
348 * @param {int} id The contact ID.
351 ACL.prototype.deselectCid = function(id) {
352 if (this.allow_cid.indexOf(id) >= 0) {
353 this.allow_cid.remove(id);
355 if (this.deny_cid.indexOf(id) >=0 ) {
356 this.deny_cid.remove(id);
358 this.remove_mention(id);