From 581522d66012035b160a67fbea41f044c5650ea2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20=28Greg=29=20Zi=C3=B3=C5=82kowski?= Date: Mon, 16 Jul 2018 12:25:44 +0200 Subject: [PATCH] Utils: Deprecate buildTermTree function (#7976) --- components/query-controls/category-select.js | 6 +--- {utils => components/query-controls}/terms.js | 0 docs/reference/deprecated.md | 1 + editor/components/page-attributes/parent.js | 6 +++- .../hierarchical-term-selector.js | 6 +++- editor/utils/terms.js | 28 ++++++++++++++++ {utils => editor/utils}/test/terms.js | 0 utils/deprecated.js | 33 +++++++++++++++++++ utils/index.js | 1 - 9 files changed, 73 insertions(+), 8 deletions(-) rename {utils => components/query-controls}/terms.js (100%) create mode 100644 editor/utils/terms.js rename {utils => editor/utils}/test/terms.js (100%) diff --git a/components/query-controls/category-select.js b/components/query-controls/category-select.js index 978dd9da84e1c9..0ee0575addaf58 100644 --- a/components/query-controls/category-select.js +++ b/components/query-controls/category-select.js @@ -1,11 +1,7 @@ -/** - * WordPress dependencies - */ -import { buildTermsTree } from '@wordpress/utils'; - /** * Internal dependencies */ +import { buildTermsTree } from './terms'; import TreeSelect from '../tree-select'; export default function CategorySelect( { label, noOptionLabel, categoriesList, selectedCategoryId, onChange } ) { diff --git a/utils/terms.js b/components/query-controls/terms.js similarity index 100% rename from utils/terms.js rename to components/query-controls/terms.js diff --git a/docs/reference/deprecated.md b/docs/reference/deprecated.md index d012d472680ace..c3219f238cc9ff 100644 --- a/docs/reference/deprecated.md +++ b/docs/reference/deprecated.md @@ -10,6 +10,7 @@ Gutenberg's deprecation policy is intended to support backwards-compatibility fo - `wp.element.pure` has been removed. Please use `wp.compose.pure` instead. - `wp.element.compose` has been removed. Please use `wp.compose.compose` instead. - `wp.element.createHigherOrderComponent` has been removed. Please use `wp.compose.createHigherOrderComponent` instead. + - `wp.utils.buildTermsTree` has been removed. ## 3.4.0 diff --git a/editor/components/page-attributes/parent.js b/editor/components/page-attributes/parent.js index f15d156a3bb2fb..a48ba1e2fb18c0 100644 --- a/editor/components/page-attributes/parent.js +++ b/editor/components/page-attributes/parent.js @@ -10,9 +10,13 @@ import { stringify } from 'querystringify'; import { __ } from '@wordpress/i18n'; import { TreeSelect, withAPIData } from '@wordpress/components'; import { compose } from '@wordpress/compose'; -import { buildTermsTree } from '@wordpress/utils'; import { withSelect, withDispatch } from '@wordpress/data'; +/** + * Internal dependencies + */ +import { buildTermsTree } from '../../utils/terms'; + export function PageAttributesParent( { parent, postType, items, onUpdateParent } ) { const isHierarchical = get( postType, [ 'hierarchical' ], false ); const parentPageLabel = get( postType, [ 'labels', 'parent_item_colon' ] ); diff --git a/editor/components/post-taxonomies/hierarchical-term-selector.js b/editor/components/post-taxonomies/hierarchical-term-selector.js index c130334fbe29db..e64e0dc60c2e0a 100644 --- a/editor/components/post-taxonomies/hierarchical-term-selector.js +++ b/editor/components/post-taxonomies/hierarchical-term-selector.js @@ -10,11 +10,15 @@ import { stringify } from 'querystring'; import { __, _x, sprintf } from '@wordpress/i18n'; import { Component } from '@wordpress/element'; import { TreeSelect, withAPIData, withSpokenMessages, Button } from '@wordpress/components'; -import { buildTermsTree } from '@wordpress/utils'; import { withSelect, withDispatch } from '@wordpress/data'; import apiRequest from '@wordpress/api-request'; import { withInstanceId, compose } from '@wordpress/compose'; +/** + * Internal dependencies + */ +import { buildTermsTree } from '../../utils/terms'; + /** * Module Constants */ diff --git a/editor/utils/terms.js b/editor/utils/terms.js new file mode 100644 index 00000000000000..cbeb9d0bb4b7ae --- /dev/null +++ b/editor/utils/terms.js @@ -0,0 +1,28 @@ +/** + * External dependencies + */ +import { groupBy } from 'lodash'; + +/** + * Returns terms in a tree form. + * + * @param {Array} flatTerms Array of terms in flat format. + * + * @return {Array} Array of terms in tree format. + */ +export function buildTermsTree( flatTerms ) { + const termsByParent = groupBy( flatTerms, 'parent' ); + const fillWithChildren = ( terms ) => { + return terms.map( ( term ) => { + const children = termsByParent[ term.id ]; + return { + ...term, + children: children && children.length ? + fillWithChildren( children ) : + [], + }; + } ); + }; + + return fillWithChildren( termsByParent[ '0' ] || [] ); +} diff --git a/utils/test/terms.js b/editor/utils/test/terms.js similarity index 100% rename from utils/test/terms.js rename to editor/utils/test/terms.js diff --git a/utils/deprecated.js b/utils/deprecated.js index b60062f8fb3301..f02e8a2f601714 100644 --- a/utils/deprecated.js +++ b/utils/deprecated.js @@ -4,6 +4,39 @@ import * as keycodesSource from '@wordpress/keycodes'; import deprecated from '@wordpress/deprecated'; +/** + * External dependencies + */ +import { groupBy } from 'lodash'; + +/** + * Returns terms in a tree form. + * + * @param {Array} flatTerms Array of terms in flat format. + * + * @return {Array} Array of terms in tree format. + */ +export function buildTermsTree( flatTerms ) { + deprecated( 'wp.utils.buildTermsTree', { + version: '3.5', + plugin: 'Gutenberg', + } ); + const termsByParent = groupBy( flatTerms, 'parent' ); + const fillWithChildren = ( terms ) => { + return terms.map( ( term ) => { + const children = termsByParent[ term.id ]; + return { + ...term, + children: children && children.length ? + fillWithChildren( children ) : + [], + }; + } ); + }; + + return fillWithChildren( termsByParent[ '0' ] || [] ); +} + // keycodes const wrapKeycodeFunction = ( source, functionName ) => ( ...args ) => { deprecated( `wp.utils.keycodes.${ functionName }`, { diff --git a/utils/index.js b/utils/index.js index 20754ba3c0dc5a..9c43c10bf45045 100644 --- a/utils/index.js +++ b/utils/index.js @@ -6,7 +6,6 @@ import { decodeEntities } from './entities'; export { decodeEntities }; export * from './mediaupload'; -export * from './terms'; // Deprecations export * from './deprecated';