| 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 |
|
| 12 |
// Check for NumberControl existence, fallback to TextControl if needed |
| 13 |
var NumberControl = components.NumberControl || components.TextControl; |
| 14 |
|
| 15 |
blocks.registerBlockType('wpforo/recent-topics', { |
| 16 |
title: __('wpForo Recent Topics', 'wpforo'), |
| 17 |
icon: 'list-view', |
| 18 |
category: 'widgets', |
| 19 |
edit: function (props) { |
| 20 |
var attributes = props.attributes; |
| 21 |
var setAttributes = props.setAttributes; |
| 22 |
var blockProps = useBlockProps(); |
| 23 |
|
| 24 |
var data = window.wpforo_block_data || {}; |
| 25 |
var boards = data.boards || []; |
| 26 |
var forums = data.forums || []; |
| 27 |
|
| 28 |
var boardOptions = boards.map(function (board) { |
| 29 |
return { value: board.boardid, label: board.title }; |
| 30 |
}); |
| 31 |
|
| 32 |
function buildHierarchicalOptions (forums, parentId, level) { |
| 33 |
var options = []; |
| 34 |
var level = level || 0; |
| 35 |
var prefix = Array(level * 2 + 1).join('\u00A0'); // Non-breaking space for indentation |
| 36 |
|
| 37 |
forums.forEach(function (forum) { |
| 38 |
if (parseInt(forum.parentid) == parentId && (parseInt(forum.boardid) == attributes.boardid || !forum.boardid)) { |
| 39 |
options.push({ |
| 40 |
value: forum.forumid, |
| 41 |
label: prefix + (level > 0 ? '\u2014 ' : '') + forum.title, |
| 42 |
}); |
| 43 |
var children = buildHierarchicalOptions(forums, forum.forumid, level + 1); |
| 44 |
if (children.length > 0) { |
| 45 |
options = options.concat(children); |
| 46 |
} |
| 47 |
} |
| 48 |
}); |
| 49 |
return options; |
| 50 |
} |
| 51 |
|
| 52 |
var forumOptions = buildHierarchicalOptions(forums, 0, 0); |
| 53 |
|
| 54 |
return el(Fragment, null, |
| 55 |
el(InspectorControls, { key: 'inspector' }, |
| 56 |
el(PanelBody, { title: __('Settings', 'wpforo') }, |
| 57 |
el(TextControl, { |
| 58 |
label: __('Title', 'wpforo'), |
| 59 |
value: attributes.title, |
| 60 |
onChange: function (value) { |
| 61 |
setAttributes({ title: value }); |
| 62 |
}, |
| 63 |
}), |
| 64 |
el(SelectControl, { |
| 65 |
label: __('Board', 'wpforo'), |
| 66 |
value: attributes.boardid, |
| 67 |
options: boardOptions, |
| 68 |
onChange: function (value) { |
| 69 |
setAttributes({ boardid: parseInt(value) }); |
| 70 |
}, |
| 71 |
}), |
| 72 |
el(ToggleControl, { |
| 73 |
label: __('Filter by forums', 'wpforo'), |
| 74 |
checked: attributes.forumids_filter, |
| 75 |
onChange: function (value) { |
| 76 |
var newAttrs = { forumids_filter: value }; |
| 77 |
if (!value) { |
| 78 |
newAttrs.forumids = []; |
| 79 |
} |
| 80 |
setAttributes(newAttrs); |
| 81 |
}, |
| 82 |
}), |
| 83 |
el(SelectControl, { |
| 84 |
label: __('Forums', 'wpforo'), |
| 85 |
multiple: true, |
| 86 |
disabled: !attributes.forumids_filter, |
| 87 |
value: (attributes.forumids || []).map(String), |
| 88 |
options: forumOptions, |
| 89 |
onChange: function (value) { |
| 90 |
setAttributes({ forumids: (value || []).map(Number) }); |
| 91 |
}, |
| 92 |
}), |
| 93 |
el(ToggleControl, { |
| 94 |
label: __('Autofilter by current forum', 'wpforo'), |
| 95 |
checked: attributes.current_forumid_filter, |
| 96 |
onChange: function (value) { |
| 97 |
setAttributes({ current_forumid_filter: value }); |
| 98 |
}, |
| 99 |
}), |
| 100 |
el(SelectControl, { |
| 101 |
label: __('Order by', 'wpforo'), |
| 102 |
value: attributes.orderby, |
| 103 |
options: [ |
| 104 |
{ label: __('Created Date', 'wpforo'), value: 'created' }, |
| 105 |
{ label: __('Modified Date', 'wpforo'), value: 'modified' }, |
| 106 |
{ label: __('Posts Count', 'wpforo'), value: 'posts' }, |
| 107 |
{ label: __('Views Count', 'wpforo'), value: 'views' }, |
| 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: __('Number of Items', 'wpforo'), |
| 127 |
value: attributes.count, |
| 128 |
min: 1, |
| 129 |
type: 'number', |
| 130 |
onChange: function (value) { |
| 131 |
setAttributes({ count: parseInt(value) || 1 }); |
| 132 |
}, |
| 133 |
}), |
| 134 |
el(ToggleControl, { |
| 135 |
label: __('Display with avatars', 'wpforo'), |
| 136 |
checked: attributes.display_avatar, |
| 137 |
onChange: function (value) { |
| 138 |
setAttributes({ display_avatar: value }); |
| 139 |
}, |
| 140 |
}), |
| 141 |
el(ToggleControl, { |
| 142 |
label: __('Refer topics to first unread post', 'wpforo'), |
| 143 |
checked: attributes.goto_unread, |
| 144 |
onChange: function (value) { |
| 145 |
setAttributes({ goto_unread: value }); |
| 146 |
}, |
| 147 |
}), |
| 148 |
el(NumberControl, { |
| 149 |
label: __('Auto Refresh Interval Seconds', 'wpforo'), |
| 150 |
value: attributes.refresh_interval, |
| 151 |
min: 0, |
| 152 |
type: 'number', |
| 153 |
onChange: function (value) { |
| 154 |
setAttributes({ refresh_interval: parseInt(value) || 0 }); |
| 155 |
}, |
| 156 |
help: __('Set 0 to disable autorefresh', 'wpforo'), |
| 157 |
}), |
| 158 |
), |
| 159 |
), |
| 160 |
el('div', blockProps, |
| 161 |
el('div', { className: 'wpf-block-placeholder' }, |
| 162 |
el('span', { className: 'dashicons dashicons-list-view' }), |
| 163 |
__('wpForo Recent Topics', 'wpforo'), |
| 164 |
), |
| 165 |
), |
| 166 |
); |
| 167 |
}, |
| 168 |
save: function () { |
| 169 |
return null; |
| 170 |
}, |
| 171 |
}); |
| 172 |
})( |
| 173 |
window.wp.blocks, |
| 174 |
window.wp.blockEditor, |
| 175 |
window.wp.components, |
| 176 |
window.wp.i18n, |
| 177 |
window.wp.element, |
| 178 |
); |
| 179 |
|