84 lines
2.9 KiB
TypeScript
Raw Normal View History

2018-04-12 14:56:51 +02:00
// (C) Copyright 2015 Martin Dougiamas
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { Component, Input, OnInit, Injector, ViewChild } from '@angular/core';
import { AddonModDataProvider } from '../../providers/data';
import { AddonModDataFieldsDelegate } from '../../providers/fields-delegate';
import { CoreDynamicComponent } from '@components/dynamic-component/dynamic-component';
/**
2018-05-08 15:10:32 +02:00
* Component that displays a database field plugin.
2018-04-12 14:56:51 +02:00
*/
@Component({
selector: 'addon-mod-data-field-plugin',
templateUrl: 'field-plugin.html',
})
export class AddonModDataFieldPluginComponent implements OnInit {
@ViewChild(CoreDynamicComponent) dynamicComponent: CoreDynamicComponent;
@Input() mode: string; // The render mode.
@Input() field: any; // The field to render.
@Input() value?: any; // The value of the field.
@Input() database?: any; // Database object.
@Input() error?: string; // Error when editing.
@Input() viewAction: string; // Action to perform.
fieldComponent: any; // Component to render the plugin.
data: any; // Data to pass to the component.
fieldLoaded: boolean;
constructor(protected injector: Injector, protected dataDelegate: AddonModDataFieldsDelegate,
2018-05-02 10:44:28 +02:00
protected dataProvider: AddonModDataProvider) {
}
2018-04-12 14:56:51 +02:00
/**
* Component being initialized.
*/
ngOnInit(): void {
if (!this.field) {
this.fieldLoaded = true;
return;
}
// Check if the plugin has defined its own component to render itself.
this.dataDelegate.getComponentForField(this.injector, this.field).then((component) => {
this.fieldComponent = component;
if (component) {
// Prepare the data to pass to the component.
this.data = {
mode: this.mode,
field: this.field,
value: this.value,
database: this.database,
error: this.error,
viewAction: this.viewAction
};
2018-05-02 10:44:28 +02:00
2018-04-12 14:56:51 +02:00
} else {
this.fieldLoaded = true;
}
});
}
/**
* Invalidate the plugin data.
*
* @return {Promise<any>} Promise resolved when done.
*/
invalidate(): Promise<any> {
return Promise.resolve(this.dynamicComponent && this.dynamicComponent.callComponentFunction('invalidate', []));
}
}