summaryrefslogtreecommitdiff
path: root/views/mods/mod_productview/productview.js
blob: 34c0df9088608f64b6c0a0c44a5c576e2f88ecc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
 * wpShopGermany, Modul Produktansichten: Seiten, Sortierung, Ansicht und Filter ohne Neuladen
 *
 * Ohne JavaScript funktionieren Links und Formulare über die Adresse. Mit JavaScript holt das Skript die Ansicht per
 * Ajax (wpsg_mod=wpsg_mod_productview, wpsg_action=refresh) und schreibt den Zustand in die Adresse. Die Attribute des
 * Shortcodes kommen signiert aus dem Feld wpsg_atts; der Server übernimmt nur Seite, Sortierung, Ansicht und Filter.
 * Produktlisten mit data-wpsg-filter (Block Produktliste) holt es mit der neuen Adresse als ganze Seite, wie ohne
 * JavaScript, und tauscht ihren Inhalt; die Statuszeile bleibt stehen und sagt die neue Anzahl an.
 */

( function() {

	'use strict';

	function flatten( prefix, value, params ) {

		if ( value !== null && typeof value === 'object' ) {

			Object.keys( value ).forEach( function( k ) { flatten( prefix + '[' + k + ']', value[ k ], params ); } );

		} else if ( value !== undefined && value !== null ) {

			params.append( prefix, String( value ) );

		}

		return params;

	}

	function filterValues() {

		const form = document.getElementById( 'wpsg_mod_productview_filter' );
		const values = {};

		if ( !form ) return values;

		new FormData( form ).forEach( function( v, name ) {

			const m = name.match( /^pvf\[(\w+)\](?:\[(\d+)\])?/ );
			if ( !m || v === '' ) return;

			if ( m[ 2 ] ) {

				values[ m[ 1 ] ] = values[ m[ 1 ] ] || {};
				values[ m[ 1 ] ][ m[ 2 ] ] = values[ m[ 1 ] ][ m[ 2 ] ] ? values[ m[ 1 ] ][ m[ 2 ] ] + ',' + v : v;

			} else values[ m[ 1 ] ] = v;

		} );

		return values;

	}

	// Laufende Anfragen je Ansicht (Position auf der Seite) und für die Produktlisten: Nur die zuletzt gestellte zählt
	const pending = [];
	let pendingLists = null;

	function refresh( box, change, url, push ) {

		const field = box.querySelector( 'input[name="wpsg_atts"]' );
		if ( !field ) return false;

		const index = Array.prototype.indexOf.call( document.querySelectorAll( '.wpsg_productview' ), box );
		if ( pending[ index ] ) pending[ index ].abort();
		const ctl = pending[ index ] = new AbortController();

		const atts = JSON.parse( field.value );
		Object.assign( atts, change );

		const params = flatten( 'wpsg_atts', atts, new URLSearchParams( { wpsg_mod: 'wpsg_mod_productview', wpsg_action: 'refresh' } ) );

		box.classList.add( 'is-loading' );
		box.setAttribute( 'aria-busy', 'true' );

		fetch( box.getAttribute( 'data-wpsg-pv-url' ) + ( box.getAttribute( 'data-wpsg-pv-url' ).indexOf( '?' ) === -1 ? '?' : '&' ) + params.toString(), { credentials: 'same-origin', signal: ctl.signal } )
			.then( function( r ) { if ( !r.ok ) throw new Error( r.status ); return r.text(); } )
			.then( function( html ) {

				if ( pending[ index ] !== ctl ) return;
				pending[ index ] = null;

				const tmp = document.createElement( 'div' );
				tmp.innerHTML = html;

				const fresh = tmp.querySelector( '.wpsg_productview' );
				if ( !fresh ) throw new Error( 'leer' );

				fresh.querySelectorAll( '.wpsg-pv__order-submit' ).forEach( function( b ) { b.hidden = true; } );
				// Eine frühere Antwort kann die Ansicht schon ersetzt haben
				document.querySelectorAll( '.wpsg_productview' )[ index ].replaceWith( fresh );
				if ( url && push !== false ) history.pushState( { wpsgPv: true }, '', url );

			} )
			.catch( function() { if ( url && pending[ index ] === ctl ) location.href = url; } );

		return true;

	}

	document.addEventListener( 'click', function( e ) {

		const a = e.target.closest( '.wpsg-pv__pager a, .wpsg-pv__modes a' );
		if ( !a ) return;

		const box = a.closest( '.wpsg_productview' );
		const change = a.hasAttribute( 'data-page' ) ? { page: a.getAttribute( 'data-page' ) } : { mode: a.getAttribute( 'data-mode' ) };

		if ( refresh( box, change, a.href ) ) e.preventDefault();

	} );

	document.addEventListener( 'change', function( e ) {

		const select = e.target.closest( '.wpsg-pv__order select' );

		if ( select ) {

			const form = select.form;
			const url = form.action + ( form.action.indexOf( '?' ) === -1 ? '?' : '&' ) + new URLSearchParams( new FormData( form ) ).toString();

			refresh( select.closest( '.wpsg_productview' ), { order: select.value, page: 1 }, url );
			return;

		}

		if ( e.target.closest( '#wpsg_mod_productview_filter' ) ) applyFilter();

	} );

	document.addEventListener( 'submit', function( e ) {

		if ( e.target.id !== 'wpsg_mod_productview_filter' ) return;

		if ( applyFilter() ) e.preventDefault();

	} );

	function refreshLists( lists, url ) {

		lists.forEach( function( list ) { list.setAttribute( 'aria-busy', 'true' ); } );

		if ( pendingLists ) pendingLists.abort();
		const ctl = pendingLists = new AbortController();

		fetch( url, { credentials: 'same-origin', signal: ctl.signal } )
			.then( function( r ) { if ( !r.ok ) throw new Error( r.status ); return r.text(); } )
			.then( function( html ) {

				if ( pendingLists !== ctl ) return;
				pendingLists = null;

				const fresh = new DOMParser().parseFromString( html, 'text/html' ).querySelectorAll( '[data-wpsg-filter]' );
				if ( fresh.length !== lists.length ) throw new Error( 'anders' );

				lists.forEach( function( list, i ) {

					const status = list.querySelector( '.wpsg-product-list__count' );

					Array.prototype.slice.call( list.children ).forEach( function( child ) { if ( child !== status ) child.remove(); } );
					Array.prototype.slice.call( fresh[ i ].children ).forEach( function( child ) { if ( !child.classList.contains( 'wpsg-product-list__count' ) ) list.appendChild( document.importNode( child, true ) ); } );

					const freshStatus = fresh[ i ].querySelector( '.wpsg-product-list__count' );
					if ( status && freshStatus ) status.textContent = freshStatus.textContent;

					list.removeAttribute( 'aria-busy' );

				} );

			} )
			.catch( function() { if ( pendingLists === ctl ) location.href = url; } );

	}

	function applyFilter() {

		const form = document.getElementById( 'wpsg_mod_productview_filter' );
		const boxes = document.querySelectorAll( '.wpsg_productview' );
		const lists = document.querySelectorAll( '[data-wpsg-filter]' );
		if ( !form || ( !boxes.length && !lists.length ) ) return false;

		const params = new URLSearchParams();
		new FormData( form ).forEach( function( v, name ) { if ( v !== '' && !/^pv-/.test( name ) ) params.append( name, v ); } );

		// Sortierung und Ansicht aus der aktuellen Adresse behalten, die Seite beginnt neu
		new URLSearchParams( location.search ).forEach( function( v, name ) { if ( /^pv-.*(order|mode)$/.test( name ) ) params.append( name, v ); } );

		const url = form.action + ( form.action.indexOf( '?' ) === -1 ? '?' : '&' ) + params.toString();
		const values = filterValues();

		boxes.forEach( function( box ) { refresh( box, { filter: values, page: 1 }, url, false ); } );
		if ( lists.length ) refreshLists( lists, url );

		history.pushState( { wpsgPv: true }, '', url );

		return true;

	}

	// Zurück und Vor: den Stand aus der Adresse neu laden
	window.addEventListener( 'popstate', function( e ) { if ( e.state && e.state.wpsgPv ) location.reload(); } );

	// Mit JavaScript sortiert die Auswahl sofort
	document.querySelectorAll( '.wpsg-pv__order-submit' ).forEach( function( b ) { b.hidden = true; } );

} )();