Skip to content

Commit 9f9cabc

Browse files
committed
minor #256 Rebuild js dist files (norkunas)
This PR was merged into the 2.x branch. Discussion ---------- Rebuild js dist files | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Tickets | N/A | License | MIT Tried to update my project deps to use 2-x.dev of LiveComponent, but now that renderer returns HTML instead of JSON, it fails, because the LiveController still uses JSON. This rebuilds dist files from #226, #245, #247 changes. Commits ------- 6906cc6 Rebuild js dist files
2 parents 0c6423c + 6906cc6 commit 9f9cabc

File tree

3 files changed

+46
-31
lines changed

3 files changed

+46
-31
lines changed

src/Dropzone/Resources/assets/dist/controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class default_1 extends Controller {
4343
});
4444
reader.readAsDataURL(file);
4545
}
46-
_dispatchEvent(name, payload) {
46+
_dispatchEvent(name, payload = {}) {
4747
this.element.dispatchEvent(new CustomEvent(name, { detail: payload }));
4848
}
4949
}

src/LazyImage/Resources/assets/dist/controller.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import { Controller } from '@hotwired/stimulus';
33
class default_1 extends Controller {
44
connect() {
55
const hd = new Image();
6+
const element = this.element;
67
const srcsetString = this._calculateSrcsetString();
78
hd.addEventListener('load', () => {
8-
this.element.src = this.srcValue;
9+
element.src = this.srcValue;
910
if (srcsetString) {
10-
this.element.srcset = srcsetString;
11+
element.srcset = srcsetString;
1112
}
1213
this._dispatchEvent('lazy-image:ready', { image: hd });
1314
});

src/LiveComponent/assets/dist/live_controller.js

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,7 @@ function setDeepData(data, propertyPath, value) {
963963
const finalKey = parts[parts.length - 1];
964964
if (typeof currentLevelData !== 'object') {
965965
const lastPart = parts.pop();
966-
throw new Error(`Cannot set data-model="${propertyPath}". They parent "${parts.join(',')}" data does not appear to be an object (it's "${currentLevelData}"). Did you forget to add exposed={"${lastPart}"} to its LiveProp?`);
966+
throw new Error(`Cannot set data-model="${propertyPath}". The parent "${parts.join('.')}" data does not appear to be an object (it's "${currentLevelData}"). Did you forget to add exposed={"${lastPart}"} to its LiveProp?`);
967967
}
968968
if (currentLevelData[finalKey] === undefined) {
969969
const lastPart = parts.pop();
@@ -1014,6 +1014,26 @@ function haveRenderedValuesChanged(originalDataJson, currentDataJson, newDataJso
10141014
return keyHasChanged;
10151015
}
10161016

1017+
function normalizeAttributesForComparison(element) {
1018+
if (element.value) {
1019+
element.setAttribute('value', element.value);
1020+
}
1021+
else if (element.hasAttribute('value')) {
1022+
element.setAttribute('value', '');
1023+
}
1024+
Array.from(element.children).forEach((child) => {
1025+
normalizeAttributesForComparison(child);
1026+
});
1027+
}
1028+
1029+
function cloneHTMLElement(element) {
1030+
const newElement = element.cloneNode(true);
1031+
if (!(newElement instanceof HTMLElement)) {
1032+
throw new Error('Could not clone element');
1033+
}
1034+
return newElement;
1035+
}
1036+
10171037
const DEFAULT_DEBOUNCE = 150;
10181038
class default_1 extends Controller {
10191039
constructor() {
@@ -1112,23 +1132,12 @@ class default_1 extends Controller {
11121132
this._makeRequest(null);
11131133
}
11141134
_getValueFromElement(element) {
1115-
const value = element.dataset.value || element.value;
1116-
if (!value) {
1117-
const clonedElement = (element.cloneNode());
1118-
if (!(clonedElement instanceof HTMLElement)) {
1119-
throw new Error('cloneNode() produced incorrect type');
1120-
}
1121-
throw new Error(`The update() method could not be called for "${clonedElement.outerHTML}": the element must either have a "data-value" or "value" attribute set.`);
1122-
}
1123-
return value;
1135+
return element.dataset.value || element.value;
11241136
}
11251137
_updateModelFromElement(element, value, shouldRender) {
11261138
const model = element.dataset.model || element.getAttribute('name');
11271139
if (!model) {
1128-
const clonedElement = (element.cloneNode());
1129-
if (!(clonedElement instanceof HTMLElement)) {
1130-
throw new Error('cloneNode() produced incorrect type');
1131-
}
1140+
const clonedElement = cloneHTMLElement(element);
11321141
throw new Error(`The update() method could not be called for "${clonedElement.outerHTML}": the element must either have a "data-model" or "name" attribute set to the model name.`);
11331142
}
11341143
this.$updateModel(model, value, shouldRender, element.hasAttribute('name') ? element.getAttribute('name') : null);
@@ -1183,7 +1192,7 @@ class default_1 extends Controller {
11831192
}
11841193
const fetchOptions = {};
11851194
fetchOptions.headers = {
1186-
'Accept': 'application/vnd.live-component+json',
1195+
'Accept': 'application/vnd.live-component+html',
11871196
};
11881197
if (action) {
11891198
url += `/${encodeURIComponent(action)}`;
@@ -1209,34 +1218,30 @@ class default_1 extends Controller {
12091218
}
12101219
const isMostRecent = this.renderPromiseStack.removePromise(thisPromise);
12111220
if (isMostRecent) {
1212-
response.json().then((data) => {
1213-
this._processRerender(data);
1221+
response.text().then((html) => {
1222+
this._processRerender(html, response);
12141223
});
12151224
}
12161225
});
12171226
}
1218-
_processRerender(data) {
1227+
_processRerender(html, response) {
12191228
if (this.isWindowUnloaded) {
12201229
return;
12211230
}
1222-
if (data.redirect_url) {
1231+
if (response.headers.get('Location')) {
12231232
if (typeof Turbo !== 'undefined') {
1224-
Turbo.visit(data.redirect_url);
1233+
Turbo.visit(response.headers.get('Location'));
12251234
}
12261235
else {
1227-
window.location.href = data.redirect_url;
1236+
window.location.href = response.headers.get('Location') || '';
12281237
}
12291238
return;
12301239
}
1231-
if (!this._dispatchEvent('live:render', data, true, true)) {
1240+
if (!this._dispatchEvent('live:render', html, true, true)) {
12321241
return;
12331242
}
12341243
this._onLoadingFinish();
1235-
if (!data.html) {
1236-
throw new Error('Missing html key on response JSON');
1237-
}
1238-
this._executeMorphdom(data.html);
1239-
this.dataValue = data.data;
1244+
this._executeMorphdom(html);
12401245
}
12411246
_clearWaitingDebouncedRenders() {
12421247
if (this.renderDebounceTimeout) {
@@ -1371,7 +1376,13 @@ class default_1 extends Controller {
13711376
morphdom(this.element, newElement, {
13721377
onBeforeElUpdated: (fromEl, toEl) => {
13731378
if (fromEl.isEqualNode(toEl)) {
1374-
return false;
1379+
const normalizedFromEl = cloneHTMLElement(fromEl);
1380+
normalizeAttributesForComparison(normalizedFromEl);
1381+
const normalizedToEl = cloneHTMLElement(toEl);
1382+
normalizeAttributesForComparison(normalizedToEl);
1383+
if (normalizedFromEl.isEqualNode(normalizedToEl)) {
1384+
return false;
1385+
}
13751386
}
13761387
const controllerName = fromEl.hasAttribute('data-controller') ? fromEl.getAttribute('data-controller') : null;
13771388
if (controllerName
@@ -1380,6 +1391,9 @@ class default_1 extends Controller {
13801391
&& !this._shouldChildLiveElementUpdate(fromEl, toEl)) {
13811392
return false;
13821393
}
1394+
if (fromEl.hasAttribute('data-live-ignore')) {
1395+
return false;
1396+
}
13831397
return true;
13841398
}
13851399
});

0 commit comments

Comments
 (0)