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];
47 ACL.prototype.remove_mention = function(id) {
48 if (!this.automention) {
51 var nick = this.data[id].nick;
52 var addr = this.data[id].addr;
55 var searchText = "!" + addr + " ";
57 var searchText = "!" + nick + "+" + id + " ";
60 var start = this.element.val().indexOf(searchText);
64 var end = start + searchText.length;
65 this.element.setSelection(start, end).replaceSelectedText('').collapseSelection(false);
68 ACL.prototype.add_mention = function(id) {
69 if (!this.automention) {
72 var nick = this.data[id].nick;
73 var addr = this.data[id].addr;
76 var searchText = "!" + addr + " ";
78 var searchText = "!" + nick + "+" + id + " ";
81 if (this.element.val().indexOf( searchText) >= 0 ) {
84 this.element.val(searchText + this.element.val()).trigger('change');
87 ACL.prototype.on_submit = function(){
88 var aclfields = $("#acl-fields").html("");
89 $(this.allow_gid).each(function(i,v){
90 aclfields.append("<input type='hidden' name='group_allow[]' value='"+v+"'>");
92 $(this.allow_cid).each(function(i,v){
93 aclfields.append("<input type='hidden' name='contact_allow[]' value='"+v+"'>");
95 $(this.deny_gid).each(function(i,v){
96 aclfields.append("<input type='hidden' name='group_deny[]' value='"+v+"'>");
98 $(this.deny_cid).each(function(i,v){
99 aclfields.append("<input type='hidden' name='contact_deny[]' value='"+v+"'>");
103 ACL.prototype.search = function(){
104 var srcstr = $("#acl-search").val();
105 this.list_content.html("");
106 this.get(0,100, srcstr);
109 ACL.prototype.on_search = function(event){
110 if (this.kp_timer) clearTimeout(this.kp_timer);
111 this.kp_timer = setTimeout( this.search.bind(this), 1000);
114 ACL.prototype.on_showall = function(event){
115 event.preventDefault()
116 event.stopPropagation();
118 if (this.showall.hasClass("selected")){
121 this.showall.addClass("selected");
133 ACL.prototype.on_button_show = function(event){
134 event.preventDefault()
135 event.stopImmediatePropagation()
136 event.stopPropagation();
138 this.set_allow($(event.target).parent().attr('id'));
143 ACL.prototype.on_button_hide = function(event){
144 event.preventDefault()
145 event.stopImmediatePropagation()
146 event.stopPropagation();
148 this.set_deny($(event.target).parent().attr('id'));
153 ACL.prototype.set_allow = function(itemid) {
155 id = parseInt(itemid.substr(1));
159 if (this.allow_gid.indexOf(id) < 0) {
160 this.allow_gid.push(id);
162 this.allow_gid.remove(id);
164 if (this.deny_gid.indexOf(id) >= 0) {
165 this.deny_gid.remove(id);
169 if (this.allow_cid.indexOf(id) < 0){
170 this.allow_cid.push(id);
171 if (this.data[id].forum == "1") {
172 // If we have select already a forum,
173 // we need to remove the old one (because friendica does
174 // allow only one forum as receiver).
175 if (this.forumCache !== null && this.forumCache !== id) {
176 this.deselectCid(this.forumCache);
178 // Update the forum cache.
179 this.forumCache = id;
180 this.add_mention(id);
183 this.allow_cid.remove(id);
184 if (this.data[id].forum == "1") {
185 this.remove_mention(id);
188 if (this.deny_cid.indexOf(id) >=0 ) {
189 this.deny_cid.remove(id);
196 ACL.prototype.set_deny = function(itemid){
198 id = parseInt(itemid.substr(1));
202 if (this.deny_gid.indexOf(id)<0){
203 this.deny_gid.push(id)
205 this.deny_gid.remove(id);
207 if (this.allow_gid.indexOf(id)>=0) this.allow_gid.remove(id);
210 if (this.data[id].forum=="1") this.remove_mention(id);
211 if (this.deny_cid.indexOf(id)<0){
212 this.deny_cid.push(id)
214 this.deny_cid.remove(id);
216 if (this.allow_cid.indexOf(id)>=0) this.allow_cid.remove(id);
222 ACL.prototype.is_show_all = function() {
223 return (this.allow_gid.length==0 && this.allow_cid.length==0 &&
224 this.deny_gid.length==0 && this.deny_cid.length==0);
227 ACL.prototype.update_view = function(){
228 if (this.is_show_all()){
229 this.showall.addClass("selected");
231 $('#jot-perms-icon').removeClass('lock').addClass('unlock');
232 $('#jot-public').show();
233 $('.profile-jot-net input').attr('disabled', false);
234 if(typeof editor != 'undefined' && editor != false) {
235 $('#profile-jot-desc').html(ispublic);
239 this.showall.removeClass("selected");
241 $('#jot-perms-icon').removeClass('unlock').addClass('lock');
242 $('#jot-public').hide();
243 $('.profile-jot-net input').attr('disabled', 'disabled');
244 $('#profile-jot-desc').html(' ');
246 $("#acl-list-content .acl-list-item").each(function(){
247 $(this).removeClass("groupshow grouphide");
250 $("#acl-list-content .acl-list-item").each(function(index, element){
251 itemid = $(element).attr('id');
253 id = parseInt(itemid.substr(1));
255 btshow = $(element).children(".acl-button-show").removeClass("selected");
256 bthide = $(element).children(".acl-button-hide").removeClass("selected");
261 if (this.allow_gid.indexOf(id)>=0){
262 btshow.addClass("selected");
263 bthide.removeClass("selected");
266 if (this.deny_gid.indexOf(id)>=0){
267 btshow.removeClass("selected");
268 bthide.addClass("selected");
272 $(this.group_uids[id]).each(function(i,v) {
273 if(uclass == "grouphide")
274 $("#c"+v).removeClass("groupshow");
276 var cls = $("#c"+v).attr('class');
277 if( cls == undefined)
279 var hiding = cls.indexOf('grouphide');
281 $("#c"+v).addClass(uclass);
287 if (this.allow_cid.indexOf(id)>=0){
288 btshow.addClass("selected");
289 bthide.removeClass("selected");
291 if (this.deny_cid.indexOf(id)>=0){
292 btshow.removeClass("selected");
293 bthide.addClass("selected");
301 ACL.prototype.get = function(start,count, search){
313 success:this.populate.bind(this)
317 ACL.prototype.populate = function(data){
318 var height = Math.ceil(data.tot / this.nw) * 42;
319 this.list_content.height(height);
321 $(data.items).each(function(index, item) {
322 if (item.separator != undefined) {
323 html = "<hr class='clear'>";
325 html = "<div class='acl-list-item {4} {5} type{2}' title='{6}' id='{2}{3}'>"+this.item_tpl+"</div>";
326 html = html.format(item.photo, item.name, item.type, item.id, (item.forum=='1'?'forum':''), item.network, item.link);
327 if (item.uids != undefined) {
328 this.group_uids[item.id] = item.uids;
331 this.list_content.append(html);
332 this.data[item.id] = item;
334 $(".acl-list-item img[data-src]", this.list_content).each(function(i, el){
335 // Add src attribute for images with a data-src attribute
336 $(el).attr('src', $(el).data("src"));
343 * @brief Deselect previous selected contact.
345 * @param {int} id The contact ID.
348 ACL.prototype.deselectCid = function(id) {
349 if (this.allow_cid.indexOf(id) >= 0) {
350 this.allow_cid.remove(id);
352 if (this.deny_cid.indexOf(id) >=0 ) {
353 this.deny_cid.remove(id);
355 this.remove_mention(id);