Files
display-featured-image-genesis/includes/js/block.js
T

291 lines
7.1 KiB
JavaScript
Raw Normal View History

2019-06-18 10:26:49 -04:00
/*
* Copyright (c) 2019 Robin Cornett
*/
2019-10-22 17:39:43 -04:00
( function ( wp, $, undefined ) {
2019-06-18 10:26:49 -04:00
'use strict';
const DFIGBlockObject = {
2019-06-18 10:26:49 -04:00
el: wp.element.createElement,
};
2019-06-23 17:39:57 -04:00
/**
* Initialize and register the block.
*/
DFIGBlockObject.init = function ( params ) {
2019-06-18 10:26:49 -04:00
const registerBlockType = wp.blocks.registerBlockType,
2019-10-22 17:39:43 -04:00
ServerSideRender = wp.components.ServerSideRender,
InspectorControls = wp.blockEditor.InspectorControls;
2019-06-23 17:39:57 -04:00
registerBlockType( params.block, {
title: params.title,
description: params.description,
keywords: params.keywords,
icon: params.icon,
category: params.category,
2019-06-21 08:45:18 -04:00
supports: {
html: false
},
2019-10-22 17:39:43 -04:00
getEditWrapperProps ( { blockAlignment } ) {
return { 'data-align': blockAlignment };
2019-06-21 08:45:18 -04:00
},
edit: props => {
const {
2019-10-22 17:39:43 -04:00
attributes,
setAttributes
} = props,
Fragment = wp.element.Fragment,
BlockControls = wp.blockEditor.BlockControls,
BlockAlignmentToolbar = wp.blockEditor.BlockAlignmentToolbar;
2019-06-26 07:16:28 -04:00
let render = DFIGBlockObject.el( ServerSideRender, {
block: params.block,
attributes: attributes
} );
2019-10-22 17:39:43 -04:00
if ( params.placeholder && !attributes[ params.required ] ) {
2019-06-26 07:16:28 -04:00
render = DFIGBlockObject.el( 'div', {
className: DFIGBlockObject.params.prefix + '-placeholder',
2019-06-26 07:16:28 -04:00
}, params.placeholder );
}
2019-06-21 08:45:18 -04:00
return [
2019-06-26 07:16:28 -04:00
render,
2019-06-23 17:39:57 -04:00
DFIGBlockObject.el( Fragment, null,
DFIGBlockObject.el( BlockControls, null,
DFIGBlockObject.el( BlockAlignmentToolbar, {
value: attributes.blockAlignment,
2019-10-22 17:39:43 -04:00
controls: [ 'wide', 'full' ],
2019-06-21 08:45:18 -04:00
onChange: ( value ) => {
2019-10-22 17:39:43 -04:00
setAttributes( { blockAlignment: value } );
2019-06-21 08:45:18 -04:00
},
} )
),
),
2019-06-23 17:39:57 -04:00
DFIGBlockObject.el( InspectorControls, {},
_getPanels( props, params ),
2019-06-25 12:37:40 -04:00
onChangeSelect( false, false, props )
2019-06-21 08:45:18 -04:00
)
];
},
save: props => {
return null;
},
2019-06-18 10:26:49 -04:00
} );
};
/**
* Get the panels for the block controls.
*
* @param props
2019-06-23 17:39:57 -04:00
* @param params
2019-06-18 10:26:49 -04:00
* @return {Array}
* @private
*/
2019-10-22 17:39:43 -04:00
function _getPanels ( props, params ) {
const panels = [],
PanelBody = wp.components.PanelBody;
2019-06-23 17:39:57 -04:00
Object.keys( params.panels ).forEach( function ( key, index ) {
if ( params.panels.hasOwnProperty( key ) ) {
2019-10-22 17:39:43 -04:00
const IndividualPanel = params.panels[ key ];
2019-06-25 13:19:10 -04:00
panels[ index ] = DFIGBlockObject.el( PanelBody, {
2019-06-21 08:45:18 -04:00
title: IndividualPanel.title,
2019-06-23 17:39:57 -04:00
initialOpen: IndividualPanel.initialOpen,
className: DFIGBlockObject.params.prefix + '-panel-' + key
}, _getControls( props, IndividualPanel.attributes ) );
2019-06-18 10:26:49 -04:00
}
} );
return panels;
}
/**
* Get all of the block controls, with defaults and options.
*
* @param props
* @param fields
* @return {Array}
* @private
*/
2019-10-22 17:39:43 -04:00
function _getControls ( props, fields ) {
2019-06-18 10:26:49 -04:00
const controls = [];
Object.keys( fields ).forEach( function ( key, index ) {
if ( fields.hasOwnProperty( key ) ) {
var skipped = [ 'blockAlignment', 'className' ];
if ( -1 !== skipped.indexOf( key ) ) {
return;
}
2019-10-22 17:39:43 -04:00
const IndividualField = fields[ key ],
control = _getControlType( IndividualField.method, IndividualField.type );
controls[ index ] = DFIGBlockObject.el( control, _getIndividualControl( key, IndividualField, props ) );
2019-06-18 10:26:49 -04:00
}
} );
return controls;
}
/**
* Get the control type.
* @param method
* @param control_type
* @return {*}
* @private
*/
2019-10-22 17:39:43 -04:00
function _getControlType ( method, control_type ) {
2019-06-18 10:26:49 -04:00
const {
2019-10-22 17:39:43 -04:00
TextControl,
SelectControl,
CheckboxControl,
TextareaControl
} = wp.components;
2019-06-18 10:26:49 -04:00
const control = TextControl;
if ( 'select' === method ) {
return SelectControl;
} else if ( 'checkbox' === method ) {
return CheckboxControl;
} else if ( 'textarea' === method ) {
return TextareaControl;
}
return control;
}
/**
* Build the individual control object. Sets up standard properties for all
* controls; then adds custom properties as needed.
*
* @param key
* @param field
* @param props
* @return {{label: *, value: *, className: string, onChange: onChange}}
* @private
*/
2019-10-22 17:39:43 -04:00
function _getIndividualControl ( key, field, props ) {
const { attributes, setAttributes } = props;
2019-06-18 10:26:49 -04:00
const control = {
label: field.label,
2019-10-22 17:39:43 -04:00
value: attributes[ key ],
className: DFIGBlockObject.params.prefix + '-' + key,
2019-06-18 10:26:49 -04:00
onChange: ( value ) => {
2019-06-25 12:53:58 -04:00
if ( 'taxonomy' === key ) {
onChangeSelect( key, value, props );
2019-06-25 12:53:58 -04:00
}
2019-10-22 17:39:43 -04:00
setAttributes( { [ key ]: value } );
2019-06-18 10:26:49 -04:00
}
};
if ( 'select' === field.method ) {
control.options = field.options;
} else if ( 'checkbox' === field.method ) {
2019-10-22 17:39:43 -04:00
control.checked = attributes[ key ];
2019-06-18 10:26:49 -04:00
}
return control;
}
2019-06-25 12:37:40 -04:00
/**
* Update values and options.
* @param select_id
* @param value
* @param props
*/
2019-10-22 17:39:43 -04:00
function onChangeSelect ( select_id, value, props ) {
if ( DFIGBlockObject.params.prefix + '/term' !== props.name ) {
2019-06-25 12:37:40 -04:00
return;
}
const data = _getAjaxData( select_id, value, props );
_doAjaxUpdate( data, select_id, props );
}
/**
*
* @param select_id
* @param value
* @param props
* @returns {{action: string, security: *}}
* @private
*/
2019-10-22 17:39:43 -04:00
function _getAjaxData ( select_id, value, props ) {
const data = {
action: DFIGBlockObject.params.prefix + '_block',
security: DFIGBlockObject.params.security
},
{ attributes } = props;
2019-06-25 12:37:40 -04:00
if ( 'taxonomy' === select_id ) {
data.taxonomy = value;
} else {
data.taxonomy = attributes.taxonomy;
}
return data;
}
/**
* Call on our ajax action and update the select
* @param data
* @param select_id
* @param props
* @return
* @private
*/
2019-10-22 17:39:43 -04:00
function _doAjaxUpdate ( data, select_id, props ) {
const { attributes, setAttributes } = props;
2019-06-25 12:37:40 -04:00
$.post( DFIGBlockObject.params.ajax_url, data, function ( response ) {
if ( undefined !== response.success && false === response.success ) {
return false;
}
const jsonData = $.parseJSON( response );
_modifySelectInput( jsonData, 'term', attributes );
if ( select_id ) {
setAttributes( {
term: '',
} );
}
} );
}
/**
* Modify the term dropdown.
* @param options
* @param key
* @param attributes
* @private
*/
2019-10-22 17:39:43 -04:00
function _modifySelectInput ( options, key, attributes ) {
const selectID = $( '.' + DFIGBlockObject.params.prefix + '-' + key + ' select' ),
2019-10-22 17:39:43 -04:00
oldValue = attributes[ key ] || '';
2019-06-25 12:37:40 -04:00
selectID.empty();
_updateSelectOptions( options, selectID, oldValue );
}
/**
* Update the select input with the new options.
* @param options
* @param selectID
* @param oldValue
* @private
*/
2019-10-22 17:39:43 -04:00
function _updateSelectOptions ( options, selectID, oldValue ) {
2019-06-25 12:37:40 -04:00
$.each( options, function ( key, value ) {
const new_option = $( '<option />' )
2019-10-22 17:39:43 -04:00
.val( key ).text( value ),
method = !key ? 'prepend' : 'append';
2019-06-25 12:37:40 -04:00
selectID.val( oldValue );
2019-10-22 17:39:43 -04:00
selectID[ method ]( new_option );
2019-06-25 12:37:40 -04:00
} );
}
2019-06-23 17:39:57 -04:00
DFIGBlockObject.params = typeof DisplayFeaturedImageBlock === 'undefined' ? '' : DisplayFeaturedImageBlock;
2019-06-18 10:26:49 -04:00
2019-06-23 17:39:57 -04:00
if ( typeof DFIGBlockObject.params !== 'undefined' ) {
2019-06-25 13:04:51 -04:00
Object.keys( DFIGBlockObject.params.blocks ).forEach( function ( key, index ) {
if ( DFIGBlockObject.params.blocks.hasOwnProperty( key ) ) {
DFIGBlockObject.init( DFIGBlockObject.params.blocks[ key ] );
2019-06-23 17:39:57 -04:00
}
} );
2019-06-18 10:26:49 -04:00
}
2019-10-22 17:39:43 -04:00
} )( wp, jQuery );