1- import Q from 'q' ;
2- import { i18n } from '@enonic/lib-admin-ui/util/Messages' ;
3- import { Router } from '../../Router' ;
4- import { ContentServerEventsHandler } from '../../event/ContentServerEventsHandler' ;
5- import { ContentSummaryAndCompareStatus } from '../../content/ContentSummaryAndCompareStatus' ;
6- import { ContentQuery } from '../../content/ContentQuery' ;
71import { AggregationGroupView } from '@enonic/lib-admin-ui/aggregation/AggregationGroupView' ;
8- import { Aggregation } from '@enonic/lib-admin-ui/aggregation/Aggregation' ;
9- import { BrowseFilterPanel } from '@enonic/lib-admin-ui/app/browse/filter/BrowseFilterPanel' ;
102import { BucketAggregation } from '@enonic/lib-admin-ui/aggregation/BucketAggregation' ;
11- import { Bucket } from '@enonic/lib-admin-ui/aggregation/Bucket' ;
12- import { BucketAggregationView } from '@enonic/lib-admin-ui/aggregation/BucketAggregationView' ;
3+ import { BrowseFilterPanel } from '@enonic/lib-admin-ui/app/browse/filter/BrowseFilterPanel' ;
4+ import { TextSearchField } from '@enonic/lib-admin-ui/app/browse/filter/TextSearchField' ;
5+ import { AuthContext } from '@enonic/lib-admin-ui/auth/AuthContext' ;
6+ import { DefaultErrorHandler } from '@enonic/lib-admin-ui/DefaultErrorHandler' ;
7+ import { DivEl } from '@enonic/lib-admin-ui/dom/DivEl' ;
8+ import { Element } from '@enonic/lib-admin-ui/dom/Element' ;
9+ import { i18n } from '@enonic/lib-admin-ui/util/Messages' ;
10+ import { cn } from '@enonic/ui' ;
11+ import Q from 'q' ;
12+ import { ContentId } from '../../content/ContentId' ;
13+ import { ContentQuery } from '../../content/ContentQuery' ;
14+ import { ContentSummary } from '../../content/ContentSummary' ;
15+ import { ContentSummaryAndCompareStatus } from '../../content/ContentSummaryAndCompareStatus' ;
1316import { ContentServerChangeItem } from '../../event/ContentServerChangeItem' ;
17+ import { ContentServerEventsHandler } from '../../event/ContentServerEventsHandler' ;
1418import { ProjectContext } from '../../project/ProjectContext' ;
15- import { ContentSummary } from '../../content/ContentSummary' ;
16- import { ContentId } from '../../content/ContentId' ;
17- import { DependenciesSection } from './DependenciesSection' ;
18- import { ContentAggregation } from './ContentAggregation' ;
19+ import { Router } from '../../Router' ;
20+ import { Branch } from '../../versioning/Branch' ;
1921import { AggregationsDisplayNamesResolver } from './AggregationsDisplayNamesResolver' ;
20- import { ContentAggregationsFetcher } from './ContentAggregationsFetcher' ;
21- import { FilterableAggregationGroupView } from './FilterableAggregationGroupView' ;
2222import { AggregationsQueryResult } from './AggregationsQueryResult' ;
23- import { Element } from '@enonic/lib-admin-ui/dom/Element' ;
24- import { DivEl } from '@enonic/lib-admin-ui/dom/DivEl' ;
25- import { ContentExportElement } from './ContentExportElement' ;
23+ import { ContentAggregation } from './ContentAggregation' ;
24+ import { ContentAggregationsFetcher } from './ContentAggregationsFetcher' ;
2625import { ContentDependency } from './ContentDependency' ;
27- import { TextSearchField } from '@enonic/lib-admin-ui/app/browse/filter/TextSearchField' ;
28- import { Branch } from '../../versioning/Branch' ;
29- import { AuthContext } from '@enonic/lib-admin-ui/auth/AuthContext' ;
30- import { cn } from '@enonic/ui' ;
26+ import { ContentExportElement } from './ContentExportElement' ;
27+ import { DependenciesSection } from './DependenciesSection' ;
3128
3229export class ContentBrowseFilterPanel < T extends ContentSummaryAndCompareStatus = ContentSummaryAndCompareStatus >
3330 extends BrowseFilterPanel < T > {
@@ -45,10 +42,12 @@ export class ContentBrowseFilterPanel<T extends ContentSummaryAndCompareStatus =
4542 constructor ( ) {
4643 super ( ) ;
4744
48- this . addClass ( cn ( 'content-browse-filter-panel bg-surface-primary text-main' ) ) ;
45+ this . addClass ( cn ( 'content-browse-filter-panel bg-surface-neutral text-main' ) ) ;
4946 this . aggregationsFetcher = this . createAggregationFetcher ( ) ;
47+ this . displayNamesResolver = new AggregationsDisplayNamesResolver ( ) ;
48+ this . dependenciesSection = new DependenciesSection ( ) ;
5049
51- this . getAndUpdateAggregations ( ) ;
50+ this . getAndUpdateAggregations ( ) . catch ( DefaultErrorHandler . handle ) ;
5251 this . initElementsAndListeners ( ) ;
5352 }
5453
@@ -127,55 +126,35 @@ export class ContentBrowseFilterPanel<T extends ContentSummaryAndCompareStatus =
127126 } ) ;
128127 }
129128
130- protected createHitsCountContainer ( ) : DivEl {
131- const hitsCounterAndClearButtonWrapper = super . createHitsCountContainer ( ) ;
132- if ( this . exportElement ) {
133- hitsCounterAndClearButtonWrapper . appendChild ( this . exportElement ) ;
134- }
129+ protected getFilterableAggregations ( ) : { name : string ; idsToKeepOnTop ?: string [ ] } [ ] {
130+ const currentUserKey = AuthContext . get ( ) . getUser ( ) . getKey ( ) . toString ( ) ;
135131
136- return hitsCounterAndClearButtonWrapper ;
132+ return [
133+ {
134+ name : ContentAggregation . OWNER . toString ( ) ,
135+ idsToKeepOnTop : [ currentUserKey ]
136+ } ,
137+ {
138+ name : ContentAggregation . MODIFIED_BY . toString ( ) ,
139+ idsToKeepOnTop : [ currentUserKey ]
140+ } ,
141+ ] ;
137142 }
138143
139- protected getAggregationEnum ( ) : Record < string , string > {
140- return ContentAggregation ;
141- }
142-
143- protected getGroupViews ( ) : AggregationGroupView [ ] {
144- this . aggregations = new Map < string , AggregationGroupView > ( ) ;
145-
146- const aggregationEnum = this . getAggregationEnum ( ) ;
147- for ( let aggrEnum in aggregationEnum ) {
148- const name : string = aggregationEnum [ aggrEnum ] ;
149- this . aggregations . set ( name , this . createGroupView ( name ) ) ;
150- }
144+ getExportOptions ( ) : { label ?: string ; action : ( ) => void } {
145+ this . exportElement = new ContentExportElement ( ) . setEnabled ( false ) . setTitle ( i18n ( 'action.export' ) ) as ContentExportElement ;
151146
152- return Array . from ( this . aggregations . values ( ) ) ;
153- }
154-
155- protected isPrincipalAggregation ( name : string ) : boolean {
156- return name === ContentAggregation . OWNER . toString ( ) || name === ContentAggregation . MODIFIED_BY . toString ( ) ;
157- }
158-
159- protected createGroupView ( name : string ) : AggregationGroupView {
160- if ( this . isPrincipalAggregation ( name ) ) {
161- const currentUserKey = this . getCurrentUserKeyAsString ( ) ;
162- if ( ! this . displayNamesResolver ) {
163- this . displayNamesResolver = new AggregationsDisplayNamesResolver ( currentUserKey ) ;
147+ return {
148+ label : i18n ( 'action.export' ) ,
149+ action : ( ) => {
150+ this . exportElement . handleExportClicked ( ) ;
164151 }
165- const aggregationGroupView = new FilterableAggregationGroupView ( name , i18n ( `field.${ name } ` ) ) ;
166- aggregationGroupView . setIdsToKeepOnToTop ( [ currentUserKey ] ) ;
167- aggregationGroupView . setResolver ( this . displayNamesResolver ) ;
168-
169- return aggregationGroupView ;
170- }
171-
172- return new AggregationGroupView ( name , i18n ( `field.${ name } ` ) ) ;
152+ } ;
173153 }
174154
175- protected appendExtraSections ( ) {
176- super . appendExtraSections ( ) ;
177- this . dependenciesSection = new DependenciesSection ( ) ;
178- this . appendChild ( this . dependenciesSection ) ;
155+ protected isFilterableAggregation ( aggregation : BucketAggregation ) : boolean {
156+ return aggregation . getName ( ) === ContentAggregation . OWNER . toString ( ) || aggregation . getName ( ) ===
157+ ContentAggregation . MODIFIED_BY . toString ( ) ;
179158 }
180159
181160 protected isExportAllowed ( ) : boolean {
@@ -185,7 +164,6 @@ export class ContentBrowseFilterPanel<T extends ContentSummaryAndCompareStatus =
185164
186165 private removeDependencyItem ( ) {
187166 this . dependenciesSection . reset ( ) ;
188- this . resetConstraints ( ) ;
189167 this . search ( ) ;
190168 Router . get ( ) . back ( ) ;
191169 }
@@ -197,7 +175,6 @@ export class ContentBrowseFilterPanel<T extends ContentSummaryAndCompareStatus =
197175 this . selectBucketByTypeOnLoad ( type ) ;
198176 }
199177
200- this . setConstraintItems ( this . dependenciesSection , [ item . getId ( ) ] ) ;
201178 this . dependenciesSection . setDependencyItem ( item ) ;
202179 }
203180
@@ -220,7 +197,7 @@ export class ContentBrowseFilterPanel<T extends ContentSummaryAndCompareStatus =
220197 }
221198
222199 private selectContentTypeBucket ( key : string ) : void {
223- ( this . aggregations . get ( ContentAggregation . CONTENT_TYPE ) . getAggregationViews ( ) [ 0 ] as BucketAggregationView ) ?. selectBucketViewByKey ( key ) ;
200+ ( this . aggregations . get ( ContentAggregation . CONTENT_TYPE ) . getAggregationViews ( ) [ 0 ] ) ?. selectBucketViewByKey ( key ) ;
224201 }
225202
226203 searchItemById ( id : ContentId ) : void {
@@ -262,7 +239,6 @@ export class ContentBrowseFilterPanel<T extends ContentSummaryAndCompareStatus =
262239
263240 setSelectedItems ( itemsIds : string [ ] ) {
264241 this . dependenciesSection . reset ( ) ;
265- super . setSelectedItems ( itemsIds ) ;
266242 }
267243
268244 protected isFilteredOrConstrained ( ) {
@@ -276,7 +252,7 @@ export class ContentBrowseFilterPanel<T extends ContentSummaryAndCompareStatus =
276252 this . updateHitsCounter ( aggregationsQueryResult . getMetadata ( ) . getTotalHits ( ) ) ;
277253 this . updateExportState ( aggregationsQueryResult ) ;
278254
279- return this . processAggregations ( aggregationsQueryResult . getAggregations ( ) ) . then ( ( ) => {
255+ return this . processAggregations ( aggregationsQueryResult . getAggregations ( ) as BucketAggregation [ ] ) . then ( ( ) => {
280256 return aggregationsQueryResult ;
281257 } ) ;
282258 } ) ;
@@ -289,25 +265,27 @@ export class ContentBrowseFilterPanel<T extends ContentSummaryAndCompareStatus =
289265 this . exportElement . setTotal ( aggregationsQueryResult . getMetadata ( ) . getTotalHits ( ) ) ;
290266 this . exportElement . setSearchInputValues ( this . getSearchInputValues ( ) ) ;
291267 this . exportElement . setDependency ( this . getDependency ( ) ) ;
292- this . exportElement . setConstraintIds ( this . hasConstraint ( ) ? this . getSelectionItems ( ) . slice ( ) : null ) ;
293268 this . exportElement . setEnabled ( aggregationsQueryResult . getMetadata ( ) . getTotalHits ( ) > 0 ) ;
294269 }
295270
296- private processAggregations ( aggregations : Aggregation [ ] ) : Q . Promise < void > {
297- this . toggleAggregationsVisibility ( aggregations ) ;
271+ private processAggregations ( aggregations : BucketAggregation [ ] ) : Q . Promise < void > {
272+ this . sortAggregations ( aggregations ) ;
298273
299- return this . displayNamesResolver . updateAggregationsDisplayNames ( aggregations , this . getCurrentUserKeyAsString ( ) ) . then ( ( ) => {
274+ return this . displayNamesResolver . updateAggregationsDisplayNames ( aggregations ) . then ( ( ) => {
300275 this . updateAggregations ( aggregations ) ;
301276 } ) ;
302277 }
303278
304- private getCurrentUserKeyAsString ( ) : string {
305- return AuthContext . get ( ) . getUser ( ) . getKey ( ) . toString ( ) ;
279+ private sortAggregations ( aggregations : BucketAggregation [ ] ) : void {
280+ const order = Object . values ( ContentAggregation ) . filter ( value => typeof value === 'string' ) as string [ ] ;
281+
282+ aggregations . sort (
283+ ( a , b ) => order . indexOf ( a . getName ( ) ) - order . indexOf ( b . getName ( ) )
284+ ) ;
306285 }
307286
308287 private getAggregations ( ) : Q . Promise < AggregationsQueryResult > {
309288 this . aggregationsFetcher . setSearchInputValues ( this . getSearchInputValues ( ) ) ;
310- this . aggregationsFetcher . setConstraintItemsIds ( this . hasConstraint ( ) ? this . getSelectionItems ( ) . slice ( ) : null ) ;
311289 this . aggregationsFetcher . setDependency ( this . getDependency ( ) ) ;
312290
313291 return this . aggregationsFetcher . getAggregations ( ) ;
@@ -335,14 +313,6 @@ export class ContentBrowseFilterPanel<T extends ContentSummaryAndCompareStatus =
335313 return null ;
336314 }
337315
338- private toggleAggregationsVisibility ( aggregations : Aggregation [ ] ) {
339- aggregations . forEach ( ( aggregation : BucketAggregation ) => {
340- const isAggregationNotEmpty : boolean = aggregation . getBuckets ( ) . some ( ( bucket : Bucket ) => bucket . docCount > 0 ) ;
341- this . aggregations . get ( aggregation . getName ( ) ) . setVisible ( isAggregationNotEmpty ) ;
342- } ) ;
343- }
344-
345-
346316 // doing a trick to avoid changing lib-admin-ui, adding all children except export button to a wrapper
347317 appendChild ( child : Element , lazyRender ?: boolean ) : Element {
348318 if ( ! this . elementsContainer ) {
0 commit comments