PluginProbe
WP Ultimate Post Grid / 4.0.1
WP Ultimate Post Grid v4.0.1
4.1.1 trunk 1.5 1.6 1.7 1.7.1 1.7.2 1.8 1.9 2.0 2.1 2.2 2.3 2.4.0 2.5.0 2.6.0 2.7.0 2.8.0 2.8.2 3.0.0 3.3.0 3.4.0 3.5.0 3.6.0 3.7.0 All 32 releases
wp-ultimate-post-grid / assets / js / admin-modal / grid / Preview.js

Preview.js in WP Ultimate Post Grid 4.0.1, at assets/js/admin-modal/grid/Preview.js

178 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import React, { Component, Fragment } from 'react';
2
3 import Api from 'Shared/Api';
4 import Loader from 'Shared/Loader';
5 import { __wpupg } from 'Shared/Translations';
6
7 export default class Preview extends Component {
8 constructor(props) {
9 super(props);
10
11 // Set initial state.
12 this.state = {
13 loading: false,
14 needsRefresh: false,
15 html: false,
16 hasError: false,
17 device: 'desktop',
18 };
19 }
20
21 componentDidCatch() {
22 this.setState({
23 hasError: true,
24 });
25 }
26
27 componentDidMount() {
28 this.previewGrid();
29 }
30
31 componentDidUpdate(prevProps) {
32 let newGrid = JSON.parse( JSON.stringify( this.props.grid ) );
33 let oldGrid = JSON.parse( JSON.stringify( prevProps.grid ) );
34
35 // Ignore grid fields.
36 const ignoreGridFields = [ 'name', 'slug', 'metadata', 'metadata_name', 'metadata_description' ];
37 for ( let ignoreField of ignoreGridFields ) {
38 delete newGrid[ ignoreField ];
39 delete oldGrid[ ignoreField ];
40 }
41
42 // Ignore filter fields
43 const ignoreFilterFields = [ 'id' ];
44 for ( let i = 0; i < newGrid.filters.length; i ++ ) {
45 for ( let ignoreField of ignoreFilterFields ) {
46 delete newGrid.filters[ i ][ ignoreField ];
47
48 if ( oldGrid.filters.length > i ) {
49 delete oldGrid.filters[ i ][ ignoreField ];
50 }
51 }
52 }
53
54 if ( JSON.stringify( newGrid ) !== JSON.stringify( oldGrid ) ) {
55 this.previewGrid();
56 }
57 }
58
59 previewGrid() {
60 if ( this.state.loading ) {
61 if ( ! this.state.needsRefresh ) {
62 this.setState({
63 needsRefresh: true,
64 });
65 }
66 } else {
67 this.setState({
68 loading: true,
69 }, () => {
70 let previewGrid = JSON.parse( JSON.stringify( this.props.grid ) );
71 previewGrid.slug = 'preview';
72
73 Api.preview.grid( previewGrid ).then((data) => {
74 this.setState({
75 html: data.html,
76 loading: false,
77 hasError: false,
78 }, () => {
79 WPUPG_Grid.init( 'wpupg-grid-preview', data.args );
80
81 // Need to refresh again.
82 if ( this.state.needsRefresh ) {
83 this.setState({
84 needsRefresh: false,
85 }, () => this.previewGrid() );
86 }
87 });
88 });
89 });
90 }
91 }
92
93 checkPremium() {
94 if ( ! wpupg_admin.addons.premium ) {
95 const { grid } = this.props;
96
97 // Data Source.
98 if ( 'posts' !== grid.type ) { return true; }
99 if ( 'custom' === grid.order_by ) { return true; }
100
101 // Pagination.
102 if ( ! [ 'none', 'pages' ].includes( grid.pagination_type ) ) { return true; }
103
104 // Filters.
105 if ( grid.filters_enabled ) {
106 for ( let i = 0; i < grid.filters.length; i++ ) {
107 const filter = grid.filters[ i ];
108
109 if ( 'isotope' !== filter.type ) { return true; }
110 if ( filter.options.count ) { return true; }
111 if ( filter.options.multiselect ) { return true; }
112 }
113 }
114 }
115
116 return false;
117 }
118
119 render() {
120 const devices = [
121 {
122 value: 'desktop',
123 label: __wpupg( 'Desktop Preview' ),
124 },
125 {
126 value: 'tablet',
127 label: __wpupg( 'Tablet Preview' ),
128 },
129 {
130 value: 'mobile',
131 label: __wpupg( 'Mobile Preview' ),
132 },
133 ];
134
135 return (
136 <div className="wpupg-admin-modal-grid-preview-container">
137 <div className="wpupg-admin-modal-fields-group-header wpupg-admin-modal-grid-preview-header">
138 {
139 devices.map((device, index) => (
140 <span
141 onClick={ () => {
142 this.setState({
143 device: device.value,
144 }, () => {
145 WPUPG_Grids[ 'wpupg-grid-preview' ].layout();
146 });
147 }}
148 data-device={ device.value }
149 className={ `wpupg-admin-modal-grid-preview-device${device.value === this.state.device ? ' active' : ''}` }
150 key={ index }
151 >{ device.label }</span>
152 ))
153 }
154 </div>
155 {
156 this.checkPremium()
157 ?
158 <p style={ { color: 'darkred' } }>
159 { __wpupg( "You've selected features that are only available in" ) } <a href="https://bootstrapped.ventures/wp-ultimate-post-grid/get-the-plugin/">WP Ultimate Post Grid Premium</a>.
160 </p>
161 :
162 <Fragment>
163 {
164 this.state.loading || this.state.hasError || false === this.state.html
165 ?
166 <Loader />
167 :
168 <div
169 className={ `wpupg-admin-modal-grid-preview wpupg-admin-modal-grid-preview-${ this.state.device }` }
170 dangerouslySetInnerHTML={ { __html: this.state.html } }
171 />
172 }
173 </Fragment>
174 }
175 </div>
176 );
177 }
178 }