<commit_before>/**
 * This file is part of the MonVoisinFaitDuBio project.
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright Copyright (c) 2016 <NAME> - <EMAIL>
 * @license    MIT License
 * @Last Modified time: 2016-12-13
 */
import { AppModule, AppStates, AppModes } from "../app.module";
import { BiopenMarker } from "../components/map/biopen-marker.component";
import { OptionValue, CategoryValue, Option, Category } from "./classes";

declare let App : AppModule;
declare var $;
declare let Twig : any;
declare let biopen_twigJs_elementInfo : any;



export class Element 
{	
	readonly id : string;
	readonly name : string;
	readonly position : L.LatLng;
	readonly address : string;
	readonly description : string;
	readonly tel : string;
	readonly webSite : string;
	readonly mail : string;
	readonly openHours : any;
	readonly openHoursDays : string[] = [];
	readonly openHoursMoreInfos : any;
	readonly mainOptionOwnerIds : number[] = [];

	optionsValues : OptionValue[] = [];
	optionValuesByCatgeory : OptionValue[][] = [];

	colorOptionId : number;
	private iconsToDisplay : OptionValue[] = [];
	private optionTree : OptionValue;

	formatedOpenHours_ = null;

	distance : number;

	isInitialized_ :boolean = false;

	// for elements module algorithms
	isDisplayed :boolean = false;

	isVisible_ : boolean = false;
	isInElementList : boolean= false;

	//TODO
	biopenMarker_ : BiopenMarker = null;
	htmlRepresentation_ = '';

	productsToDisplay_ : any = {};

	starChoiceForRepresentation = '';
	isShownAlone : boolean= false;

	isFavorite : boolean= false;

	needToBeUpdatedWhenShown : boolean = true;

	constructor(elementJson : any)
	{
		this.id = elementJson.id;
		this.name = elementJson.name;
		this.position = L.latLng(elementJson.coordinates.lat, elementJson.coordinates.lng);
		this.address = elementJson.address;
		this.description = elementJson.description || '';
		this.tel = elementJson.tel ? elementJson.tel.replace(/(.{2})(?!$)/g,"$1 ") : '';	
		this.webSite = elementJson.webSite;
		this.mail = elementJson.mail;
		this.openHours = elementJson.openHours;
		this.openHoursMoreInfos =  elementJson.openHoursMoreInfos;

		// initialize formated open hours
		this.getFormatedOpenHours();

		let newOption : OptionValue, ownerId : number;
		for (let optionValueJson of elementJson.optionValues)
		{
			newOption = new OptionValue(optionValueJson);

			//ownerId = newOption.option.mainOwnerId;
			if (newOption.option.isMainOption()) this.mainOptionOwnerIds.push(newOption.optionId);
			//if (this.mainOptionOwnerIds.indexOf(ownerId) == -1) 

			this.optionsValues.push(newOption);

			// put options value in specific easy accessible array for better performance
			if (!this.optionValuesByCatgeory[newOption.option.ownerId]) this.optionValuesByCatgeory[newOption.option.ownerId] = [];
			this.optionValuesByCatgeory[newOption.option.ownerId].push(newOption);
		}

		this.distance = elementJson.distance ? Math.round(elementJson.distance) : null;	

	}	

	getOptionValueByCategoryId($categoryId)
	{
		return this.optionValuesByCatgeory[$categoryId] || [];
	}	

	initialize() 
	{		
		this.createOptionsTree();
		this.updateIconsToDisplay();

		this.biopenMarker_ = new BiopenMarker(this.id, this.position);
		this.isInitialized_ = true;	
	}

	show() 
	{		
		if (!this.isInitialized_) this.initialize();	
		//this.update();
		//this.biopenMarker_.update();
		this.biopenMarker_.show();
		this.isVisible_ = true;		
	};

	hide() 
	{		
		if (this.biopenMarker_) this.biopenMarker_.hide();
		this.isVisible_ = false;
		// unbound events (click etc...)?
		//if (constellationMode) $('#directory-content-list #element-info-'+this.id).hide();
	};

	update($force : boolean = false)
	{
		//console.log("marker update needToBeUpdated", this.needToBeUpdatedWhenShown);
		if (this.needToBeUpdatedWhenShown || App.mode == AppModes.List || $force)
		{
			this.updateIconsToDisplay();

			let optionValuesToUpdate = this.getCurrOptionsValues().filter( (optionValue) => optionValue.isFilledByFilters);
			optionValuesToUpdate.push(this.getCurrMainOptionValue());
			for(let optionValue of optionValuesToUpdate) this.updateOwnerColor(optionValue);

			this.colorOptionId = this.iconsToDisplay.length > 0 && this.getIconsToDisplay()[0] ? this.getIconsToDisplay()[0].colorOptionId : null;	

			if (this.marker) this.marker.update();
			this.needToBeUpdatedWhenShown = false;
		}		
	}

