<commit_before>(function(){
    var app = angular.module("roster", []);

    /* Controller for Rostermenu */
    app.controller("RosterMenuController", function(){
        this.checkoutAddJid = function(event){
            if(event.key == "Enter")
                Roster_ajaxSearchContact(event.target.value);
        };
    });

    /* Controller for Rosterlist */
    app.controller("RosterController", function($scope){
        $scope.contacts = localStorage.getObject('rosterContacts') || [];
        $scope.groups = [];
        
        /* Dictionaries */
        $scope.lookupgroups = {};
        $scope.lookupjid = {};

        $scope.initContacts = function(list){
            if($scope.contacts.length == 0){
                for(var i = 0; i < list.length; i++){
                    /* New group */
                    if(!(list[i].groupname in $scope.lookupgroups)){
                        el = {
                            'agroup': list[i].groupname,
                            'agroupitems': [],
                            'tombstone': false,
                        };
                        $scope.pushInPlace(el, $scope.contacts, groupnameCompare);
                        
                        /* Create a reference in the localstorage for toggling */
                        localStorage.setItem("rosterGroup_"+list[0].groupname, true);
                    }
                    /* New jid */
                    if(!(list[i].jid in $scope.lookupjid)){
                        el = {
                            'ajid': list[i].jid,
                            'aval': list[i].value,
                            'ajiditems': [],
                            'tombstone': false,
                        };
                        $scope.pushInPlace(el, $scope.lookupgroups[list[i].groupname].agroupitems, jidAvalCompare);
                    }
                    /* New ressource (can't just push the whole set of same jid because there is no set) */
                    if(!$scope.isInJidItems(list[i].jid, list[i].ressource)){
                        $scope.pushInPlace(list[i], $scope.lookupjid[list[i].jid].ajiditems, ressourceCompare);
                    }
                }
            }
            /* Rebound from cache */
            else{
                for(var i = 0; i < $scope.contacts.length; i++){
                    if(!$scope.contacts[i].tombstone){
                        $scope.lookupgroups[$scope.contacts[i].agroup] = $scope.contacts[i];
                        for(var j = 0; j < $scope.contacts[i].agroupitems.length; j++){
                            if(!$scope.contacts[i].agroupitems[j].tombstone)
                                $scope.lookupjid[$scope.contacts[i].agroupitems[j].ajid] = $scope.contacts[i].agroupitems[j];
                        }
                    }
                }
            }
            
            $scope.$apply();
        };
        
        /* $scope.isInGroupItems is only for debugging purpose */
        $scope.isInGroupItems = function(group, jid){
            l = $scope.lookupgroups[group].agroupitems.length;
            for(var i = 0; i < l; i++){
                if($scope.lookupgroups[group].agroupitems[i].ajid == jid)
                    return i;
            }
            return false;
        };
        
        $scope.isInJidItems = function(jid, ressource){
            l = $scope.lookupjid[jid].ajiditems.length;
            for(var i = 0; i < l; i++){
                if($scope.lookupjid[jid].ajiditems[i].ressource == ressource)
                    return true;
            }
            return false;
        };
        
        $scope.initGroups = function(list){
            for (var i in list){
                if(localStorage.getItem("rosterGroup_"+i) == null){
                    list[i] = true;
                    localStorage.setItem("rosterGroup_"+i, true);
                }
                else list[i] = localStorage.getItem("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); 
            //if(comparer == groupnameCompare){
            array.splice(index, 0, element);
            
            /* Update dictionnary from the appropriate index */
            for(var i=index; i<array.length; i++){
                dico[array[i][key]] = array[i];
                //if(comparer == groupnameCompare){
            }
        };

        $scope.updateContact = function(list){
            console.log(list);
            if($scope.contacts === null) $scope.contacts = [];
            /* Group change */
            if((list[0].jid in $scope.lookupjid) 
                && !($scope.lookupjid[list[0].jid].ajiditems[0].groupname == list[0].groupname)){
                /* Kill jid from old location or whole group if it's the only jid */
                oldgroupname = $scope.lookupjid[list[0].jid].ajiditems[0].groupname;
                if($scope.lookupgroups[oldgroupname].agroupitems.length == 1)
                    $scope.lookupgroups[oldgroupname].tombstone = true;
                else
                    $scope.lookupjid[list[0].jid].tombstone = true;
                
                console.log("Groupchange: "+oldgroupname+"["+$scope.lookupgroups[oldgroupname].agroupitems.length+"] => "+list[0].groupname);
            }
            /* New group is not in the list */
            if(!(list[0].groupname in $scope.lookupgroups)) {
                console.log("Creation of new group: "+list[0].groupname);
                /* Create group */
                el = {
                    'agroup': list[0].groupname,
                    'agroupitems': [],
                    'tombstone': false,
                };
                $scope.pushInPlace(el, $scope.contacts, groupnameCompare);
                /* Reference in the localstorage for toggling */
                localStorage.setItem("rosterGroup_"+list[0].groupname, true);
            }
                
            /* Jid is in the list and no group change */
            if(list[0].jid in $scope.lookupjid 
                && ($scope.lookupjid[list[0].jid].ajiditems[0].groupname == list[0].groupname)){
                console.log("Change value of "+list[0].jid+" in "+list[0].groupname+" group.");
                //$scope.lookupgroups[list[0].groupname].agroupitems[gi].ajiditems = list
                //var gi = $scope.isInGroupItems(list[0].groupname, list[0].jid);
                $scope.lookupjid[list[0].jid].aval = list[0].value;
                $scope.lookupjid[list[0].jid].ajiditems = list;<commit_msg>- Fixed roster caching + changing group<commit_after>(function(){
    var app = angular.module("roster", []);

    /* Controller for Rostermenu */
    app.controller("RosterMenuController", function(){
        this.checkoutAddJid = function(event){
            if(event.key == "Enter")
                Roster_ajaxSearchContact(event.target.value);
        };
    });

    /* Controller for Rosterlist */
    app.controller("RosterController", function($scope){
        $scope.contacts = localStorage.getObject('rosterContacts') || [];
        $scope.groups = [];
        
        /* Dictionaries */
        $scope.lookupgroups = {};
        $scope.lookupjid = {};

        $scope.initContacts = function(list){
            if($scope.contacts.length == 0){
                for(var i = 0; i < list.length; i++){
                    /* New group */
                    if(!(list[i].groupname in $scope.lookupgroups)){
                        el = {
                            'agroup': list[i].groupname,
                            'agroupitems': [],
                            'tombstone': false,
                        };
                        $scope.pushInPlace(el, $scope.contacts, groupnameCompare);
                        
                        /* Create a reference in the localstorage for toggling */
                        localStorage.setItem("rosterGroup_"+list[0].groupname, true);
                    }
                    /* New jid */
                    if(!(list[i].jid in $scope.lookupjid)){
                        el = {
                            'ajid': list[i].jid,
                            'aval': list[i].value,
                            'ajiditems': [],
                            'tombstone': false,
                        };
                        $scope.pushInPlace(el, $scope.lookupgroups[list[i].groupname].agroupitems, jidAvalCompare);
                    }
                    /* New ressource (can't just push the whole set of same jid because there is no set) */
                    if(!$scope.isInJidItems(list[i].jid, list[i].ressource)){
                        $scope.pushInPlace(list[i], $scope.lookupjid[list[i].jid].ajiditems, ressourceCompare);
                    }
                }
            }
            /* Rebound from cache */
            else{
                for(var i = 0; i < $scope.contacts.length; i++){
                    if(!$scope.contacts[i].tombstone){
                        $scope.lookupgroups[$scope.contacts[i].agroup] = $scope.contacts[i];
                        for(var j = 0; j < $scope.contacts[i].agroupitems.length; j++){
                            if(!$scope.contacts[i].agroupitems[j].tombstone)
                                $scope.lookupjid[$scope.contacts[i].agroupitems[j].ajid] = $scope.contacts[i].agroupitems[j];
                            else
                                $scope.contacts[i].agroupitems.splice(j, 1);                        }
                    }
                    else
                        $scope.contacts.splice(i, 1);
                }
            }
            
            $scope.$apply();
        };
        
        /* $scope.isInGroupItems is only for debugging purpose */
        $scope.isInGroupItems = function(group, jid){
            l = $scope.lookupgroups[group].agroupitems.length;
            for(var i = 0; i < l; i++){
                if($scope.lookupgroups[group].agroupitems[i].ajid == jid)
                    return i;
            }
            return false;
        };
        
        $scope.isInJidItems = function(jid, ressource){
            l = $scope.lookupjid[jid].ajiditems.length;
            for(var i = 0; i < l; i++){
                if($scope.lookupjid[jid].ajiditems[i].ressource == ressource)
                    return true;
            }
            return false;
        };
        
        $scope.initGroups = function(list){
            for (var i in list){
                if(localStorage.getItem("rosterGroup_"+i) == null){
                    list[i] = true;
                    localStorage.setItem("rosterGroup_"+i, true);
                }
                else list[i] = localStorage.getItem("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); 
            //if(comparer == groupnameCompare){
            array.splice(index, 0, element);
            
            /* Update dictionnary from the appropriate index */
            for(var i=index; i<array.length; i++){
                dico[array[i][key]] = array[i];
                //if(comparer == groupnameCompare){
            }
        };

        $scope.updateContact = function(list){
            console.log(list);
            if($scope.contacts === null) $scope.contacts = [];
            /* Group change */
            if((list[0].jid in $scope.lookupjid) 
                && !($scope.lookupjid[list[0].jid].ajiditems[0].groupname == list[0].groupname)){
                /* Kill jid from old location or whole group if it's the only jid */
                oldgroupname = $scope.lookupjid[list[0].jid].ajiditems[0].groupname;
                if($scope.lookupgroups[oldgroupname].agroupitems.length == 1)
                    $scope.lookupgroups[oldgroupname].tombstone = true;
                else
                    $scope.lookupjid[list[0].jid].tombstone = true;
                
                console.log("Removed jid from oldgroup : "+oldgroupname+"["+$scope.lookupgroups[oldgroupname].agroupitems.length+"] => "+list[0].groupname);
            }
            /* New group is not in the list */
            if(!(list[0].groupname in $scope.lookupgroups)) {
                console.log("Creation of new group: "+list[0].groupname);
                /* Create group */
                el = {
                    'agroup': list[0].groupname,
                    'agroupitems': [],
                    'tombstone': false,
                };
                $scope.pushInPlace(el, $scope.contacts, groupnameCompare);
                /* Reference in the localstorage for toggling */
                localStorage.setItem("rosterGroup_"+list[0].groupname, true);
            }
            /* New Group has been killed before */
            /*else if($scope.lookupgroups[list[0].groupname].tombstone) {
                console.log("Tombstone is true for "+list[0].groupname);
                $scope.lookupgroups[list[0].groupname].tombstone = false;
                $scope.lookupgroups[list[0].groupname].agroupitems = [];
            }*/
                
            /* Jid is in the list and no group change */
            if(list[0].jid in $scope.lookupjid 
                && ($scope.lookupjid[list[0].jid].ajiditems[0].groupname == list[0].groupname)){
                console.log("Change value of "+list[0].jid+" in "+list[0].groupname+" group.");
                //$scope.lookupgroups[list[0].groupname].agroupitems[gi].ajiditems = list
                //var gi = $scope.isInGroupItems(list[0].groupname, list[0].jid);
                $scope.lookupjid[list[0].jid].aval = list[0].value;
                $scope.lookupjid[list[0].jid].ajiditems = list;
{'fraction_non_alphanumeric': 0.13314086209211762, 'fraction_numerical': 0.004834161407278098, 'mean_word_length': 1.36053882725832, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2617234', 'n_tokens_mistral': 4156, 'n_tokens_neox': 3964, 'n_words': 693}