Skip to content

Commit

Permalink
Merge pull request #957 from geonetwork/DH/open-data-switch
Browse files Browse the repository at this point in the history
[Editor]: Add an open data switch
  • Loading branch information
cmoinier authored Aug 23, 2024
2 parents 825a487 + 3a02a29 commit dd042ce
Show file tree
Hide file tree
Showing 18 changed files with 204 additions and 2 deletions.
50 changes: 50 additions & 0 deletions apps/metadata-editor-e2e/src/e2e/edit.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,56 @@ describe('editor form', () => {
.should('not.exist')
})
})
describe('Access and constraints', () => {
describe('Open data switch', () => {
describe('When the open data switch is checked', () => {
beforeEach(() => {
cy.visit('/edit/accroche_velos')
cy.get('md-editor-page-selector')
.find('gn-ui-button')
.last()
.click()
})
it('should not display the licence form field', () => {
cy.get('gn-ui-form-field-license').should('not.exist')
})
it('should display the license form field when toggled', () => {
cy.get('[data-cy="openDataToggle"]').click()
cy.get('gn-ui-form-field-license').should('be.visible')
cy.get('gn-ui-form-field-license')
.find('button')
.children('div')
.first()
.invoke('text')
.should('eq', ' Open Licence (Etalab) ')
})
})
describe('When the open data switch is unchecked', () => {
beforeEach(() => {
cy.visit(
'/edit/fr-120066022-jdd-f20f8125-877e-46dc-8cf8-2a8a372045eb'
)
cy.get('md-editor-page-selector')
.find('gn-ui-button')
.last()
.click()
})
it('should display the licence form field', () => {
cy.get('gn-ui-form-field-license').should('be.visible')
cy.get('gn-ui-form-field-license')
.find('button')
.children('div')
.first()
.invoke('text')
.should('eq', ' Creative Commons CC-BY ')
})
it('should hide the license form field when toggled', () => {
cy.get('[data-cy="openDataToggle"]').click()
cy.get('gn-ui-form-field-license').should('not.exist')
})
})
})
})
})

describe('date range in sortable list', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<gn-ui-check-toggle
[label]="'editor.record.form.classification.opendata' | translate"
[value]="value"
(toggled)="onOpenDataToggled($event)"
data-cy="openDataToggle"
></gn-ui-check-toggle>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { ComponentFixture, TestBed } from '@angular/core/testing'
import { FormFieldOpenDataComponent } from './form-field-open-data.component'
import { TranslateModule } from '@ngx-translate/core'
import { FormControl } from '@angular/forms'

jest.mock('./../../../../fields.config', () => {
return {
OPEN_DATA_LICENSES: ['CC-BY'],
}
})

describe('FormFieldOpenDataComponent', () => {
let component: FormFieldOpenDataComponent
let fixture: ComponentFixture<FormFieldOpenDataComponent>

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [FormFieldOpenDataComponent, TranslateModule.forRoot()],
}).compileComponents()

fixture = TestBed.createComponent(FormFieldOpenDataComponent)
component = fixture.componentInstance
component.control = new FormControl()
component.control.setValue([{ text: 'odc-by' }])
fixture.detectChanges()
})

it('should create', () => {
expect(component).toBeTruthy()
})

it('should emit the new license value on toggle', () => {
const control = new FormControl()
const setValueSpy = jest.spyOn(control, 'setValue')
component.control = control

component.onOpenDataToggled(true)

expect(setValueSpy).toHaveBeenCalledWith([{ text: 'CC-BY' }])
})

