Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow-content-type config of ContentSelector is not working inside si… #5815

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions modules/lib/src/main/resources/assets/js/app/ContentFormContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {ContentTypeName} from '@enonic/lib-admin-ui/schema/content/ContentTypeNa
import {ContentId} from './content/ContentId';
import {ContentPath} from './content/ContentPath';
import {Project} from './settings/data/project/Project';
import {ApplicationKey} from '@enonic/lib-admin-ui/application/ApplicationKey';

export class ContentFormContext
extends FormContext {
Expand All @@ -16,9 +17,11 @@ export class ContentFormContext

private persistedContent?: Content;

private contentTypeName?: ContentTypeName;
private readonly contentTypeName?: ContentTypeName;

private project?: Project;
private readonly project?: Project;

private readonly applicationKey?: ApplicationKey;

private contentUpdatedListeners: { (content: Content): void }[] = [];

Expand All @@ -29,6 +32,7 @@ export class ContentFormContext
this.persistedContent = builder.persistedContent;
this.contentTypeName = builder.contentTypeName;
this.project = builder.project;
this.applicationKey = builder.applicationKey;
}

getSite(): Site {
Expand Down Expand Up @@ -66,6 +70,10 @@ export class ContentFormContext
return this.project;
}

getApplicationKey(): ApplicationKey {
return this.applicationKey;
}

createInputTypeViewContext(inputTypeConfig: any, parentPropertyPath: PropertyPath, input: Input): ContentInputTypeViewContext {
const viewContext = <ContentInputTypeViewContext> {
formContext: this,
Expand All @@ -74,7 +82,8 @@ export class ContentFormContext
parentDataPath: parentPropertyPath,
site: this.getSite(),
content: this.getPersistedContent(),
project: this.getProject()
project: this.getProject(),
applicationKey: this.applicationKey
};

this.contentUpdatedListeners.push(content => {
Expand All @@ -90,6 +99,7 @@ export class ContentFormContext
.setPersistedContent(this.persistedContent)
.setContentTypeName(this.contentTypeName)
.setProject(this.project)
.setApplicationKey(this.applicationKey)
.setFormState(this.getFormState())
.setShowEmptyFormItemSetOccurrences(this.getShowEmptyFormItemSetOccurrences())
.setValidationErrors(this.getValidationErrors());
Expand All @@ -111,26 +121,33 @@ export class ContentFormContextBuilder

project: Project;

public setSite(value: Site): ContentFormContextBuilder {
applicationKey: ApplicationKey;

public setSite(value: Site): this {
this.site = value;
return this;
}

public setPersistedContent(value: Content): ContentFormContextBuilder {
public setPersistedContent(value: Content): this {
this.persistedContent = value;
return this;
}

public setContentTypeName(value: ContentTypeName): ContentFormContextBuilder {
public setContentTypeName(value: ContentTypeName): this {
this.contentTypeName = value;
return this;
}

public setProject(value: Project): ContentFormContextBuilder {
public setProject(value: Project): this {
this.project = value;
return this;
}

public setApplicationKey(value: ApplicationKey): this {
this.applicationKey = value;
return this;
}

public build(): ContentFormContext {
return new ContentFormContext(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import {InputTypeViewContext} from '@enonic/lib-admin-ui/form/inputtype/InputTyp
import {ContentFormContext} from '../ContentFormContext';
import {Site} from '../content/Site';
import {ContentSummary} from '../content/ContentSummary';
import {ContentPath} from '../content/ContentPath';
import {Project} from '../settings/data/project/Project';
import {ApplicationKey} from '@enonic/lib-admin-ui/application/ApplicationKey';

export interface ContentInputTypeViewContext
extends InputTypeViewContext {
Expand All @@ -16,4 +16,6 @@ export interface ContentInputTypeViewContext

project?: Project;

applicationKey?: ApplicationKey;

}
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ export class ContentSelector
.setContentTypeNames(this.allowedContentTypes)
.setRelationshipType(this.relationshipType)
.setContent(this.context.content)
.setProject(this.context.project);
.setProject(this.context.project)
.setApplicationKey(this.context.applicationKey);
}

protected doCreateContentComboBoxBuilder(): ContentComboBoxBuilder<ContentTreeSelectorItem> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,10 @@ export class SiteConfiguratorSelectedOptionView

private createFormView(siteConfig: ApplicationConfig): FormView {
const context: ContentFormContext =
<ContentFormContext>this.formContext.cloneBuilder().setFormState(new FormState(this.isNew)).build();
<ContentFormContext>this.formContext.cloneBuilder()
.setApplicationKey(this.application.getApplicationKey())
.setFormState(new FormState(this.isNew))
.build();
const formView: FormView =
<FormView>new FormView(context, this.application.getForm(), siteConfig.getConfig()).addClass('site-form');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ import {ContentSummaryJson} from '../../../content/ContentSummaryJson';
import {ContentSelectorRequest} from '../../../resource/ContentSelectorRequest';
import {ListByIdSelectorRequest} from '../../../resource/ListByIdSelectorRequest';
import {Project} from '../../../settings/data/project/Project';
import {ApplicationKey} from '@enonic/lib-admin-ui/application/ApplicationKey';

export class ContentSummaryOptionDataLoader<DATA extends ContentTreeSelectorItem>
extends OptionDataLoader<DATA> {

protected readonly project?: Project;

protected readonly applicationKey: ApplicationKey;

private readonly treeRequest: ContentTreeSelectorQueryRequest<DATA> | ListByIdSelectorRequest<DATA>;

private readonly flatRequest: ContentSelectorQueryRequest<ContentSummaryJson, ContentSummary>;
Expand All @@ -40,29 +43,30 @@ export class ContentSummaryOptionDataLoader<DATA extends ContentTreeSelectorItem
super();

this.project = builder?.project;
this.applicationKey = builder?.applicationKey;
this.smartTreeMode = builder ? builder.smartTreeMode : true;

this.flatRequest = new ContentSelectorQueryRequest().setRequestProject(this.project);
this.treeRequest = this.smartTreeMode ? new ContentTreeSelectorQueryRequest<DATA>().setRequestProject(this.project) :
new ListByIdSelectorRequest<DATA>().setRequestProject(this.project);

this.flatRequest.setApplicationKey(builder?.applicationKey);
this.treeRequest.setApplicationKey(builder?.applicationKey);

if (builder) {
this.initRequests(builder);
}

}

load(postLoad: boolean = false): Q.Promise<DATA[]> {

if (!this.isTreeLoadMode) {
return this.sendAndParseFlatRequest(true, postLoad);
}

this.treeRequest.setContent(null);

this.notifyLoadingData(postLoad);
return this.loadItems().then(data => {

return this.loadItems().then((data: DATA[]) => {
this.notifyLoadedData(data, postLoad);
return data;
});
Expand Down Expand Up @@ -248,6 +252,8 @@ export class ContentSummaryOptionDataLoaderBuilder {

project: Project;

applicationKey: ApplicationKey;

public setContentTypeNames(contentTypeNames: string[]): ContentSummaryOptionDataLoaderBuilder {
this.contentTypeNames = contentTypeNames;
return this;
Expand Down Expand Up @@ -278,6 +284,11 @@ export class ContentSummaryOptionDataLoaderBuilder {
return this;
}

public setApplicationKey(key: ApplicationKey): ContentSummaryOptionDataLoaderBuilder {
this.applicationKey = key;
return this;
}

build(): ContentSummaryOptionDataLoader<ContentTreeSelectorItem> {
return new ContentSummaryOptionDataLoader(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {HttpMethod} from '@enonic/lib-admin-ui/rest/HttpMethod';
import {ContentSummary} from '../content/ContentSummary';
import {CmsContentResourceRequest} from './CmsContentResourceRequest';
import {ResultMetadata} from './ResultMetadata';
import {ApplicationKey} from '@enonic/lib-admin-ui/application/ApplicationKey';

export abstract class ContentSelectorRequest<CONTENT>
extends CmsContentResourceRequest<CONTENT[]> {
Expand Down Expand Up @@ -39,11 +40,13 @@ export abstract class ContentSelectorRequest<CONTENT>

private relationshipType: string;

private applicationKey: ApplicationKey;

protected loaded: boolean;

protected results: CONTENT[] = [];

constructor() {
protected constructor() {
super();
this.setMethod(HttpMethod.POST);

Expand Down Expand Up @@ -116,6 +119,10 @@ export abstract class ContentSelectorRequest<CONTENT>
this.queryExpr = queryExpr;
}

setApplicationKey(key: ApplicationKey): void {
this.applicationKey = key;
}

protected createSearchExpression(searchString: string): Expression {
return new PathMatchExpressionBuilder()
.setSearchString(searchString)
Expand Down Expand Up @@ -155,7 +162,8 @@ export abstract class ContentSelectorRequest<CONTENT>
inputName: this.getInputName(),
contentTypeNames: this.contentTypeNames,
allowedContentPaths: this.allowedContentPaths,
relationshipType: this.relationshipType
relationshipType: this.relationshipType,
applicationKey: this.applicationKey?.toString() || null
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@ export class ProjectApplicationSelectedOptionView
const app: Application = this.getOption().getDisplayValue();
const isNew: boolean = !this.currentConfigSet;
const propSet: PropertySet = new PropertyTree(isNew ? new PropertySet() : this.currentConfigSet).getRoot();
const context: FormContext = ContentFormContext.create().setProject(this.project).setFormState(new FormState(isNew)).build();
const context: FormContext = ContentFormContext.create()
.setProject(this.project)
.setApplicationKey(app.getApplicationKey())
.setFormState(new FormState(isNew))
.build();
const formView: FormView = new FormViewWrapper(context, app.getForm(), propSet);

formView.onLayoutFinished(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
package com.enonic.xp.app.contentstudio.rest.resource.content;

import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import com.google.common.base.Preconditions;

import com.enonic.xp.app.ApplicationKey;
import com.enonic.xp.app.ApplicationWildcardMatcher;
import com.enonic.xp.app.contentstudio.rest.resource.content.json.ContentSelectorQueryJson;
import com.enonic.xp.content.*;
import com.enonic.xp.content.Content;
import com.enonic.xp.content.ContentConstants;
import com.enonic.xp.content.ContentQuery;
import com.enonic.xp.content.ContentRelativePathResolver;
import com.enonic.xp.content.ContentService;
import com.enonic.xp.node.NodeIndexPath;
import com.enonic.xp.query.expr.*;
import com.enonic.xp.query.expr.CompareExpr;
import com.enonic.xp.query.expr.ConstraintExpr;
import com.enonic.xp.query.expr.FieldExpr;
import com.enonic.xp.query.expr.LogicalExpr;
import com.enonic.xp.query.expr.QueryExpr;
import com.enonic.xp.query.expr.ValueExpr;
import com.enonic.xp.query.parser.QueryParser;
import com.enonic.xp.schema.content.ContentType;
import com.enonic.xp.schema.content.ContentTypeName;
Expand All @@ -14,11 +30,6 @@
import com.enonic.xp.schema.relationship.RelationshipTypeName;
import com.enonic.xp.schema.relationship.RelationshipTypeService;
import com.enonic.xp.site.Site;
import com.google.common.base.Preconditions;

import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static com.google.common.base.Strings.isNullOrEmpty;

Expand Down Expand Up @@ -63,27 +74,53 @@ public ContentQuery createQuery()

private ContentTypeNames getContentTypeNamesFromJson()
{
List<String> contentTypeNames = this.contentQueryJson.getContentTypeNames();
final List<String> contentTypeNames = this.contentQueryJson.getContentTypeNames();

if ( contentTypeNames.isEmpty() )
{
return this.getContentTypeNamesFromRelationshipType();
}

if ( this.content != null )
final ApplicationKey applicationKey = getApplicationKey();

if ( applicationKey != null )
{
final ApplicationWildcardMatcher<ContentTypeName> wildcardMatcher =
new ApplicationWildcardMatcher<>( this.content.getType().getApplicationKey(), ContentTypeName::toString,
this.contentTypeParseMode );

final Predicate<ContentTypeName> filter =
contentTypeNames.stream().map( wildcardMatcher::createPredicate ).reduce( Predicate::or ).orElse( s -> false );
return ContentTypeNames.from(
contentTypeService.getAll().stream().map( ContentType::getName ).filter( filter ).collect( Collectors.toList() ) );
return this.filterContentTypeNames( applicationKey );
}

return ContentTypeNames.from( contentTypeNames );
}

private ApplicationKey getApplicationKey()
{
if ( this.contentQueryJson.getApplicationKey() != null )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to have it tested

{
return this.contentQueryJson.getApplicationKey();
}

if ( this.content != null )
{
return this.content.getType().getApplicationKey();
}

return null;
}

private ContentTypeNames filterContentTypeNames( final ApplicationKey applicationKey )
{
final ApplicationWildcardMatcher<ContentTypeName> wildcardMatcher =
new ApplicationWildcardMatcher<>( applicationKey, ContentTypeName::toString, this.contentTypeParseMode );

final Predicate<ContentTypeName> filter = this.contentQueryJson.getContentTypeNames()
.stream()
.map( wildcardMatcher::createPredicate )
.reduce( Predicate::or )
.orElse( s -> false );

return ContentTypeNames.from(
contentTypeService.getAll().stream().map( ContentType::getName ).filter( filter ).collect( Collectors.toList() ) );
}

private ContentTypeNames getContentTypeNamesFromRelationshipType()
{
if ( this.contentQueryJson.getRelationshipType() == null )
Expand Down
Loading