From e4260aa92a7a698dc1ced0690c4888a3118ad1cf Mon Sep 17 00:00:00 2001 From: Albert Gasset Date: Mon, 8 Jul 2019 12:37:05 +0200 Subject: [PATCH] MOBILE-2201 tag: Link handlers --- src/core/tag/providers/index-link-handler.ts | 81 +++++++++++++++++++ src/core/tag/providers/search-link-handler.ts | 70 ++++++++++++++++ src/core/tag/tag.module.ts | 13 ++- 3 files changed, 162 insertions(+), 2 deletions(-) create mode 100644 src/core/tag/providers/index-link-handler.ts create mode 100644 src/core/tag/providers/search-link-handler.ts diff --git a/src/core/tag/providers/index-link-handler.ts b/src/core/tag/providers/index-link-handler.ts new file mode 100644 index 000000000..c8e1d7c49 --- /dev/null +++ b/src/core/tag/providers/index-link-handler.ts @@ -0,0 +1,81 @@ +// (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 { CoreContentLinksHandlerBase } from '@core/contentlinks/classes/base-handler'; +import { CoreContentLinksAction } from '@core/contentlinks/providers/delegate'; +import { CoreContentLinksHelperProvider } from '@core/contentlinks/providers/helper'; +import { CoreTagProvider } from './tag'; + +/** + * Handler to treat links to tag index. + */ +@Injectable() +export class CoreTagIndexLinkHandler extends CoreContentLinksHandlerBase { + name = 'CoreTagIndexLinkHandler'; + pattern = /\/tag\/index\.php/; + + constructor(private tagProvider: CoreTagProvider, private linkHelper: CoreContentLinksHelperProvider) { + super(); + } + + /** + * Get the list of actions for a link (url). + * + * @param {string[]} siteIds List of sites the URL belongs to. + * @param {string} url The URL to treat. + * @param {any} params The params of the URL. E.g. 'mysite.com?id=1' -> {id: 1} + * @param {number} [courseId] Course ID related to the URL. Optional but recommended. + * @param {any} [data] Extra data to handle the URL. + * @return {CoreContentLinksAction[]|Promise} List of (or promise resolved with list of) actions. + */ + getActions(siteIds: string[], url: string, params: any, courseId?: number, data?: any): + CoreContentLinksAction[] | Promise { + return [{ + action: (siteId, navCtrl?): void => { + const pageParams = { + tagId: parseInt(params.id, 10) || 0, + tagName: params.tag || '', + collectionId: parseInt(params.tc, 10) || 0, + areaId: parseInt(params.ta, 10) || 0, + fromContextId: parseInt(params.from, 10) || 0, + contextId: parseInt(params.ctx, 10) || 0, + recursive: parseInt(params.rec, 10) || 1 + }; + + if (!pageParams.tagId && (!pageParams.tagName || !pageParams.collectionId)) { + this.linkHelper.goInSite(navCtrl, 'CoreTagSearchPage', {}, siteId); + } else if (pageParams.areaId) { + this.linkHelper.goInSite(navCtrl, 'CoreTagIndexAreaPage', pageParams, siteId); + } else { + this.linkHelper.goInSite(navCtrl, 'CoreTagIndexPage', pageParams, siteId); + } + } + }]; + } + + /** + * Check if the handler is enabled for a certain site (site + user) and a URL. + * If not defined, defaults to true. + * + * @param {string} siteId The site ID. + * @param {string} url The URL to treat. + * @param {any} params The params of the URL. E.g. 'mysite.com?id=1' -> {id: 1} + * @param {number} [courseId] Course ID related to the URL. Optional but recommended. + * @return {boolean|Promise} Whether the handler is enabled for the URL and site. + */ + isEnabled(siteId: string, url: string, params: any, courseId?: number): boolean | Promise { + return this.tagProvider.areTagsAvailable(siteId); + } +} diff --git a/src/core/tag/providers/search-link-handler.ts b/src/core/tag/providers/search-link-handler.ts new file mode 100644 index 000000000..68ea7cb96 --- /dev/null +++ b/src/core/tag/providers/search-link-handler.ts @@ -0,0 +1,70 @@ +// (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 { CoreContentLinksHandlerBase } from '@core/contentlinks/classes/base-handler'; +import { CoreContentLinksAction } from '@core/contentlinks/providers/delegate'; +import { CoreContentLinksHelperProvider } from '@core/contentlinks/providers/helper'; +import { CoreTagProvider } from './tag'; + +/** + * Handler to treat links to tag search. + */ +@Injectable() +export class CoreTagSearchLinkHandler extends CoreContentLinksHandlerBase { + name = 'CoreTagSearchLinkHandler'; + pattern = /\/tag\/search\.php/; + + constructor(private tagProvider: CoreTagProvider, private linkHelper: CoreContentLinksHelperProvider) { + super(); + } + + /** + * Get the list of actions for a link (url). + * + * @param {string[]} siteIds List of sites the URL belongs to. + * @param {string} url The URL to treat. + * @param {any} params The params of the URL. E.g. 'mysite.com?id=1' -> {id: 1} + * @param {number} [courseId] Course ID related to the URL. Optional but recommended. + * @param {any} [data] Extra data to handle the URL. + * @return {CoreContentLinksAction[]|Promise} List of (or promise resolved with list of) actions. + */ + getActions(siteIds: string[], url: string, params: any, courseId?: number, data?: any): + CoreContentLinksAction[] | Promise { + return [{ + action: (siteId, navCtrl?): void => { + const pageParams = { + collectionId: parseInt(params.tc, 10) || 0, + query: params.query || '', + }; + + this.linkHelper.goInSite(navCtrl, 'CoreTagSearchPage', pageParams, siteId); + } + }]; + } + + /** + * Check if the handler is enabled for a certain site (site + user) and a URL. + * If not defined, defaults to true. + * + * @param {string} siteId The site ID. + * @param {string} url The URL to treat. + * @param {any} params The params of the URL. E.g. 'mysite.com?id=1' -> {id: 1} + * @param {number} [courseId] Course ID related to the URL. Optional but recommended. + * @return {boolean|Promise} Whether the handler is enabled for the URL and site. + */ + isEnabled(siteId: string, url: string, params: any, courseId?: number): boolean | Promise { + return this.tagProvider.areTagsAvailable(siteId); + } +} diff --git a/src/core/tag/tag.module.ts b/src/core/tag/tag.module.ts index baa55d98f..970e66e46 100644 --- a/src/core/tag/tag.module.ts +++ b/src/core/tag/tag.module.ts @@ -14,10 +14,13 @@ import { NgModule } from '@angular/core'; import { CoreMainMenuDelegate } from '@core/mainmenu/providers/delegate'; +import { CoreContentLinksDelegate } from '@core/contentlinks/providers/delegate'; import { CoreTagProvider } from './providers/tag'; import { CoreTagHelperProvider } from './providers/helper'; import { CoreTagAreaDelegate } from './providers/area-delegate'; import { CoreTagMainMenuHandler } from './providers/mainmenu-handler'; +import { CoreTagIndexLinkHandler } from './providers/index-link-handler'; +import { CoreTagSearchLinkHandler } from './providers/search-link-handler'; @NgModule({ declarations: [ @@ -28,12 +31,18 @@ import { CoreTagMainMenuHandler } from './providers/mainmenu-handler'; CoreTagProvider, CoreTagHelperProvider, CoreTagAreaDelegate, - CoreTagMainMenuHandler + CoreTagMainMenuHandler, + CoreTagIndexLinkHandler, + CoreTagSearchLinkHandler ] }) export class CoreTagModule { - constructor(mainMenuDelegate: CoreMainMenuDelegate, mainMenuHandler: CoreTagMainMenuHandler) { + constructor(mainMenuDelegate: CoreMainMenuDelegate, mainMenuHandler: CoreTagMainMenuHandler, + contentLinksDelegate: CoreContentLinksDelegate, indexLinkHandler: CoreTagIndexLinkHandler, + searchLinkHandler: CoreTagSearchLinkHandler) { mainMenuDelegate.registerHandler(mainMenuHandler); + contentLinksDelegate.registerHandler(indexLinkHandler); + contentLinksDelegate.registerHandler(searchLinkHandler); } }