it('should emit the event value on toggle', () => {
const toggledSpy = jest.spyOn(component.visibilityChange, 'emit')
component.onOpenDataToggled(true)
expect(toggledSpy).toHaveBeenCalledWith(true)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import {
ChangeDetectionStrategy,
Component,
EventEmitter,
Input,
OnChanges,

Check warning on line 6 in libs/feature/editor/src/lib/components/record-form/form-field/form-field-open-data/form-field-open-data.component.ts

View workflow job for this annotation

GitHub Actions / Format check, lint, unit tests

'OnChanges' is defined but never used
OnInit,
Output,
SimpleChanges,

Check warning on line 9 in libs/feature/editor/src/lib/components/record-form/form-field/form-field-open-data/form-field-open-data.component.ts

View workflow job for this annotation

GitHub Actions / Format check, lint, unit tests

'SimpleChanges' is defined but never used
} from '@angular/core'
import { FormControl } from '@angular/forms'
import { CheckToggleComponent } from '@geonetwork-ui/ui/inputs'
import { TranslateModule } from '@ngx-translate/core'
import { OPEN_DATA_LICENSES } from './../../../../fields.config'
import { Subscription } from 'rxjs'

@Component({
selector: 'gn-ui-form-field-open-data',
templateUrl: './form-field-open-data.component.html',
styleUrls: ['./form-field-open-data.component.css'],
standalone: true,
imports: [CheckToggleComponent, TranslateModule],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class FormFieldOpenDataComponent implements OnInit {
@Input() control: FormControl
value = false
@Output() visibilityChange = new EventEmitter<boolean>()
subscription: Subscription

get config() {
return OPEN_DATA_LICENSES
}

ngOnInit() {
this.initToggle()
this.subscription = new Subscription()

this.subscription.add(
this.control.valueChanges.subscribe((value) => {
this.value = this.config.includes(value[0].text)
this.visibilityChange.emit(this.value)
})
)
}

initToggle() {
this.value = this.config.includes(this.control.value[0].text)
this.visibilityChange.emit(this.value)
}

onOpenDataToggled(boolean) {
if (boolean) {
this.control.setValue([{ text: this.config[0] }])
}
this.value = !this.value
this.visibilityChange.emit(boolean)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<div class="flex flex-col h-full">
<ng-container *ngIf="isOpenData">
<gn-ui-form-field-open-data
[control]="formControl"
(visibilityChange)="onVisibilityChange($event)"
></gn-ui-form-field-open-data>
</ng-container>
<div class="flex flex-col h-full" *ngIf="!isHidden">
<ng-container *ngIf="withoutWrapper; else withGenericWrapper">
<ng-container *ngTemplateOutlet="fieldContent"></ng-container>
</ng-container>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { EditorFacade } from '../../../+state/editor.facade'
import { DATASET_RECORDS } from '@geonetwork-ui/common/fixtures'
import { NO_ERRORS_SCHEMA } from '@angular/core'
import { HttpClientTestingModule } from '@angular/common/http/testing'
import { FormControl } from '@angular/forms'
import { getGlobalConfig } from '@geonetwork-ui/util/app-config'

Check warning on line 20 in libs/feature/editor/src/lib/components/record-form/form-field/form-field.component.spec.ts

View workflow job for this annotation

GitHub Actions / Format check, lint, unit tests

'getGlobalConfig' is defined but never used

class EditorFacadeMock {
record$ = new BehaviorSubject(DATASET_RECORDS[0])
Expand Down Expand Up @@ -71,7 +73,7 @@ describe('FormFieldComponent', () => {
let formField
beforeEach(() => {
component.model = 'licenses'
component.value = 'cc-by'
component.formControl = new FormControl([{ text: 'cc-by' }])
fixture.detectChanges()
formField = fixture.debugElement.query(
By.directive(FormFieldLicenseComponent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ChangeDetectionStrategy,
Component,
ElementRef,
EventEmitter,

Check warning on line 6 in libs/feature/editor/src/lib/components/record-form/form-field/form-field.component.ts

View workflow job for this annotation

GitHub Actions / Format check, lint, unit tests

'EventEmitter' is defined but never used
Input,
Output,
ViewChild,
Expand Down Expand Up @@ -33,6 +34,7 @@ import { map, take } from 'rxjs/operators'
import { EditorFacade } from '../../../+state/editor.facade'
import { FormFieldConfig } from '../../../models'
import { FormFieldContactsForResourceComponent } from './form-field-contacts-for-resource/form-field-contacts-for-resource.component'
import { FormFieldOpenDataComponent } from './form-field-open-data/form-field-open-data.component'

@Component({
selector: 'gn-ui-form-field',
Expand Down Expand Up @@ -61,6 +63,7 @@ import { FormFieldContactsForResourceComponent } from './form-field-contacts-for
TranslateModule,
FormFieldOverviewsComponent,
FormFieldContactsForResourceComponent,
FormFieldOpenDataComponent,
],
})
export class FormFieldComponent {
Expand All @@ -73,6 +76,7 @@ export class FormFieldComponent {
}

@Output() valueChange: Observable<unknown>
isHidden = false

@ViewChild('titleInput') titleInput: ElementRef

Expand All @@ -91,6 +95,10 @@ export class FormFieldComponent {
this.titleInput.nativeElement.children[0].focus()
}

onVisibilityChange(visibility: boolean) {
this.isHidden = visibility
}

get isTitle() {
return this.model === 'title'
}
Expand Down Expand Up @@ -131,4 +139,8 @@ export class FormFieldComponent {
get withoutWrapper() {
return this.model === 'title' || this.model === 'abstract'
}

get isOpenData() {
return this.model === 'licenses'
}
}
12 changes: 12 additions & 0 deletions libs/feature/editor/src/lib/fields.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,15 @@ export const DEFAULT_CONFIGURATION: EditorConfig = {
},
],
}

/************************************************************
*************** LICENSES *****************
************************************************************
*/
export const OPEN_DATA_LICENSES: string[] = [
'etalab',
'etalab-v2',
'odbl',
'odc-by',
'pddl',
]
Binary file modified support-services/docker-entrypoint-initdb.d/dump
Binary file not shown.
1 change: 1 addition & 0 deletions translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@
"editor.record.form.field.title": "Metadaten-Titel",
"editor.record.form.field.uniqueIdentifier": "Eindeutige Kennung (ID)",
"editor.record.form.field.updateFrequency": "Aktualisierungshäufigkeit",
"editor.record.form.classification.opendata": "",
"editor.record.form.license.cc-by": "Creative Commons CC-BY",
"editor.record.form.license.cc-by-sa": "Creative Commons CC-BY-SA",
"editor.record.form.license.cc-zero": "Creative Commons CC-0",
Expand Down
1 change: 1 addition & 0 deletions translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
"editor.record.form.bottomButtons.comeBackLater": "Come back later",
"editor.record.form.bottomButtons.next": "Next",
"editor.record.form.bottomButtons.previous": "Previous",
"editor.record.form.classification.opendata": "Open Data",
"editor.record.form.field.abstract": "Abstract",
"editor.record.form.field.contactsForResource.noContact": "Please provide at least one point of contact responsible for the data.",
"editor.record.form.field.keywords": "Keywords",
Expand Down
1 change: 1 addition & 0 deletions translations/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
"editor.record.form.bottomButtons.comeBackLater": "",
"editor.record.form.bottomButtons.next": "",
"editor.record.form.bottomButtons.previous": "",
"editor.record.form.classification.opendata": "",
"editor.record.form.field.abstract": "",
"editor.record.form.field.contactsForResource.noContact": "",
"editor.record.form.field.keywords": "",
Expand Down
1 change: 1 addition & 0 deletions translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
"editor.record.form.bottomButtons.comeBackLater": "Revenir plus tard",
"editor.record.form.bottomButtons.next": "Suivant",
"editor.record.form.bottomButtons.previous": "Précédent",
"editor.record.form.classification.opendata": "Données ouvertes",
"editor.record.form.field.abstract": "Résumé",
"editor.record.form.field.contactsForResource.noContact": "Veuillez renseigner au moins un point de contact responsable de la donnée.",
"editor.record.form.field.keywords": "Mots-clés",
Expand Down
1 change: 1 addition & 0 deletions translations/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
"editor.record.form.bottomButtons.comeBackLater": "",
"editor.record.form.bottomButtons.next": "",
"editor.record.form.bottomButtons.previous": "",
"editor.record.form.classification.opendata": "",
"editor.record.form.field.abstract": "",
"editor.record.form.field.contactsForResource.noContact": "",
"editor.record.form.field.keywords": "",
Expand Down
1 change: 1 addition & 0 deletions translations/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
"editor.record.form.bottomButtons.comeBackLater": "",
"editor.record.form.bottomButtons.next": "",
"editor.record.form.bottomButtons.previous": "",
"editor.record.form.classification.opendata": "",
"editor.record.form.field.abstract": "",
"editor.record.form.field.contactsForResource.noContact": "",
"editor.record.form.field.keywords": "",
Expand Down
1 change: 1 addition & 0 deletions translations/pt.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
"editor.record.form.bottomButtons.comeBackLater": "",
"editor.record.form.bottomButtons.next": "",
"editor.record.form.bottomButtons.previous": "",
"editor.record.form.classification.opendata": "",
"editor.record.form.field.abstract": "",
"editor.record.form.field.contactsForResource.noContact": "",
"editor.record.form.field.keywords": "",
Expand Down
1 change: 1 addition & 0 deletions translations/sk.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
"editor.record.form.bottomButtons.comeBackLater": "",
"editor.record.form.bottomButtons.next": "",
"editor.record.form.bottomButtons.previous": "",
"editor.record.form.classification.opendata": "",
"editor.record.form.field.abstract": "",
"editor.record.form.field.contactsForResource.noContact": "",
"editor.record.form.field.keywords": "",
Expand Down

0 comments on commit dd042ce

Please sign in to comment.