2018-02-19 13:01:15 +01: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 { Directive, Input, OnInit, ElementRef, Optional } from '@angular/core';
|
|
|
|
import { NavController } from 'ionic-angular';
|
2018-02-20 15:35:53 +01:00
|
|
|
import { CoreDomUtilsProvider } from '../../../providers/utils/dom';
|
2018-02-19 13:01:15 +01:00
|
|
|
import { CoreUtilsProvider } from '../../../providers/utils/utils';
|
2018-02-20 13:24:01 +01:00
|
|
|
import { CoreSiteAddonsProvider } from '../providers/siteaddons';
|
2018-02-19 13:01:15 +01:00
|
|
|
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).
|
2018-02-20 13:24:01 +01:00
|
|
|
*
|
|
|
|
* Example usages:
|
|
|
|
*
|
|
|
|
* A button to go to a new content page:
|
|
|
|
*
|
|
|
|
* <button ion-button core-site-addons-new-content title="<% certificate.name %>" component="mod_certificate"
|
|
|
|
* method="mobile_issues_view" [args]="{cmid: <% cmid %>, courseid: <% courseid %>}">
|
|
|
|
* {{ 'addon.mod_certificate_coursecertificate.viewissued' | translate }}
|
|
|
|
* </button>
|
|
|
|
*
|
|
|
|
* A button to load new content in current page using a param from otherdata:
|
|
|
|
*
|
|
|
|
* <button ion-button core-site-addons-new-content component="mod_certificate" method="mobile_issues_view"
|
|
|
|
* [args]="{cmid: <% cmid %>, courseid: <% courseid %>}" samePage="true" [useOtherData]="['userid']">
|
|
|
|
* {{ 'addon.mod_certificate_coursecertificate.viewissued' | translate }}
|
|
|
|
* </button>
|
2018-02-19 13:01:15 +01:00
|
|
|
*/
|
|
|
|
@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.
|
2018-02-20 13:24:01 +01:00
|
|
|
@Input() samePage: boolean | string; // Whether to display the content in same page or open a new one. Defaults to new page.
|
|
|
|
@Input() useOtherData: any[]; // Whether to include other data in the args. @see CoreSiteAddonsProvider.loadOtherDataInArgs.
|
2018-02-20 15:35:53 +01:00
|
|
|
@Input() form: string; // ID or name to identify a form. The form will be obtained from document.forms.
|
|
|
|
// If supplied and form is found, the form data will be retrieved and sent to the new content.
|
2018-02-19 13:01:15 +01:00
|
|
|
|
|
|
|
protected element: HTMLElement;
|
|
|
|
|
|
|
|
constructor(element: ElementRef, protected utils: CoreUtilsProvider, protected navCtrl: NavController,
|
2018-02-20 15:35:53 +01:00
|
|
|
@Optional() protected parentContent: CoreSiteAddonsAddonContentComponent, protected domUtils: CoreDomUtilsProvider,
|
2018-02-20 13:24:01 +01:00
|
|
|
protected siteAddonsProvider: CoreSiteAddonsProvider) {
|
2018-02-19 13:01:15 +01:00
|
|
|
this.element = element.nativeElement || element;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Component being initialized.
|
|
|
|
*/
|
|
|
|
ngOnInit(): void {
|
|
|
|
this.element.addEventListener('click', (ev: Event): void => {
|
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
|
2018-02-20 15:35:53 +01:00
|
|
|
let args = this.args || {};
|
2018-02-20 13:24:01 +01:00
|
|
|
|
|
|
|
if (this.parentContent) {
|
|
|
|
args = this.siteAddonsProvider.loadOtherDataInArgs(this.args, this.parentContent.otherData, this.useOtherData);
|
|
|
|
}
|
|
|
|
|
2018-02-20 15:35:53 +01:00
|
|
|
if (this.form && document.forms[this.form]) {
|
|
|
|
args = Object.assign(args, this.domUtils.getDataFromForm(document.forms[this.form]));
|
|
|
|
}
|
|
|
|
|
2018-02-19 13:01:15 +01:00
|
|
|
if (this.utils.isTrueOrOne(this.samePage)) {
|
|
|
|
// Update the parent content (if it exists).
|
|
|
|
if (this.parentContent) {
|
2018-02-20 13:24:01 +01:00
|
|
|
this.parentContent.updateContent(this.component, this.method, args);
|
2018-02-19 13:01:15 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.navCtrl.push('CoreSiteAddonsAddonPage', {
|
|
|
|
title: this.title,
|
|
|
|
component: this.component,
|
|
|
|
method: this.method,
|
2018-02-20 13:24:01 +01:00
|
|
|
args: args
|
2018-02-19 13:01:15 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|