Skip to content

Commit e8a21ca

Browse files
committed
Remove unused QueryStringPlugin
1 parent 599bac9 commit e8a21ca

File tree

8 files changed

+112
-187
lines changed

8 files changed

+112
-187
lines changed

src/LiveComponent/assets/dist/Component/plugins/QueryStringPlugin.d.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/LiveComponent/assets/dist/live_controller.d.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ export default class LiveControllerDefault extends Controller<HTMLElement> imple
4949
type: StringConstructor;
5050
default: string;
5151
};
52-
queryMapping: {
53-
type: ObjectConstructor;
54-
default: {};
55-
};
5652
};
5753
readonly nameValue: string;
5854
readonly urlValue: string;
@@ -76,11 +72,6 @@ export default class LiveControllerDefault extends Controller<HTMLElement> imple
7672
readonly debounceValue: number;
7773
readonly fingerprintValue: string;
7874
readonly requestMethodValue: 'get' | 'post';
79-
readonly queryMappingValue: {
80-
[p: string]: {
81-
name: string;
82-
};
83-
};
8475
private proxiedComponent;
8576
private mutationObserver;
8677
component: Component;

src/LiveComponent/assets/dist/live_controller.js

Lines changed: 104 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class RequestBuilder {
3333
fetchOptions.headers = {
3434
Accept: 'application/vnd.live-component+html',
3535
'X-Requested-With': 'XMLHttpRequest',
36-
'X-Live-Url': window.location.pathname + window.location.search
36+
'X-Live-Url': window.location.pathname + window.location.search,
3737
};
3838
const totalFiles = Object.entries(files).reduce((total, current) => total + current.length, 0);
3939
const hasFingerprints = Object.keys(children).length > 0;
@@ -1805,7 +1805,109 @@ class ExternalMutationTracker {
18051805
return element.tagName === 'FONT' && element.getAttribute('style') === 'vertical-align: inherit;';
18061806
}
18071807
}
1808-
1808+
function isValueEmpty(value) {
1809+
if (null === value || value === '' || undefined === value || (Array.isArray(value) && value.length === 0)) {
1810+
return true;
1811+
}
1812+
if (typeof value !== 'object') {
1813+
return false;
1814+
}
1815+
for (const key of Object.keys(value)) {
1816+
if (!isValueEmpty(value[key])) {
1817+
return false;
1818+
}
1819+
}
1820+
return true;
1821+
}
1822+
function toQueryString(data) {
1823+
const buildQueryStringEntries = (data, entries = {}, baseKey = '') => {
1824+
Object.entries(data).forEach(([iKey, iValue]) => {
1825+
const key = baseKey === '' ? iKey : `${baseKey}[${iKey}]`;
1826+
if ('' === baseKey && isValueEmpty(iValue)) {
1827+
entries[key] = '';
1828+
}
1829+
else if (null !== iValue) {
1830+
if (typeof iValue === 'object') {
1831+
entries = { ...entries, ...buildQueryStringEntries(iValue, entries, key) };
1832+
}
1833+
else {
1834+
entries[key] = encodeURIComponent(iValue)
1835+
.replace(/%20/g, '+')
1836+
.replace(/%2C/g, ',');
1837+
}
1838+
}
1839+
});
1840+
return entries;
1841+
};
1842+
const entries = buildQueryStringEntries(data);
1843+
return Object.entries(entries)
1844+
.map(([key, value]) => `${key}=${value}`)
1845+
.join('&');
1846+
}
1847+
function fromQueryString(search) {
1848+
search = search.replace('?', '');
1849+
if (search === '')
1850+
return {};
1851+
const insertDotNotatedValueIntoData = (key, value, data) => {
1852+
const [first, second, ...rest] = key.split('.');
1853+
if (!second) {
1854+
data[key] = value;
1855+
return value;
1856+
}
1857+
if (data[first] === undefined) {
1858+
data[first] = Number.isNaN(Number.parseInt(second)) ? {} : [];
1859+
}
1860+
insertDotNotatedValueIntoData([second, ...rest].join('.'), value, data[first]);
1861+
};
1862+
const entries = search.split('&').map((i) => i.split('='));
1863+
const data = {};
1864+
entries.forEach(([key, value]) => {
1865+
value = decodeURIComponent(String(value || '').replace(/\+/g, '%20'));
1866+
if (!key.includes('[')) {
1867+
data[key] = value;
1868+
}
1869+
else {
1870+
if ('' === value)
1871+
return;
1872+
const dotNotatedKey = key.replace(/\[/g, '.').replace(/]/g, '');
1873+
insertDotNotatedValueIntoData(dotNotatedKey, value, data);
1874+
}
1875+
});
1876+
return data;
1877+
}
1878+
class UrlUtils extends URL {
1879+
has(key) {
1880+
const data = this.getData();
1881+
return Object.keys(data).includes(key);
1882+
}
1883+
set(key, value) {
1884+
const data = this.getData();
1885+
data[key] = value;
1886+
this.setData(data);
1887+
}
1888+
get(key) {
1889+
return this.getData()[key];
1890+
}
1891+
remove(key) {
1892+
const data = this.getData();
1893+
delete data[key];
1894+
this.setData(data);
1895+
}
1896+
getData() {
1897+
if (!this.search) {
1898+
return {};
1899+
}
1900+
return fromQueryString(this.search);
1901+
}
1902+
setData(data) {
1903+
this.search = toQueryString(data);
1904+
}
1905+
}
1906+
class HistoryStrategy {
1907+
static replace(url) {
1908+
history.replaceState(history.state, '', url);
1909+
}
1910+
}
18091911
class UnsyncedInputsTracker {
18101912
constructor(component, modelElementResolver) {
18111913
this.elementEventListeners = [
@@ -1984,110 +2086,6 @@ class ValueStore {
19842086
}
19852087
}
19862088

1987-
function isValueEmpty(value) {
1988-
if (null === value || value === '' || undefined === value || (Array.isArray(value) && value.length === 0)) {
1989-
return true;
1990-
}
1991-
if (typeof value !== 'object') {
1992-
return false;
1993-
}
1994-
for (const key of Object.keys(value)) {
1995-
if (!isValueEmpty(value[key])) {
1996-
return false;
1997-
}
1998-
}
1999-
return true;
2000-
}
2001-
function toQueryString(data) {
2002-
const buildQueryStringEntries = (data, entries = {}, baseKey = '') => {
2003-
Object.entries(data).forEach(([iKey, iValue]) => {
2004-
const key = baseKey === '' ? iKey : `${baseKey}[${iKey}]`;
2005-
if ('' === baseKey && isValueEmpty(iValue)) {
2006-
entries[key] = '';
2007-
}
2008-
else if (null !== iValue) {
2009-
if (typeof iValue === 'object') {
2010-
entries = { ...entries, ...buildQueryStringEntries(iValue, entries, key) };
2011-
}
2012-
else {
2013-
entries[key] = encodeURIComponent(iValue)
2014-
.replace(/%20/g, '+')
2015-
.replace(/%2C/g, ',');
2016-
}
2017-
}
2018-
});
2019-
return entries;
2020-
};
2021-
const entries = buildQueryStringEntries(data);
2022-
return Object.entries(entries)
2023-
.map(([key, value]) => `${key}=${value}`)
2024-
.join('&');
2025-
}
2026-
function fromQueryString(search) {
2027-
search = search.replace('?', '');
2028-
if (search === '')
2029-
return {};
2030-
const insertDotNotatedValueIntoData = (key, value, data) => {
2031-
const [first, second, ...rest] = key.split('.');
2032-
if (!second) {
2033-
data[key] = value;
2034-
return value;
2035-
}
2036-
if (data[first] === undefined) {
2037-
data[first] = Number.isNaN(Number.parseInt(second)) ? {} : [];
2038-
}
2039-
insertDotNotatedValueIntoData([second, ...rest].join('.'), value, data[first]);
2040-
};
2041-
const entries = search.split('&').map((i) => i.split('='));
2042-
const data = {};
2043-
entries.forEach(([key, value]) => {
2044-
value = decodeURIComponent(String(value || '').replace(/\+/g, '%20'));
2045-
if (!key.includes('[')) {
2046-
data[key] = value;
2047-
}
2048-
else {
2049-
if ('' === value)
2050-
return;
2051-
const dotNotatedKey = key.replace(/\[/g, '.').replace(/]/g, '');
2052-
insertDotNotatedValueIntoData(dotNotatedKey, value, data);
2053-
}
2054-
});
2055-
return data;
2056-
}
2057-
class UrlUtils extends URL {
2058-
has(key) {
2059-
const data = this.getData();
2060-
return Object.keys(data).includes(key);
2061-
}
2062-
set(key, value) {
2063-
const data = this.getData();
2064-
data[key] = value;
2065-
this.setData(data);
2066-
}
2067-
get(key) {
2068-
return this.getData()[key];
2069-
}
2070-
remove(key) {
2071-
const data = this.getData();
2072-
delete data[key];
2073-
this.setData(data);
2074-
}
2075-
getData() {
2076-
if (!this.search) {
2077-
return {};
2078-
}
2079-
return fromQueryString(this.search);
2080-
}
2081-
setData(data) {
2082-
this.search = toQueryString(data);
2083-
}
2084-
}
2085-
class HistoryStrategy {
2086-
static replace(url) {
2087-
history.replaceState(history.state, '', url);
2088-
}
2089-
}
2090-
20912089
class Component {
20922090
constructor(element, name, props, listeners, id, backend, elementDriver) {
20932091
this.fingerprint = '';
@@ -2885,25 +2883,6 @@ class PollingPlugin {
28852883
}
28862884
}
28872885

2888-
class QueryStringPlugin {
2889-
constructor(mapping) {
2890-
this.mapping = mapping;
2891-
}
2892-
attachToComponent(component) {
2893-
component.on('render:finished', (component) => {
2894-
const urlUtils = new UrlUtils(window.location.href);
2895-
const currentUrl = urlUtils.toString();
2896-
Object.entries(this.mapping).forEach(([prop, mapping]) => {
2897-
const value = component.valueStore.get(prop);
2898-
urlUtils.set(mapping.name, value);
2899-
});
2900-
if (currentUrl !== urlUtils.toString()) {
2901-
HistoryStrategy.replace(urlUtils);
2902-
}
2903-
});
2904-
}
2905-
}
2906-
29072886
class SetValueOntoModelFieldsPlugin {
29082887
attachToComponent(component) {
29092888
this.synchronizeValueOfModelFields(component);
@@ -3113,7 +3092,6 @@ class LiveControllerDefault extends Controller {
31133092
new PageUnloadingPlugin(),
31143093
new PollingPlugin(),
31153094
new SetValueOntoModelFieldsPlugin(),
3116-
new QueryStringPlugin(this.queryMappingValue),
31173095
new ChildComponentPlugin(this.component),
31183096
];
31193097
plugins.forEach((plugin) => {
@@ -3244,7 +3222,6 @@ LiveControllerDefault.values = {
32443222
debounce: { type: Number, default: 150 },
32453223
fingerprint: { type: String, default: '' },
32463224
requestMethod: { type: String, default: 'post' },
3247-
queryMapping: { type: Object, default: {} },
32483225
};
32493226
LiveControllerDefault.backendFactory = (controller) => new Backend(controller.urlValue, controller.requestMethodValue);
32503227

src/LiveComponent/assets/src/Backend/RequestBuilder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default class {
2626
fetchOptions.headers = {
2727
Accept: 'application/vnd.live-component+html',
2828
'X-Requested-With': 'XMLHttpRequest',
29-
'X-Live-Url' : window.location.pathname + window.location.search
29+
'X-Live-Url': window.location.pathname + window.location.search,
3030
};
3131

3232
const totalFiles = Object.entries(files).reduce((total, current) => total + current.length, 0);

src/LiveComponent/assets/src/Component/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import HookManager from '../HookManager';
77
import { executeMorphdom } from '../morphdom';
88
import ExternalMutationTracker from '../Rendering/ExternalMutationTracker';
99
import { normalizeModelName } from '../string_utils';
10+
import { HistoryStrategy, UrlUtils } from "../url_utils";
1011
import type { ElementDriver } from './ElementDriver';
1112
import type { PluginInterface } from './plugins/PluginInterface';
1213
import UnsyncedInputsTracker from './UnsyncedInputsTracker';
1314
import ValueStore from './ValueStore';
14-
import {HistoryStrategy, UrlUtils} from "../url_utils";
1515

1616
declare const Turbo: any;
1717

src/LiveComponent/assets/src/Component/plugins/QueryStringPlugin.ts

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/LiveComponent/assets/src/live_controller.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import LoadingPlugin from './Component/plugins/LoadingPlugin';
88
import PageUnloadingPlugin from './Component/plugins/PageUnloadingPlugin';
99
import type { PluginInterface } from './Component/plugins/PluginInterface';
1010
import PollingPlugin from './Component/plugins/PollingPlugin';
11-
import QueryStringPlugin from './Component/plugins/QueryStringPlugin';
1211
import SetValueOntoModelFieldsPlugin from './Component/plugins/SetValueOntoModelFieldsPlugin';
1312
import ValidatedFieldsPlugin from './Component/plugins/ValidatedFieldsPlugin';
1413
import { type DirectiveModifier, parseDirectives } from './Directive/directives_parser';
@@ -50,7 +49,6 @@ export default class LiveControllerDefault extends Controller<HTMLElement> imple
5049
debounce: { type: Number, default: 150 },
5150
fingerprint: { type: String, default: '' },
5251
requestMethod: { type: String, default: 'post' },
53-
queryMapping: { type: Object, default: {} },
5452
};
5553

5654
declare readonly nameValue: string;
@@ -69,7 +67,6 @@ export default class LiveControllerDefault extends Controller<HTMLElement> imple
6967
declare readonly debounceValue: number;
7068
declare readonly fingerprintValue: string;
7169
declare readonly requestMethodValue: 'get' | 'post';
72-
declare readonly queryMappingValue: { [p: string]: { name: string } };
7370

7471
/** The component, wrapped in the convenience Proxy */
7572
private proxiedComponent: Component;
@@ -309,7 +306,6 @@ export default class LiveControllerDefault extends Controller<HTMLElement> imple
309306
new PageUnloadingPlugin(),
310307
new PollingPlugin(),
311308
new SetValueOntoModelFieldsPlugin(),
312-
new QueryStringPlugin(this.queryMappingValue),
313309
new ChildComponentPlugin(this.component),
314310
];
315311
plugins.forEach((plugin) => {

0 commit comments

Comments
 (0)