	updateOwnerColor($optionValue : OptionValue)
	{
		//console.log("updateOwnerColor", $optionValue.option.name);
		if ($optionValue.option.useColorForMarker)
		{<commit_msg>Fixs optionValue undefined bug<commit_after>/**
 * This file is part of the MonVoisinFaitDuBio project.
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright Copyright (c) 2016 <NAME> - <EMAIL>
 * @license    MIT License
 * @Last Modified time: 2016-12-13
 */
import { AppModule, AppStates, AppModes } from "../app.module";
import { BiopenMarker } from "../components/map/biopen-marker.component";
import { OptionValue, CategoryValue, Option, Category } from "./classes";

declare let App : AppModule;
declare var $;
declare let Twig : any;
declare let biopen_twigJs_elementInfo : any;



export class Element 
{	
	readonly id : string;
	readonly name : string;
	readonly position : L.LatLng;
	readonly address : string;
	readonly description : string;
	readonly tel : string;
	readonly webSite : string;
	readonly mail : string;
	readonly openHours : any;
	readonly openHoursDays : string[] = [];
	readonly openHoursMoreInfos : any;
	readonly mainOptionOwnerIds : number[] = [];

	optionsValues : OptionValue[] = [];
	optionValuesByCatgeory : OptionValue[][] = [];

	colorOptionId : number;
	private iconsToDisplay : OptionValue[] = [];
	private optionTree : OptionValue;

	formatedOpenHours_ = null;

	distance : number;

	isInitialized_ :boolean = false;

	// for elements module algorithms
	isDisplayed :boolean = false;

	isVisible_ : boolean = false;
	isInElementList : boolean= false;

	//TODO
	biopenMarker_ : BiopenMarker = null;
	htmlRepresentation_ = '';

	productsToDisplay_ : any = {};

	starChoiceForRepresentation = '';
	isShownAlone : boolean= false;

	isFavorite : boolean= false;

	needToBeUpdatedWhenShown : boolean = true;

	constructor(elementJson : any)
	{
		this.id = elementJson.id;
		this.name = elementJson.name;
		this.position = L.latLng(elementJson.coordinates.lat, elementJson.coordinates.lng);
		this.address = elementJson.address;
		this.description = elementJson.description || '';
		this.tel = elementJson.tel ? elementJson.tel.replace(/(.{2})(?!$)/g,"$1 ") : '';	
		this.webSite = elementJson.webSite;
		this.mail = elementJson.mail;
		this.openHours = elementJson.openHours;
		this.openHoursMoreInfos =  elementJson.openHoursMoreInfos;

		// initialize formated open hours
		this.getFormatedOpenHours();

		let newOption : OptionValue, ownerId : number;
		for (let optionValueJson of elementJson.optionValues)
		{
			newOption = new OptionValue(optionValueJson);

			//ownerId = newOption.option.mainOwnerId;
			if (newOption.option.isMainOption()) this.mainOptionOwnerIds.push(newOption.optionId);
			//if (this.mainOptionOwnerIds.indexOf(ownerId) == -1) 

			this.optionsValues.push(newOption);

			// put options value in specific easy accessible array for better performance
			if (!this.optionValuesByCatgeory[newOption.option.ownerId]) this.optionValuesByCatgeory[newOption.option.ownerId] = [];
			this.optionValuesByCatgeory[newOption.option.ownerId].push(newOption);
		}

		this.distance = elementJson.distance ? Math.round(elementJson.distance) : null;	

	}	

	getOptionValueByCategoryId($categoryId)
	{
		return this.optionValuesByCatgeory[$categoryId] || [];
	}	

	initialize() 
	{		
		this.createOptionsTree();
		this.updateIconsToDisplay();

		this.biopenMarker_ = new BiopenMarker(this.id, this.position);
		this.isInitialized_ = true;	
	}

	show() 
	{		
		if (!this.isInitialized_) this.initialize();	
		//this.update();
		//this.biopenMarker_.update();
		this.biopenMarker_.show();
		this.isVisible_ = true;		
	};

	hide() 
	{		
		if (this.biopenMarker_) this.biopenMarker_.hide();
		this.isVisible_ = false;
		// unbound events (click etc...)?
		//if (constellationMode) $('#directory-content-list #element-info-'+this.id).hide();
	};

	update($force : boolean = false)
	{
		//console.log("marker update needToBeUpdated", this.needToBeUpdatedWhenShown);
		if (this.needToBeUpdatedWhenShown || App.mode == AppModes.List || $force)
		{
			this.updateIconsToDisplay();

			let optionValuesToUpdate = this.getCurrOptionsValues().filter( (optionValue) => optionValue.isFilledByFilters);
			optionValuesToUpdate.push(this.getCurrMainOptionValue());
			for(let optionValue of optionValuesToUpdate) this.updateOwnerColor(optionValue);

			this.colorOptionId = this.iconsToDisplay.length > 0 && this.getIconsToDisplay()[0] ? this.getIconsToDisplay()[0].colorOptionId : null;	

			if (this.marker) this.marker.update();
			this.needToBeUpdatedWhenShown = false;
		}		
	}

	updateOwnerColor($optionValue : OptionValue)
	{
		if (!$optionValue) return;
		//console.log("updateOwnerColor", $optionValue.option.name);
		if ($optionValue.option.useColorForMarker)
		{
{'fraction_non_alphanumeric': 0.1253210616438356, 'fraction_numerical': 0.003852739726027397, 'mean_word_length': 4.532859680284192, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '1795901', 'n_tokens_mistral': 3206, 'n_tokens_neox': 2925, 'n_words': 618}