{"size": 2875, "ext": "js", "max_stars_repo_path": "bitrix/modules/landing/install/js/landing/ui/card/iconoptionscard/src/iconoptionscard.js", "max_stars_repo_name": "Poulhouse/dev_don360", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "bitrix/modules/landing/install/js/landing/ui/card/iconoptionscard/src/iconoptionscard.js", "max_issues_repo_name": "Poulhouse/dev_don360", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "bitrix/modules/landing/install/js/landing/ui/card/iconoptionscard/src/iconoptionscard.js", "max_forks_repo_name": "Poulhouse/dev_don360", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 23.0, "max_line_length": 106, "alphanum_fraction": 0.6055652174}
import {Dom, Event, Tag, Type} from 'main.core';
import {BaseCard} from 'landing.ui.card.basecard';
import {Loc} from 'landing.loc';

import './css/style.css';
import {IconPanel} from 'landing.ui.panel.iconpanel';

/**
 * @memberOf BX.Landing.UI.Card
 */
export class IconOptionsCard extends BaseCard
{
	options: [string] = [];

	constructor()
	{
		super();
		this.setEventNamespace('BX.Landing.UI.Card.IconOptionsCard');
	}

	getLayout(): HTMLDivElement
	{
		return this.cache.remember('layout', () =>
		{
			return Tag.render`
				<div class="landing-ui-card landing-ui-card-icon-options --hide">
					<div class="landing-ui-card-icon-options-title">${Loc.getMessage('LANDING_ICONS_CHANGE_STYLE')}</div>
					<div class="landing-ui-card-icon-options-items"></div>
				</div>
			`;
		});
	}

	getOptionsLayout(): HTMLElement
	{
		return this.getLayout().querySelector('.landing-ui-card-icon-options-items');
	}

	getOptions(): [string]
	{
		return this.options;
	}

	setOptions(options: [], activeOption: string)
	{
		if (options.length > 0)
		{
			this.options = options;
			Dom.clean(this.getOptionsLayout());
			Dom.removeClass(this.getLayout(), '--hide');
			options.forEach(option =>
			{
				const isActive = (option === activeOption) ? ' --active' : '';
				const optionLayout = Tag.render`<span class="${option}${isActive}"></span>`;

				Event.bind(optionLayout, 'click', this.onOptionClick.bind(this, option));

				Dom.append(optionLayout, this.getOptionsLayout());
			});
		}
	}

	onOptionClick(option)
	{
		this.getOptionsLayout().querySelectorAll('span').forEach(optionItem => {
			Dom.removeClass(optionItem, '--active');
			if (Dom.hasClass(optionItem, option))
			{
				Dom.addClass(optionItem, '--active')
			}
		});

		this.emit('onChange', {option: option});
	}

	setOptionsByItem(classList: [])
	{
		IconPanel
			.getLibraries()
			.then(libraries => {
				if (classList.length > 0)
				{
					const iconOptions = new Set();
					let iconOptionActive;

					libraries.forEach(library =>
					{
						library.categories.forEach(category =>
						{
							category.items.forEach(item =>
							{
								if (Type.isObject(item))
								{
									const foundedOptions = item.options.filter(option =>
										classList.every(iconClass => option.split(' ').includes(iconClass))
									);
									if (foundedOptions.length > 0)
									{
										item.options.forEach(option => {iconOptions.add(option)});
										iconOptionActive = foundedOptions[0];
									}
								}
								else
								{
									if (
										classList.every(iconClass => item.split(' ').includes(iconClass))
									)
									{
										iconOptions.add(item);
										iconOptionActive = item;
									}
								}
							})
						})
					});

					if (iconOptions.size > 0)
					{
						this.setOptions([...iconOptions], iconOptionActive);
					}
				}
			});
	}
}
