<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */



importScript([
	"../../lib/jquery.js",
	"../../lib/jstree/jquery.jstree.js"
]);


TeamFilter = function(){};


TeamFilter.newInstance=function(field_name){
	var self=new TeamFilter();

	self.isFilter=true;
	self.name="Teams";
	self.disableUI=false;
	self.field_name=field_name;
	self.selectedEmails=[];

	Thread.run("get people", function(){
		//GET ALL PEOPLE
		var people=(yield (ESQuery.run({
			"from":"org_chart",
			"select":[
				{"name":"id", "value":"org_chart.id"},
				{"name":"name", "value":"org_chart.name"},
				{"name":"email", "value":"get(org_chart, \"email\")"},
				{"name":"manager", "value":"get(org_chart, \"manager\")"}
			]
		}))).list;

		var others={
			"email":"<EMAIL>",
			"name":"<NAME>",
			"id":"mail=<EMAIL>,"
		};
		people.push(others);

		people.push({
			"email":"<EMAIL>",
			"name":"Nobody",
			"id":"mail=<EMAIL>,"
		});

		people.push({
			"email":"<EMAIL>",
			"name":"Community",
			"id":"mail=<EMAIL>,"
		});



		//USE THIS TO SIMPLIFY THE TREE SELECTION
		self.managers={};
		people.forall(function(v, i){
			v.id=v.id.between("mail=", ",");

			if (v.manager==null){
				if (!["<EMAIL>", "<EMAIL>", "<EMAIL>"].contains(v.id)){
					v.manager="<EMAIL>";
				}//endif
			}else{
				v.manager = v.manager.between("mail=", ",");
			}//endif
			
			if (v.email==null) v.email=v.id;

			if (self.managers[v.id])
				Log.warning(v.id+" is not unique");
			self.managers[v.id]= v.manager;

			//USED BY TREE VIEW
			v.data=v.name;
			v.attr={"id":v.id};
		});

		var hier=Hierarchy.fromList({
			"from":people,
			"id_field":"id",
			"child_field":"children",
			"parent_field":"manager"
		});
		others.children.map(function(v, i){
			//PULL OUT THE TOP LEVEL 'PEOPLE' WITH CHILDREN
			if (v.children && v.manager=="<EMAIL>"){
				v.manager=null;
//				others.children.remove(v);
				hier.prepend(v);
			}//endif
			return v;
		});
		others.children.subtract(hier);

		self.injectHTML(hier);

		//JSTREE WILL NOT BE LOADED YET
		//HOPEFULLY IT WILL EXIST WHEN THE HEAD EXISTS
//		'#' + myid.replace(/(:|\.)/g,'\\$1');

		while($("#"+CNV.String2JQuery("<EMAIL>")).length==0){
			yield (Thread.sleep(100));
		}//while

		self.people=people;
	});
	return self;
};


TeamFilter.prototype.makeHTML=function(){
	return '<div id="teams" style="300px"></div>';
};


TeamFilter.prototype.getSummary=function(){
	var html = "Teams: ";
	var teams=Thread.runSynchronously(this.getSelectedPeople());
	if (teams.length == 0){
		html += "All";
	} else{
		html +=teams.map(function(p, i){return p.name;}).join(", ");
	}//endif
	return html;
};//method


TeamFilter.prototype.getSelectedPeople=function(){
	var self=this;

	while(!self.people){
		yield (Thread.sleep(100));
	}//while

	//CONVERT SELECTED LIST INTO PERSONS
	var selected = this.selectedEmails.map(function(email){
		for(var i = self.people.length; i--;){
			if (self.people[i].id==email) return self.people[i];
		}//for
	});
	yield selected;
};//method



//RETURN SOMETHING SIMPLE ENOUGH TO BE USED IN A URL
TeamFilter.prototype.getSimpleState=function(){
	if (this.selectedEmails.length==0) return undefined;
	return this.selectedEmails.join(",");
};

//RETURN SOMETHING SIMPLE ENOUGH TO BE USED IN A URL
TeamFilter.prototype.setSimpleState=function(value){
	if (!value || value==""){
		this.selectedEmails=[];
	}else{
		this.selectedEmails=value.split(",");
	}//endif
	this.refresh();
};


TeamFilter.prototype.makeFilter = function(field_name){
	if (this.selectedEmails.length == 0) return ESQuery.TrueFilter;
	field_name=nvl(field_name, this.field_name);
	if (field_name==null) return ESQuery.TrueFilter;

	var selected = Thread.runSynchronously(this.getSelectedPeople());
	if (selected.length == 0) return ESQuery.TrueFilter;

	//FIND BZ EMAILS THAT THE GIVEN LIST MAP TO
	var getEmail=function(list, children){
		children.forall(function(child, i){
			if (child.email)
				list.push(child.email);
			if (child.children)
				getEmail(list, child.children);
		});
	};//method
	var bzEmails=[];
	getEmail(bzEmails, selected);

	if (bzEmails.length==0) return ESQuery.TrueFilter;

	var output={"or":[]};

	if (bzEmails.contains("<EMAIL>")){
		bzEmails.remove("<EMAIL>");
		var allEmails=this.people.map(function(v, i){return v.email;});
		allEmails.push("<EMAIL>");
		
		output.or.push({"not":{"terms":Map.newInstance(field_name, allEmails)}});
	}//endif

	if (bzEmails.length!=0) output.or.push({"terms":Map.newInstance(field_name, bzEmails)});

	return output;
};//method



