MOBILE-3656 wiki: Add go to wiki first page feature
This commit is contained in:
		
							parent
							
								
									a881722a50
								
							
						
					
					
						commit
						2f0eb0088b
					
				| @ -101,6 +101,7 @@ export class AddonModWikiIndexComponent extends CoreCourseModuleMainActivityComp | ||||
|     protected ignoreManualSyncEvent = false; // Whether manual sync event should be ignored.
 | ||||
|     protected currentUserId?: number; // Current user ID.
 | ||||
|     protected hasEdited = false; // Whether the user has opened the edit page.
 | ||||
|     protected currentPath!: string; | ||||
| 
 | ||||
|     constructor( | ||||
|         protected content?: IonContent, | ||||
| @ -120,6 +121,7 @@ export class AddonModWikiIndexComponent extends CoreCourseModuleMainActivityComp | ||||
|         this.currentUserId = CoreSites.getCurrentSiteUserId(); | ||||
|         this.isMainPage = !this.pageId && !this.pageTitle; | ||||
|         this.currentPage = this.pageId; | ||||
|         this.currentPath = CoreNavigator.getCurrentPath(); | ||||
|         this.listenEvents(); | ||||
| 
 | ||||
|         try { | ||||
| @ -212,6 +214,7 @@ export class AddonModWikiIndexComponent extends CoreCourseModuleMainActivityComp | ||||
|             this.wiki = await AddonModWiki.getWiki(this.courseId, this.module.id); | ||||
| 
 | ||||
|             this.dataRetrieved.emit(this.wiki); | ||||
|             AddonModWiki.wikiPageOpened(this.wiki.id, this.currentPath); | ||||
| 
 | ||||
|             if (sync) { | ||||
|                 // Try to synchronize the wiki.
 | ||||
| @ -421,40 +424,16 @@ export class AddonModWikiIndexComponent extends CoreCourseModuleMainActivityComp | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Get the wiki home view. If cannot determine or it's current view, return undefined. | ||||
|      * Get path to the wiki home view. If cannot determine or it's current view, return undefined. | ||||
|      * | ||||
|      * @return The view controller of the home view | ||||
|      * @return The path of the home view | ||||
|      */ | ||||
|     protected getWikiHomeView(): unknown { | ||||
|         if (!this.wiki?.id) { | ||||
|     protected getWikiHomeView(): string | undefined { | ||||
|         if (!this.wiki) { | ||||
|             return; | ||||
|         } | ||||
| 
 | ||||
|         // @todo
 | ||||
|         // const views = this.navCtrl.getViews();
 | ||||
| 
 | ||||
|         // // Go back in history until we find a page that doesn't belong to current wiki.
 | ||||
|         // for (let i = views.length - 2; i >= 0; i--) {
 | ||||
|         //     const view = views[i];
 | ||||
| 
 | ||||
|         //     if (view.component.name != 'AddonModWikiIndexPage') {
 | ||||
|         //         if (i == views.length - 2) {
 | ||||
|         //             // Next view is current view, return undefined.
 | ||||
|         //             return;
 | ||||
|         //         }
 | ||||
| 
 | ||||
|         //         // This view is no longer from wiki, return the next view.
 | ||||
|         //         return views[i + 1];
 | ||||
|         //     }
 | ||||
| 
 | ||||
|         //     // Check that the view belongs to the same wiki as current view.
 | ||||
|         //     const wikiId = view.data.wikiId ? view.data.wikiId : view.data.module.instance;
 | ||||
| 
 | ||||
|         //     if (!wikiId || wikiId != this.wiki.id) {
 | ||||
|         //         // Wiki has changed, return the next view.
 | ||||
|         //         return views[i + 1];
 | ||||
|         //     }
 | ||||
|         // }
 | ||||
|         return AddonModWiki.getFirstWikiPageOpened(this.wiki.id, this.currentPath); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
| @ -591,7 +570,7 @@ export class AddonModWikiIndexComponent extends CoreCourseModuleMainActivityComp | ||||
|             componentProps: { | ||||
|                 pages: this.subwikiPages, | ||||
|                 selected: this.currentPageObj && 'id' in this.currentPageObj && this.currentPageObj.id, | ||||
|                 // homeView: @todo this.getWikiHomeView(),
 | ||||
|                 homeView: this.getWikiHomeView(), | ||||
|                 moduleId: this.module.id, | ||||
|                 courseId: this.courseId, | ||||
|             }, | ||||
| @ -609,7 +588,7 @@ export class AddonModWikiIndexComponent extends CoreCourseModuleMainActivityComp | ||||
|         if (result.data) { | ||||
|             if (result.data.type == 'home') { | ||||
|                 // Go back to the initial page of the wiki.
 | ||||
|                 // @todo this.navCtrl.popTo(page.goto);
 | ||||
|                 CoreNavigator.navigateToSitePath(result.data.goto); | ||||
|             } else { | ||||
|                 this.goToPage(result.data.goto); | ||||
|             } | ||||
| @ -840,6 +819,9 @@ export class AddonModWikiIndexComponent extends CoreCourseModuleMainActivityComp | ||||
| 
 | ||||
|         this.manualSyncObserver?.off(); | ||||
|         this.newPageObserver?.off(); | ||||
|         if (this.wiki) { | ||||
|             AddonModWiki.wikiPageClosed(this.wiki.id, this.currentPath); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|  | ||||
| @ -12,7 +12,7 @@ | ||||
|     <nav> | ||||
|         <ion-list> | ||||
|             <!-- Go to "home". --> | ||||
|             <ion-item class="ion-text-wrap" *ngIf="homeView" (click)="goToWikiHome()"> | ||||
|             <ion-item class="ion-text-wrap" *ngIf="homeView" (click)="goToWikiHome()" tappable> | ||||
|                 <ion-icon name="fas-home" slot="start"></ion-icon> | ||||
|                 <ion-label>{{ 'addon.mod_wiki.gowikihome' | translate }}</ion-label> | ||||
|             </ion-item> | ||||
| @ -21,7 +21,7 @@ | ||||
|                     <ion-label>{{ letter.label }}</ion-label> | ||||
|                 </ion-item-divider> | ||||
|                 <ion-item class="ion-text-wrap" *ngFor="let page of letter.pages" (click)="goToPage(page)" | ||||
|                     [class.core-selected-item]="selected == page.id"> | ||||
|                     [class.core-selected-item]="selected == page.id" tappable> | ||||
|                     <ion-icon name="fas-home" slot="start" *ngIf="page.firstpage"></ion-icon> | ||||
|                     <ion-label> | ||||
|                         <core-format-text [text]="page.title" contextLevel="module" [contextInstanceId]="moduleId" | ||||
|  | ||||
| @ -30,7 +30,7 @@ export class AddonModWikiMapModalComponent implements OnInit { | ||||
|     @Input() selected?: number; | ||||
|     @Input() moduleId?: number; | ||||
|     @Input() courseId?: number; | ||||
|     @Input() homeView: unknown; // @todo
 | ||||
|     @Input() homeView?: string; | ||||
| 
 | ||||
|     map: AddonModWikiPagesMapLetter[] = []; // Map of pages, categorized by letter.
 | ||||
| 
 | ||||
| @ -54,7 +54,7 @@ export class AddonModWikiMapModalComponent implements OnInit { | ||||
|      * Go back to the initial page of the wiki. | ||||
|      */ | ||||
|     goToWikiHome(): void { | ||||
|         // @todo ModalController.dismiss({ type: 'home', goto: this.homeView });
 | ||||
|         ModalController.dismiss({ type: 'home', goto: this.homeView }); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|  | ||||
| @ -19,6 +19,7 @@ import { CoreCourseCommonModWSOptions } from '@features/course/services/course'; | ||||
| import { CoreCourseLogHelper } from '@features/course/services/log-helper'; | ||||
| import { CoreTagItem } from '@features/tag/services/tag'; | ||||
| import { CoreApp } from '@services/app'; | ||||
| import { CoreNavigator } from '@services/navigator'; | ||||
| import { CoreSites, CoreSitesCommonWSOptions, CoreSitesReadingStrategy } from '@services/sites'; | ||||
| import { CoreUtils } from '@services/utils/utils'; | ||||
| import { CoreWSExternalFile, CoreWSExternalWarning } from '@services/ws'; | ||||
| @ -41,6 +42,7 @@ export class AddonModWikiProvider { | ||||
|     static readonly RENEW_LOCK_TIME = 30000; // Milliseconds.
 | ||||
| 
 | ||||
|     protected subwikiListsCache: {[wikiId: number]: AddonModWikiSubwikiListData} = {}; | ||||
|     protected wikiFirstViewedPage: Record<string, Record<number, string>> = {}; | ||||
| 
 | ||||
|     constructor() { | ||||
|         // Clear subwiki lists cache on logout.
 | ||||
| @ -87,6 +89,23 @@ export class AddonModWikiProvider { | ||||
|         return response.pageid; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Get the first page opened for a wiki in the app if it isn't the current one. | ||||
|      * | ||||
|      * @param wikiId Wiki ID. | ||||
|      * @param path Path. | ||||
|      */ | ||||
|     getFirstWikiPageOpened(wikiId: number, path: string): string | undefined { | ||||
|         const tab = CoreNavigator.getMainMenuTabFromPath(path); | ||||
|         if (!tab) { | ||||
|             return; | ||||
|         } | ||||
| 
 | ||||
|         if (this.wikiFirstViewedPage[tab] && this.wikiFirstViewedPage[tab][wikiId] !== path) { | ||||
|             return this.wikiFirstViewedPage[tab][wikiId]; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Get a wiki page contents. | ||||
|      * | ||||
| @ -806,6 +825,47 @@ export class AddonModWikiProvider { | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * If this page is the first opened page for a wiki, remove the stored path so it's no longer the first viewed page. | ||||
|      * | ||||
|      * @param wikiId Wiki ID. | ||||
|      * @param path Path. | ||||
|      */ | ||||
|     wikiPageClosed(wikiId: number, path: string): void { | ||||
|         const tab = CoreNavigator.getMainMenuTabFromPath(path); | ||||
|         if (!tab) { | ||||
|             return; | ||||
|         } | ||||
| 
 | ||||
|         this.wikiFirstViewedPage[tab] = this.wikiFirstViewedPage[tab] || {}; | ||||
| 
 | ||||
|         if (this.wikiFirstViewedPage[tab][wikiId] === path) { | ||||
|             delete this.wikiFirstViewedPage[tab][wikiId]; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * If this page is the first opened page for a wiki, save its path so we can go back to it. | ||||
|      * | ||||
|      * @param wikiId Wiki ID. | ||||
|      * @param path Path. | ||||
|      */ | ||||
|     wikiPageOpened(wikiId: number, path: string): void { | ||||
|         const tab = CoreNavigator.getMainMenuTabFromPath(path); | ||||
|         if (!tab) { | ||||
|             return; | ||||
|         } | ||||
| 
 | ||||
|         this.wikiFirstViewedPage[tab] = this.wikiFirstViewedPage[tab] || {}; | ||||
| 
 | ||||
|         if (this.wikiFirstViewedPage[tab][wikiId]) { | ||||
|             // There's already an opened page for this wiki.
 | ||||
|             return; | ||||
|         } | ||||
| 
 | ||||
|         this.wikiFirstViewedPage[tab][wikiId] = path; | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| export const AddonModWiki = makeSingleton(AddonModWikiProvider); | ||||
|  | ||||
| @ -96,8 +96,17 @@ export class CoreNavigatorService { | ||||
|      * @return Current main menu tab or null if the current route is not using the main menu. | ||||
|      */ | ||||
|     getCurrentMainMenuTab(): string | null { | ||||
|         const currentPath = this.getCurrentPath(); | ||||
|         const matches = /^\/main\/([^/]+).*$/.exec(currentPath); | ||||
|         return this.getMainMenuTabFromPath(this.getCurrentPath()); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Get main menu tab from a path. | ||||
|      * | ||||
|      * @param path The path to check. | ||||
|      * @return Path's main menu tab or null if the path is not using the main menu. | ||||
|      */ | ||||
|     getMainMenuTabFromPath(path: string): string | null { | ||||
|         const matches = /^\/main\/([^/]+).*$/.exec(path); | ||||
| 
 | ||||
|         return matches?.[1] ?? null; | ||||
|     } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user