controls.js
1 month ago
edit.js
1 month ago
fields-filters.js
1 month ago
fields-html-markup.js
1 month ago
fields-main.js
1 month ago
fields-post-settings.js
1 month ago
fields-stats-tag.js
1 month ago
fields-main.js
122 lines
| 1 | import { escape_html, unescape_html } from '../../utils'; |
| 2 | |
| 3 | const { Fragment } = wp.element; |
| 4 | const { CheckboxControl, SelectControl, TextControl } = wp.components; |
| 5 | const { __ } = wp.i18n; |
| 6 | |
| 7 | export function MainFields({ attributes, setAttributes }) { |
| 8 | const { |
| 9 | title, |
| 10 | limit, |
| 11 | order_by, |
| 12 | range, |
| 13 | time_quantity, |
| 14 | time_unit, |
| 15 | freshness |
| 16 | } = attributes; |
| 17 | |
| 18 | const labelTitle = __('Title', 'wordpress-popular-posts'); |
| 19 | const labelLimit = __('Limit', 'wordpress-popular-posts'); |
| 20 | const labelSortBy = __('Sort posts by', 'wordpress-popular-posts'); |
| 21 | const labelTimeRange = __('Time Range', 'wordpress-popular-posts'); |
| 22 | const labelTimeQuantity = __('Time Quantity', 'wordpress-popular-posts'); |
| 23 | const labelTimeUnit = __('Time Unit', 'wordpress-popular-posts'); |
| 24 | const labelFreshness = __('Display only posts published within the selected Time Range', 'wordpress-popular-posts'); |
| 25 | |
| 26 | const sortingOptions = [ |
| 27 | {label: __('Total views', 'wordpress-popular-posts'), value: 'views'}, |
| 28 | {label: __('Avg. daily views', 'wordpress-popular-posts'), value: 'avg'}, |
| 29 | {label: __('Comments', 'wordpress-popular-posts'), value: 'comments'} |
| 30 | ]; |
| 31 | |
| 32 | const timeRangeOptions = [ |
| 33 | {label: __('Last 24 Hours', 'wordpress-popular-posts'), value: 'last24hours'}, |
| 34 | {label: __('Last 7 days', 'wordpress-popular-posts'), value: 'last7days'}, |
| 35 | {label: __('Last 30 days', 'wordpress-popular-posts'), value: 'last30days'}, |
| 36 | {label: __('All-time', 'wordpress-popular-posts'), value: 'all'}, |
| 37 | {label: __('Custom', 'wordpress-popular-posts'), value: 'custom'}, |
| 38 | ]; |
| 39 | |
| 40 | const timeUnitOptions = [ |
| 41 | {label: __('Minute(s)', 'wordpress-popular-posts'), value: 'minute'}, |
| 42 | {label: __('Hour(s)', 'wordpress-popular-posts'), value: 'hour'}, |
| 43 | {label: __('Day(s)', 'wordpress-popular-posts'), value: 'day'} |
| 44 | ]; |
| 45 | |
| 46 | const onTitleChange = (value) => { |
| 47 | setAttributes({ title: escape_html(unescape_html(value)) }); |
| 48 | }; |
| 49 | |
| 50 | const onLimitChange = (value) => { |
| 51 | setAttributes({ limit: parseInt(value, 10) }); |
| 52 | }; |
| 53 | |
| 54 | const onOrderByChange = (value) => { |
| 55 | setAttributes({ order_by: value }); |
| 56 | }; |
| 57 | |
| 58 | const onTimeRangeChange = (value) => { |
| 59 | setAttributes({ range: value }); |
| 60 | }; |
| 61 | |
| 62 | const onTimeQuantityChange = (value) => { |
| 63 | setAttributes({ time_quantity: parseInt(value, 10) }); |
| 64 | }; |
| 65 | |
| 66 | const onTimeUnitChange = (value) => { |
| 67 | setAttributes({ time_unit: value }); |
| 68 | }; |
| 69 | |
| 70 | const onFreshnessChange = (value) => { |
| 71 | setAttributes({ freshness: value }); |
| 72 | }; |
| 73 | |
| 74 | return <Fragment> |
| 75 | <TextControl |
| 76 | label={labelTitle} |
| 77 | value={title} |
| 78 | onChange={onTitleChange} |
| 79 | /> |
| 80 | <TextControl |
| 81 | label={labelLimit} |
| 82 | type='number' |
| 83 | value={limit} |
| 84 | min='1' |
| 85 | onChange={onLimitChange} |
| 86 | /> |
| 87 | <SelectControl |
| 88 | label={labelSortBy} |
| 89 | value={order_by} |
| 90 | options={sortingOptions} |
| 91 | onChange={onOrderByChange} |
| 92 | /> |
| 93 | <SelectControl |
| 94 | label={labelTimeRange} |
| 95 | value={range} |
| 96 | options={timeRangeOptions} |
| 97 | onChange={onTimeRangeChange} |
| 98 | /> |
| 99 | { range === 'custom' && |
| 100 | <div className='option-subset'> |
| 101 | <TextControl |
| 102 | label={labelTimeQuantity} |
| 103 | type='number' |
| 104 | value={time_quantity} |
| 105 | min='1' |
| 106 | onChange={onTimeQuantityChange} |
| 107 | /> |
| 108 | <SelectControl |
| 109 | label={labelTimeUnit} |
| 110 | value={time_unit} |
| 111 | options={timeUnitOptions} |
| 112 | onChange={onTimeUnitChange} |
| 113 | /> |
| 114 | </div> |
| 115 | } |
| 116 | <CheckboxControl |
| 117 | label={labelFreshness} |
| 118 | checked={freshness} |
| 119 | onChange={onFreshnessChange} |
| 120 | /> |
| 121 | </Fragment>; |
| 122 | } |