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) 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] || []);
24 this.list_content = $("#acl-list-content");
25 this.item_tpl = unescape($(".acl-list-item[rel=acl-template]").html());
26 this.showall = $("#acl-showall");
28 if (preset.length==0) this.showall.addClass("selected");
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));
37 /* add/remove mentions */
38 this.element = $("#profile-jot-text");
39 this.htmlelm = this.element.get()[0];
45 ACL.prototype.remove_mention = function(id) {
46 if (!this.automention) {
49 var nick = this.data[id].nick;
50 var addr = this.data[id].addr;
53 var searchText = "@" + addr + " ";
55 var searchText = "@" + nick + "+" + id + " ";
58 var start = this.element.val().indexOf(searchText);
62 var end = start + searchText.length;
63 this.element.setSelection(start, end).replaceSelectedText('').collapseSelection(false);
66 ACL.prototype.add_mention = function(id) {
67 if (!this.automention) {
70 var nick = this.data[id].nick;
71 var addr = this.data[id].addr;
74 var searchText = "@" + addr + " ";
76 var searchText = "@" + nick + "+" + id + " ";
79 if (this.element.val().indexOf( searchText) >= 0 ) {
82 this.element.val(searchText + this.element.val()).trigger('change');
85 ACL.prototype.on_submit = function(){
86 var aclfields = $("#acl-fields").html("");
87 $(this.allow_gid).each(function(i,v){
88 aclfields.append("<input type='hidden' name='group_allow[]' value='"+v+"'>");
90 $(this.allow_cid).each(function(i,v){
91 aclfields.append("<input type='hidden' name='contact_allow[]' value='"+v+"'>");
93 $(this.deny_gid).each(function(i,v){
94 aclfields.append("<input type='hidden' name='group_deny[]' value='"+v+"'>");
96 $(this.deny_cid).each(function(i,v){
97 aclfields.append("<input type='hidden' name='contact_deny[]' value='"+v+"'>");
101 ACL.prototype.search = function(){
102 var srcstr = $("#acl-search").val();
103 this.list_content.html("");
104 this.get(0,100, srcstr);
107 ACL.prototype.on_search = function(event){
108 if (this.kp_timer) clearTimeout(this.kp_timer);
109 this.kp_timer = setTimeout( this.search.bind(this), 1000);
112 ACL.prototype.on_showall = function(event){
113 event.preventDefault()
114 event.stopPropagation();
116 if (this.showall.hasClass("selected")){
119 this.showall.addClass("selected");
131 ACL.prototype.on_button_show = function(event){
132 event.preventDefault()
133 event.stopImmediatePropagation()
134 event.stopPropagation();
136 this.set_allow($(event.target).parent().attr('id'));
140 ACL.prototype.on_button_hide = function(event){
141 event.preventDefault()
142 event.stopImmediatePropagation()
143 event.stopPropagation();
145 this.set_deny($(event.target).parent().attr('id'));
150 ACL.prototype.set_allow = function(itemid){
152 id = parseInt(itemid.substr(1));
156 if (this.allow_gid.indexOf(id)<0){
157 this.allow_gid.push(id)
159 this.allow_gid.remove(id);
161 if (this.deny_gid.indexOf(id)>=0) this.deny_gid.remove(id);
164 if (this.allow_cid.indexOf(id)<0){
165 this.allow_cid.push(id)
166 if (this.data[id].forum=="1") this.add_mention(id);
168 this.allow_cid.remove(id);
169 if (this.data[id].forum=="1") this.remove_mention(id);
171 if (this.deny_cid.indexOf(id)>=0) this.deny_cid.remove(id);
177 ACL.prototype.set_deny = function(itemid){
179 id = parseInt(itemid.substr(1));
183 if (this.deny_gid.indexOf(id)<0){
184 this.deny_gid.push(id)
186 this.deny_gid.remove(id);
188 if (this.allow_gid.indexOf(id)>=0) this.allow_gid.remove(id);
191 if (this.data[id].forum=="1") this.remove_mention(id);
192 if (this.deny_cid.indexOf(id)<0){
193 this.deny_cid.push(id)
195 this.deny_cid.remove(id);
197 if (this.allow_cid.indexOf(id)>=0) this.allow_cid.remove(id);
203 ACL.prototype.is_show_all = function() {
204 return (this.allow_gid.length==0 && this.allow_cid.length==0 &&
205 this.deny_gid.length==0 && this.deny_cid.length==0);
208 ACL.prototype.update_view = function(){
209 if (this.is_show_all()){
210 this.showall.addClass("selected");
212 $('#jot-perms-icon').removeClass('lock').addClass('unlock');
213 $('#jot-public').show();
214 $('.profile-jot-net input').attr('disabled', false);
215 if(typeof editor != 'undefined' && editor != false) {
216 $('#profile-jot-desc').html(ispublic);
220 this.showall.removeClass("selected");
222 $('#jot-perms-icon').removeClass('unlock').addClass('lock');
223 $('#jot-public').hide();
224 $('.profile-jot-net input').attr('disabled', 'disabled');
225 $('#profile-jot-desc').html(' ');
227 $("#acl-list-content .acl-list-item").each(function(){
228 $(this).removeClass("groupshow grouphide");
231 $("#acl-list-content .acl-list-item").each(function(index, element){
232 itemid = $(element).attr('id');
234 id = parseInt(itemid.substr(1));
236 btshow = $(element).children(".acl-button-show").removeClass("selected");
237 bthide = $(element).children(".acl-button-hide").removeClass("selected");
242 if (this.allow_gid.indexOf(id)>=0){
243 btshow.addClass("selected");
244 bthide.removeClass("selected");
247 if (this.deny_gid.indexOf(id)>=0){
248 btshow.removeClass("selected");
249 bthide.addClass("selected");
253 $(this.group_uids[id]).each(function(i,v) {
254 if(uclass == "grouphide")
255 $("#c"+v).removeClass("groupshow");
257 var cls = $("#c"+v).attr('class');
258 if( cls == undefined)
260 var hiding = cls.indexOf('grouphide');
262 $("#c"+v).addClass(uclass);
268 if (this.allow_cid.indexOf(id)>=0){
269 btshow.addClass("selected");
270 bthide.removeClass("selected");
272 if (this.deny_cid.indexOf(id)>=0){
273 btshow.removeClass("selected");
274 bthide.addClass("selected");
283 ACL.prototype.get = function(start,count, search){
295 success:this.populate.bind(this)
299 ACL.prototype.populate = function(data){
300 var height = Math.ceil(data.tot / this.nw) * 42;
301 this.list_content.height(height);
303 $(data.items).each(function(index, item) {
304 if (item.separator != undefined) {
305 html = "<hr class='clear'>";
307 html = "<div class='acl-list-item {4} {5} type{2}' title='{6}' id='{2}{3}'>"+this.item_tpl+"</div>";
308 html = html.format(item.photo, item.name, item.type, item.id, (item.forum=='1'?'forum':''), item.network, item.link);
309 if (item.uids != undefined) {
310 this.group_uids[item.id] = item.uids;
313 this.list_content.append(html);
314 this.data[item.id] = item;
316 $(".acl-list-item img[data-src]", this.list_content).each(function(i, el){
317 // Add src attribute for images with a data-src attribute
318 $(el).attr('src', $(el).data("src"));