TeamFilter.prototype.refresh = function(){
	//FIND WHAT IS IN STATE, AND UPDATE STATE
	this.disableUI=true;
	var selected=yield(this.getSelectedPeople());

	var f=$('#teamList');
	f.jstree("deselect_all");
	selected.forall(function(p){
		f.jstree("select_node", "#"+CNV.String2JQuery(p.id));
		f.jstree("check_node", "#"+CNV.String2JQuery(p.id));
	});

	this.disableUI=false;
	yield null;
};


TeamFilter.prototype.injectHTML = function(hier){
	var html = '<div id="teamList"></div>';
	$("#teams").html(html);

	var self=this;

	$("#teamList").jstree({
		"json_data":{
			"data":hier
		},
		"themes":{
			"icons":false,
			"dots":false
		},
//		"checkbox":{
//			"two_state":true
//		},
		"plugins":[ "themes", "json_data", "ui", "checkbox" ]
	}).bind("change_state.jstree", function (e, data){
		if (self.disableUI) return;
	
		//WE NOW HAVE A RIDICULOUS NUMBER OF CHECKED ITEMS, REDUCE TO MINIMUM COVER
		var minCover=[];

		//FIRST MAKE A HASH OF CHECKED ITEMS
		var checked={};
		$(".jstree-checked").each(function(){
			checked[$(this).attr("id")]={};
		});

		//IF MANAGER IS CHECKED, THEN DO NOT INCLUDE
		forAllKey(checked, function(id, v, m){
			if (checked[self.managers[id]]) return;
			minCover.push(id);
		});
		minCover.sort();

		//HAS ANYTHING CHANGED?
		var hasChanged=false;
		if (self.selectedEmails.length!=minCover.length) hasChanged=true;
		if (!hasChanged) for(var i=minCover.length;i--;){
			if (minCover[i]!=self.selectedEmails[i]) hasChanged=true;
		}//for

		if (hasChanged){
			self.selectedEmails=minCover;
			GUI.refresh();
		}//endif
	});


};


<commit_msg>fix project dashboard<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */



importScript([
	"../../lib/jquery.js",
	"../../lib/jstree/jquery.jstree.js"
]);


TeamFilter = function(){};


TeamFilter.newInstance=function(field_name){
	var self=new TeamFilter();

	self.isFilter=true;
	self.name="Teams";
	self.disableUI=false;
	self.field_name=field_name;
	self.selectedEmails=[];

	Thread.run("get people", function(){
		//GET ALL PEOPLE
		var people=(yield (ESQuery.run({
			"from":"org_chart",
			"select":[
				{"name":"id", "value":"org_chart.id"},
				{"name":"name", "value":"org_chart.name"},
				{"name":"email", "value":"get(org_chart, \"email\")"},
				{"name":"manager", "value":"get(org_chart, \"manager\")"}
			]
		}))).list;

		var others={
			"email":"<EMAIL>",
			"name":"Other MoCo",
			"id":"mail=<EMAIL>,"
		};
		people.push(others);

		people.push({
			"email":"<EMAIL>",
			"name":"Nobody",
			"id":"mail=<EMAIL>,"
		});

		people.push({
			"email":"<EMAIL>",
			"name":"Community",
			"id":"mail=<EMAIL>,"
		});



		//USE THIS TO SIMPLIFY THE TREE SELECTION
		self.managers={};
		people.forall(function(v, i){
			v.id=v.id.between("mail=", ",");

			if (v.manager==null){
				if (!["<EMAIL>", "<EMAIL>", "<EMAIL>"].contains(v.id)){
					v.manager="<EMAIL>";
				}//endif
			}else{
				v.manager = v.manager.between("mail=", ",");
			}//endif
			
			if (v.email==null) v.email=v.id;

			if (self.managers[v.id])
				Log.warning(v.id+" is not unique");
			self.managers[v.id]= v.manager;

			//USED BY TREE VIEW
			v.data=v.name;
			v.attr={"id":v.id};
		});

		var hier=Hierarchy.fromList({
			"from":people,
			"id_field":"id",
			"child_field":"children",
			"parent_field":"manager"
		});
		others.children.map(function(v, i){
			//PULL OUT THE TOP LEVEL 'PEOPLE' WITH CHILDREN
			if (v.children && v.manager=="<EMAIL>"){
				v.manager=null;
				others.children.remove(v);
				hier.prepend(v);
			}//endif
			return v;
		});
		others.children.subtract(hier);

		self.injectHTML(hier);

		//JSTREE WILL NOT BE LOADED YET
		//HOPEFULLY IT WILL EXIST WHEN THE HEAD EXISTS
//		'#' + myid.replace(/(:|\.)/g,'\\$1');

		while($("#"+CNV.String2JQuery("<EMAIL>")).length==0){
			yield (Thread.sleep(100));
		}//while

		self.people=people;
	});
	return self;
};


