PageSourceSearch

https://rhtw.de/wp-content/plugins/wp-postratings/js/wp-postratings.js?ver=2.0.0

js rhtw.de collected 2026-10-04 02:23:18 UTC 3,731 bytes, 141 lines download raw bytes

1/**
2 * WP-PostRatings front end.
3 *
4 * Considerably smaller since 2.0.0. Hovering and filling the scale are CSS --
5 * `label:hover ~ label` on a reversed row -- so this no longer rewrites image
6 * sources on every mouse move. All it does is post the vote and swap in the
7 * result.
8 *
9 * One delegated listener on document covers every rating on the page, whether
10 * it was in the original markup or arrived later.
11 */
12( function() {
13	'use strict';
14
15	const l10n = window.wpPostRatingsL10n || {};
16
17	/** Posts currently mid-vote, so a second click cannot double-submit. */
18	const inFlight = new Set();
19
20	/**
21	 * Post a vote and replace the control with whatever comes back.
22	 *
23	 * @param {Element} control The fieldset or button group that was used.
24	 * @param {number}  rating  Value chosen.
25	 * @return {void}
26	 */
27	function vote( control, rating ) {
28		const postId = Number( control.dataset.postId );
29
30		if ( ! postId || ! rating ) {
31			return;
32		}
33
34		if ( inFlight.has( postId ) ) {
35			// eslint-disable-next-line no-alert -- Long-standing behaviour: warns rather than queueing a second vote.
36			window.alert( l10n.textWait );
37			return;
38		}
39
40		const container = document.getElementById( 'wp-postratings-' + postId );
41
42		if ( ! container ) {
43			return;
44		}
45
46		const nonce = container.dataset.nonce || container.getAttribute( 'data-nonce' );
47
48		inFlight.add( postId );
49
50		// aria-busy rather than an opacity tween: it dims the control through
51		// CSS *and* tells assistive technology the region is updating. Always
52		// set, because the second half of that is not a preference. A site that
53		// wants no dimming styles .wp-postratings[aria-busy="true"].
54		container.setAttribute( 'aria-busy', 'true' );
55
56		const body = new URLSearchParams();
57
58		body.append( 'action', 'wp_postratings' );
59		body.append( 'pid', postId );
60		body.append( 'rate', rating );
61		body.append( 'wp_postratings_' + postId + '_nonce', nonce );
62
63		/**
64		 * Put the control back into a usable state.
65		 *
66		 * @param {string} [html] Replacement markup.
67		 */
68		function finish( html ) {
69			if ( 'string' === typeof html ) {
70				container.innerHTML = html;
71			}
72
73			container.removeAttribute( 'aria-busy' );
74			inFlight.delete( postId );
75		}
76
77		window
78			.fetch( l10n.ajaxUrl, {
79				method: 'POST',
80				credentials: 'same-origin',
81				headers: {
82					'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
83				},
84				body: body.toString(),
85			} )
86			.then( function( response ) {
87				return response.text();
88			} )
89			.then( finish )
90			.catch( function() {
91				finish();
92			} );
93	}
94
95	/**
96	 * Attach the delegated listeners.
97	 *
98	 * @return {void}
99	 */
100	function start() {
101		// A scale is radio inputs, so "change" is the right signal: it fires
102		// for a click and for arrow-key navigation alike, which is what makes
103		// the control usable from the keyboard without any extra handling.
104		document.addEventListener( 'change', function( event ) {
105			const input = event.target.closest( '.wp-postratings-scale input[type="radio"]' );
106
107			if ( ! input ) {
108				return;
109			}
110
111			const control = input.closest( '.wp-postratings-vote' );
112
113			if ( control ) {
114				vote( control, Number( input.value ) );
115			}
116		} );
117
118		// An up/down pair is two buttons, not a scale.
119		document.addEventListener( 'click', function( event ) {
120			const button = event.target.closest( '.wp-postratings-updown button' );
121
122			if ( ! button ) {
123				return;
124			}
125
126			event.preventDefault();
127
128			const control = button.closest( '.wp-postratings-vote' );
129
130			if ( control ) {
131				vote( control, Number( button.dataset.rating ) );
132			}
133		} );
134	}
135
136	if ( 'loading' === document.readyState ) {
137		document.addEventListener( 'DOMContentLoaded', start );
138	} else {
139		start();
140	}
141}() );

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.