Fix block script dependencies

This commit is contained in:
Robin Cornett
2019-10-22 17:39:43 -04:00
parent b4628b3e36
commit 390cacc607
3 changed files with 51 additions and 51 deletions
+49 -49
View File
@@ -2,7 +2,7 @@
* Copyright (c) 2019 Robin Cornett
*/
(function ( wp, undefined ) {
( function ( wp, $, undefined ) {
'use strict';
const DFIGBlockObject = {
el: wp.element.createElement,
@@ -13,8 +13,8 @@
*/
DFIGBlockObject.init = function ( params ) {
const registerBlockType = wp.blocks.registerBlockType,
ServerSideRender = wp.components.ServerSideRender,
InspectorControls = wp.blockEditor.InspectorControls;
ServerSideRender = wp.components.ServerSideRender,
InspectorControls = wp.blockEditor.InspectorControls;
registerBlockType( params.block, {
title: params.title,
@@ -26,23 +26,23 @@
html: false
},
getEditWrapperProps( {blockAlignment} ) {
return {'data-align': blockAlignment};
getEditWrapperProps ( { blockAlignment } ) {
return { 'data-align': blockAlignment };
},
edit: props => {
const {
attributes,
setAttributes
} = props,
Fragment = wp.element.Fragment,
BlockControls = wp.blockEditor.BlockControls,
BlockAlignmentToolbar = wp.blockEditor.BlockAlignmentToolbar;
attributes,
setAttributes
} = props,
Fragment = wp.element.Fragment,
BlockControls = wp.blockEditor.BlockControls,
BlockAlignmentToolbar = wp.blockEditor.BlockAlignmentToolbar;
let render = DFIGBlockObject.el( ServerSideRender, {
block: params.block,
attributes: attributes
} );
if ( params.placeholder && ! attributes[ params.required ] ) {
if ( params.placeholder && !attributes[ params.required ] ) {
render = DFIGBlockObject.el( 'div', {
className: DFIGBlockObject.params.prefix + '-placeholder',
}, params.placeholder );
@@ -54,9 +54,9 @@
DFIGBlockObject.el( BlockControls, null,
DFIGBlockObject.el( BlockAlignmentToolbar, {
value: attributes.blockAlignment,
controls: ['wide', 'full'],
controls: [ 'wide', 'full' ],
onChange: ( value ) => {
setAttributes( {blockAlignment: value} );
setAttributes( { blockAlignment: value } );
},
} )
),
@@ -82,12 +82,12 @@
* @return {Array}
* @private
*/
function _getPanels( props, params ) {
const panels = [],
PanelBody = wp.components.PanelBody;
function _getPanels ( props, params ) {
const panels = [],
PanelBody = wp.components.PanelBody;
Object.keys( params.panels ).forEach( function ( key, index ) {
if ( params.panels.hasOwnProperty( key ) ) {
const IndividualPanel = params.panels[key];
const IndividualPanel = params.panels[ key ];
panels[ index ] = DFIGBlockObject.el( PanelBody, {
title: IndividualPanel.title,
initialOpen: IndividualPanel.initialOpen,
@@ -107,7 +107,7 @@
* @return {Array}
* @private
*/
function _getControls( props, fields ) {
function _getControls ( props, fields ) {
const controls = [];
Object.keys( fields ).forEach( function ( key, index ) {
if ( fields.hasOwnProperty( key ) ) {
@@ -115,9 +115,9 @@
if ( -1 !== skipped.indexOf( key ) ) {
return;
}
const IndividualField = fields[key],
control = _getControlType( IndividualField.method, IndividualField.type );
controls[index] = DFIGBlockObject.el( control, _getIndividualControl( key, IndividualField, props ) );
const IndividualField = fields[ key ],
control = _getControlType( IndividualField.method, IndividualField.type );
controls[ index ] = DFIGBlockObject.el( control, _getIndividualControl( key, IndividualField, props ) );
}
} );
@@ -131,13 +131,13 @@
* @return {*}
* @private
*/
function _getControlType( method, control_type ) {
function _getControlType ( method, control_type ) {
const {
TextControl,
SelectControl,
CheckboxControl,
TextareaControl
} = wp.components;
TextControl,
SelectControl,
CheckboxControl,
TextareaControl
} = wp.components;
const control = TextControl;
if ( 'select' === method ) {
return SelectControl;
@@ -160,24 +160,24 @@
* @return {{label: *, value: *, className: string, onChange: onChange}}
* @private
*/
function _getIndividualControl( key, field, props ) {
const {attributes, setAttributes} = props;
function _getIndividualControl ( key, field, props ) {
const { attributes, setAttributes } = props;
const control = {
label: field.label,
value: attributes[key],
value: attributes[ key ],
className: DFIGBlockObject.params.prefix + '-' + key,
onChange: ( value ) => {
if ( 'taxonomy' === key ) {
onChangeSelect( key, value, props );
}
setAttributes( {[key]: value} );
setAttributes( { [ key ]: value } );
}
};
if ( 'select' === field.method ) {
control.options = field.options;
} else if ( 'checkbox' === field.method ) {
control.checked = attributes[key];
control.checked = attributes[ key ];
}
return control;
@@ -189,7 +189,7 @@
* @param value
* @param props
*/
function onChangeSelect( select_id, value, props ) {
function onChangeSelect ( select_id, value, props ) {
if ( DFIGBlockObject.params.prefix + '/term' !== props.name ) {
return;
}
@@ -205,12 +205,12 @@
* @returns {{action: string, security: *}}
* @private
*/
function _getAjaxData( select_id, value, props ) {
const data = {
action: DFIGBlockObject.params.prefix + '_block',
security: DFIGBlockObject.params.security
},
{attributes} = props;
function _getAjaxData ( select_id, value, props ) {
const data = {
action: DFIGBlockObject.params.prefix + '_block',
security: DFIGBlockObject.params.security
},
{ attributes } = props;
if ( 'taxonomy' === select_id ) {
data.taxonomy = value;
} else {
@@ -228,8 +228,8 @@
* @return
* @private
*/
function _doAjaxUpdate( data, select_id, props ) {
const {attributes, setAttributes} = props;
function _doAjaxUpdate ( data, select_id, props ) {
const { attributes, setAttributes } = props;
$.post( DFIGBlockObject.params.ajax_url, data, function ( response ) {
if ( undefined !== response.success && false === response.success ) {
@@ -254,9 +254,9 @@
* @param attributes
* @private
*/
function _modifySelectInput( options, key, attributes ) {
function _modifySelectInput ( options, key, attributes ) {
const selectID = $( '.' + DFIGBlockObject.params.prefix + '-' + key + ' select' ),
oldValue = attributes[key] || '';
oldValue = attributes[ key ] || '';
selectID.empty();
_updateSelectOptions( options, selectID, oldValue );
}
@@ -268,13 +268,13 @@
* @param oldValue
* @private
*/
function _updateSelectOptions( options, selectID, oldValue ) {
function _updateSelectOptions ( options, selectID, oldValue ) {
$.each( options, function ( key, value ) {
const new_option = $( '<option />' )
.val( key ).text( value ),
method = ! key ? 'prepend' : 'append';
.val( key ).text( value ),
method = !key ? 'prepend' : 'append';
selectID.val( oldValue );
selectID[method]( new_option );
selectID[ method ]( new_option );
} );
}
@@ -287,4 +287,4 @@
}
} );
}
} )( wp );
} )( wp, jQuery );