diff --git a/8-web-components/2-custom-elements/1-live-timer/solution.md b/8-web-components/2-custom-elements/1-live-timer/solution.md index a9eacc880..cb2a762ed 100644 --- a/8-web-components/2-custom-elements/1-live-timer/solution.md +++ b/8-web-components/2-custom-elements/1-live-timer/solution.md @@ -1,4 +1,4 @@ -Please note: -1. We clear `setInterval` timer when the element is removed from the document. That's important, otherwise it continues ticking even if not needed any more. And the browser can't clear the memory from this element and referenced by it. -2. We can access current date as `elem.date` property. All class methods and properties are naturally element methods and properties. +Nota bene: +1. Quando rimuoviamo l'elemento dal documento, eseguiamo il clear di `setInterval` per il timer. È importante, altrimenti continuerà a ticchettare anche se non più necessario. Inoltre il browser avendo ancora un riferimento ad esso, non sarà in grado di pulire la sua memoria. +2. Deve essere possibile accedere alla data corrente tramite la proprietà `elem.date`. Tutti i metodi della classe e le relative proprietà devono anche essere metodi e proprietà dell'elemento. diff --git a/8-web-components/2-custom-elements/1-live-timer/solution.view/live-timer.js b/8-web-components/2-custom-elements/1-live-timer/solution.view/live-timer.js index a53d72e00..279d49dc8 100644 --- a/8-web-components/2-custom-elements/1-live-timer/solution.view/live-timer.js +++ b/8-web-components/2-custom-elements/1-live-timer/solution.view/live-timer.js @@ -24,7 +24,7 @@ class LiveTimer extends HTMLElement { } disconnectedCallback() { - clearInterval(this.timer); // important to let the element be garbage-collected + clearInterval(this.timer); // importante per permettere all'elemento di essere gestito dal garbage-collector } } diff --git a/8-web-components/2-custom-elements/1-live-timer/source.view/index.html b/8-web-components/2-custom-elements/1-live-timer/source.view/index.html index 878120241..287ab3bf4 100644 --- a/8-web-components/2-custom-elements/1-live-timer/source.view/index.html +++ b/8-web-components/2-custom-elements/1-live-timer/source.view/index.html @@ -1,8 +1,8 @@ - + - + diff --git a/8-web-components/2-custom-elements/1-live-timer/source.view/live-timer.js b/8-web-components/2-custom-elements/1-live-timer/source.view/live-timer.js index e2fe2b69f..1cc2b15d0 100644 --- a/8-web-components/2-custom-elements/1-live-timer/source.view/live-timer.js +++ b/8-web-components/2-custom-elements/1-live-timer/source.view/live-timer.js @@ -1,6 +1,6 @@ class LiveTimer extends HTMLElement { - /* your code here */ + /* inserite qui il vostro codice */ } diff --git a/8-web-components/2-custom-elements/1-live-timer/task.md b/8-web-components/2-custom-elements/1-live-timer/task.md index 1feb7490a..74460ff71 100644 --- a/8-web-components/2-custom-elements/1-live-timer/task.md +++ b/8-web-components/2-custom-elements/1-live-timer/task.md @@ -1,14 +1,14 @@ -# Live timer element +# Elemento live timer -We already have `` element to show a nicely formatted time. +Abbiamo già un elemento `` per mostrare un orario formattato. -Create `` element to show the current time: -1. It should use `` internally, not duplicate its functionality. -2. Ticks (updates) every second. -3. For every tick, a custom event named `tick` should be generated, with the current date in `event.detail` (see chapter ). +Create un elemento `` per mostrare l'orario corrente con queste specifiche: +1. Internamente dovrebbe usare ``, senza duplicarne la funzionalità. +2. Un tick (aggiornamento) ogni secondo. +3. Per ogni tick, deve essere generato un evento personalizzato chiamato `tick`, con la data corrente dentro `event.detail` (guardare il capitolo ). -Usage: +Uso: ```html diff --git a/8-web-components/2-custom-elements/article.md b/8-web-components/2-custom-elements/article.md index a84ed1192..faa753bd4 100644 --- a/8-web-components/2-custom-elements/article.md +++ b/8-web-components/2-custom-elements/article.md @@ -1,81 +1,81 @@ -# Custom elements +# Elementi personalizzati -We can create custom HTML elements, described by our class, with its own methods and properties, events and so on. +Possiamo creare elementi HTML personalizzati, dichiarati e descritti tramite delle apposite classi, ognuno con i suoi metodi, proprietà, eventi e così via. -Once a custom element is defined, we can use it on par with built-in HTML elements. +Una volta definito un elemento personalizzato, possiamo usarlo al pari di qualunque altro elemento HTML built-in. -That's great, as HTML dictionary is rich, but not infinite. There are no ``, ``, ``... Just think of any other tag we might need. +Ciò è grandioso, essendo che il dizionario HTML è molto ricco, ma non infinito. Non ci sono ``, ``, ``...Ci basti pensare a qualunque altro tag di cui avremmo necessità. -We can define them with a special class, and then use as if they were always a part of HTML. +Possiamo definirli con delle classi speciali, ed usarli come se fossero sempre stati parte dell'HTML. -There are two kinds of custom elements: +Gli elementi personalizzati si dividono in due categorie: -1. **Autonomous custom elements** -- "all-new" elements, extending the abstract `HTMLElement` class. -2. **Customized built-in elements** -- extending built-in elements, like a customized button, based on `HTMLButtonElement` etc. +1. **Elementi personalizzati autonomi** -- elementi "nuovi di zecca", che estendono la classe astratta `HTMLElement`. +2. **Elementi built-in personalizzati** -- estendono gli elementi built-in, ad esempio un pulsante personalizzato, basato su `HTMLButtonElement` etc. -First we'll cover autonomous elements, and then move to customized built-in ones. +Prima di tutto, affrontiamo gli elementi autonomi, dopodiché ci sposteremo a quelli built-in personalizzati. -To create a custom element, we need to tell the browser several details about it: how to show it, what to do when the element is added or removed to page, etc. +Per creare un elemento personalizzato, abbiamo bisogno di comunicare al browser una serie di dettagli relativi: come mostrarlo, cosa fare una volta che l'elemento viene aggiunto o rimosso dalla pagina, etc. -That's done by making a class with special methods. That's easy, as there are only few methods, and all of them are optional. +Ciò viene fatto creando una classe con dei metodi appositi. È facile, dato che ci sono pochi metodi, tutti opzionali. -Here's a sketch with the full list: +Ecco uno schema predefinito per la classe, con la lista completa: ```js class MyElement extends HTMLElement { constructor() { super(); - // element created + // elemento creato } connectedCallback() { - // browser calls this method when the element is added to the document - // (can be called many times if an element is repeatedly added/removed) + // il browser chiama questo metodo quando l'elemento viene aggiunto al documento + // (può essere chiamato tante volte se un elemento viene raggiunto o rimosso) } disconnectedCallback() { - // browser calls this method when the element is removed from the document - // (can be called many times if an element is repeatedly added/removed) + // il browser chiama questo metodo quando l'elemento viene rimosso dal documento + // (può essere chiamato tante volte se un elemento viene aggiunto o rimosso) } static get observedAttributes() { - return [/* array of attribute names to monitor for changes */]; + return [/* un array di nomi di attributi per monitorare le modifiche */]; } attributeChangedCallback(name, oldValue, newValue) { - // called when one of attributes listed above is modified + // chiamato quando uno degli attributi della lista precedente viene modificato } adoptedCallback() { - // called when the element is moved to a new document - // (happens in document.adoptNode, very rarely used) + // chiamato quando l'elemento viene spostato su un nuovo documento + // (avviene in document.adoptNode, usato molto raramente) } - // there can be other element methods and properties + // possono esserci altri metodi e proprietà per l'elemento } ``` -After that, we need to register the element: +Dopodiché, possiamo registrare l'elemento: ```js -// let the browser know that is served by our new class +// fa in modo che il browser sappia che viene fornito dalla nostra classe customElements.define("my-element", MyElement); ``` -Now for any HTML elements with tag ``, an instance of `MyElement` is created, and the aforementioned methods are called. We also can `document.createElement('my-element')` in JavaScript. +Adesso, per ogni elemento HTML con tag ``, verrà creata un'istanza di `MyElement`, e chiamati i sopracitati metodi. Possiamo anche chiamare `document.createElement('my-element')` in JavaScript. -```smart header="Custom element name must contain a hyphen `-`" -Custom element name must have a hyphen `-`, e.g. `my-element` and `super-button` are valid names, but `myelement` is not. +```smart header="Il nome degli elementi custom devono contenere un trattino `-`" +I nomi degli elementi personalizzati devono contenere un trattino `-`, ad esempio `my-element` e `super-button` sono nomi validi, al contrario di `myelement` che non lo è. -That's to ensure that there are no name conflicts between built-in and custom HTML elements. +Ciò ci assicura l'assenza di conflitti tra i nostri elementi HTML personalizzati e quelli built-in. ``` -## Example: "time-formatted" +## Esempio: "time-formatted" -For example, there already exists `