Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an edit view with angular2-schema-form on test app.
- Loading branch information
1 parent
73dad27
commit 1590953
Showing
12 changed files
with
181 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,96 @@ | ||
import { Component } from '@angular/core'; | ||
import { Component, OnInit } from '@angular/core'; | ||
|
||
import { GlobalNavigation, Breadcrumbs } from '@plone/restapi-angular'; | ||
import { ViewView } from '@plone/restapi-angular'; | ||
import { Breadcrumbs, EditView, GlobalNavigation, Services, ViewView } from '@plone/restapi-angular'; | ||
import { Target } from 'angular-traversal'; | ||
|
||
@Component({ | ||
selector: 'custom-breadcrumbs', | ||
templateUrl: './breadcrumbs.html' | ||
}) | ||
export class CustomBreadcrumbs extends Breadcrumbs {} | ||
export class CustomBreadcrumbs extends Breadcrumbs { | ||
} | ||
|
||
@Component({ | ||
selector: 'custom-navigation', | ||
templateUrl: './navigation.html' | ||
}) | ||
export class CustomGlobalNavigation extends GlobalNavigation {} | ||
export class CustomGlobalNavigation extends GlobalNavigation { | ||
} | ||
|
||
@Component({ | ||
selector: 'custom-view', | ||
templateUrl: './view.html' | ||
}) | ||
export class CustomViewView extends ViewView { | ||
mode: 'view' | 'edit' = 'view'; | ||
mode: 'view' | 'edit' | 'advanced-edit' = 'view'; | ||
downloaded = false; | ||
changeMode(mode: 'view' | 'edit') { | ||
|
||
changeMode(mode: 'view' | 'edit' | 'advanced-edit') { | ||
this.mode = mode; | ||
} | ||
|
||
} | ||
|
||
@Component({ | ||
selector: 'custom-sf-edit', | ||
template: `<sf-form [schema]="schema" [model]="model" [actions]="actions"></sf-form>` | ||
}) | ||
export class CustomSfEditView extends EditView implements OnInit { | ||
|
||
schema: any; | ||
actions: any = {}; | ||
|
||
constructor(services: Services) { | ||
super(services); | ||
this.schema = { | ||
'properties': {}, | ||
'buttons': [ | ||
{ id: 'save', label: 'Save' }, | ||
{ id: 'cancel', label: 'Cancel' } | ||
] | ||
}; | ||
} | ||
|
||
onTraverse(target: Target) { | ||
super.onTraverse(target); | ||
const model = target.context; | ||
this.actions = { | ||
save: this.onSave.bind(this), | ||
cancel: this.onCancel.bind(this) | ||
}; | ||
this.services.resource.type(target.context['@type']).subscribe(schema => { | ||
schema.buttons = [ | ||
{ id: 'save', label: 'Save' }, | ||
{ id: 'cancel', label: 'Cancel' } | ||
]; | ||
// FIX THE SCHEMA AND THE MODEL | ||
for (const property in schema.properties) { | ||
if (!schema.properties.hasOwnProperty(property)) { | ||
continue; | ||
} | ||
if (property === 'allow_discussion') { | ||
schema.properties[property].type = 'boolean'; | ||
} | ||
if (property === 'effective' || property === 'expires') { | ||
schema.properties[property].widget = 'date'; | ||
} | ||
} | ||
|
||
this.schema = schema; | ||
this.model = model; | ||
}); | ||
} | ||
|
||
onSave(schemaForm: any) { | ||
const model = schemaForm.value; | ||
Object.keys(model).forEach((key: string) => { | ||
if (model[key] === '' && this.schema.properties[key].widget.id === 'date') { | ||
model[key] = null; | ||
} | ||
}); | ||
this.services.resource.update(this.path, model).subscribe(() => { | ||
this.services.traverser.traverse(this.path); | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.