TeamFilter.prototype.makeHTML=function(){
	return '<div id="teams" style="300px"></div>';
};


TeamFilter.prototype.getSummary=function(){
	var html = "Teams: ";
	var teams=Thread.runSynchronously(this.getSelectedPeople());
	if (teams.length == 0){
		html += "All";
	} else{
		html +=teams.map(function(p, i){return p.name;}).join(", ");
	}//endif
	return html;
};//method


TeamFilter.prototype.getSelectedPeople=function(){
	var self=this;

	while(!self.people){
		yield (Thread.sleep(100));
	}//while

	//CONVERT SELECTED LIST INTO PERSONS
	var selected = this.selectedEmails.map(function(email){
		for(var i = self.people.length; i--;){
			if (self.people[i].id==email) return self.people[i];
		}//for
	});
	yield selected;
};//method



//RETURN SOMETHING SIMPLE ENOUGH TO BE USED IN A URL
TeamFilter.prototype.getSimpleState=function(){
	if (this.selectedEmails.length==0) return undefined;
	return this.selectedEmails.join(",");
};

//RETURN SOMETHING SIMPLE ENOUGH TO BE USED IN A URL
TeamFilter.prototype.setSimpleState=function(value){
	if (!value || value==""){
		this.selectedEmails=[];
	}else{
		this.selectedEmails=value.split(",");
	}//endif
	this.refresh();
};


TeamFilter.prototype.makeFilter = function(field_name){
	if (this.selectedEmails.length == 0) return ESQuery.TrueFilter;
	field_name=nvl(field_name, this.field_name);
	if (field_name==null) return ESQuery.TrueFilter;

	var selected = Thread.runSynchronously(this.getSelectedPeople());
	if (selected.length == 0) return ESQuery.TrueFilter;

	//FIND BZ EMAILS THAT THE GIVEN LIST MAP TO
	var getEmail=function(list, children){
		children.forall(function(child, i){
			if (child.email)
				list.push(child.email);
			if (child.children)
				getEmail(list, child.children);
		});
	};//method
	var bzEmails=[];
	getEmail(bzEmails, selected);

	if (bzEmails.length==0) return ESQuery.TrueFilter;

	var output={"or":[]};

	if (bzEmails.contains("<EMAIL>")){
		bzEmails.remove("<EMAIL>");
		var allEmails=this.people.map(function(v, i){return v.email;});
		allEmails.push("<EMAIL>");
		
		output.or.push({"not":{"terms":Map.newInstance(field_name, allEmails)}});
	}//endif

	if (bzEmails.length!=0) output.or.push({"terms":Map.newInstance(field_name, bzEmails)});

	return output;
};//method



TeamFilter.prototype.refresh = function(){
	//FIND WHAT IS IN STATE, AND UPDATE STATE
	this.disableUI=true;
	var selected=yield(this.getSelectedPeople());

	var f=$('#teamList');
	f.jstree("deselect_all");
	selected.forall(function(p){
		f.jstree("select_node", "#"+CNV.String2JQuery(p.id));
		f.jstree("check_node", "#"+CNV.String2JQuery(p.id));
	});

	this.disableUI=false;
	yield null;
};


TeamFilter.prototype.injectHTML = function(hier){
	var html = '<div id="teamList"></div>';
	$("#teams").html(html);

	var self=this;

	$("#teamList").jstree({
		"json_data":{
			"data":hier
		},
		"themes":{
			"icons":false,
			"dots":false
		},
//		"checkbox":{
//			"two_state":true
//		},
		"plugins":[ "themes", "json_data", "ui", "checkbox" ]
	}).bind("change_state.jstree", function (e, data){
		if (self.disableUI) return;
	
		//WE NOW HAVE A RIDICULOUS NUMBER OF CHECKED ITEMS, REDUCE TO MINIMUM COVER
		var minCover=[];

		//FIRST MAKE A HASH OF CHECKED ITEMS
		var checked={};
		$(".jstree-checked").each(function(){
			checked[$(this).attr("id")]={};
		});

		//IF MANAGER IS CHECKED, THEN DO NOT INCLUDE
		forAllKey(checked, function(id, v, m){
			if (checked[self.managers[id]]) return;
			minCover.push(id);
		});
		minCover.sort();

		//HAS ANYTHING CHANGED?
		var hasChanged=false;
		if (self.selectedEmails.length!=minCover.length) hasChanged=true;
		if (!hasChanged) for(var i=minCover.length;i--;){
			if (minCover[i]!=self.selectedEmails[i]) hasChanged=true;
		}//for

		if (hasChanged){
			self.selectedEmails=minCover;
			GUI.refresh();
		}//endif
	});


};



{'fraction_non_alphanumeric': 0.20254574383452664, 'fraction_numerical': 0.0038186157517899762, 'mean_word_length': 4.110162601626016, 'pattern_counts': {'":': 72, '<': 42, '<?xml version=': 0, '>': 42, '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': '4910871', 'n_tokens_mistral': 5270, 'n_tokens_neox': 4638, 'n_words': 903}