From 89e22c980ecb4cf08d74778daff2b56ab0ba82e6 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Mon, 28 May 2018 11:42:32 +0200 Subject: [PATCH] MOBILE-2353 wiki: Implement link handlers --- .../mod/wiki/providers/create-link-handler.ts | 136 ++++++++++++++++++ .../mod/wiki/providers/edit-link-handler.ts | 66 +++++++++ .../mod/wiki/providers/index-link-handler.ts | 29 ++++ .../providers/page-or-map-link-handler.ts | 111 ++++++++++++++ src/addon/mod/wiki/wiki.module.ts | 19 ++- 5 files changed, 359 insertions(+), 2 deletions(-) create mode 100644 src/addon/mod/wiki/providers/create-link-handler.ts create mode 100644 src/addon/mod/wiki/providers/edit-link-handler.ts create mode 100644 src/addon/mod/wiki/providers/index-link-handler.ts create mode 100644 src/addon/mod/wiki/providers/page-or-map-link-handler.ts diff --git a/src/addon/mod/wiki/providers/create-link-handler.ts b/src/addon/mod/wiki/providers/create-link-handler.ts new file mode 100644 index 000000000..7eb5287ab --- /dev/null +++ b/src/addon/mod/wiki/providers/create-link-handler.ts @@ -0,0 +1,136 @@ +// (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 { NavController, ViewController } from 'ionic-angular'; +import { CoreDomUtilsProvider } from '@providers/utils/dom'; +import { CoreContentLinksHandlerBase } from '@core/contentlinks/classes/base-handler'; +import { CoreContentLinksAction } from '@core/contentlinks/providers/delegate'; +import { CoreContentLinksHelperProvider } from '@core/contentlinks/providers/helper'; +import { CoreCourseProvider } from '@core/course/providers/course'; +import { CoreCourseHelperProvider } from '@core/course/providers/helper'; +import { AddonModWikiProvider } from './wiki'; + +/** + * Handler to treat links to create a wiki page. + */ +@Injectable() +export class AddonModWikiCreateLinkHandler extends CoreContentLinksHandlerBase { + name = 'AddonModWikiCreateLinkHandler'; + featureName = 'CoreCourseModuleDelegate_AddonModWiki'; + pattern = /\/mod\/wiki\/create\.php.*([\&\?]swid=\d+)/; + + constructor(protected domUtils: CoreDomUtilsProvider, protected wikiProvider: AddonModWikiProvider, + protected courseHelper: CoreCourseHelperProvider, protected linkHelper: CoreContentLinksHelperProvider, + protected courseProvider: CoreCourseProvider) { + super(); + } + + /** + * Check if the current view is a wiki page of the same wiki. + * + * @param {ViewController} activeView Active view. + * @param {number} subwikiId Subwiki ID to check. + * @param {string} siteId Site ID. + * @return {Promise} Promise resolved with boolean: whether current view belongs to the same wiki. + */ + protected currentStateIsSameWiki(activeView: ViewController, subwikiId: number, siteId: string): Promise { + + if (activeView && activeView.component.name == 'AddonModWikiIndexPage') { + if (activeView.data.subwikiId == subwikiId) { + // Same subwiki, so it's same wiki. + return Promise.resolve(true); + + } else if (activeView.data.pageId) { + // Get the page contents to check the subwiki. + return this.wikiProvider.getPageContents(activeView.data.pageId, false, false, siteId).then((page) => { + return page.subwikiid == subwikiId; + }).catch(() => { + // Not found, return false. + return false; + }); + + } else if (activeView.data.wikiId) { + // Check if the subwiki belongs to this wiki. + return this.wikiProvider.wikiHasSubwiki(activeView.data.wikiId, subwikiId, false, false, siteId); + + } else if (activeView.data.courseId && activeView.data.module) { + const moduleId = activeView.data.module && activeView.data.module.id; + if (moduleId) { + // Get the wiki. + return this.wikiProvider.getWiki(activeView.data.courseId, moduleId, false, siteId).then((wiki) => { + // Check if the subwiki belongs to this wiki. + return this.wikiProvider.wikiHasSubwiki(wiki.id, subwikiId, false, false, siteId); + }).catch(() => { + // Not found, return false. + return false; + }); + } + } + } + + return Promise.resolve(false); + } + + /** + * 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. + * @return {CoreContentLinksAction[]|Promise} List of (or promise resolved with list of) actions. + */ + getActions(siteIds: string[], url: string, params: any, courseId?: number): + CoreContentLinksAction[] | Promise { + + courseId = courseId || params.courseid || params.cid; + + return [{ + action: (siteId, navCtrl?: NavController): void => { + const modal = this.domUtils.showModalLoading(), + subwikiId = parseInt(params.swid, 10), + activeView = navCtrl && navCtrl.getActive(); + + // Check if the link is inside the same wiki. + this.currentStateIsSameWiki(activeView, subwikiId, siteId).then((isSameWiki) => { + if (isSameWiki) { + // User is seeing the wiki, we can get the module from the wiki params. + if (activeView && activeView.data.module && activeView.data.module.id) { + // We already have it in the params. + return activeView.data.module; + } else if (activeView && activeView.data.wikiId) { + return this.courseProvider.getModuleBasicInfoByInstance(activeView.data.wikiId, 'wiki', siteId) + .catch(() => { + // Not found. + }); + } + } + }).then((module) => { + // Return the params. + const pageParams = { + module: module, + courseId: courseId || (module && module.course) || (activeView && activeView.data.courseId), + pageTitle: params.title, + subwikiId: subwikiId + }; + + this.linkHelper.goInSite(navCtrl, 'AddonModWikiEditPage', pageParams, siteId); + }).finally(() => { + modal.dismiss(); + }); + } + }]; + } +} diff --git a/src/addon/mod/wiki/providers/edit-link-handler.ts b/src/addon/mod/wiki/providers/edit-link-handler.ts new file mode 100644 index 000000000..5c74cb4d7 --- /dev/null +++ b/src/addon/mod/wiki/providers/edit-link-handler.ts @@ -0,0 +1,66 @@ +// (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 { CoreTextUtilsProvider } from '@providers/utils/text'; +import { CoreContentLinksHandlerBase } from '@core/contentlinks/classes/base-handler'; +import { CoreContentLinksAction } from '@core/contentlinks/providers/delegate'; +import { CoreContentLinksHelperProvider } from '@core/contentlinks/providers/helper'; + +/** + * Handler to treat links to edit a wiki page. + */ +@Injectable() +export class AddonModWikiEditLinkHandler extends CoreContentLinksHandlerBase { + name = 'AddonModWikiEditLinkHandler'; + featureName = 'CoreCourseModuleDelegate_AddonModWiki'; + pattern = /\/mod\/wiki\/edit\.php.*([\&\?]pageid=\d+)/; + + constructor(protected linkHelper: CoreContentLinksHelperProvider, protected textUtils: CoreTextUtilsProvider) { + 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. + * @return {CoreContentLinksAction[]|Promise} List of (or promise resolved with list of) actions. + */ + getActions(siteIds: string[], url: string, params: any, courseId?: number): + CoreContentLinksAction[] | Promise { + + courseId = courseId || params.courseid || params.cid; + + return [{ + action: (siteId, navCtrl?): void => { + + let section = ''; + if (typeof params.section != 'undefined') { + section = this.textUtils.decodeURIComponent(params.section.replace(/\+/g, ' ')); + } + + const pageParams = { + courseId: courseId, + section: section, + pageId: parseInt(params.pageid, 10) + }; + + this.linkHelper.goInSite(navCtrl, 'AddonModWikiEditPage', pageParams, siteId); + } + }]; + } +} diff --git a/src/addon/mod/wiki/providers/index-link-handler.ts b/src/addon/mod/wiki/providers/index-link-handler.ts new file mode 100644 index 000000000..212cbaa64 --- /dev/null +++ b/src/addon/mod/wiki/providers/index-link-handler.ts @@ -0,0 +1,29 @@ +// (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 { CoreContentLinksModuleIndexHandler } from '@core/contentlinks/classes/module-index-handler'; +import { CoreCourseHelperProvider } from '@core/course/providers/helper'; + +/** + * Handler to treat links to wiki index. + */ +@Injectable() +export class AddonModWikiIndexLinkHandler extends CoreContentLinksModuleIndexHandler { + name = 'AddonModWikiIndexLinkHandler'; + + constructor(courseHelper: CoreCourseHelperProvider) { + super(courseHelper, 'AddonModWiki', 'wiki'); + } +} diff --git a/src/addon/mod/wiki/providers/page-or-map-link-handler.ts b/src/addon/mod/wiki/providers/page-or-map-link-handler.ts new file mode 100644 index 000000000..8a5bf9e23 --- /dev/null +++ b/src/addon/mod/wiki/providers/page-or-map-link-handler.ts @@ -0,0 +1,111 @@ +// (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 { CoreDomUtilsProvider } from '@providers/utils/dom'; +import { CoreContentLinksHandlerBase } from '@core/contentlinks/classes/base-handler'; +import { CoreContentLinksAction } from '@core/contentlinks/providers/delegate'; +import { CoreContentLinksHelperProvider } from '@core/contentlinks/providers/helper'; +import { CoreCourseHelperProvider } from '@core/course/providers/helper'; +import { AddonModWikiProvider } from './wiki'; + +/** + * Handler to treat links to a wiki page or the wiki map. + */ +@Injectable() +export class AddonModWikiPageOrMapLinkHandler extends CoreContentLinksHandlerBase { + name = 'AddonModWikiPageOrMapLinkHandler'; + featureName = 'CoreCourseModuleDelegate_AddonModWiki'; + pattern = /\/mod\/wiki\/(view|map)\.php.*([\&\?]pageid=\d+)/; + + constructor(protected domUtils: CoreDomUtilsProvider, protected wikiProvider: AddonModWikiProvider, + protected courseHelper: CoreCourseHelperProvider, protected 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. + * @return {CoreContentLinksAction[]|Promise} List of (or promise resolved with list of) actions. + */ + getActions(siteIds: string[], url: string, params: any, courseId?: number): + CoreContentLinksAction[] | Promise { + + courseId = courseId || params.courseid || params.cid; + + return [{ + action: (siteId, navCtrl?): void => { + const modal = this.domUtils.showModalLoading(), + pageId = parseInt(params.pageid, 10), + action = url.indexOf('mod/wiki/map.php') != -1 ? 'map' : 'page'; + + // Get the page data to obtain wikiId, subwikiId, etc. + this.wikiProvider.getPageContents(pageId, false, false, siteId).then((page) => { + let promise; + if (courseId) { + promise = Promise.resolve(courseId); + } else { + promise = this.courseHelper.getModuleCourseIdByInstance(page.wikiid, 'wiki', siteId); + } + + return promise.then((courseId) => { + const pageParams = { + courseId: courseId, + pageId: page.id, + pageTitle: page.title, + wikiId: page.wikiid, + subwikiId: page.subwikiid, + action: action + }; + + this.linkHelper.goInSite(navCtrl, 'AddonModWikiIndexPage', pageParams, siteId); + }); + }).catch((error) => { + + this.domUtils.showErrorModalDefault(error, 'addon.mod_wiki.errorloadingpage', true); + }).finally(() => { + modal.dismiss(); + }); + } + }]; + } + + /** + * 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 { + const isMap = url.indexOf('mod/wiki/map.php') != -1; + + if (params.id && !isMap) { + // ID param is more prioritary than pageid in index page, it's a index URL. + return false; + } else if (isMap && typeof params.option != 'undefined' && params.option != 5) { + // Map link but the option isn't "Page list", not supported. + return false; + } + + return true; + } +} diff --git a/src/addon/mod/wiki/wiki.module.ts b/src/addon/mod/wiki/wiki.module.ts index ed21a0d9d..a85ced279 100644 --- a/src/addon/mod/wiki/wiki.module.ts +++ b/src/addon/mod/wiki/wiki.module.ts @@ -16,6 +16,7 @@ import { NgModule } from '@angular/core'; import { CoreCronDelegate } from '@providers/cron'; import { CoreCourseModuleDelegate } from '@core/course/providers/module-delegate'; import { CoreCourseModulePrefetchDelegate } from '@core/course/providers/module-prefetch-delegate'; +import { CoreContentLinksDelegate } from '@core/contentlinks/providers/delegate'; import { AddonModWikiComponentsModule } from './components/components.module'; import { AddonModWikiProvider } from './providers/wiki'; import { AddonModWikiOfflineProvider } from './providers/wiki-offline'; @@ -23,6 +24,10 @@ import { AddonModWikiSyncProvider } from './providers/wiki-sync'; import { AddonModWikiModuleHandler } from './providers/module-handler'; import { AddonModWikiPrefetchHandler } from './providers/prefetch-handler'; import { AddonModWikiSyncCronHandler } from './providers/sync-cron-handler'; +import { AddonModWikiIndexLinkHandler } from './providers/index-link-handler'; +import { AddonModWikiPageOrMapLinkHandler } from './providers/page-or-map-link-handler'; +import { AddonModWikiCreateLinkHandler } from './providers/create-link-handler'; +import { AddonModWikiEditLinkHandler } from './providers/edit-link-handler'; @NgModule({ declarations: [ @@ -36,16 +41,26 @@ import { AddonModWikiSyncCronHandler } from './providers/sync-cron-handler'; AddonModWikiSyncProvider, AddonModWikiModuleHandler, AddonModWikiPrefetchHandler, - AddonModWikiSyncCronHandler + AddonModWikiSyncCronHandler, + AddonModWikiIndexLinkHandler, + AddonModWikiPageOrMapLinkHandler, + AddonModWikiCreateLinkHandler, + AddonModWikiEditLinkHandler ] }) export class AddonModWikiModule { constructor(moduleDelegate: CoreCourseModuleDelegate, moduleHandler: AddonModWikiModuleHandler, prefetchDelegate: CoreCourseModulePrefetchDelegate, prefetchHandler: AddonModWikiPrefetchHandler, - cronDelegate: CoreCronDelegate, syncHandler: AddonModWikiSyncCronHandler, ) { + cronDelegate: CoreCronDelegate, syncHandler: AddonModWikiSyncCronHandler, linksDelegate: CoreContentLinksDelegate, + indexHandler: AddonModWikiIndexLinkHandler, pageOrMapHandler: AddonModWikiPageOrMapLinkHandler, + createHandler: AddonModWikiCreateLinkHandler, editHandler: AddonModWikiEditLinkHandler) { moduleDelegate.registerHandler(moduleHandler); prefetchDelegate.registerHandler(prefetchHandler); cronDelegate.register(syncHandler); + linksDelegate.registerHandler(indexHandler); + linksDelegate.registerHandler(pageOrMapHandler); + linksDelegate.registerHandler(createHandler); + linksDelegate.registerHandler(editHandler); } }