diff --git a/2-ui/2-events/04-default-browser-action/1-why-return-false-fails/solution.md b/2-ui/2-events/04-default-browser-action/1-why-return-false-fails/solution.md index 4d175ca01..2d4508785 100644 --- a/2-ui/2-events/04-default-browser-action/1-why-return-false-fails/solution.md +++ b/2-ui/2-events/04-default-browser-action/1-why-return-false-fails/solution.md @@ -1,16 +1,16 @@ -When the browser reads the `on*` attribute like `onclick`, it creates the handler from its content. +Quando il browser legge un attributo `on*` come `onclick`, crea il gestore dal suo contenuto. -For `onclick="handler()"` the function will be: +Nel caso di `onclick="handler()"` la funzione sarà: ```js function(event) { - handler() // the content of onclick + handler() // il contenuto del click } ``` -Now we can see that the value returned by `handler()` is not used and does not affect the result. +Possiamo osservare che il valore restituito da `handler()` non viene usato e non influenza il risultato. -The fix is simple: +Il fix è semplice: ```html run -the browser will go to w3.org +il browser andrà su w3.org ``` -The browser follows the URL on click, but we don't want it. +Il browser andrà all'URL al click, ma non è ciò che vogliamo. -How to fix? +Come si può sistemare? diff --git a/2-ui/2-events/04-default-browser-action/2-catch-link-navigation/solution.md b/2-ui/2-events/04-default-browser-action/2-catch-link-navigation/solution.md index 25079cb8d..9e3457dcc 100644 --- a/2-ui/2-events/04-default-browser-action/2-catch-link-navigation/solution.md +++ b/2-ui/2-events/04-default-browser-action/2-catch-link-navigation/solution.md @@ -1,5 +1,5 @@ -That's a great use of the event delegation pattern. +Questo è un ottimo uso del pattern event delegation. -In real life instead of asking we can send a "logging" request to the server that saves the information about where the visitor left. Or we can load the content and show it right in the page (if allowable). +In applicazioni concrete, invece di chiedere, potremmo inviare una richiesta di "logging" al server, che salvi l'informazione relativa al momento in cui l'utente ha deciso di abbandonare la pagina. Oppure potremmo caricare il contenuto e mostrarlo direttamente in pagina (se permesso). -All we need is to catch the `contents.onclick` and use `confirm` to ask the user. A good idea would be to use `link.getAttribute('href')` instead of `link.href` for the URL. See the solution for details. +Tutto ciò di cui abbiamo bisogno è di catturare i `contents.onclick` ed usare `confirm` per chiedere all'utente. Una buona idea potrebbe essere quella di usare `link.getAttribute('href')` invece di `link.href` per l'URL. Guarda la soluzione per i dettagli. diff --git a/2-ui/2-events/04-default-browser-action/2-catch-link-navigation/task.md b/2-ui/2-events/04-default-browser-action/2-catch-link-navigation/task.md index 6ca456c2c..2eeb91e7f 100644 --- a/2-ui/2-events/04-default-browser-action/2-catch-link-navigation/task.md +++ b/2-ui/2-events/04-default-browser-action/2-catch-link-navigation/task.md @@ -2,15 +2,15 @@ importance: 5 --- -# Catch links in the element +# Catturare i links nell'elemento -Make all links inside the element with `id="contents"` ask the user if they really want to leave. And if they don't then don't follow. +Fare in modo che tutti i links dentro l'elemento con `id="contents"` chiedano all'utente se vuole davvero abbandonare la pagina al click. Se risponde no, allora non deve seguire il link. -Like this: +Come nell'esempio seguente: [iframe height=100 border=1 src="solution"] -Details: +Dettagli: -- HTML inside the element may be loaded or regenerated dynamically at any time, so we can't find all links and put handlers on them. Use event delegation. -- The content may have nested tags. Inside links too, like `...`. +- L'HTML dentro l'elemento potrebbe essere caricato o rigenerato in qualunque istante, quindi non possiamo trovare tutti i links ed inserirvi dei gestori all'interno. Utilizzare invece la event delegation. +- Il contenuto può contenere tags annidati. Anche dentro i links, per esempio potremmo avere `...`. diff --git a/2-ui/2-events/04-default-browser-action/3-image-gallery/solution.md b/2-ui/2-events/04-default-browser-action/3-image-gallery/solution.md index 5ff60b5c0..793dca5eb 100644 --- a/2-ui/2-events/04-default-browser-action/3-image-gallery/solution.md +++ b/2-ui/2-events/04-default-browser-action/3-image-gallery/solution.md @@ -1 +1 @@ -The solution is to assign the handler to the container and track clicks. If a click is on the `` link, then change `src` of `#largeImg` to the `href` of the thumbnail. +La soluzione è quella di assegnare il gestore al contenitore e tenere traccia dei click. Se un click è sul link ``, allora modificare il `src` di `#largeImg` con l'`href` della thumbnail. diff --git a/2-ui/2-events/04-default-browser-action/3-image-gallery/task.md b/2-ui/2-events/04-default-browser-action/3-image-gallery/task.md index f7571cc80..2c0297a89 100644 --- a/2-ui/2-events/04-default-browser-action/3-image-gallery/task.md +++ b/2-ui/2-events/04-default-browser-action/3-image-gallery/task.md @@ -2,12 +2,12 @@ importance: 5 --- -# Image gallery +# Galleria di immagini -Create an image gallery where the main image changes by the click on a thumbnail. +Creare una galleria di immagini dove l'immagine principale cambi al click su una thumbnail. -Like this: +Come in questo esempio: [iframe src="solution" height=600] -P.S. Use event delegation. +P.S.: Utilizzate event delegation. diff --git a/2-ui/2-events/04-default-browser-action/article.md b/2-ui/2-events/04-default-browser-action/article.md index ceac160c1..53e3804fc 100644 --- a/2-ui/2-events/04-default-browser-action/article.md +++ b/2-ui/2-events/04-default-browser-action/article.md @@ -1,43 +1,43 @@ -# Browser default actions +# Azioni predefinite del browser -Many events automatically lead to certain actions performed by the browser. +Molti eventi vengono ricondotti verso determinate azioni del browser. -For instance: +Per esempio: -- A click on a link - initiates navigation to its URL. -- A click on a form submit button - initiates its submission to the server. -- Pressing a mouse button over a text and moving it - selects the text. +- Un click su un link - inizializza la navigazione verso il suo URL. +- Un click su un pulsante di invio di un form - inizializza l'invio dello stesso al server. +- Premendo il pulsante del mouse e spostandoci su un testo - lo si seleziona. -If we handle an event in JavaScript, we may not want the corresponding browser action to happen, and want to implement another behavior instead. +Quando gestiamo un evento con JavaScript, potremmo non volere che la corrispondente azione del browser avvenga, vorremmo invece implementare un altro comportamento. -## Preventing browser actions +## Prevenire le azioni del browser -There are two ways to tell the browser we don't want it to act: +Ci sono due maniere per comunicare al browser che non vogliamo che esegua l'azione predefinita: -- The main way is to use the `event` object. There's a method `event.preventDefault()`. -- If the handler is assigned using `on` (not by `addEventListener`), then returning `false` also works the same. +- La maniera più comune è quella di usare il metodo `event.preventDefault()`, incluso nell'oggetto `event`. +- Se il gestore viene assegnato tramite `on` (e non tramite `addEventListener`), allora restituire `false` sortirà lo stesso effetto. -In this HTML a click on a link doesn't lead to navigation, browser doesn't do anything: +In questo HTML un click su un link non porta a navigarne l'URL, e di fatto il browser non fa nulla: ```html autorun height=60 no-beautify -Click here -or -here +Clicca qui +o +qui ``` -In the next example we'll use this technique to create a JavaScript-powered menu. +Nel prossimo esempio useremo questa tecnica per creare un menù potenziato tramite JavaScript. -```warn header="Returning `false` from a handler is an exception" -The value returned by an event handler is usually ignored. +```warn header="Restituire `false` da un gestore è un'eccezione" +Il valore restituito da un gestore di eventi solitamente viene ignorato. -The only exception is `return false` from a handler assigned using `on`. +L'unica eccezione è il `return false` di un gestore assegnato con l'uso di `on`. -In all other cases, `return` value is ignored. In particular, there's no sense in returning `true`. +In tutti gli altri casi, il valore del `return` viene ignorato. Nello specifico, non ha alcun senso restituire `true`. ``` -### Example: the menu +### Esempio: il menù -Consider a site menu, like this: +Consideriamo un menù di questo tipo: ```html ``` -Here's how it looks with some CSS: +Ecco come appare applicando qualche stile CSS: [iframe height=70 src="menu" link edit] -Menu items are implemented as HTML-links ``, not buttons ` + - ``` -Now, in addition to that context menu we'd like to implement document-wide context menu. +Adesso, in aggiunta a questo menù, ci piacerebbe implementare un menù contestuale sul *documento*. -Upon right click, the closest context menu should show up. +Al click sul tasto destro, dovrebbe comparire il relativo menù contestuale. ```html autorun height=80 no-beautify run -

