| 1 |
(function(wp) { |
| 2 |
|
| 3 |
/** |
| 4 |
* Registers a new block provided a unique name and an object defining its behavior. |
| 5 |
* @link https://github.com/WordPress/gutenberg/tree/master/blocks#api |
| 6 |
*/ |
| 7 |
var registerBlockType = wp.blocks.registerBlockType; |
| 8 |
|
| 9 |
/** |
| 10 |
* Returns a new element of given type. Element is an abstraction layer atop React. |
| 11 |
* @link https://github.com/WordPress/gutenberg/tree/master/packages/element#element |
| 12 |
*/ |
| 13 |
var el = wp.element.createElement; |
| 14 |
|
| 15 |
/** |
| 16 |
* Retrieves the translation of text. |
| 17 |
* @link https://github.com/WordPress/gutenberg/tree/master/i18n#api |
| 18 |
*/ |
| 19 |
var __ = wp.i18n.__; |
| 20 |
|
| 21 |
/** |
| 22 |
* This variable is used to keep the very first shortcode |
| 23 |
* after the loading of the page. |
| 24 |
*/ |
| 25 |
var currentShortcode = null; |
| 26 |
|
| 27 |
/** |
| 28 |
* Every block starts by registering a new block type definition. |
| 29 |
* @link https://wordpress.org/gutenberg/handbook/block-api/ |
| 30 |
*/ |
| 31 |
registerBlockType('vikbooking/gutenberg-shortcodes', { |
| 32 |
/** |
| 33 |
* This is the block display title, which can be translated with `i18n` functions. |
| 34 |
* The block inserter will show this name. |
| 35 |
*/ |
| 36 |
title: __('VikBooking Shortcode', 'vikbooking'), |
| 37 |
|
| 38 |
/** |
| 39 |
* This is the block description, which is displayed within the right sidebar. |
| 40 |
*/ |
| 41 |
description: __('Add a shortcode configured through VikBooking.', 'vikbooking'), |
| 42 |
|
| 43 |
/** |
| 44 |
* The icon can be a DASHICON or a SVG entity. |
| 45 |
*/ |
| 46 |
icon: 'building', |
| 47 |
|
| 48 |
/** |
| 49 |
* Blocks are grouped into categories to help users browse and discover them. |
| 50 |
* The categories provided by core are `common`, `embed`, `formatting`, `layout` and `widgets`. |
| 51 |
*/ |
| 52 |
category: 'widgets', |
| 53 |
|
| 54 |
/** |
| 55 |
* Sometimes a block could have aliases that help users discover it while searching. |
| 56 |
* You can do so by providing an array of terms (which can be translated). |
| 57 |
* It is only allowed to add as much as three terms per block. |
| 58 |
*/ |
| 59 |
keywords: [ |
| 60 |
__('shortcodes'), __('list'), __('page'), |
| 61 |
], |
| 62 |
|
| 63 |
/** |
| 64 |
* Optional block extended support features. |
| 65 |
*/ |
| 66 |
supports: { |
| 67 |
// do not edit as HTML |
| 68 |
html: false, |
| 69 |
|
| 70 |
// use the block just once per post |
| 71 |
multiple: false, |
| 72 |
|
| 73 |
// don't allow the block to be converted into a reusable block |
| 74 |
reusable: false, |
| 75 |
}, |
| 76 |
|
| 77 |
/** |
| 78 |
* Attributes provide the structured data needs of a block. |
| 79 |
* They can exist in different forms when they are serialized, |
| 80 |
* but they are declared together under a common interface. |
| 81 |
*/ |
| 82 |
attributes: { |
| 83 |
shortcode: { |
| 84 |
type: 'string', |
| 85 |
source: 'html', |
| 86 |
selector: 'div', |
| 87 |
}, |
| 88 |
toggler: { |
| 89 |
type: 'string', |
| 90 |
default: '0', |
| 91 |
} |
| 92 |
}, |
| 93 |
|
| 94 |
/** |
| 95 |
* The edit function describes the structure of your block in the context of the editor. |
| 96 |
* This represents what the editor will render when the block is used. |
| 97 |
* @link https://wordpress.org/gutenberg/handbook/block-edit-save/#edit |
| 98 |
* |
| 99 |
* @param Object props Properties passed from the editor. |
| 100 |
* |
| 101 |
* @return Element Element to render. |
| 102 |
*/ |
| 103 |
edit: function(props) { |
| 104 |
|
| 105 |
// iterate vikbooking shortcodes to build select options |
| 106 |
var options = []; |
| 107 |
|
| 108 |
var shortcodes_boxes = []; |
| 109 |
|
| 110 |
if (currentShortcode === null) { |
| 111 |
// if not set, define current value |
| 112 |
currentShortcode = props.attributes.shortcode; |
| 113 |
} |
| 114 |
|
| 115 |
// insert empty option |
| 116 |
options.push({ |
| 117 |
label: __('- pick a shortcode -', 'vikbooking'), |
| 118 |
value: '', |
| 119 |
}); |
| 120 |
|
| 121 |
// evaluate if toggler checkbox is checked |
| 122 |
var togglerChecked = props.attributes.toggler && props.attributes.toggler == '1'; |
| 123 |
|
| 124 |
for (var group in VIKBOOKING_SHORTCODES_BLOCK) { |
| 125 |
var groups = VIKBOOKING_SHORTCODES_BLOCK[group]; |
| 126 |
|
| 127 |
for (var i = 0; i < groups.length; i++) { |
| 128 |
var data = groups[i]; |
| 129 |
|
| 130 |
var post_id = parseInt(data.post_id); |
| 131 |
|
| 132 |
// push option only in case: |
| 133 |
// - toggler is enabled (see all) |
| 134 |
// - the shortcode is not assigned to any post |
| 135 |
// - the shortcode is equals to the current one |
| 136 |
if (togglerChecked || !post_id || data.shortcode == currentShortcode) { |
| 137 |
|
| 138 |
options.push({ |
| 139 |
label: data.name, |
| 140 |
value: data.shortcode, |
| 141 |
}); |
| 142 |
|
| 143 |
// build ASSIGNEE field |
| 144 |
var assigneeField = null; |
| 145 |
|
| 146 |
if (post_id && data.shortcode != currentShortcode) { |
| 147 |
assigneeField = el( |
| 148 |
'a', |
| 149 |
{ |
| 150 |
href: 'javascript:void(0);', |
| 151 |
onClick: function() { |
| 152 |
alert(__('This shortcode is already used by a different post. If you select this shortcode, it will be detached from the existing post and assigned to this new post.', 'vikbooking')); |
| 153 |
}, |
| 154 |
}, |
| 155 |
el( |
| 156 |
'span', |
| 157 |
{ |
| 158 |
className: 'assigned', |
| 159 |
}, |
| 160 |
__('Post #', 'vikbooking') + post_id |
| 161 |
) |
| 162 |
); |
| 163 |
} else { |
| 164 |
// safe shortcode |
| 165 |
assigneeField = el('span', {}, (post_id ? __('Post #', 'vikbooking') + post_id : '--')); |
| 166 |
} |
| 167 |
|
| 168 |
// check if the box should be displayed |
| 169 |
var toggled = props.attributes.shortcode == data.shortcode; |
| 170 |
|
| 171 |
// setup information div |
| 172 |
shortcodes_boxes.push(el( |
| 173 |
'div', |
| 174 |
{ |
| 175 |
className: 'vikbooking-shortcode-info-box' + (toggled ? ' toggled' : ''), |
| 176 |
}, |
| 177 |
// create child elements |
| 178 |
el( |
| 179 |
'div', |
| 180 |
{ |
| 181 |
className: 'vbo-sh-info-control' |
| 182 |
}, |
| 183 |
[ |
| 184 |
el('label', {}, __('Type:', 'vikbooking')), |
| 185 |
el('span', {}, group), |
| 186 |
] |
| 187 |
), |
| 188 |
el( |
| 189 |
'div', |
| 190 |
{ |
| 191 |
className: 'vbo-sh-info-control' |
| 192 |
}, |
| 193 |
[ |
| 194 |
el('label', {}, __('Name:', 'vikbooking')), |
| 195 |
el('span', {}, data.name), |
| 196 |
] |
| 197 |
), |
| 198 |
el( |
| 199 |
'div', |
| 200 |
{ |
| 201 |
className: 'vbo-sh-info-control' |
| 202 |
}, |
| 203 |
[ |
| 204 |
el('label', {}, __('Created on:', 'vikbooking')), |
| 205 |
el('span', {}, data.createdon), |
| 206 |
] |
| 207 |
), |
| 208 |
el( |
| 209 |
'div', |
| 210 |
{ |
| 211 |
className: 'vbo-sh-info-control' |
| 212 |
}, |
| 213 |
[ |
| 214 |
el('label', {}, __('Assignee:', 'vikbooking')), |
| 215 |
assigneeField |
| 216 |
] |
| 217 |
), |
| 218 |
el( |
| 219 |
'div', |
| 220 |
{ |
| 221 |
className: 'vbo-sh-info-control' |
| 222 |
}, |
| 223 |
el( |
| 224 |
wp.components.TextareaControl, |
| 225 |
{ |
| 226 |
label: __('Shortcode:', 'vikbooking'), |
| 227 |
value: data.shortcode, |
| 228 |
readonly: true, |
| 229 |
} |
| 230 |
) |
| 231 |
), |
| 232 |
)); |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
// build SVG for shortcode dashicon |
| 238 |
var svg = el( |
| 239 |
'svg', |
| 240 |
{ |
| 241 |
className: 'dashicon dashicons-shortcode', |
| 242 |
role: 'img', |
| 243 |
focusable: false, |
| 244 |
xmlns: 'http://www.w3.org/2000/svg', |
| 245 |
width: '20', |
| 246 |
height: '20', |
| 247 |
viewBox: '0 0 20 20', |
| 248 |
}, |
| 249 |
el( |
| 250 |
'path', |
| 251 |
{ |
| 252 |
d: 'M6 14H4V6h2V4H2v12h4M7.1 17h2.1l3.7-14h-2.1M14 4v2h2v8h-2v2h4V4', |
| 253 |
} |
| 254 |
) |
| 255 |
); |
| 256 |
|
| 257 |
/** |
| 258 |
* Use the new version of InspectorControls |
| 259 |
* provided by WordPress 5.4, if supported. |
| 260 |
* |
| 261 |
* @since 1.3.1 |
| 262 |
*/ |
| 263 |
var InspectorControls; |
| 264 |
|
| 265 |
if (wp.blockEditor && wp.blockEditor.InspectorControls) { |
| 266 |
// use new version |
| 267 |
InspectorControls = wp.blockEditor.InspectorControls; |
| 268 |
} else { |
| 269 |
// fallback to the older one |
| 270 |
InspectorControls = wp.editor.InspectorControls; |
| 271 |
} |
| 272 |
|
| 273 |
// setup inspector (right-side area) |
| 274 |
var controls = el( |
| 275 |
// create InspectorControls element |
| 276 |
InspectorControls, |
| 277 |
// define inspector properties |
| 278 |
{ |
| 279 |
key: 'controls', |
| 280 |
}, |
| 281 |
// add accordion |
| 282 |
el( |
| 283 |
wp.components.PanelBody, |
| 284 |
{ |
| 285 |
title: __('Shortcode'), |
| 286 |
initialOpen: true, |
| 287 |
}, |
| 288 |
// add shortcodes information |
| 289 |
shortcodes_boxes, |
| 290 |
el( |
| 291 |
wp.components.ToggleControl, |
| 292 |
{ |
| 293 |
label: 'See all', |
| 294 |
checked: togglerChecked, |
| 295 |
help: togglerChecked ? __('Display all the existing shortcodes.', 'vikbooking') : __('Toggle to display also the shortcodes that are already assigned to a post.', 'vikbooking'), |
| 296 |
onChange: function(toggler) { |
| 297 |
props.setAttributes({toggler: toggler ? "1" : "0"}); |
| 298 |
} |
| 299 |
} |
| 300 |
) |
| 301 |
) |
| 302 |
); |
| 303 |
|
| 304 |
return [ |
| 305 |
controls, |
| 306 |
el( |
| 307 |
// create <div> wrapper |
| 308 |
'div', |
| 309 |
// define wrapper properties |
| 310 |
{ |
| 311 |
className: 'vbo-shortcode-admin-wrapper', |
| 312 |
}, |
| 313 |
// <div> contains select |
| 314 |
el( |
| 315 |
// create <select> for shortcode |
| 316 |
wp.components.SelectControl, |
| 317 |
// define select properties |
| 318 |
{ |
| 319 |
label: [svg, __('Shortcode')], |
| 320 |
value: props.attributes.shortcode, |
| 321 |
onChange: function(shortcode) { |
| 322 |
props.setAttributes({shortcode: shortcode}); |
| 323 |
}, |
| 324 |
options: options, |
| 325 |
className: 'wp-block-shortcode', |
| 326 |
} |
| 327 |
) |
| 328 |
) |
| 329 |
]; |
| 330 |
}, |
| 331 |
|
| 332 |
/** |
| 333 |
* The save function defines the way in which the different attributes should be combined |
| 334 |
* into the final markup, which is then serialized by Gutenberg into `post_content`. |
| 335 |
* @link https://wordpress.org/gutenberg/handbook/block-edit-save/#save |
| 336 |
* |
| 337 |
* @return Element Element to render. |
| 338 |
*/ |
| 339 |
save: function(props) { |
| 340 |
var shortcode = props.attributes.shortcode; |
| 341 |
|
| 342 |
return el( |
| 343 |
'div', |
| 344 |
{ |
| 345 |
className: props.className, |
| 346 |
}, |
| 347 |
shortcode |
| 348 |
); |
| 349 |
} |
| 350 |
}); |
| 351 |
|
| 352 |
})(window.wp); |
| 353 |
|