MOBILE-2309 sitehome: Implement provider and register handler
parent
80e783f911
commit
3d795ea39f
|
@ -57,6 +57,7 @@ import { CoreCoursesModule } from '../core/courses/courses.module';
|
|||
import { CoreFileUploaderModule } from '../core/fileuploader/fileuploader.module';
|
||||
import { CoreSharedFilesModule } from '../core/sharedfiles/sharedfiles.module';
|
||||
import { CoreCourseModule } from '../core/course/course.module';
|
||||
import { CoreSiteHomeModule } from '../core/sitehome/sitehome.module';
|
||||
|
||||
// Addon modules.
|
||||
import { AddonCalendarModule } from '../addon/calendar/calendar.module';
|
||||
|
@ -92,6 +93,7 @@ export function createTranslateLoader(http: HttpClient) {
|
|||
CoreFileUploaderModule,
|
||||
CoreSharedFilesModule,
|
||||
CoreCourseModule,
|
||||
CoreSiteHomeModule,
|
||||
AddonCalendarModule
|
||||
],
|
||||
bootstrap: [IonicApp],
|
||||
|
|
|
@ -1,12 +1,14 @@
|
|||
<div class="core-tabs-bar">
|
||||
<ng-container *ngFor="let tab of tabs; let idx = index">
|
||||
<a *ngIf="tab.show" [attr.aria-selected]="selected == idx" (click)="selectTab(idx)">
|
||||
<ion-icon *ngIf="tab.icon" [name]="tab.icon"></ion-icon>
|
||||
<span *ngIf="tab.title">{{ tab.title }}</span>
|
||||
<ion-badge *ngIf="tab.badge" [color]="tab.badgeStyle" class="tab-badge">{{tab.badge}}</ion-badge>
|
||||
</a>
|
||||
</ng-container>
|
||||
</div>
|
||||
<div #originalTabs>
|
||||
<ng-content></ng-content>
|
||||
</div>
|
||||
<core-loading [hideUntil]="hideUntil">
|
||||
<div class="core-tabs-bar">
|
||||
<ng-container *ngFor="let tab of tabs; let idx = index">
|
||||
<a *ngIf="tab.show" [attr.aria-selected]="selected == idx" (click)="selectTab(idx)">
|
||||
<ion-icon *ngIf="tab.icon" [name]="tab.icon"></ion-icon>
|
||||
<span *ngIf="tab.title">{{ tab.title }}</span>
|
||||
<ion-badge *ngIf="tab.badge" [color]="tab.badgeStyle" class="tab-badge">{{tab.badge}}</ion-badge>
|
||||
</a>
|
||||
</ng-container>
|
||||
</div>
|
||||
<div #originalTabs>
|
||||
<ng-content></ng-content>
|
||||
</div>
|
||||
</core-loading>
|
|
@ -12,7 +12,8 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
import { Component, Input, Output, EventEmitter, OnInit, AfterViewInit, ViewChild, ElementRef } from '@angular/core';
|
||||
import { Component, Input, Output, EventEmitter, OnInit, OnChanges, AfterViewInit, ViewChild, ElementRef,
|
||||
SimpleChange } from '@angular/core';
|
||||
import { CoreTabComponent } from './tab';
|
||||
|
||||
/**
|
||||
|
@ -35,14 +36,17 @@ import { CoreTabComponent } from './tab';
|
|||
selector: 'core-tabs',
|
||||
templateUrl: 'tabs.html'
|
||||
})
|
||||
export class CoreTabsComponent implements OnInit, AfterViewInit {
|
||||
export class CoreTabsComponent implements OnInit, AfterViewInit, OnChanges {
|
||||
@Input() selectedIndex?: number = 0; // Index of the tab to select.
|
||||
@Input() hideUntil: boolean; // Determine when should the contents be shown.
|
||||
@Output() ionChange: EventEmitter<CoreTabComponent> = new EventEmitter<CoreTabComponent>(); // Emitted when the tab changes.
|
||||
@ViewChild('originalTabs') originalTabsRef: ElementRef;
|
||||
|
||||
tabs: CoreTabComponent[] = []; // List of tabs.
|
||||
selected: number; // Selected tab number.
|
||||
protected originalTabsContainer: HTMLElement; // The container of the original tabs. It will include each tab's content.
|
||||
protected initialized = false;
|
||||
protected afterViewInitTriggered = false;
|
||||
|
||||
constructor() {}
|
||||
|
||||
|
@ -57,22 +61,24 @@ export class CoreTabsComponent implements OnInit, AfterViewInit {
|
|||
* View has been initialized.
|
||||
*/
|
||||
ngAfterViewInit() {
|
||||
let selectedIndex = this.selectedIndex || 0,
|
||||
selectedTab = this.tabs[selectedIndex];
|
||||
|
||||
if (!selectedTab.enabled || !selectedTab.show) {
|
||||
// The tab is not enabled or not shown. Get the first tab that is enabled.
|
||||
selectedTab = this.tabs.find((tab, index) => {
|
||||
if (tab.enabled && tab.show) {
|
||||
selectedIndex = index;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
this.afterViewInitTriggered = true;
|
||||
if (!this.initialized && this.hideUntil) {
|
||||
// Tabs should be shown, initialize them.
|
||||
this.initializeTabs();
|
||||
}
|
||||
}
|
||||
|
||||
if (selectedTab) {
|
||||
this.selectTab(selectedIndex);
|
||||
/**
|
||||
* Detect changes on input properties.
|
||||
*/
|
||||
ngOnChanges(changes: {[name: string]: SimpleChange}) {
|
||||
// We need to wait for ngAfterViewInit because we need core-tab components to be executed.
|
||||
if (!this.initialized && this.hideUntil && this.afterViewInitTriggered) {
|
||||
// Tabs should be shown, initialize them.
|
||||
// Use a setTimeout so child core-tab update their inputs before initializing the tabs.
|
||||
setTimeout(() => {
|
||||
this.initializeTabs();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -114,6 +120,31 @@ export class CoreTabsComponent implements OnInit, AfterViewInit {
|
|||
return this.tabs[this.selected];
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the tabs, determining the first tab to be shown.
|
||||
*/
|
||||
protected initializeTabs() : void {
|
||||
let selectedIndex = this.selectedIndex || 0,
|
||||
selectedTab = this.tabs[selectedIndex];
|
||||
|
||||
if (!selectedTab.enabled || !selectedTab.show) {
|
||||
// The tab is not enabled or not shown. Get the first tab that is enabled.
|
||||
selectedTab = this.tabs.find((tab, index) => {
|
||||
if (tab.enabled && tab.show) {
|
||||
selectedIndex = index;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
if (selectedTab) {
|
||||
this.selectTab(selectedIndex);
|
||||
}
|
||||
|
||||
this.initialized = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a tab from the list of tabs.
|
||||
*
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
|
||||
import { NgModule } from '@angular/core';
|
||||
import { CoreCoursesProvider } from './providers/courses';
|
||||
import { CoreCoursesMainMenuHandler } from './providers/handlers';
|
||||
import { CoreCoursesMainMenuHandler } from './providers/mainmenu-handler';
|
||||
import { CoreCoursesMyOverviewProvider } from './providers/my-overview';
|
||||
import { CoreCoursesDelegate } from './providers/delegate';
|
||||
import { CoreMainMenuDelegate } from '../mainmenu/providers/delegate';
|
||||
|
|
|
@ -17,7 +17,12 @@
|
|||
<ion-refresher-content pullingText="{{ 'core.pulltorefresh' | translate }}"></ion-refresher-content>
|
||||
</ion-refresher>
|
||||
|
||||
<core-tabs selectedIndex="1">
|
||||
<core-tabs [selectedIndex]="firstSelectedTab" [hideUntil]="tabsReady">
|
||||
<!-- Site home tab. -->
|
||||
<core-tab [show]="siteHomeEnabled" [title]="'core.sitehome.sitehome' | translate" (ionSelect)="tabChanged('sitehome')">
|
||||
<!-- @todo. -->
|
||||
</core-tab>
|
||||
|
||||
<!-- Timeline tab. -->
|
||||
<core-tab [title]="'core.courses.timeline' | translate" (ionSelect)="tabChanged('timeline')">
|
||||
<div no-padding [hidden]="!(timeline.loaded || timelineCourses.loaded)">
|
||||
|
|
|
@ -14,10 +14,12 @@
|
|||
|
||||
import { Component, OnDestroy } from '@angular/core';
|
||||
import { IonicPage, NavController } from 'ionic-angular';
|
||||
import { CoreSitesProvider } from '../../../../providers/sites';
|
||||
import { CoreDomUtilsProvider } from '../../../../providers/utils/dom';
|
||||
import { CoreCoursesProvider } from '../../providers/courses';
|
||||
import { CoreCoursesMyOverviewProvider } from '../../providers/my-overview';
|
||||
import { CoreCourseHelperProvider } from '../../../course/providers/helper';
|
||||
import { CoreSiteHomeProvider } from '../../../sitehome/providers/sitehome';
|
||||
import * as moment from 'moment';
|
||||
|
||||
/**
|
||||
|
@ -29,6 +31,9 @@ import * as moment from 'moment';
|
|||
templateUrl: 'my-overview.html',
|
||||
})
|
||||
export class CoreCoursesMyOverviewPage implements OnDestroy {
|
||||
firstSelectedTab: number;
|
||||
siteHomeEnabled: boolean;
|
||||
tabsReady: boolean = false;
|
||||
tabShown = 'courses';
|
||||
timeline = {
|
||||
sort: 'sortbydates',
|
||||
|
@ -64,13 +69,24 @@ export class CoreCoursesMyOverviewPage implements OnDestroy {
|
|||
|
||||
constructor(private navCtrl: NavController, private coursesProvider: CoreCoursesProvider,
|
||||
private domUtils: CoreDomUtilsProvider, private myOverviewProvider: CoreCoursesMyOverviewProvider,
|
||||
private courseHelper: CoreCourseHelperProvider) {}
|
||||
private courseHelper: CoreCourseHelperProvider, private sitesProvider: CoreSitesProvider,
|
||||
private siteHomeProvider: CoreSiteHomeProvider) {}
|
||||
|
||||
/**
|
||||
* View loaded.
|
||||
*/
|
||||
ionViewDidLoad() {
|
||||
this.searchEnabled = !this.coursesProvider.isSearchCoursesDisabledInSite();
|
||||
|
||||
// Decide which tab to load first.
|
||||
this.siteHomeProvider.isAvailable().then((enabled) => {
|
||||
let site = this.sitesProvider.getCurrentSite(),
|
||||
displaySiteHome = site.getInfo() && site.getInfo().userhomepage === 0;
|
||||
|
||||
this.siteHomeEnabled = enabled;
|
||||
this.firstSelectedTab = displaySiteHome ? 0 : 2;
|
||||
this.tabsReady = true;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -18,7 +18,7 @@ import { CoreMainMenuHandler, CoreMainMenuHandlerData } from '../../mainmenu/pro
|
|||
import { CoreCoursesMyOverviewProvider } from '../providers/my-overview';
|
||||
|
||||
/**
|
||||
* Handler to inject an option into main menu.
|
||||
* Handler to add My Courses or My Overview into main menu.
|
||||
*/
|
||||
@Injectable()
|
||||
export class CoreCoursesMainMenuHandler implements CoreMainMenuHandler {
|
|
@ -404,49 +404,6 @@ export class CoreLoginHelperProvider {
|
|||
*/
|
||||
goToSiteInitialPage() : Promise<any> {
|
||||
return this.appProvider.getRootNavController().setRoot('CoreMainMenuPage');
|
||||
// return this.isMyOverviewEnabled().then((myOverview) => {
|
||||
// let myCourses = !myOverview && this.isMyCoursesEnabled(),
|
||||
// site = this.sitesProvider.getCurrentSite(),
|
||||
// promise;
|
||||
|
||||
// if (!site) {
|
||||
// return Promise.reject(null);
|
||||
// }
|
||||
|
||||
// // Check if frontpage is needed to be shown. (If configured or if any of the other avalaible).
|
||||
// if ((site.getInfo() && site.getInfo().userhomepage === 0) || (!myCourses && !myOverview)) {
|
||||
// promise = this.isFrontpageEnabled();
|
||||
// } else {
|
||||
// promise = Promise.resolve(false);
|
||||
// }
|
||||
|
||||
// return promise.then((frontpage) => {
|
||||
// // Check avalaibility in priority order.
|
||||
// let pageName,
|
||||
// params;
|
||||
|
||||
// // @todo Use real pages names when they are implemented.
|
||||
// if (frontpage) {
|
||||
// pageName = 'Frontpage';
|
||||
// } else if (myOverview) {
|
||||
// pageName = 'MyOverview';
|
||||
// } else if (myCourses) {
|
||||
// pageName = 'MyCourses';
|
||||
// } else {
|
||||
// // Anything else available, go to the user profile.
|
||||
// pageName = 'User';
|
||||
// params = {
|
||||
// userId: site.getUserId()
|
||||
// };
|
||||
// }
|
||||
|
||||
// if (setRoot) {
|
||||
// return navCtrl.setRoot(pageName, params, {animate: false});
|
||||
// } else {
|
||||
// return navCtrl.push(pageName, params);
|
||||
// }
|
||||
// });
|
||||
// });
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -547,45 +504,6 @@ export class CoreLoginHelperProvider {
|
|||
return !!CoreConfigConstants.siteurl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the app is configured to use a fixed URL (only 1).
|
||||
*
|
||||
* @return {Promise<boolean>} Promise resolved with boolean: whether there is 1 fixed URL.
|
||||
*/
|
||||
protected isFrontpageEnabled() : Promise<boolean> {
|
||||
// var $mmaFrontpage = $mmAddonManager.get('$mmaFrontpage');
|
||||
// if ($mmaFrontpage && !$mmaFrontpage.isDisabledInSite()) {
|
||||
// return $mmaFrontpage.isFrontpageAvailable().then(() => {
|
||||
// return true;
|
||||
// }).catch(() => {
|
||||
// return false;
|
||||
// });
|
||||
// }
|
||||
// @todo: Implement it when front page is implemented.
|
||||
return Promise.resolve(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if My Courses is enabled.
|
||||
*
|
||||
* @return {boolean} Whether My Courses is enabled.
|
||||
*/
|
||||
protected isMyCoursesEnabled() : boolean {
|
||||
// @todo: Implement it when My Courses is implemented.
|
||||
return false;
|
||||
// return !$mmCourses.isMyCoursesDisabledInSite();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if My Overview is enabled.
|
||||
*
|
||||
* @return {Promise<boolean>} Promise resolved with boolean: whether My Overview is enabled.
|
||||
*/
|
||||
protected isMyOverviewEnabled() : Promise<boolean> {
|
||||
// @todo: Implement it when My Overview is implemented.
|
||||
return Promise.resolve(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if current site is logged out, triggering mmCoreEventSessionExpired if it is.
|
||||
*
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
<ion-tabs *ngIf="loaded" #mainTabs>
|
||||
<ion-tabs *ngIf="loaded" #mainTabs [selectedIndex]="initialTab">
|
||||
<ion-tab [enabled]="false" [show]="false" [root]="redirectPage" [rootParams]="redirectParams"></ion-tab>
|
||||
<ion-tab *ngFor="let tab of tabs" [root]="tab.page" [tabTitle]="tab.title | translate" [tabIcon]="tab.icon" class="{{tab.class}}"></ion-tab>
|
||||
</ion-tabs>
|
|
@ -54,6 +54,7 @@ export class CoreMainMenuPage implements OnDestroy {
|
|||
loaded: boolean;
|
||||
redirectPage: string;
|
||||
redirectParams: any;
|
||||
initialTab: number;
|
||||
|
||||
protected subscription;
|
||||
protected moreTabData = {
|
||||
|
@ -79,25 +80,36 @@ export class CoreMainMenuPage implements OnDestroy {
|
|||
return;
|
||||
}
|
||||
|
||||
let site = this.sitesProvider.getCurrentSite(),
|
||||
displaySiteHome = site.getInfo() && site.getInfo().userhomepage === 0;
|
||||
|
||||
this.subscription = this.menuDelegate.getHandlers().subscribe((handlers) => {
|
||||
handlers = handlers.slice(0, CoreMainMenuProvider.NUM_MAIN_HANDLERS); // Get main handlers.
|
||||
|
||||
// Check if handlers are already in tabs. Add the ones that aren't.
|
||||
// @todo: https://github.com/ionic-team/ionic/issues/13633
|
||||
for (let i in handlers) {
|
||||
for (let i = 0; i < handlers.length; i++) {
|
||||
let handler = handlers[i],
|
||||
found = false;
|
||||
found = false,
|
||||
shouldSelect = (displaySiteHome && handler.name == 'CoreSiteHome') ||
|
||||
(!displaySiteHome && handler.name == 'CoreCourses');
|
||||
|
||||
for (let j in this.tabs) {
|
||||
for (let j = 0; j < this.tabs.length; j++) {
|
||||
let tab = this.tabs[j];
|
||||
if (tab.title == handler.title && tab.icon == handler.icon) {
|
||||
found = true;
|
||||
if (shouldSelect) {
|
||||
this.initialTab = j;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
this.tabs.push(handler);
|
||||
if (shouldSelect) {
|
||||
this.initialTab = this.tabs.length;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -79,6 +79,18 @@ export interface CoreMainMenuHandlerData {
|
|||
class?: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Data returned by the delegate for each handler.
|
||||
*/
|
||||
export interface CoreMainMenuHandlerToDisplay extends CoreMainMenuHandlerData {
|
||||
|
||||
/**
|
||||
* Name of the handler.
|
||||
* @type {string}
|
||||
*/
|
||||
name?: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Service to interact with plugins to be shown in the main menu. Provides functions to register a plugin
|
||||
* and notify an update in the data.
|
||||
|
@ -90,7 +102,7 @@ export class CoreMainMenuDelegate {
|
|||
protected enabledHandlers: {[s: string]: CoreMainMenuHandler} = {};
|
||||
protected loaded = false;
|
||||
protected lastUpdateHandlersStart: number;
|
||||
protected siteHandlers: Subject<CoreMainMenuHandlerData[]> = new BehaviorSubject<CoreMainMenuHandlerData[]>([]);
|
||||
protected siteHandlers: Subject<CoreMainMenuHandlerToDisplay[]> = new BehaviorSubject<CoreMainMenuHandlerToDisplay[]>([]);
|
||||
|
||||
constructor(logger: CoreLoggerProvider, private sitesProvider: CoreSitesProvider, eventsProvider: CoreEventsProvider) {
|
||||
this.logger = logger.getInstance('CoreMainMenuDelegate');
|
||||
|
@ -123,7 +135,7 @@ export class CoreMainMenuDelegate {
|
|||
*
|
||||
* @return {Subject<CoreMainMenuHandlerData[]>} An observable that will receive the handlers.
|
||||
*/
|
||||
getHandlers() : Subject<CoreMainMenuHandlerData[]> {
|
||||
getHandlers() : Subject<CoreMainMenuHandlerToDisplay[]> {
|
||||
return this.siteHandlers;
|
||||
}
|
||||
|
||||
|
@ -223,7 +235,9 @@ export class CoreMainMenuDelegate {
|
|||
|
||||
for (let name in this.enabledHandlers) {
|
||||
let handler = this.enabledHandlers[name],
|
||||
data = handler.getDisplayData();
|
||||
data: CoreMainMenuHandlerToDisplay = handler.getDisplayData();
|
||||
|
||||
data.name = handler.name;
|
||||
|
||||
handlersData.push({
|
||||
data: data,
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"sitehome": "Site home",
|
||||
"sitenews": "Site announcements"
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
// (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 { Injectable } from '@angular/core';
|
||||
import { CoreSiteHomeProvider } from './sitehome';
|
||||
import { CoreMainMenuHandler, CoreMainMenuHandlerData } from '../../mainmenu/providers/delegate';
|
||||
import { CoreCoursesMyOverviewProvider } from '../../courses/providers/my-overview';
|
||||
|
||||
/**
|
||||
* Handler to add Site Home into main menu.
|
||||
*/
|
||||
@Injectable()
|
||||
export class CoreSiteHomeMainMenuHandler implements CoreMainMenuHandler {
|
||||
name = 'CoreSiteHome';
|
||||
priority = 1000;
|
||||
isOverviewEnabled: boolean;
|
||||
|
||||
constructor(private siteHomeProvider: CoreSiteHomeProvider, private myOverviewProvider: CoreCoursesMyOverviewProvider) {}
|
||||
|
||||
/**
|
||||
* Check if the handler is enabled on a site level.
|
||||
*
|
||||
* @return {boolean} Whether or not the handler is enabled on a site level.
|
||||
*/
|
||||
isEnabled(): boolean|Promise<boolean> {
|
||||
// Check if my overview is enabled.
|
||||
return this.myOverviewProvider.isEnabled().then((enabled) => {
|
||||
if (enabled) {
|
||||
// My overview is enabled, Site Home will be inside the overview page.
|
||||
return false;
|
||||
}
|
||||
|
||||
// My overview not enabled, check if site home is enabled.
|
||||
return this.siteHomeProvider.isAvailable();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the data needed to render the handler.
|
||||
*
|
||||
* @return {CoreMainMenuHandlerData} Data needed to render the handler.
|
||||
*/
|
||||
getDisplayData(): CoreMainMenuHandlerData {
|
||||
return {
|
||||
icon: 'home',
|
||||
title: 'core.sitehome.sitehome',
|
||||
page: 'CoreSiteHomeIndexPage',
|
||||
class: 'core-sitehome-handler'
|
||||
};
|
||||
}
|
||||
}
|
|
@ -0,0 +1,104 @@
|
|||
// (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 { Injectable } from '@angular/core';
|
||||
import { CoreLoggerProvider } from '../../../providers/logger';
|
||||
import { CoreSitesProvider } from '../../../providers/sites';
|
||||
import { CoreSite } from '../../../classes/site';
|
||||
import { CoreCourseProvider } from '../../course/providers/course';
|
||||
|
||||
/**
|
||||
* Service that provides some features regarding site home.
|
||||
*/
|
||||
@Injectable()
|
||||
export class CoreSiteHomeProvider {
|
||||
protected logger;
|
||||
|
||||
constructor(logger: CoreLoggerProvider, private sitesProvider: CoreSitesProvider, private courseProvider: CoreCourseProvider) {
|
||||
this.logger = logger.getInstance('CoreSiteHomeProvider');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether or not the frontpage is available for the current site.
|
||||
*
|
||||
* @param {string} [siteId] The site ID. If not defined, current site.
|
||||
* @return {Promise<boolean>} Promise resolved with boolean: whether it's available.
|
||||
*/
|
||||
isAvailable(siteId?: string) : Promise<boolean> {
|
||||
return this.sitesProvider.getSite(siteId).then((site) => {
|
||||
// First check if it's disabled.
|
||||
if (this.isDisabledInSite(site)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Use a WS call to check if there's content in the site home.
|
||||
const siteHomeId = site.getSiteHomeId(),
|
||||
preSets = {emergencyCache: false};
|
||||
|
||||
this.logger.debug('Using WS call to check if site home is available.');
|
||||
|
||||
return this.courseProvider.getSections(siteHomeId, false, true, preSets, site.id).then((sections) : any => {
|
||||
if (!sections || !sections.length) {
|
||||
return Promise.reject(null);
|
||||
}
|
||||
|
||||
for (let i = 0; i < sections.length; i++) {
|
||||
let section = sections[i];
|
||||
if (section.summary || (section.modules && section.modules.length)) {
|
||||
// It has content, return true.
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return Promise.reject(null);
|
||||
}).catch(() => {
|
||||
const config = site.getStoredConfig();
|
||||
if (config && config.frontpageloggedin) {
|
||||
const items = config.frontpageloggedin.split(',');
|
||||
if (items.length > 0) {
|
||||
// It's enabled.
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
}).catch(() => {
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if Site Home is disabled in a certain site.
|
||||
*
|
||||
* @param {string} [siteId] Site Id. If not defined, use current site.
|
||||
* @return {Promise<boolean>} Promise resolved with true if disabled, rejected or resolved with false otherwise.
|
||||
*/
|
||||
isDisabled(siteId?: string) : Promise<boolean> {
|
||||
return this.sitesProvider.getSite(siteId).then((site) => {
|
||||
return this.isDisabledInSite(site);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if Site Home is disabled in a certain site.
|
||||
*
|
||||
* @param {CoreSite} [site] Site. If not defined, use current site.
|
||||
* @return {boolean} Whether it's disabled.
|
||||
*/
|
||||
isDisabledInSite(site: CoreSite) : boolean {
|
||||
site = site || this.sitesProvider.getCurrentSite();
|
||||
return site.isFeatureDisabled('$mmSideMenuDelegate_mmaFrontpage');
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
// (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 { CoreSiteHomeProvider } from './providers/sitehome';
|
||||
import { CoreSiteHomeMainMenuHandler } from './providers/mainmenu-handler';
|
||||
import { CoreMainMenuDelegate } from '../mainmenu/providers/delegate';
|
||||
|
||||
@NgModule({
|
||||
declarations: [],
|
||||
imports: [
|
||||
],
|
||||
providers: [
|
||||
CoreSiteHomeProvider,
|
||||
CoreSiteHomeMainMenuHandler
|
||||
],
|
||||
exports: []
|
||||
})
|
||||
export class CoreSiteHomeModule {
|
||||
constructor(mainMenuDelegate: CoreMainMenuDelegate, mainMenuHandler: CoreSiteHomeMainMenuHandler) {
|
||||
mainMenuDelegate.registerHandler(mainMenuHandler);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue