PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.6.8
VikBooking Hotel Booking Engine & PMS v1.6.8
1.8.16 1.8.15 1.8.14 1.8.13 1.8.12 1.8.11 1.8.10 1.8.9 1.8.6 1.8.7 1.8.8 trunk 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 All 37 releases
vikbooking / admin / resources / js / gutenberg-widgets.js

gutenberg-widgets.js in VikBooking Hotel Booking Engine & PMS 1.6.8, at admin/resources/js/gutenberg-widgets.js

172 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function(wp) {
2 'use strict';
3
4 /**
5 * Helper function used to convert a VikBooking widget into a block.
6 *
7 * @param object manifest
8 *
9 * @return void
10 */
11 window.vboRegisterBlockEditor = (manifest) => {
12 /**
13 * Registers a new block provided a unique name and an object defining its behavior.
14 * @link https://github.com/WordPress/gutenberg/tree/master/blocks#api
15 */
16 const registerBlockType = wp.blocks.registerBlockType;
17
18 /**
19 * Returns a new element of given type. Element is an abstraction layer atop React.
20 * @link https://github.com/WordPress/gutenberg/tree/master/packages/element#element
21 */
22 const el = wp.element.createElement;
23
24 /**
25 * Retrieves the translation of text.
26 * @link https://github.com/WordPress/gutenberg/tree/master/i18n#api
27 */
28 const __ = wp.i18n.__;
29
30 /**
31 * Every block starts by registering a new block type definition.
32 * @link https://wordpress.org/gutenberg/handbook/block-api/
33 */
34 registerBlockType(manifest.id, {
35 /**
36 * This is the block display title, which can be translated with `i18n` functions.
37 * The block inserter will show this name.
38 */
39 title: manifest.title,
40
41 /**
42 * This is the block description, which is displayed within the right sidebar.
43 */
44 description: manifest.description,
45
46 /**
47 * The icon can be a DASHICON or a SVG entity.
48 * NOTE: we need to use a different icon because Gutenberg seems
49 * to have problems in displaying the coffee icon.
50 */
51 icon: manifest.icon,
52
53 /**
54 * Blocks are grouped into categories to help users browse and discover them.
55 * The categories provided by core are `common`, `embed`, `formatting`, `layout` and `widgets`.
56 */
57 category: manifest.category,
58
59 /**
60 * Sometimes a block could have aliases that help users discover it while searching.
61 * You can do so by providing an array of terms (which can be translated).
62 * It is only allowed to add as much as three terms per block.
63 */
64 keywords: manifest.keywords,
65
66 /**
67 * Optional block extended support features.
68 */
69 supports: manifest.supports,
70
71 /**
72 * Attributes provide the structured data needs of a block.
73 * They can exist in different forms when they are serialized,
74 * but they are declared together under a common interface.
75 */
76 attributes: manifest.attributes,
77
78 /**
79 * The edit function describes the structure of your block in the context of the editor.
80 * This represents what the editor will render when the block is used.
81 * @link https://wordpress.org/gutenberg/handbook/block-edit-save/#edit
82 *
83 * @param Object props Properties passed from the editor.
84 *
85 * @return Element Element to render.
86 */
87 edit: (props) => {
88 // use the new version of InspectorControls
89 // provided by WordPress 5.4, if supported
90 let InspectorControls, InspectorAdvancedControls;
91
92 if (wp.blockEditor && wp.blockEditor.InspectorControls) {
93 // use new version
94 InspectorControls = wp.blockEditor.InspectorControls;
95 InspectorAdvancedControls = wp.blockEditor.InspectorAdvancedControls;
96 } else {
97 // fallback to the older one
98 InspectorControls = wp.editor.InspectorControls;
99 InspectorAdvancedControls = wp.editor.InspectorAdvancedControls;
100 }
101
102 let navBar = [], advancedFields = [];
103
104 // iterate all the form fieldsets
105 manifest.form.forEach((fieldset, index) => {
106 let fields = [];
107
108 // iterate all the fields under this tab
109 fieldset.fields.forEach((field) => {
110 // create element according to the provided fields
111 let fieldControl = window.vboCreateControlElement(field, props);
112
113 // register the element within the list of fields
114 fields.push(fieldControl);
115 });
116
117 if (fieldset.name !== 'advanced') {
118 // add accordion
119 navBar.push(
120 el(
121 wp.components.PanelBody,
122 {
123 title: fieldset.title,
124 initialOpen: index === 0,
125 key: fieldset.name,
126 },
127 fields
128 )
129 );
130 } else {
131 advancedFields.push(fields);
132 }
133
134 });
135
136 // setup inspector (right-side area)
137 let controls = el(
138 // create InspectorControls element
139 InspectorControls,
140 // define inspector properties
141 {
142 key: 'controls',
143 },
144 navBar
145 );
146
147 // setup advanced tab
148 let advancedControls = el(
149 // create InspectorAdvancedControls element
150 InspectorAdvancedControls,
151 // define inspector properties
152 {
153 key: 'controls',
154 },
155 advancedFields
156 );
157
158 return [
159 controls,
160 advancedControls,
161 el(
162 wp.serverSideRender,
163 {
164 block: manifest.id,
165 attributes: props.attributes,
166 }
167 )
168 ];
169 }
170 });
171 }
172 })(window.wp);