Skip to content

Commit

Permalink
use separate files for web component css and template
Browse files Browse the repository at this point in the history
Signed-off-by: yzamir <[email protected]>
  • Loading branch information
yaacov committed Sep 12, 2023
1 parent e8e4db7 commit ece9687
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 22 deletions.
15 changes: 15 additions & 0 deletions rose/server/web/settings/settings-form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<form class="form">
<div class="form-group">
<label for="driver1" class="label">Driver 1:</label>
<input type="text" id="driver1" name="driver1" placeholder="http://driver.student.svc.cluster.local:8081" class="input-field">
</div>
<div class="form-group">
<label for="driver2" class="label">Driver 2:</label>
<input type="text" id="driver2" name="driver2" class="input-field">
</div>
<div class="form-group">
<button type="button" id="submit-button" class="submit-button">Send</button>
<button type="button" id="cancel-button" class="cancel-button">Cancel</button>
</div>
</form>
<div class="error-message" id="error-message"></div>
56 changes: 34 additions & 22 deletions rose/server/web/settings/settings-form.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,49 @@
// Load the HTML template for the web component
async function loadTemplate(path) {
try {
const response = await fetch(path);
return await response.text();
} catch (error) {
console.error('Error loading template:', error);
}
}

// Load the CSS and create a Constructible Stylesheet
async function loadStylesheet(path) {
try {
const response = await fetch(path);
const cssText = await response.text();

const sheet = new CSSStyleSheet();
sheet.replaceSync(cssText);

return sheet;
} catch (error) {
console.error('Error loading stylesheet:', error);
}
}

// Load both the template and stylesheet concurrently
const [innerHTML, sheet] = await Promise.all([
loadTemplate('./settings-form.html'),
loadStylesheet('./settings-form.css')
]);

class SettingsForm extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
}

connectedCallback() {
this.render();
this.shadowRoot.innerHTML = innerHTML;
this.shadowRoot.adoptedStyleSheets = [sheet];

this.loadDrivers();
this.shadowRoot.querySelector('#submit-button').addEventListener('click', this.sendDrivers.bind(this));
this.shadowRoot.querySelector('#cancel-button').addEventListener('click', this.cancelSetting.bind(this));
}

render() {
this.shadowRoot.innerHTML = `
<link rel="stylesheet" href="./settings-form.css">
<form class="form">
<div class="form-group">
<label for="driver1" class="label">Driver 1:</label>
<input type="text" id="driver1" name="driver1" placeholder="http://driver.student.svc.cluster.local:8081" class="input-field">
</div>
<div class="form-group">
<label for="driver2" class="label">Driver 2:</label>
<input type="text" id="driver2" name="driver2" class="input-field">
</div>
<div class="form-group">
<button type="button" id="submit-button" class="submit-button">Send</button>
<button type="button" id="cancel-button" class="cancel-button">Cancel</button>
</div>
</form>
<div class="error-message" id="error-message"></div>
`;
}

displayError(message) {
const errorMessageElement = this.shadowRoot.getElementById('error-message');
errorMessageElement.textContent = message;
Expand Down

0 comments on commit ece9687

Please sign in to comment.