Right-click here for the document context menu

- +

Click sul tasto destro per il menù contestuale del documento

+ ``` -The problem is that when we click on `elem`, we get two menus: the button-level and (the event bubbles up) the document-level menu. +Il problema è che così facendo, cliccando su `elem`, otterremmo due menù: quello del pulsante (l'evento va risalendo per via del bubbling) e quello del documento. -How to fix it? One of solutions is to think like: "When we handle right-click in the button handler, let's stop its bubbling" and use `event.stopPropagation()`: +Come possiamo evitarlo? Una delle soluzioni è fare questo ragionamento: "Quando gestiamo il click sul tasto destro nel gestore del pulsante, interrompiamo il bubbling" e usiamo `event.stopPropagation()`: ```html autorun height=80 no-beautify run -

Right-click for the document menu

- +

Click sul tasto destro per il documento

+ ``` -Now the button-level menu works as intended. But the price is high. We forever deny access to information about right-clicks for any outer code, including counters that gather statistics and so on. That's quite unwise. +A questo punto il menù del pulsante funzionerà come previsto. Ma il prezzo sarà alto, perché a quel punto negheremo per sempre l'accesso alle informazioni relative ai click sul tasto destro, a qualunque altro codice esterno, inclusi contatori che raccolgono statistiche e così via. Ed è una cosa poco saggia. -An alternative solution would be to check in the `document` handler if the default action was prevented? If it is so, then the event was handled, and we don't need to react on it. +Una soluzione alternativa potrebbe essere quella di controllare nel gestore del `document` se l'azione predefinita sia stata prevenuta. Se così fosse, significherebbe che l'evento è stato gestito, e quindi non sarà necessario gestirlo nuovamente. ```html autorun height=80 no-beautify run -

