MOBILE-2333 siteaddons: Implement new-content directive

main
Dani Palou 2018-02-19 13:01:15 +01:00
parent fec9fa6efa
commit fb278791c1
5 changed files with 111 additions and 3 deletions

View File

@ -30,6 +30,7 @@ import { CorePipesModule } from '../../pipes/pipes.module';
import { CoreCourseComponentsModule } from '../../core/course/components/components.module';
import { CoreCourseDirectivesModule } from '../../core/course/directives/directives.module';
import { CoreCoursesComponentsModule } from '../../core/courses/components/components.module';
import { CoreSiteAddonsDirectivesModule } from '../../core/siteaddons/directives/directives.module';
import { CoreSiteHomeComponentsModule } from '../../core/sitehome/components/components.module';
import { CoreUserComponentsModule } from '../../core/user/components/components.module';
@ -82,7 +83,7 @@ export class CoreCompileHtmlComponent implements OnChanges, OnDestroy {
protected IMPORTS = [
IonicModule, TranslateModule.forChild(), CoreComponentsModule, CoreDirectivesModule, CorePipesModule,
CoreCourseComponentsModule, CoreCoursesComponentsModule, CoreSiteHomeComponentsModule, CoreUserComponentsModule,
CoreCourseDirectivesModule
CoreCourseDirectivesModule, CoreSiteAddonsDirectivesModule
];
// Other Ionic/Angular providers that don't depend on where they are injected.

View File

@ -13,11 +13,9 @@
// limitations under the License.
import { Directive, Input, OnInit, ElementRef } from '@angular/core';
import { NavController } from 'ionic-angular';
import { CoreCourseProvider } from '../providers/course';
import { CoreCourseHelperProvider } from '../providers/helper';
import { CoreDomUtilsProvider } from '../../../providers/utils/dom';
import { CoreUtilsProvider } from '../../../providers/utils/utils';
/**
* Directive to allow downloading and open the main file of a module.

View File

@ -65,4 +65,22 @@ export class CoreSiteAddonsAddonContentComponent implements OnInit {
return this.fetchContent();
});
}
/**
* Update the content, usually with a different method or params.
*
* @param {string} component New component.
* @param {string} method New method.
* @param {any} args New params.
*/
updateContent(component: string, method: string, args: any): void {
this.component = component;
this.method = method;
this.args = args;
this.dataLoaded = false;
this.fetchContent().finally(() => {
this.dataLoaded = true;
});
}
}

View File

@ -0,0 +1,27 @@
// (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 { NgModule } from '@angular/core';
import { CoreSiteAddonsNewContentDirective } from './new-content';
@NgModule({
declarations: [
CoreSiteAddonsNewContentDirective
],
imports: [],
exports: [
CoreSiteAddonsNewContentDirective
]
})
export class CoreSiteAddonsDirectivesModule {}

View File

@ -0,0 +1,64 @@
// (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 { Directive, Input, OnInit, ElementRef, Optional } from '@angular/core';
import { NavController } from 'ionic-angular';
import { CoreUtilsProvider } from '../../../providers/utils/utils';
import { CoreSiteAddonsAddonContentComponent } from '../components/addon-content/addon-content';
/**
* Directive to display a new site addon content when clicked. This new content can be displayed in a new page or in the
* current page (only if the current page is already displaying a site addon content).
*/
@Directive({
selector: '[core-site-addons-new-content]'
})
export class CoreSiteAddonsNewContentDirective implements OnInit {
@Input() component: string; // The component of the new content.
@Input() method: string; // The method to get the new content.
@Input() args: any; // The params to get the new content.
@Input() title: string; // The title to display with the new content. Only if samePage=false.
@Input() samePage?: boolean | string; // Whether to display the content in same page or open a new one. Defaults to new page.
protected element: HTMLElement;
constructor(element: ElementRef, protected utils: CoreUtilsProvider, protected navCtrl: NavController,
@Optional() protected parentContent: CoreSiteAddonsAddonContentComponent) {
this.element = element.nativeElement || element;
}
/**
* Component being initialized.
*/
ngOnInit(): void {
this.element.addEventListener('click', (ev: Event): void => {
ev.preventDefault();
ev.stopPropagation();
if (this.utils.isTrueOrOne(this.samePage)) {
// Update the parent content (if it exists).
if (this.parentContent) {
this.parentContent.updateContent(this.component, this.method, this.args);
}
} else {
this.navCtrl.push('CoreSiteAddonsAddonPage', {
title: this.title,
component: this.component,
method: this.method,
args: this.args
});
}
});
}
}