From fb278791c195ddf00d4af073a3d446044c8791e0 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Mon, 19 Feb 2018 13:01:15 +0100 Subject: [PATCH] MOBILE-2333 siteaddons: Implement new-content directive --- src/components/compile-html/compile-html.ts | 3 +- .../directives/download-module-main-file.ts | 2 - .../components/addon-content/addon-content.ts | 18 ++++++ .../directives/directives.module.ts | 27 ++++++++ src/core/siteaddons/directives/new-content.ts | 64 +++++++++++++++++++ 5 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 src/core/siteaddons/directives/directives.module.ts create mode 100644 src/core/siteaddons/directives/new-content.ts diff --git a/src/components/compile-html/compile-html.ts b/src/components/compile-html/compile-html.ts index 44cb24bac..14d3fc082 100644 --- a/src/components/compile-html/compile-html.ts +++ b/src/components/compile-html/compile-html.ts @@ -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. diff --git a/src/core/course/directives/download-module-main-file.ts b/src/core/course/directives/download-module-main-file.ts index b2a8d4324..569a215fb 100644 --- a/src/core/course/directives/download-module-main-file.ts +++ b/src/core/course/directives/download-module-main-file.ts @@ -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. diff --git a/src/core/siteaddons/components/addon-content/addon-content.ts b/src/core/siteaddons/components/addon-content/addon-content.ts index a842088d1..bf1d8317e 100644 --- a/src/core/siteaddons/components/addon-content/addon-content.ts +++ b/src/core/siteaddons/components/addon-content/addon-content.ts @@ -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; + }); + } } diff --git a/src/core/siteaddons/directives/directives.module.ts b/src/core/siteaddons/directives/directives.module.ts new file mode 100644 index 000000000..709291135 --- /dev/null +++ b/src/core/siteaddons/directives/directives.module.ts @@ -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 {} diff --git a/src/core/siteaddons/directives/new-content.ts b/src/core/siteaddons/directives/new-content.ts new file mode 100644 index 000000000..b4cd49b0c --- /dev/null +++ b/src/core/siteaddons/directives/new-content.ts @@ -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 + }); + } + }); + } +}