|
| 1 | +/** |
| 2 | + * Games Collector Editor |
| 3 | + * |
| 4 | + * All the js that is admin-only (e.g. Gutenberg). |
| 5 | + */ |
| 6 | + |
| 7 | +// Define the textdomain. |
| 8 | +wp.i18n.setLocaleData( { '': {} }, 'games-collector' ); |
| 9 | + |
| 10 | +// Load WP. |
| 11 | +// import WPAPI from 'wpapi'; |
| 12 | +// import 'whatwg-fetch'; |
| 13 | +// import apiFetch from '@wordpress/api-fetch'; |
| 14 | +// import { addQueryArgs } from '@wordpress/url'; |
| 15 | + |
| 16 | +// Load the editor-specific styles. |
| 17 | +import '../../sass/editor.scss'; |
| 18 | + |
| 19 | +// Load front-end styles. |
| 20 | +import '../../sass/style.scss'; |
| 21 | + |
| 22 | +// Load the Gutenberg icons. |
| 23 | +import icons from './icons'; |
| 24 | + |
| 25 | +// Load internal Gutenberg stuff. |
| 26 | +const { __ } = wp.i18n; |
| 27 | +const { registerBlockType } = wp.blocks; |
| 28 | +const { |
| 29 | + Spinner, |
| 30 | + TextControl, |
| 31 | +} = wp.components; |
| 32 | +const { withSelect } = wp.data; |
| 33 | + |
| 34 | +/** |
| 35 | + * Make the first letter of a string uppercase. |
| 36 | + * |
| 37 | + * Mirror's PHP's ucfirst function. |
| 38 | + * |
| 39 | + * @param {string} string The string to process. |
| 40 | + * @return {string} The string with the first letter capitalized. |
| 41 | + */ |
| 42 | +function ucfirst( string ) { |
| 43 | + return string.charAt(0).toUpperCase() + string.slice(1); |
| 44 | +} |
| 45 | + |
| 46 | +// Register the all games Gutenberg block. |
| 47 | +registerBlockType( 'games-collector/add-all-games', { |
| 48 | + title: __( 'All Games', 'games-collector' ), |
| 49 | + description: __( 'Add all games to any post or page.', 'games-collector' ), |
| 50 | + category: 'widgets', |
| 51 | + icon: { |
| 52 | + src: icons.dice |
| 53 | + }, |
| 54 | + keywords: [ |
| 55 | + __( 'Games Collector', 'games-collector' ), |
| 56 | + __( 'game list', 'games-collector' ), |
| 57 | + __( 'all games', 'games-collector' ), |
| 58 | + ], |
| 59 | + edit: withSelect( query => { |
| 60 | + return { |
| 61 | + posts: query( 'core' ).getEntityRecords( 'postType', 'gc_game', { per_page: -1, orderby: 'title', order: 'asc' } ) |
| 62 | + }; |
| 63 | + } )( ( { posts, className, isSelected } ) => { |
| 64 | + if ( ! posts ) { |
| 65 | + return ( |
| 66 | + <p className={ className } > |
| 67 | + <Spinner /> |
| 68 | + { __( 'Loading Posts', 'games-collector' ) } |
| 69 | + </p> |
| 70 | + ); |
| 71 | + } |
| 72 | + if ( 0 === posts.length ) { |
| 73 | + return <p>{ __( 'No Posts', 'games-collector' ) }</p>; |
| 74 | + } |
| 75 | + return ( |
| 76 | + <div className={ className }> |
| 77 | + { posts.map( post => { |
| 78 | + let divId = `game-${ post.id }-info`, |
| 79 | + title = ( 'undefined' !== typeof post.url ) ? ` |
| 80 | + <a href=${ post.url.toString() }><span className="game-title" id="game-${ post.id }-title">${ post.title.rendered }</span></a>` : `<span className="game-title" id="game-${ post.id }-title">${ post.title.rendered }</span>`, |
| 81 | + numPlayers = { |
| 82 | + id: `game-${ post.id }-num-players`, |
| 83 | + total: ( |
| 84 | + 'undefined' === typeof post.max_players || |
| 85 | + post.min_players[0] === post.max_players[0] |
| 86 | + ) ? post.min_players[0] : `${ post.min_players[0] } - ${ post.max_players[0] }`, |
| 87 | + }, |
| 88 | + playingTime = { |
| 89 | + id: `game-${ post.id }-playing-time`, |
| 90 | + message: `${ post.time } minutes`, |
| 91 | + }, |
| 92 | + age = { |
| 93 | + id: `game-${ post.id }-age`, |
| 94 | + message: `${ post.age }+`, |
| 95 | + }, |
| 96 | + difficulty = { |
| 97 | + id: `game-${ post.id }-difficulty`, |
| 98 | + }, |
| 99 | + attributes = { |
| 100 | + id: `game-${ post.id }-attributes`, |
| 101 | + message: `${ post.attributes.join( ', ' ) }`, |
| 102 | + }; |
| 103 | + |
| 104 | + return ( |
| 105 | + <div className={ className }> |
| 106 | + <div dangerouslySetInnerHTML={{ __html: title }} /> |
| 107 | + <div className="game-info" id={ divId }> |
| 108 | + <span className="gc-icon icon-game-players">{ icons.players }</span><span className="game-num-players" id={ numPlayers.id }>{ numPlayers.total }</span> |
| 109 | + <span className="gc-icon icon-game-time">{ icons.time }</span><span className="game-playing-time" id={ playingTime.id }>{ playingTime.message }</span> |
| 110 | + <span className="gc-icon icon-game-age">{ icons.age }</span><span className="game-age" id={ age.id }>{ age.message }</span> |
| 111 | + <span className="gc-icon icon-game-difficulty">{ icons.difficulty }</span><span className="game-difficulty" id={ difficulty.id }>{ ucfirst( post.difficulty[0] ) }</span> |
| 112 | + <div className="game-attributes"> |
| 113 | + <span className="gc-icon icon-game-attributes">{ icons.tags }</span><span className="game-attributes" id={ attributes.id }>{ attributes.message }</span> |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + </div> |
| 117 | + ); |
| 118 | + }) } |
| 119 | + </div> |
| 120 | + ); |
| 121 | + } ) // end withSelect |
| 122 | + , // end edit |
| 123 | + save() { |
| 124 | + // Rendering in PHP |
| 125 | + return null; |
| 126 | + }, |
| 127 | +}); |
0 commit comments