| 1 |
(function (blocks, editor, components, i18n, element) { |
| 2 |
var el = element.createElement; |
| 3 |
var Fragment = element.Fragment; |
| 4 |
var __ = i18n.__; |
| 5 |
var InspectorControls = editor.InspectorControls; |
| 6 |
var useBlockProps = editor.useBlockProps; |
| 7 |
var PanelBody = components.PanelBody; |
| 8 |
var TextControl = components.TextControl; |
| 9 |
var ToggleControl = components.ToggleControl; |
| 10 |
var SelectControl = components.SelectControl; |
| 11 |
var CheckboxControl = components.CheckboxControl; |
| 12 |
var RadioControl = components.RadioControl; |
| 13 |
|
| 14 |
// Check for NumberControl existence, fallback to TextControl if needed |
| 15 |
var NumberControl = components.NumberControl || components.TextControl; |
| 16 |
|
| 17 |
blocks.registerBlockType('wpforo/recent-posts', { |
| 18 |
title: __('wpForo Recent Posts', 'wpforo'), |
| 19 |
icon: 'format-chat', |
| 20 |
category: 'widgets', |
| 21 |
edit: function (props) { |
| 22 |
var attributes = props.attributes; |
| 23 |
var setAttributes = props.setAttributes; |
| 24 |
var blockProps = useBlockProps(); |
| 25 |
|
| 26 |
var data = window.wpforo_block_data || {}; |
| 27 |
var boards = data.boards || []; |
| 28 |
var forums = data.forums || []; |
| 29 |
|
| 30 |
var boardOptions = boards.map(function (board) { |
| 31 |
return { value: board.boardid, label: board.title }; |
| 32 |
}); |
| 33 |
|
| 34 |
function buildHierarchicalOptions (forums, parentId, level) { |
| 35 |
var options = []; |
| 36 |
var level = level || 0; |
| 37 |
var prefix = Array(level * 2 + 1).join('\u00A0'); // Non-breaking space for indentation |
| 38 |
|
| 39 |
forums.forEach(function (forum) { |
| 40 |
if (parseInt(forum.parentid) == parentId && (parseInt(forum.boardid) == attributes.boardid || !forum.boardid)) { |
| 41 |
options.push({ |
| 42 |
value: forum.forumid, |
| 43 |
label: prefix + (level > 0 ? '\u2014 ' : '') + forum.title, |
| 44 |
}); |
| 45 |
var children = buildHierarchicalOptions(forums, forum.forumid, level + 1); |
| 46 |
if (children.length > 0) { |
| 47 |
options = options.concat(children); |
| 48 |
} |
| 49 |
} |
| 50 |
}); |
| 51 |
return options; |
| 52 |
} |
| 53 |
|
| 54 |
var forumOptions = buildHierarchicalOptions(forums, 0, 0); |
| 55 |
|
| 56 |
return el(Fragment, null, |
| 57 |
el(InspectorControls, { key: 'inspector' }, |
| 58 |
el(PanelBody, { title: __('Settings', 'wpforo') }, |
| 59 |
el(TextControl, { |
| 60 |
label: __('Title', 'wpforo'), |
| 61 |
value: attributes.title, |
| 62 |
onChange: function (value) { |
| 63 |
setAttributes({ title: value }); |
| 64 |
}, |
| 65 |
}), |
| 66 |
el(SelectControl, { |
| 67 |
label: __('Board', 'wpforo'), |
| 68 |
value: attributes.boardid, |
| 69 |
options: boardOptions, |
| 70 |
onChange: function (value) { |
| 71 |
setAttributes({ boardid: parseInt(value) }); |
| 72 |
}, |
| 73 |
}), |
| 74 |
el(ToggleControl, { |
| 75 |
label: __('Filter by forums', 'wpforo'), |
| 76 |
checked: attributes.forumids_filter, |
| 77 |
onChange: function (value) { |
| 78 |
var newAttrs = { forumids_filter: value }; |
| 79 |
if (!value) { |
| 80 |
newAttrs.forumids = []; |
| 81 |
} |
| 82 |
setAttributes(newAttrs); |
| 83 |
}, |
| 84 |
}), |
| 85 |
el(SelectControl, { |
| 86 |
label: __('Forums', 'wpforo'), |
| 87 |
multiple: true, |
| 88 |
disabled: !attributes.forumids_filter, |
| 89 |
value: (attributes.forumids || []).map(String), |
| 90 |
options: forumOptions, |
| 91 |
onChange: function (value) { |
| 92 |
setAttributes({ forumids: (value || []).map(Number) }); |
| 93 |
}, |
| 94 |
}), |
| 95 |
el(ToggleControl, { |
| 96 |
label: __('Autofilter by current forum', 'wpforo'), |
| 97 |
checked: attributes.current_forumid_filter, |
| 98 |
onChange: function (value) { |
| 99 |
setAttributes({ current_forumid_filter: value }); |
| 100 |
}, |
| 101 |
}), |
| 102 |
el(SelectControl, { |
| 103 |
label: __('Order by', 'wpforo'), |
| 104 |
value: attributes.orderby, |
| 105 |
options: [ |
| 106 |
{ label: __('Created Date', 'wpforo'), value: 'created' }, |
| 107 |
{ label: __('Modified Date', 'wpforo'), value: 'modified' }, |
| 108 |
], |
| 109 |
onChange: function (value) { |
| 110 |
setAttributes({ orderby: value }); |
| 111 |
}, |
| 112 |
}), |
| 113 |
el(SelectControl, { |
| 114 |
label: __('Order', 'wpforo'), |
| 115 |
value: attributes.order, |
| 116 |
options: [ |
| 117 |
{ label: __('DESC', 'wpforo'), value: 'DESC' }, |
| 118 |
{ label: __('ASC', 'wpforo'), value: 'ASC' }, |
| 119 |
{ label: __('Random', 'wpforo'), value: 'RAND' }, |
| 120 |
], |
| 121 |
onChange: function (value) { |
| 122 |
setAttributes({ order: value }); |
| 123 |
}, |
| 124 |
}), |
| 125 |
el(NumberControl, { |
| 126 |
label: __('Limit Per Topic', 'wpforo'), |
| 127 |
value: attributes.limit_per_topic, |
| 128 |
min: 0, |
| 129 |
type: 'number', |
| 130 |
onChange: function (value) { |
| 131 |
setAttributes({ limit_per_topic: parseInt(value) || 0 }); |
| 132 |
}, |
| 133 |
help: __('set 0 to remove this limit', 'wpforo'), |
| 134 |
}), |
| 135 |
el(NumberControl, { |
| 136 |
label: __('Number of Items', 'wpforo'), |
| 137 |
value: attributes.count, |
| 138 |
min: 1, |
| 139 |
type: 'number', |
| 140 |
onChange: function (value) { |
| 141 |
setAttributes({ count: parseInt(value) || 1 }); |
| 142 |
}, |
| 143 |
}), |
| 144 |
el(NumberControl, { |
| 145 |
label: __('Excerpt Length', 'wpforo'), |
| 146 |
value: attributes.excerpt_length, |
| 147 |
min: 0, |
| 148 |
type: 'number', |
| 149 |
onChange: function (value) { |
| 150 |
setAttributes({ excerpt_length: parseInt(value) || 0 }); |
| 151 |
}, |
| 152 |
}), |
| 153 |
el(ToggleControl, { |
| 154 |
label: __('Display with avatars', 'wpforo'), |
| 155 |
checked: attributes.display_avatar, |
| 156 |
onChange: function (value) { |
| 157 |
setAttributes({ display_avatar: value }); |
| 158 |
}, |
| 159 |
}), |
| 160 |
el(ToggleControl, { |
| 161 |
label: __('Exclude First Posts', 'wpforo'), |
| 162 |
checked: attributes.exclude_firstposts, |
| 163 |
onChange: function (value) { |
| 164 |
setAttributes({ exclude_firstposts: value }); |
| 165 |
}, |
| 166 |
}), |
| 167 |
el(ToggleControl, { |
| 168 |
label: __('Display Only Unread Posts', 'wpforo'), |
| 169 |
checked: attributes.display_only_unread, |
| 170 |
onChange: function (value) { |
| 171 |
setAttributes({ display_only_unread: value }); |
| 172 |
}, |
| 173 |
}), |
| 174 |
el(ToggleControl, { |
| 175 |
label: __('Display [new] indicator', 'wpforo'), |
| 176 |
checked: attributes.display_new_indicator, |
| 177 |
onChange: function (value) { |
| 178 |
setAttributes({ display_new_indicator: value }); |
| 179 |
}, |
| 180 |
}), |
| 181 |
el(NumberControl, { |
| 182 |
label: __('Auto Refresh Interval Seconds', 'wpforo'), |
| 183 |
value: attributes.refresh_interval, |
| 184 |
min: 0, |
| 185 |
type: 'number', |
| 186 |
onChange: function (value) { |
| 187 |
setAttributes({ refresh_interval: parseInt(value) || 0 }); |
| 188 |
}, |
| 189 |
help: __('Set 0 to disable autorefresh', 'wpforo'), |
| 190 |
}), |
| 191 |
), |
| 192 |
), |
| 193 |
el('div', blockProps, |
| 194 |
el('div', { className: 'wpf-block-placeholder' }, |
| 195 |
el('span', { className: 'dashicons dashicons-format-chat' }), |
| 196 |
__('wpForo Recent Posts', 'wpforo'), |
| 197 |
), |
| 198 |
), |
| 199 |
); |
| 200 |
}, |
| 201 |
save: function () { |
| 202 |
return null; |
| 203 |
}, |
| 204 |
}); |
| 205 |
})( |
| 206 |
window.wp.blocks, |
| 207 |
window.wp.blockEditor, |
| 208 |
window.wp.components, |
| 209 |
window.wp.i18n, |
| 210 |
window.wp.element, |
| 211 |
); |
| 212 |
|