MOBILE-3745 core: Add new openPopover function

main
Pau Ferrer Ocaña 2021-05-04 15:57:31 +02:00
parent 7af5c510bf
commit 43ed1d9917
11 changed files with 88 additions and 100 deletions

View File

@ -34,7 +34,7 @@ import { CoreCategoryData, CoreCourses, CoreEnrolledCourseData } from '@features
import { CoreCoursesHelper } from '@features/courses/services/courses-helper'; import { CoreCoursesHelper } from '@features/courses/services/courses-helper';
import { AddonCalendarFilterPopoverComponent } from '../../components/filter/filter'; import { AddonCalendarFilterPopoverComponent } from '../../components/filter/filter';
import moment from 'moment'; import moment from 'moment';
import { Network, NgZone, PopoverController } from '@singletons'; import { Network, NgZone } from '@singletons';
import { CoreNavigator } from '@services/navigator'; import { CoreNavigator } from '@services/navigator';
import { Params } from '@angular/router'; import { Params } from '@angular/router';
import { Subscription } from 'rxjs'; import { Subscription } from 'rxjs';
@ -535,7 +535,7 @@ export class AddonCalendarDayPage implements OnInit, OnDestroy {
* @param event Event. * @param event Event.
*/ */
async openFilter(event: MouseEvent): Promise<void> { async openFilter(event: MouseEvent): Promise<void> {
const popover = await PopoverController.create({ await CoreDomUtils.openPopover({
component: AddonCalendarFilterPopoverComponent, component: AddonCalendarFilterPopoverComponent,
componentProps: { componentProps: {
courses: this.courses, courses: this.courses,
@ -543,7 +543,6 @@ export class AddonCalendarDayPage implements OnInit, OnDestroy {
}, },
event, event,
}); });
await popover.present();
} }
/** /**

View File

@ -23,7 +23,7 @@ import { AddonCalendar, AddonCalendarProvider } from '../../services/calendar';
import { AddonCalendarOffline } from '../../services/calendar-offline'; import { AddonCalendarOffline } from '../../services/calendar-offline';
import { AddonCalendarSync, AddonCalendarSyncProvider } from '../../services/calendar-sync'; import { AddonCalendarSync, AddonCalendarSyncProvider } from '../../services/calendar-sync';
import { AddonCalendarFilter, AddonCalendarHelper } from '../../services/calendar-helper'; import { AddonCalendarFilter, AddonCalendarHelper } from '../../services/calendar-helper';
import { Network, NgZone, PopoverController } from '@singletons'; import { Network, NgZone } from '@singletons';
import { Subscription } from 'rxjs'; import { Subscription } from 'rxjs';
import { CoreEnrolledCourseData } from '@features/courses/services/courses'; import { CoreEnrolledCourseData } from '@features/courses/services/courses';
import { ActivatedRoute, Params } from '@angular/router'; import { ActivatedRoute, Params } from '@angular/router';
@ -333,7 +333,7 @@ export class AddonCalendarIndexPage implements OnInit, OnDestroy {
* @param event Event. * @param event Event.
*/ */
async openFilter(event: MouseEvent): Promise<void> { async openFilter(event: MouseEvent): Promise<void> {
const popover = await PopoverController.create({ await CoreDomUtils.openPopover({
component: AddonCalendarFilterPopoverComponent, component: AddonCalendarFilterPopoverComponent,
componentProps: { componentProps: {
courses: this.courses, courses: this.courses,
@ -341,7 +341,6 @@ export class AddonCalendarIndexPage implements OnInit, OnDestroy {
}, },
event, event,
}); });
await popover.present();
} }
/** /**

View File

@ -35,7 +35,7 @@ import { CoreConstants } from '@/core/constants';
import { AddonCalendarFilterPopoverComponent } from '../../components/filter/filter'; import { AddonCalendarFilterPopoverComponent } from '../../components/filter/filter';
import { Params } from '@angular/router'; import { Params } from '@angular/router';
import { Subscription } from 'rxjs'; import { Subscription } from 'rxjs';
import { Network, NgZone, PopoverController } from '@singletons'; import { Network, NgZone } from '@singletons';
import { CoreCoursesHelper } from '@features/courses/services/courses-helper'; import { CoreCoursesHelper } from '@features/courses/services/courses-helper';
import { CoreUtils } from '@services/utils/utils'; import { CoreUtils } from '@services/utils/utils';
import { CoreNavigator } from '@services/navigator'; import { CoreNavigator } from '@services/navigator';
@ -616,7 +616,7 @@ export class AddonCalendarListPage implements OnInit, OnDestroy {
* @param event Event. * @param event Event.
*/ */
async openFilter(event: MouseEvent): Promise<void> { async openFilter(event: MouseEvent): Promise<void> {
const popover = await PopoverController.create({ await CoreDomUtils.openPopover({
component: AddonCalendarFilterPopoverComponent, component: AddonCalendarFilterPopoverComponent,
componentProps: { componentProps: {
courses: this.courses, courses: this.courses,
@ -624,7 +624,6 @@ export class AddonCalendarListPage implements OnInit, OnDestroy {
}, },
event, event,
}); });
await popover.present();
} }
/** /**

View File

@ -28,7 +28,7 @@ import {
AddonModForumReplyDiscussionData, AddonModForumReplyDiscussionData,
} from '@addons/mod/forum/services/forum'; } from '@addons/mod/forum/services/forum';
import { AddonModForumOffline, AddonModForumOfflineDiscussion } from '@addons/mod/forum/services/forum-offline'; import { AddonModForumOffline, AddonModForumOfflineDiscussion } from '@addons/mod/forum/services/forum-offline';
import { PopoverController, Translate } from '@singletons'; import { Translate } from '@singletons';
import { CoreCourseContentsPage } from '@features/course/pages/contents/contents'; import { CoreCourseContentsPage } from '@features/course/pages/contents/contents';
import { AddonModForumHelper } from '@addons/mod/forum/services/forum-helper'; import { AddonModForumHelper } from '@addons/mod/forum/services/forum-helper';
import { CoreGroups, CoreGroupsProvider } from '@services/groups'; import { CoreGroups, CoreGroupsProvider } from '@services/groups';
@ -667,7 +667,7 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom
* @param discussion Discussion. * @param discussion Discussion.
*/ */
async showOptionsMenu(event: Event, discussion: AddonModForumDiscussion): Promise<void> { async showOptionsMenu(event: Event, discussion: AddonModForumDiscussion): Promise<void> {
const popover = await PopoverController.create({ const popoverData = await CoreDomUtils.openPopover<{ action?: string; value: boolean }>({
component: AddonModForumDiscussionOptionsMenuComponent, component: AddonModForumDiscussionOptionsMenuComponent,
componentProps: { componentProps: {
discussion, discussion,
@ -677,20 +677,16 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom
event, event,
}); });
await popover.present(); if (popoverData && popoverData.action) {
switch (popoverData.action) {
const result = await popover.onDidDismiss<{ action?: string; value: boolean }>();
if (result.data && result.data.action) {
switch (result.data.action) {
case 'lock': case 'lock':
discussion.locked = result.data.value; discussion.locked = popoverData.value;
break; break;
case 'pin': case 'pin':
discussion.pinned = result.data.value; discussion.pinned = popoverData.value;
break; break;
case 'star': case 'star':
discussion.starred = result.data.value; discussion.starred = popoverData.value;
break; break;
default: default:
break; break;

View File

@ -40,7 +40,7 @@ import {
AddonModForumUpdateDiscussionPostWSOptionsObject, AddonModForumUpdateDiscussionPostWSOptionsObject,
} from '../../services/forum'; } from '../../services/forum';
import { CoreTag } from '@features/tag/services/tag'; import { CoreTag } from '@features/tag/services/tag';
import { PopoverController, Translate } from '@singletons'; import { Translate } from '@singletons';
import { CoreFileUploader } from '@features/fileuploader/services/fileuploader'; import { CoreFileUploader } from '@features/fileuploader/services/fileuploader';
import { IonContent } from '@ionic/angular'; import { IonContent } from '@ionic/angular';
import { AddonModForumSync } from '../../services/forum-sync'; import { AddonModForumSync } from '../../services/forum-sync';
@ -218,7 +218,7 @@ export class AddonModForumPostComponent implements OnInit, OnDestroy, OnChanges
* @param event Click Event. * @param event Click Event.
*/ */
async showOptionsMenu(event: Event): Promise<void> { async showOptionsMenu(event: Event): Promise<void> {
const popover = await PopoverController.create({ const popoverData = await CoreDomUtils.openPopover<{ action?: string }>({
component: AddonModForumPostOptionsMenuComponent, component: AddonModForumPostOptionsMenuComponent,
componentProps: { componentProps: {
post: this.post, post: this.post,
@ -228,12 +228,8 @@ export class AddonModForumPostComponent implements OnInit, OnDestroy, OnChanges
event, event,
}); });
await popover.present(); if (popoverData && popoverData.action) {
switch (popoverData.action) {
const result = await popover.onDidDismiss<{ action?: string }>();
if (result.data && result.data.action) {
switch (result.data.action) {
case 'edit': case 'edit':
this.editPost(); this.editPost();
break; break;

View File

@ -27,7 +27,7 @@ import { IonContent } from '@ionic/angular';
import { CoreSites } from '@services/sites'; import { CoreSites } from '@services/sites';
import { CoreDomUtils } from '@services/utils/dom'; import { CoreDomUtils } from '@services/utils/dom';
import { CoreTextUtils } from '@services/utils/text'; import { CoreTextUtils } from '@services/utils/text';
import { PopoverController, Translate } from '@singletons'; import { Translate } from '@singletons';
import { CoreEventObserver, CoreEvents } from '@singletons/events'; import { CoreEventObserver, CoreEvents } from '@singletons/events';
import { import {
AddonModGlossary, AddonModGlossary,
@ -405,7 +405,7 @@ export class AddonModGlossaryIndexComponent extends CoreCourseModuleMainActivity
* @param event Event. * @param event Event.
*/ */
async openModePicker(event: MouseEvent): Promise<void> { async openModePicker(event: MouseEvent): Promise<void> {
const popover = await PopoverController.create({ const mode = await CoreDomUtils.openPopover<AddonModGlossaryFetchMode>({
component: AddonModGlossaryModePickerPopoverComponent, component: AddonModGlossaryModePickerPopoverComponent,
componentProps: { componentProps: {
browseModes: this.glossary!.browsemodes, browseModes: this.glossary!.browsemodes,
@ -414,11 +414,6 @@ export class AddonModGlossaryIndexComponent extends CoreCourseModuleMainActivity
event, event,
}); });
await popover.present();
const result = await popover.onDidDismiss<AddonModGlossaryFetchMode>();
const mode = result.data;
if (mode) { if (mode) {
if (mode !== this.fetchMode) { if (mode !== this.fetchMode) {
this.changeFetchMode(mode); this.changeFetchMode(mode);

View File

@ -16,10 +16,10 @@ import { BehaviorSubject } from 'rxjs';
import { CoreQuestionHelper } from '@features/question/services/question-helper'; import { CoreQuestionHelper } from '@features/question/services/question-helper';
import { CoreQuestionsAnswers } from '@features/question/services/question'; import { CoreQuestionsAnswers } from '@features/question/services/question';
import { PopoverController } from '@singletons';
import { CoreLogger } from '@singletons/logger'; import { CoreLogger } from '@singletons/logger';
import { AddonModQuizConnectionErrorComponent } from '../components/connection-error/connection-error'; import { AddonModQuizConnectionErrorComponent } from '../components/connection-error/connection-error';
import { AddonModQuiz, AddonModQuizAttemptWSData, AddonModQuizQuizWSData } from '../services/quiz'; import { AddonModQuiz, AddonModQuizAttemptWSData, AddonModQuizQuizWSData } from '../services/quiz';
import { CoreDomUtils } from '@services/utils/dom';
/** /**
* Class to support auto-save in quiz. Every certain seconds, it will check if there are changes in the current page answers * Class to support auto-save in quiz. Every certain seconds, it will check if there are changes in the current page answers
@ -197,13 +197,10 @@ export class AddonModQuizAutoSave {
}; };
this.popoverShown = true; this.popoverShown = true;
this.popover = await PopoverController.create({ this.popover = await CoreDomUtils.openPopover({
component: AddonModQuizConnectionErrorComponent, component: AddonModQuizConnectionErrorComponent,
event: <Event> event, event: <Event> event,
}); });
await this.popover.present();
await this.popover.onDidDismiss();
this.popoverShown = false; this.popoverShown = false;
} }

View File

@ -27,7 +27,7 @@ import { CoreSites } from '@services/sites';
import { CoreDomUtils } from '@services/utils/dom'; import { CoreDomUtils } from '@services/utils/dom';
import { CoreTextUtils } from '@services/utils/text'; import { CoreTextUtils } from '@services/utils/text';
import { CoreUtils } from '@services/utils/utils'; import { CoreUtils } from '@services/utils/utils';
import { PopoverController, Translate } from '@singletons'; import { Translate } from '@singletons';
import { CoreEventObserver, CoreEvents } from '@singletons/events'; import { CoreEventObserver, CoreEvents } from '@singletons/events';
import { Md5 } from 'ts-md5'; import { Md5 } from 'ts-md5';
import { AddonModWikiPageDBRecord } from '../../services/database/wiki'; import { AddonModWikiPageDBRecord } from '../../services/database/wiki';
@ -794,7 +794,7 @@ export class AddonModWikiIndexComponent extends CoreCourseModuleMainActivityComp
* @param event Event. * @param event Event.
*/ */
async showSubwikiPicker(event: MouseEvent): Promise<void> { async showSubwikiPicker(event: MouseEvent): Promise<void> {
const popover = await PopoverController.create({ const popoverData = await CoreDomUtils.openPopover<AddonModWikiSubwiki>({
component: AddonModWikiSubwikiPickerComponent, component: AddonModWikiSubwikiPickerComponent,
componentProps: { componentProps: {
subwikis: this.subwikiData.subwikis, subwikis: this.subwikiData.subwikis,
@ -803,12 +803,8 @@ export class AddonModWikiIndexComponent extends CoreCourseModuleMainActivityComp
event, event,
}); });
await popover.present(); if (popoverData) {
this.goToSubwiki(popoverData.id, popoverData.userid, popoverData.groupid, popoverData.canedit);
const result = await popover.onDidDismiss();
if (result.data) {
this.goToSubwiki(result.data.id, result.data.userid, result.data.groupid, result.data.canedit);
} }
} }

View File

@ -17,7 +17,7 @@ import { Subject } from 'rxjs';
import { auditTime } from 'rxjs/operators'; import { auditTime } from 'rxjs/operators';
import { CoreDomUtils } from '@services/utils/dom'; import { CoreDomUtils } from '@services/utils/dom';
import { CoreUtils } from '@services/utils/utils'; import { CoreUtils } from '@services/utils/utils';
import { PopoverController, Translate } from '@singletons'; import { Translate } from '@singletons';
import { CoreContextMenuItemComponent } from './context-menu-item'; import { CoreContextMenuItemComponent } from './context-menu-item';
import { CoreContextMenuPopoverComponent } from './context-menu-popover'; import { CoreContextMenuPopoverComponent } from './context-menu-popover';
@ -176,26 +176,23 @@ export class CoreContextMenuComponent implements OnInit, OnDestroy {
*/ */
async showContextMenu(event: MouseEvent): Promise<void> { async showContextMenu(event: MouseEvent): Promise<void> {
if (!this.expanded) { if (!this.expanded) {
const popover = await PopoverController.create(
{
event,
component: CoreContextMenuPopoverComponent,
componentProps: {
title: this.title,
items: this.items,
},
showBackdrop: true,
id: this.uniqueId,
},
);
await popover.present();
this.expanded = true; this.expanded = true;
const data = await popover.onDidDismiss<CoreContextMenuItemComponent>(); const popoverData = await CoreDomUtils.openPopover<CoreContextMenuItemComponent>({
event,
component: CoreContextMenuPopoverComponent,
componentProps: {
title: this.title,
items: this.items,
},
showBackdrop: true,
id: this.uniqueId,
});
this.expanded = false; this.expanded = false;
if (data.data) { if (popoverData) {
data.data.onClosed?.emit(); popoverData.onClosed?.emit();
} }
} }

View File

@ -19,7 +19,7 @@ import { CoreDomUtils } from '@services/utils/dom';
import { CoreCourses, CoreCoursesProvider } from '@features/courses/services/courses'; import { CoreCourses, CoreCoursesProvider } from '@features/courses/services/courses';
import { CoreCourse, CoreCourseProvider } from '@features/course/services/course'; import { CoreCourse, CoreCourseProvider } from '@features/course/services/course';
import { CoreCourseHelper, CorePrefetchStatusInfo } from '@features/course/services/course-helper'; import { CoreCourseHelper, CorePrefetchStatusInfo } from '@features/course/services/course-helper';
import { PopoverController, Translate } from '@singletons'; import { Translate } from '@singletons';
import { CoreConstants } from '@/core/constants'; import { CoreConstants } from '@/core/constants';
import { CoreEnrolledCourseDataWithExtraInfoAndOptions } from '../../services/courses-helper'; import { CoreEnrolledCourseDataWithExtraInfoAndOptions } from '../../services/courses-helper';
import { CoreCoursesCourseOptionsMenuComponent } from '../course-options-menu/course-options-menu'; import { CoreCoursesCourseOptionsMenuComponent } from '../course-options-menu/course-options-menu';
@ -198,7 +198,7 @@ export class CoreCoursesCourseProgressComponent implements OnInit, OnDestroy {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
const popover = await PopoverController.create({ const popoverData = await CoreDomUtils.openPopover<string>({
component: CoreCoursesCourseOptionsMenuComponent, component: CoreCoursesCourseOptionsMenuComponent,
componentProps: { componentProps: {
course: this.course, course: this.course,
@ -207,37 +207,32 @@ export class CoreCoursesCourseProgressComponent implements OnInit, OnDestroy {
}, },
event: e, event: e,
}); });
await popover.present();
const action = await popover.onDidDismiss<string>(); switch (popoverData) {
case 'download':
if (action.data) { if (!this.prefetchCourseData.loading) {
switch (action.data) { this.prefetchCourse(e);
case 'download': }
if (!this.prefetchCourseData.loading) { break;
this.prefetchCourse(e); case 'delete':
} if (this.courseStatus == 'downloaded' || this.courseStatus == 'outdated') {
break; this.deleteCourse();
case 'delete': }
if (this.courseStatus == 'downloaded' || this.courseStatus == 'outdated') { break;
this.deleteCourse(); case 'hide':
} this.setCourseHidden(true);
break; break;
case 'hide': case 'show':
this.setCourseHidden(true); this.setCourseHidden(false);
break; break;
case 'show': case 'favourite':
this.setCourseHidden(false); this.setCourseFavourite(true);
break; break;
case 'favourite': case 'unfavourite':
this.setCourseFavourite(true); this.setCourseFavourite(false);
break; break;
case 'unfavourite': default:
this.setCourseFavourite(false); break;
break;
default:
break;
}
} }
} }

View File

@ -15,7 +15,7 @@
import { Injectable, SimpleChange, ElementRef, KeyValueChanges } from '@angular/core'; import { Injectable, SimpleChange, ElementRef, KeyValueChanges } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser'; import { DomSanitizer } from '@angular/platform-browser';
import { IonContent } from '@ionic/angular'; import { IonContent } from '@ionic/angular';
import { ModalOptions, AlertOptions, AlertButton, TextFieldTypes } from '@ionic/core'; import { ModalOptions, PopoverOptions, AlertOptions, AlertButton, TextFieldTypes } from '@ionic/core';
import { Md5 } from 'ts-md5'; import { Md5 } from 'ts-md5';
import { CoreApp } from '@services/app'; import { CoreApp } from '@services/app';
@ -796,7 +796,7 @@ export class CoreDomUtilsProvider {
el.addEventListener('click', async (ev: Event) => { el.addEventListener('click', async (ev: Event) => {
const html = el.getAttribute('data-html'); const html = el.getAttribute('data-html');
const popover = await PopoverController.create({ await CoreDomUtils.openPopover({
component: CoreBSTooltipComponent, component: CoreBSTooltipComponent,
componentProps: { componentProps: {
content, content,
@ -804,7 +804,6 @@ export class CoreDomUtilsProvider {
}, },
event: ev, event: ev,
}); });
await popover.present();
}); });
}); });
} }
@ -1721,6 +1720,26 @@ export class CoreDomUtilsProvider {
return await this.openModal<T>(modalOptions); return await this.openModal<T>(modalOptions);
} }
/**
* Opens a popover.
*
* @param popoverOptions Modal Options.
*/
async openPopover<T = void>(
popoverOptions: PopoverOptions,
): Promise<T | undefined> {
const popover = await PopoverController.create(popoverOptions);
await popover.present();
// If onDidDismiss is nedded we can add a new param to the function to wait one function or the other.
const result = await popover.onWillDismiss<T>();
if (result?.data) {
return result?.data;
}
}
/** /**
* View an image in a modal. * View an image in a modal.
* *