(function(){ var app = angular.module("roster", []); /* Controller for Rosterlist */ app.controller("RosterController", function($scope){ /* Cache variables */ $scope.lsJid = localStorage.getItem("username").replace("@", "at"); $scope.lsRoster = localStorage.getObject($scope.lsJid + "_Roster") || {}; $scope.lsGroupState = "groupState" in $scope.lsRoster ? $scope.lsRoster.groupState : {}; $scope.contacts = []; $scope.groups = []; /* Dictionaries */ $scope.lookupgroups = {}; $scope.lookupjid = {}; $scope.initContacts = function(list){ document.getElementById("spinner").style.display = "block"; /* Sort groups alphabetically */ list.sort(groupnameCompare); $scope.contacts = list; /* Populate dictionaries */ for(var i = 0; i < $scope.contacts.length; i++){ $scope.lookupgroups[$scope.contacts[i].agroup] = $scope.contacts[i]; /* Sort jid by presence and alphabetically */ $scope.contacts[i].agroupitems.sort(jidAvalCompare); for(var j = 0; j < $scope.contacts[i].agroupitems.length; j++){ $scope.lookupjid[$scope.contacts[i].agroupitems[j].ajid] = $scope.contacts[i].agroupitems[j]; } } document.getElementById("spinner").style.display = "none"; $scope.$apply(); }; $scope.initGroups = function(list){ for(var i in list){ if(!("rosterGroup_"+i in $scope.lsGroupState)){ list[i] = true; $scope.lsGroupState["rosterGroup_" + i] = true; } else list[i] = $scope.lsGroupState["rosterGroup_" + i]; } $scope.groups = list; $scope.$apply(); }; $scope.deleteContact = function(jid){ $scope.lookupjid[jid].tombstone = true; $scope.$apply(); }; $scope.pushInPlace = function(element, array, comparer){ if(array === $scope.contacts){ dico = $scope.lookupgroups; key = "agroup"; } else { dico = $scope.lookupjid; key = "ajid"; } /* Put element in the right place inside array */ index = locationOf(element, array, comparer); array.splice(index, 0, element); /* Update dictionary from the appropriate index */ for(var i=index; i> 1; // >>1 = /2 var c = comparer(element, array[pivot]); if ((end - start) <= 1){ return (c == -1) ? pivot : pivot+1; } switch (c) { case -1: return locationOf(element, array, comparer, start, pivot); case 0: return pivot; case 1: return locationOf(element, array, comparer, pivot, end); }; }; /* Object comparison functions */ var groupnameCompare = function(a, b) { return a.agroup.localeCompare(b.agroup); }; /* Presence + alphabetical comparison */ var jidAvalCompare = function(a, b) { n = a.aval - b.aval; if(n == 0){ n = a.atruename.localeCompare(b.atruename); } return n ? n < 0 ? -1 : 1 : 0; }; var Roster = { init : function() { var search = document.querySelector('#rostersearch'); var roster = document.querySelector('#roster'); var rosterlist = document.querySelector('#rosterlist'); search.oninput = function(event) { if(search.value.length > 0) { movim_add_class(roster, 'search'); } else { movim_remove_class(roster, 'search'); } // We clear the old search var selector_clear = '#rosterlist div > li.found'; var li = document.querySelectorAll(selector_clear); for(i = 0; i < li.length; i++) { movim_remove_class(li.item(i), 'found'); } // We select the interesting li var selector = '#rosterlist div > li[title*="' + accentsTidy(search.value) + '"]:not(.subheader)'; li = document.querySelectorAll(selector); if(li != null && li.item(0) != null ){ var g = li.item(0).parentNode.querySelector('.subheader'); movim_add_class(g, 'found'); for(i = 0; i < li.length; i++) { if(li.item(i).parentNode.firstChild != g){ g = li.item(i).parentNode.querySelector('.subheader'); movim_add_class(g, 'found'); } movim_add_class(li.item(i), 'found'); } } }; }, refresh: function() { var items = document.querySelectorAll('#rosterlist div > li:not(.subheader)'); var i = 0; while(i < items.length) { items[i].onclick = function(){Roster.clickOnContact(this);}; i++; } }, reset: function(list) { for(i = 0; i < list.length; i++) { movim_remove_class(list[i], 'active'); } }, clearSearch: function() { var search = document.querySelector('#rostersearch'); if(search) { search.value = ''; search.oninput(); } }, setFound : function(jid) { document.querySelector('input[name=searchjid]').value = jid; }, clickOnContact : function(e) { Contact_ajaxGetContact(e.id); Contact_ajaxRefreshFeed(e.id); /*recalculated at each click*/ var it = document.querySelectorAll('#rosterlist div > li:not(.subheader)'); Roster.reset(it); Roster.clearSearch(); movim_add_class(e, 'active'); }, } MovimWebsocket.attach(function() { Roster_ajaxGetRoster(); Roster.refresh(); Notification.current('contacts'); }); movim_add_onload(function(){ Roster.init(); });