Right-click for the document menu (added a check for event.defaultPrevented)

- +

Click sul tasto destro per il menu del documento (aggiunto un controllo per event.defaultPrevented)

+ ``` -Now everything also works correctly. If we have nested elements, and each of them has a context menu of its own, that would also work. Just make sure to check for `event.defaultPrevented` in each `contextmenu` handler. +Ora funziona tutto correttamente. Se abbiamo elementi annidati, ed ognuno di essi ha un suo menù contestuale, funzionerà anche questo. Dobbiamo solo assicurarci di controllare `event.defaultPrevented` in ogni gestore di `contextmenu`. ```smart header="event.stopPropagation() and event.preventDefault()" -As we can clearly see, `event.stopPropagation()` and `event.preventDefault()` (also known as `return false`) are two different things. They are not related to each other. +Come possiamo chiaramente notare, `event.stopPropagation()` ed `event.preventDefault()` (conosciuto anche come `return false`) sono due cose diverse. Non sono relazionate tra loro. ``` -```smart header="Nested context menus architecture" -There are also alternative ways to implement nested context menus. One of them is to have a single global object with a handler for `document.oncontextmenu`, and also methods that allow us to store other handlers in it. +```smart header="Architettura dei menù contestuali annidati" +Ci sono pure dei modi alternativi per implementare i menù contestuali annidati. Uno di questi è quello di avere un singolo oggetto globale con un solo gestore per `document.oncontextmenu`, e metodi che ci permettono di gestire altri gestori al suo interno. -The object will catch any right-click, look through stored handlers and run the appropriate one. +L'oggetto catturerà ogni click sul tasto destro, controllando tra i suoi gestori ed eseguire quello appropriato. -But then each piece of code that wants a context menu should know about that object and use its help instead of the own `contextmenu` handler. +Ma in questo caso ogni pezzo di codice che vuole implementare un menù contestuale, dovrebbe conoscere l'esistenza di questo oggetto e del suo supporto, invece di avere il proprio gestore per `contextmenu`. ``` -## Summary +## Riepilogo -There are many default browser actions: +Ci sono tante azioni predefinite del browser: -- `mousedown` -- starts the selection (move the mouse to select). -- `click` on `` -- checks/unchecks the `input`. -- `submit` -- clicking an `` or hitting `key:Enter` inside a form field causes this event to happen, and the browser submits the form after it. -- `keydown` -- pressing a key may lead to adding a character into a field, or other actions. -- `contextmenu` -- the event happens on a right-click, the action is to show the browser context menu. -- ...there are more... +- `mousedown` -- comincia una selezione (spostare il mouse per continuare a selezionare). +- `click` su `` -- check/uncheck sull'`input`. +- `submit` -- cliccando su `` o premendo su `key:Enter` dentro un campo del form, scatena questo evento, ed il browser invia il form subito dopo. +- `keydown` -- premendo un tasto può portare ad aggiungere un carattere dentro un campo, o altre azioni. +- `contextmenu` -- viene scatenato al click sul tasto destro, e l'azione che ne deriva è quella di mostrare il menù contestuale del browser. +- ...e molti altri... -All the default actions can be prevented if we want to handle the event exclusively by JavaScript. +Tutte le azione predefinite possono essere prevenute se vogliamo gestire gli eventi esclusivamente tramite JavaScript. -To prevent a default action -- use either `event.preventDefault()` or `return false`. The second method works only for handlers assigned with `on`. +Per prevenire un'azione predefinita -- si possono usare `event.preventDefault()` oppure `return false`. Il secondo metodo è valido solo con gestori assegnati con `on`. -The `passive: true` option of `addEventListener` tells the browser that the action is not going to be prevented. That's useful for some mobile events, like `touchstart` and `touchmove`, to tell the browser that it should not wait for all handlers to finish before scrolling. +L'opzione `passive: true` di `addEventListener` comunica al browser che l'azione non sarà prevenuta. La sua utilità si palesa per eventi su dispositivi mobiles, come ad esempio `touchstart` e `touchmove`, per comunicare al browser che non deve attendere l'esecuzione di tutti i gestori prima di effettuare lo scrolling. -If the default action was prevented, the value of `event.defaultPrevented` becomes `true`, otherwise it's `false`. +Se l'azione predefinita è stata prevenuta, il valore di `event.defaultPrevented` diventa `true`, altrimenti è `false`. -```warn header="Stay semantic, don't abuse" -Technically, by preventing default actions and adding JavaScript we can customize the behavior of any elements. For instance, we can make a link `
` work like a button, and a button `