Skip to content

Commit

Permalink
Project import generated by Copybara
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 721404516
  • Loading branch information
jimper authored and copybara-github committed Jan 30, 2025
1 parent 0bdfbf8 commit 739622b
Show file tree
Hide file tree
Showing 6 changed files with 261 additions and 106 deletions.
115 changes: 115 additions & 0 deletions src/components/configurator/page-settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/**
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import '../ui-controls/config-section';
import '../ui-controls/configurator-checkbox';
import '../ui-controls/targeting-input';

import {localized} from '@lit/localize';
import {html, LitElement} from 'lit';
import {customElement, property, query, queryAll} from 'lit/decorators.js';

import {
SamplePageConfig,
SamplePrivacyConfig,
} from '../../model/sample-config.js';
import {
configNames,
pageConfigNames,
privacyConfigNames,
} from '../../model/settings.js';
import {TargetingInput} from '../ui-controls/targeting-input.js';

/**
* Page-level configurator settings.
*/
@localized()
@customElement('page-settings')
export class PageSettings extends LitElement {
@query('configurator-checkbox#sra') private sraInput!: HTMLInputElement;
@queryAll('.privacy configurator-checkbox')
private privacySettings!: HTMLInputElement[];
@query('targeting-input') private targetingInput!: TargetingInput;

/**
* Gets the active page-level configuration.
*/
@property({attribute: 'config', type: Object}) config: SamplePageConfig = {};

private handleUpdate() {
this.config.sra = this.sraInput.checked;

this.config.privacy = this.config.privacy || {};
this.privacySettings.forEach(input => {
this.config.privacy![input.id as keyof SamplePrivacyConfig] =
input.checked;
});

this.config.targeting = this.targetingInput.config;

// Fire an event to let the configurator know a value has changed.
this.dispatchEvent(
new CustomEvent('update', {bubbles: true, composed: true}),
);
}

private renderCheckbox(id: string, label: string, checked = false) {
return html`<configurator-checkbox
id="${id}"
label="${label}"
?checked="${checked}"
@update="${this.handleUpdate}"
></configurator-checkbox>`;
}

private renderGeneralSettings() {
return this.renderCheckbox('sra', pageConfigNames.sra!(), this.config.sra);
}

private renderPrivacySettings() {
const privacy = this.config.privacy || {};
return html`<config-section
class="privacy"
title="${pageConfigNames.privacy!()}"
>
${Object.keys(privacyConfigNames).map((setting: string) => {
const key = setting as keyof SamplePrivacyConfig;
return this.renderCheckbox(
key,
privacyConfigNames[key]!(),
privacy[key],
);
})}
</config-section>`;
}

private renderPageTargeting() {
return html`<targeting-input
class="page"
title="${pageConfigNames.targeting!()}"
.config="${this.config.targeting || []}"
@update="${this.handleUpdate}"
>
</targeting-input>`;
}

render() {
return html`<config-section title="${configNames.page!()}">
${this.renderGeneralSettings()} ${this.renderPrivacySettings()}
${this.renderPageTargeting()}
</config-section>`;
}
}
119 changes: 14 additions & 105 deletions src/components/sample-configurator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/

import './gpt-playground';
import './configurator/page-settings';
import './configurator/slot-settings';
import './ui-controls/config-section';
import './ui-controls/targeting-input';

import {localized, msg} from '@lit/localize';
import {css, html, LitElement, TemplateResult} from 'lit';
Expand All @@ -29,18 +29,13 @@ import ts from 'typescript';
import * as base64url from '../../src/util/base64url.js';
import * as urlHash from '../../src/util/url-hash.js';
import type {SampleConfig} from '../model/sample-config.js';
import {
configNames,
pageConfigNames,
privacyConfigNames,
templateConfigNames,
} from '../model/settings.js';
import {configNames, templateConfigNames} from '../model/settings.js';
import {createTemplate} from '../template/template-factory.js';
import {Template} from '../template/template.js';

import {PageSettings} from './configurator/page-settings.js';
import {SlotSettings} from './configurator/slot-settings.js';
import {GptPlayground} from './gpt-playground.js';
import {TargetingInput} from './ui-controls/targeting-input.js';

// Constant UI strings.
const strings = {
Expand All @@ -61,8 +56,8 @@ export class SampleConfigurator extends LitElement {

@state() private template: Template;
@query('gpt-playground') private playground!: GptPlayground;
@query('targeting-input.page') private pageTargetingInput!: TargetingInput;
@query('slot-settings') private slotSettings!: SlotSettings;
@query('page-settings') private pageSettings!: PageSettings;

static styles = css`
:host {
Expand Down Expand Up @@ -92,24 +87,10 @@ export class SampleConfigurator extends LitElement {
overflow: scroll;
}
config-section label {
display: block;
}
config-section input[type='checkbox'] {
float: left;
}
config-section select {
float: right;
}
config-section.page div,
config-section.privacy div {
width: 33%;
min-width: 200px;
}
config-section.template div {
width: 50%;
min-width: 200px;
Expand All @@ -131,10 +112,6 @@ export class SampleConfigurator extends LitElement {
this.template = createTemplate(this.config);
}

private getInputById(id: string): HTMLInputElement {
return this.renderRoot.querySelector(`#${id}`) as HTMLInputElement;
}

private getSelectById(id: string): HTMLSelectElement {
return this.renderRoot.querySelector(`#${id}`) as HTMLSelectElement;
}
Expand Down Expand Up @@ -171,28 +148,6 @@ export class SampleConfigurator extends LitElement {
}
}

