-
-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change
MORPHER
settings to follow the existing conventions.
- Loading branch information
Showing
10 changed files
with
132 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,24 @@ | ||
import { MorphdomMorpher } from "./morphers/morphdom.js"; | ||
import { AlpineMorpher } from "./morphers/alpine.js"; | ||
import { isEmpty } from "./utils.js"; | ||
|
||
const MORPHER_CLASSES = { | ||
morphdom: MorphdomMorpher, | ||
alpine: AlpineMorpher, | ||
}; | ||
|
||
export function getMorpher(morpherSettings) { | ||
const morpherName = morpherSettings.NAME; | ||
|
||
if (isEmpty(morpherName)) { | ||
throw Error(" Missing morpher name"); | ||
} | ||
|
||
const MorpherClass = MORPHER_CLASSES[morpherName]; | ||
|
||
export function getMorpher(morpherName, morpherOptions) { | ||
const MorpherClass = { | ||
morphdom: MorphdomMorpher, | ||
alpine: AlpineMorpher, | ||
}[morpherName]; | ||
if (MorpherClass) { | ||
return new MorpherClass(morpherOptions); | ||
return new MorpherClass(morpherSettings); | ||
} | ||
throw Error(`No morpher found for: ${morpherName}`); | ||
|
||
throw Error(`Unknown morpher: ${morpherName}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,31 @@ | ||
{% load static %} | ||
|
||
{{ MORPHER_OPTIONS|json_script:"unicorn:settings:morpher-options" }} | ||
{{ MORPHER|json_script:"unicorn:settings:morpher" }} | ||
|
||
{% if MINIFIED %} | ||
<script src="{% static 'unicorn/js/unicorn.min.js' %}"></script> | ||
<script> | ||
var url = "{% url 'django_unicorn:message' %}"; | ||
var morpherOptions = JSON.parse(document.getElementById("unicorn:settings:morpher-options").textContent); | ||
var morpher = getMorpher("{{ MORPHER }}", morpherOptions); | ||
const url = "{% url 'django_unicorn:message' %}"; | ||
|
||
const morpherSettings = JSON.parse(document.getElementById("unicorn:settings:morpher").textContent); | ||
const morpher = getMorpher(morpherSettings); | ||
|
||
Unicorn.init(url, "{{ CSRF_HEADER_NAME }}", "{{ CSRF_COOKIE_NAME }}", morpher); | ||
|
||
</script> | ||
{% else %} | ||
<script type="module"> | ||
import * as Unicorn from "{% static 'unicorn/js/unicorn.js' %}"; | ||
import { getMorpher } from "{% static 'unicorn/js/morpher.js' %}"; | ||
|
||
// Set Unicorn to the global, so it can be used by components | ||
window.Unicorn = Unicorn; | ||
|
||
var url = "{% url 'django_unicorn:message' %}"; | ||
var morpherOptions = JSON.parse(document.getElementById("unicorn:settings:morpher-options").textContent); | ||
var morpher = getMorpher("{{ MORPHER }}", morpherOptions); | ||
const url = "{% url 'django_unicorn:message' %}"; | ||
|
||
const morpherSettings = JSON.parse(document.getElementById("unicorn:settings:morpher").textContent); | ||
const morpher = getMorpher(morpherSettings); | ||
|
||
Unicorn.init(url, "{{ CSRF_HEADER_NAME }}", "{{ CSRF_COOKIE_NAME }}", morpher); | ||
</script> | ||
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,60 @@ | ||
# Custom Morphers | ||
|
||
Morpher is a library used to update specific parts of the DOM element instead of replacing the entire element. This improves performance and maintains the state of unchanged DOM elements, such as the cursor position in an input. | ||
The morpher is a library used to update specific parts of the DOM element instead of replacing the entire element. This improves performance and maintains the state of unchanged DOM elements, such as the cursor position in an input. | ||
|
||
The default morpher used in Unicorn is [morphdom](https://github.com/patrick-steele-idem/morphdom). If you don't change any settings, morphdom will be used. However, you can switch to a different morpher by setting the "MORPHER" parameter. Each morpher has its own set of configurable settings, which can be adjusted using the "MORPHER_OPTIONS" parameter in the "UNICORN" setting. | ||
The default morpher used in Unicorn is [`morphdom`](https://github.com/patrick-steele-idem/morphdom). The only alternative morpher available is the [Alpine.js morph plugin](https://alpinejs.dev/plugins/morph). | ||
|
||
Currently, the only alternative morpher available is the [Alpine.js Morph Plugin](https://alpinejs.dev/plugins/morph). | ||
## `Morphdom` | ||
|
||
## Morphdom | ||
`morphdom` is the default morpher so no extra settings or installation is required to use it. | ||
|
||
Since it's the default morpher, and it's built into Unicorn, no extra steps are required to use it. | ||
|
||
### Morphdom Settings | ||
### Example settings | ||
|
||
```python | ||
# File: settings.py | ||
# settings.py | ||
|
||
UNICORN = { | ||
"MORPHER": "morphdom", | ||
"MORHER_OPTIONS": { | ||
"RELOAD_SCRIPT_ELEMENTS": False, | ||
}, | ||
... | ||
"MORPHER": { | ||
"NAME": "morphdom", | ||
"RELOAD_SCRIPT_ELEMENTS": True, | ||
} | ||
... | ||
} | ||
``` | ||
|
||
### Morphdom Options | ||
|
||
- **RELOAD_SCRIPT_ELEMENTS** (default is `False`): Whether `script` elements in a template should be "re-run" after a template has been re-rendered. | ||
## `Alpine` | ||
|
||
## Alpine | ||
Components which use both `Unicorn` and `Alpine.js` should use the `Alpine.js` morpher to prevent losing state when it gets re-rendered. | ||
|
||
The Alpine morpher is helpful if you use Alpine.js and need to keep the Alpine state of your components after a template has been re-rendered. | ||
### Example settings | ||
|
||
```python | ||
# settings.py | ||
|
||
### Alpine Options | ||
UNICORN = { | ||
... | ||
"MORPHER": { | ||
"NAME": "alpine", | ||
} | ||
... | ||
} | ||
``` | ||
|
||
The Alpine.js morpher doesn't have any options. | ||
```{note} | ||
`RELOAD_SCRIPT_ELEMENTS` is not currently supported for the `Alpine.js` morpher. | ||
``` | ||
|
||
### Alpine Installation | ||
### JavaScript Installation | ||
|
||
To use the Alpine.js morpher, you need to include Alpine.js and Alpine.js Morpher plugin in your template. You can do this by adding the following line to your template: | ||
`Alpine.js` is not included in `Unicorn` so you will need to manually include it. Make sure to include `Alpine.js` and the morpher plugin by adding the following line to your template before `{% unicorn_scripts %}`. | ||
|
||
```html | ||
... | ||
<head> | ||
... | ||
<script defer src="https://unpkg.com/@alpinejs/[email protected]/dist/cdn.min.js"></script> | ||
<script defer src="https://unpkg.com/[email protected]/dist/cdn.min.js"></script> | ||
{% unicorn_scripts %} | ||
</head> | ||
``` | ||
|
||
Then switch to the Alpine.js morpher in your settings: | ||
|
||
```python | ||
# File: settings.py | ||
|
||
UNICORN = { | ||
"MORPHER": "alpine", | ||
} | ||
... | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters