Adds orderBy and orderByMetaKey attrs to items list block. #735.

This commit is contained in:
mateuswetah 2022-11-24 12:25:55 -03:00
parent 4f2242e262
commit 49354a7d68
6 changed files with 34 additions and 9 deletions

View File

@ -102,6 +102,10 @@
"type": "string", "type": "string",
"default": "date" "default": "date"
}, },
"orderByMetaKey": {
"type": "string",
"default": ""
},
"blockId": { "blockId": {
"type": "string", "type": "string",
"default": "" "default": ""

View File

@ -1,7 +1,7 @@
const { useBlockProps } = (tainacan_blocks.wp_version < '5.2' ? wp.editor : wp.blockEditor ); const { useBlockProps } = (tainacan_blocks.wp_version < '5.2' ? wp.editor : wp.blockEditor );
export default [ export default [
/* Deprecated on 0.19.3 due to the introduction of orderBy */ /* Deprecated on 0.19.3 due to the introduction of orderBy and orderByMetaKey */
{ {
attributes: { attributes: {
"content": { "content": {

View File

@ -32,6 +32,7 @@ export default function({ attributes, setAttributes, className, isSelected, clie
maxItemsNumber, maxItemsNumber,
order, order,
orderBy, orderBy,
orderByMetaKey,
searchString, searchString,
selectedItems, selectedItems,
isLoading, isLoading,
@ -303,25 +304,35 @@ export default function({ attributes, setAttributes, className, isSelected, clie
} }
// Set up sorting order // Set up sorting order
if (order != '' && showSearchBar) if (queryObject.order != '' && !showSearchBar)
queryObject.order = order;
else if (queryObject.order != '')
setAttributes({ order: queryObject.order }); setAttributes({ order: queryObject.order });
else if (order != '')
queryObject.order = order;
else { else {
queryObject.order = 'asc'; queryObject.order = 'asc';
setAttributes({ order: 'asc' }); setAttributes({ order: 'asc' });
} }
// Set up sorting order // Set up sorting orderby
if (orderBy != '') if (queryObject.orderby != '')
queryObject.orderby = orderBy;
else if (queryObject.orderby != '')
setAttributes({ orderBy: queryObject.orderby }); setAttributes({ orderBy: queryObject.orderby });
else if (orderBy != 'date')
queryObject.orderby = orderBy;
else { else {
queryObject.orderby = 'date'; queryObject.orderby = 'date';
setAttributes({ orderBy: 'date' }); setAttributes({ orderBy: 'date' });
} }
// Set up sorting metakey (used by some orderby)
if (queryObject.metakey != '')
setAttributes({ orderByMetaKey: queryObject.metakey });
else if (orderByMetaKey != '')
queryObject.metakey = orderByMetaKey;
else {
queryObject.metakey = '';
setAttributes({ orderByMetaKey: '' });
}
// Set up search string // Set up search string
if (searchString != undefined) if (searchString != undefined)
queryObject.search = searchString; queryObject.search = searchString;

View File

@ -15,6 +15,7 @@ export default function({ attributes, className }) {
maxItemsNumber, maxItemsNumber,
order, order,
orderBy, orderBy,
orderByMetaKey,
showSearchBar, showSearchBar,
showCollectionHeader, showCollectionHeader,
showCollectionLabel, showCollectionLabel,
@ -58,6 +59,7 @@ export default function({ attributes, className }) {
max-items-number={ maxItemsNumber } max-items-number={ maxItemsNumber }
order={ order } order={ order }
orderBy={ orderBy } orderBy={ orderBy }
orderByMetaKey={ orderByMetaKey }
tainacan-api-root={ tainacan_blocks.root } tainacan-api-root={ tainacan_blocks.root }
tainacan-base-url={ tainacan_blocks.base_url } tainacan-base-url={ tainacan_blocks.base_url }
id={ 'wp-block-tainacan-dynamic-items-list_' + blockId }> id={ 'wp-block-tainacan-dynamic-items-list_' + blockId }>

View File

@ -45,6 +45,7 @@ export default (element) => {
imageSize: 'tainacan-medium', imageSize: 'tainacan-medium',
order: 'asc', order: 'asc',
orderBy: 'date', orderBy: 'date',
orderByMetaKey: '',
showSearchBar: false, showSearchBar: false,
showCollectionHeader: false, showCollectionHeader: false,
showCollectionLabel: false, showCollectionLabel: false,
@ -77,6 +78,7 @@ export default (element) => {
maxItemsNumber: this.maxItemsNumber, maxItemsNumber: this.maxItemsNumber,
order: this.order, order: this.order,
orderBy: this.orderBy, orderBy: this.orderBy,
orderByMetaKey: this.orderByMetaKey,
showSearchBar: this.showSearchBar, showSearchBar: this.showSearchBar,
showCollectionHeader: this.showCollectionHeader, showCollectionHeader: this.showCollectionHeader,
showCollectionLabel: this.showCollectionLabel, showCollectionLabel: this.showCollectionLabel,
@ -110,6 +112,7 @@ export default (element) => {
this.maxItemsNumber = this.$el.attributes['max-items-number'] != undefined ? this.$el.attributes['max-items-number'].value : undefined; this.maxItemsNumber = this.$el.attributes['max-items-number'] != undefined ? this.$el.attributes['max-items-number'].value : undefined;
this.order = this.$el.attributes['order'] != undefined ? this.$el.attributes['order'].value : undefined; this.order = this.$el.attributes['order'] != undefined ? this.$el.attributes['order'].value : undefined;
this.orderBy = this.$el.attributes['order-by'] != undefined ? this.$el.attributes['order-by'].value : undefined; this.orderBy = this.$el.attributes['order-by'] != undefined ? this.$el.attributes['order-by'].value : undefined;
this.orderByMetaKey = this.$el.attributes['order-by-meta-key'] != undefined ? this.$el.attributes['order-by-meta-key'].value : undefined;
this.showSearchBar = this.$el.attributes['show-search-bar'] != undefined ? this.$el.attributes['show-search-bar'].value == 'true' : false; this.showSearchBar = this.$el.attributes['show-search-bar'] != undefined ? this.$el.attributes['show-search-bar'].value == 'true' : false;
this.showCollectionHeader = this.$el.attributes['show-collection-header'] != undefined ? this.$el.attributes['show-collection-header'].value == 'true' : false; this.showCollectionHeader = this.$el.attributes['show-collection-header'] != undefined ? this.$el.attributes['show-collection-header'].value == 'true' : false;
this.showCollectionLabel = this.$el.attributes['show-collection-label'] != undefined ? this.$el.attributes['show-collection-label'].value == 'true' : false; this.showCollectionLabel = this.$el.attributes['show-collection-label'] != undefined ? this.$el.attributes['show-collection-label'].value == 'true' : false;

View File

@ -300,6 +300,7 @@ export default {
imageSize: String, imageSize: String,
order: String, order: String,
orderBy: String, orderBy: String,
orderByMetaKey: String,
showSearchBar: Boolean, showSearchBar: Boolean,
showCollectionHeader: Boolean, showCollectionHeader: Boolean,
showCollectionLabel: Boolean, showCollectionLabel: Boolean,
@ -420,6 +421,10 @@ export default {
if (this.orderBy != undefined) if (this.orderBy != undefined)
queryObject.orderby = this.orderBy; queryObject.orderby = this.orderBy;
// Set up orderByMetaKey
if (this.orderByMetaKey != undefined)
queryObject.metakey = this.orderByMetaKey;
// Set up search string // Set up search string
if (this.searchString != undefined) if (this.searchString != undefined)
queryObject.search = this.searchString; queryObject.search = this.searchString;