MOBILE-2856 data: Fix single entry view for offline entries

main
Albert Gasset 2019-04-10 18:24:28 +02:00
parent fccf79bbd2
commit cd77fb2f4e
1 changed files with 60 additions and 35 deletions

View File

@ -135,8 +135,13 @@ export class AddonModDataEntryPage implements OnDestroy {
this.title = data.name || this.title;
this.data = data;
return this.setEntryIdFromOffset(data.id, this.offset, this.selectedGroup).then(() => {
return this.dataProvider.getDatabaseAccessInformation(data.id);
return this.dataProvider.getFields(this.data.id).then((fieldsData) => {
this.fields = this.utils.arrayToObject(fieldsData, 'id');
this.fieldsArray = fieldsData;
});
}).then(() => {
return this.setEntryFromOffset().then(() => {
return this.dataProvider.getDatabaseAccessInformation(this.data.id);
});
}).then((accessData) => {
this.access = accessData;
@ -153,20 +158,11 @@ export class AddonModDataEntryPage implements OnDestroy {
}
});
}).then(() => {
return this.dataProvider.getFields(this.data.id).then((fieldsData) => {
this.fields = this.utils.arrayToObject(fieldsData, 'id');
this.fieldsArray = fieldsData;
return this.dataHelper.fetchEntry(this.data, fieldsData, this.entryId);
});
}).then((entry) => {
this.entry = entry.entry;
this.ratingInfo = entry.ratinginfo;
const actions = this.dataHelper.getActions(this.data, this.access, this.entry);
const templte = this.data.singletemplate || this.dataHelper.getDefaultTemplate('single', this.fieldsArray);
this.entryHtml = this.dataHelper.displayShowFields(templte, this.fieldsArray, this.entry, this.offset, 'show', actions);
const template = this.data.singletemplate || this.dataHelper.getDefaultTemplate('single', this.fieldsArray);
this.entryHtml = this.dataHelper.displayShowFields(template, this.fieldsArray, this.entry, this.offset, 'show',
actions);
this.showComments = actions.comments;
const entries = {};
@ -176,7 +172,9 @@ export class AddonModDataEntryPage implements OnDestroy {
this.jsData = {
fields: this.fields,
entries: entries,
data: this.data
data: this.data,
module: this.module,
group: this.selectedGroup
};
}).catch((message) => {
if (!refresh) {
@ -249,7 +247,7 @@ export class AddonModDataEntryPage implements OnDestroy {
*/
setGroup(groupId: number): Promise<any> {
this.selectedGroup = groupId;
this.offset = 0;
this.offset = null;
this.entry = null;
this.entryId = null;
this.entryLoaded = false;
@ -258,46 +256,73 @@ export class AddonModDataEntryPage implements OnDestroy {
}
/**
* Convenience function to translate offset to entry identifier and set next/previous entries.
* Convenience function to fetch the entry and set next/previous entries.
*
* @param {number} dataId Data Id.
* @param {number} [offset] Offset of the entry.
* @param {number} [groupId] Group Id to get the entry.
* @return {Promise<any>} Resolved when done.
*/
protected setEntryIdFromOffset(dataId: number, offset?: number, groupId?: number): Promise<any> {
if (typeof offset != 'number') {
protected setEntryFromOffset(): Promise<any> {
const emptyOffset = typeof this.offset != 'number';
if (emptyOffset && typeof this.entryId == 'number') {
// Entry id passed as navigation parameter instead of the offset.
// We don't display next/previous buttons in this case.
this.nextOffset = null;
this.previousOffset = null;
return Promise.resolve();
return this.dataHelper.fetchEntry(this.data, this.fieldsArray, this.entryId).then((entry) => {
this.entry = entry.entry;
this.ratingInfo = entry.ratinginfo;
});
}
const perPage = AddonModDataProvider.PER_PAGE;
const page = Math.floor(offset / perPage);
const pageOffset = offset % perPage;
const page = !emptyOffset && this.offset >= 0 ? Math.floor(this.offset / perPage) : 0;
return this.dataProvider.getEntries(dataId, groupId, undefined, undefined, page, perPage).then((entries) => {
if (!entries || !entries.entries || !entries.entries.length || pageOffset >= entries.entries.length) {
return Promise.reject(null);
return this.dataHelper.fetchEntries(this.data, this.fieldsArray, this.selectedGroup, undefined, undefined, '0', 'DESC',
page, perPage).then((entries) => {
const pageEntries = entries.offlineEntries.concat(entries.entries);
let pageIndex; // Index of the entry when concatenating offline and online page entries.
if (emptyOffset) {
// No offset passed, display the first entry.
pageIndex = 0;
} else if (this.offset > 0) {
// Online entry.
pageIndex = this.offset % perPage + entries.offlineEntries.length;
} else {
// Offline entry.
pageIndex = this.offset + entries.offlineEntries.length;
}
this.entryId = entries.entries[pageOffset].id;
this.previousOffset = offset > 0 ? offset - 1 : null;
if (pageOffset + 1 < entries.entries.length) {
this.entry = pageEntries[pageIndex];
this.entryId = this.entry.id;
this.previousOffset = page > 0 || pageIndex > 0 ? this.offset - 1 : null;
let promise;
if (pageIndex + 1 < pageEntries.length) {
// Not the last entry on the page;
this.nextOffset = offset + 1;
} else if (entries.entries.length < perPage) {
this.nextOffset = this.offset + 1;
} else if (pageEntries.length < perPage) {
// Last entry of the last page.
this.nextOffset = null;
} else {
// Last entry of the page, check if there are more pages.
return this.dataProvider.getEntries(dataId, groupId, undefined, undefined, page + 1, perPage).then((entries) => {
this.nextOffset = entries && entries.entries && entries.entries.length > 0 ? offset + 1 : null;
promise = this.dataProvider.getEntries(this.data.id, this.selectedGroup, '0', 'DESC', page + 1, perPage)
.then((entries) => {
this.nextOffset = entries && entries.entries && entries.entries.length > 0 ? this.offset + 1 : null;
});
}
return Promise.resolve(promise).then(() => {
if (this.entryId > 0) {
// Online entry, we need to fetch the the rating info.
return this.dataProvider.getEntry(this.data.id, this.entryId).then((entry) => {
this.ratingInfo = entry.ratinginfo;
});
}
});
});
}