private updateBooleanSettings() {
const config = structuredClone(this.config);

// Ensure relevant configs are defined.
config.page = config.page || {};
config.page.privacy = config.page.privacy || {};

// Update page settings.
const page = config.page;
page.sra = this.getInputById('sra').checked;

// Update privacy settings.
const privacy = config.page.privacy;
privacy.ltd = this.getInputById('ltd').checked;
privacy.npa = this.getInputById('npa').checked;
privacy.rdp = this.getInputById('rdp').checked;
privacy.tfcd = this.getInputById('tfcd').checked;
privacy.tfua = this.getInputById('tfua').checked;

this.config = config;
}

private updateStringSettings() {
const config = structuredClone(this.config);

Expand All @@ -210,74 +165,25 @@ export class SampleConfigurator extends LitElement {
this.config = config;
}

private updatePageTargeting() {
private updateSettings() {
const config = structuredClone(this.config);

// Ensure relevant configs are defined.
config.page = config.page || {};
config.page.targeting = this.pageTargetingInput.config;

this.config = config;
}

private updateSlotSettings() {
const config = structuredClone(this.config);

// Ensure relevant configs are defined.
config.page.privacy = config.page.privacy || {};
config.slots = config.slots || [];

config.page = this.pageSettings.config;
config.slots = this.slotSettings.config;

this.config = config;
}

private checkbox(setting: string, name: string, enabled = false) {
return html` <div>
<input
type="checkbox"
id="${setting}"
?checked="${enabled}"
@click="${this.updateBooleanSettings}"
/>
<label for="${setting}">${name}</label>
</div>`;
}

private renderPageSettings() {
const settings = this.config?.page;

return html`<config-section class="page" title="${configNames.page!()}">
${this.checkbox('sra', pageConfigNames.sra!(), settings?.sra)}
${this.renderPrivacySettings()}
<targeting-input
class="page"
.config="${settings?.targeting}"
title="${pageConfigNames.targeting!()}"
@update="${this.updatePageTargeting}"
>
</targeting-input>
</config-section>`;
}

private renderPrivacySettings() {
const settings = this.config?.page?.privacy;

return html`<config-section
class="privacy"
title="${pageConfigNames.privacy!()}"
>
${this.checkbox('tfcd', privacyConfigNames.tfcd!(), settings?.tfcd)}
${this.checkbox('ltd', privacyConfigNames.ltd!(), settings?.ltd)}
${this.checkbox('npa', privacyConfigNames.npa!(), settings?.npa)}
${this.checkbox('rdp', privacyConfigNames.rdp!(), settings?.rdp)}
${this.checkbox('tfua', privacyConfigNames.tfua!(), settings?.tfua)}
</config-section>`;
}

private renderSlotSettings() {
return html` <slot-settings
title="${configNames.slots()}"
.config="${this.config?.slots}"
@update="${this.updateSlotSettings}"
@update="${this.updateSettings}"
>
</slot-settings>`;
}
Expand Down Expand Up @@ -319,8 +225,11 @@ export class SampleConfigurator extends LitElement {
<span>${strings.configuratorTitle()}</span>
</div>
<div id="configurator-settings">
${this.renderPageSettings()} ${this.renderSlotSettings()}
${this.renderTemplateSettings()}
<page-settings
.config="${this.config?.page || {}}"
@update="${this.updateSettings}"
></page-settings>
${this.renderSlotSettings()} ${this.renderTemplateSettings()}
</div>
</div>`;
}
Expand Down
78 changes: 78 additions & 0 deletions src/components/ui-controls/configurator-checkbox.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {localized} from '@lit/localize';
import {css, html, LitElement} from 'lit';
import {customElement, property} from 'lit/decorators.js';
import {ifDefined} from 'lit/directives/if-defined.js';

@localized()
@customElement('configurator-checkbox')
export class ConfiguratorCheckbox extends LitElement {
static styles = css`
:host {
display: flex;
width: 33%;
min-width: 200px;
}
input[type='checkbox'] {
align-self: flex-start;
}
`;

/**
* ID content attribute of the element.
*
* Will be automatically generated if not specified.
*/
@property({type: String}) id = `checkbox-${Date.now().toString()}`;

/**
* User-friendly label for the element.
*/
@property({type: String}) label?: string;

/**
* Name content attribute of the element.
*/
@property({type: String}) name?: string;

/**
* Whether the checkbox is currently selected.
*/
@property({type: Boolean}) checked = false;

private handleClick(event: Event) {
this.checked = (event.target as HTMLInputElement).checked;

// Fire an event to let the configurator know a value has changed.
this.dispatchEvent(
new CustomEvent('update', {bubbles: true, composed: true}),
);
}

render() {
return html`<input
type="checkbox"
id="${this.id}"
name="${ifDefined(this.name)}"
?checked="${this.checked}"
@click="${this.handleClick}"
/>
<label for="${this.id}">${this.label}</label>`;
}
}
Loading

0 comments on commit 739622b

Please sign in to comment.