MOBILE-2856 data: Fix single entry view for offline entries
parent
fccf79bbd2
commit
cd77fb2f4e
|
@ -135,8 +135,13 @@ export class AddonModDataEntryPage implements OnDestroy {
|
||||||
this.title = data.name || this.title;
|
this.title = data.name || this.title;
|
||||||
this.data = data;
|
this.data = data;
|
||||||
|
|
||||||
return this.setEntryIdFromOffset(data.id, this.offset, this.selectedGroup).then(() => {
|
return this.dataProvider.getFields(this.data.id).then((fieldsData) => {
|
||||||
return this.dataProvider.getDatabaseAccessInformation(data.id);
|
this.fields = this.utils.arrayToObject(fieldsData, 'id');
|
||||||
|
this.fieldsArray = fieldsData;
|
||||||
|
});
|
||||||
|
}).then(() => {
|
||||||
|
return this.setEntryFromOffset().then(() => {
|
||||||
|
return this.dataProvider.getDatabaseAccessInformation(this.data.id);
|
||||||
});
|
});
|
||||||
}).then((accessData) => {
|
}).then((accessData) => {
|
||||||
this.access = accessData;
|
this.access = accessData;
|
||||||
|
@ -153,20 +158,11 @@ export class AddonModDataEntryPage implements OnDestroy {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}).then(() => {
|
}).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 actions = this.dataHelper.getActions(this.data, this.access, this.entry);
|
||||||
|
|
||||||
const templte = this.data.singletemplate || this.dataHelper.getDefaultTemplate('single', this.fieldsArray);
|
const template = this.data.singletemplate || this.dataHelper.getDefaultTemplate('single', this.fieldsArray);
|
||||||
this.entryHtml = this.dataHelper.displayShowFields(templte, this.fieldsArray, this.entry, this.offset, 'show', actions);
|
this.entryHtml = this.dataHelper.displayShowFields(template, this.fieldsArray, this.entry, this.offset, 'show',
|
||||||
|
actions);
|
||||||
this.showComments = actions.comments;
|
this.showComments = actions.comments;
|
||||||
|
|
||||||
const entries = {};
|
const entries = {};
|
||||||
|
@ -176,7 +172,9 @@ export class AddonModDataEntryPage implements OnDestroy {
|
||||||
this.jsData = {
|
this.jsData = {
|
||||||
fields: this.fields,
|
fields: this.fields,
|
||||||
entries: entries,
|
entries: entries,
|
||||||
data: this.data
|
data: this.data,
|
||||||
|
module: this.module,
|
||||||
|
group: this.selectedGroup
|
||||||
};
|
};
|
||||||
}).catch((message) => {
|
}).catch((message) => {
|
||||||
if (!refresh) {
|
if (!refresh) {
|
||||||
|
@ -249,7 +247,7 @@ export class AddonModDataEntryPage implements OnDestroy {
|
||||||
*/
|
*/
|
||||||
setGroup(groupId: number): Promise<any> {
|
setGroup(groupId: number): Promise<any> {
|
||||||
this.selectedGroup = groupId;
|
this.selectedGroup = groupId;
|
||||||
this.offset = 0;
|
this.offset = null;
|
||||||
this.entry = null;
|
this.entry = null;
|
||||||
this.entryId = null;
|
this.entryId = null;
|
||||||
this.entryLoaded = false;
|
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.
|
* @return {Promise<any>} Resolved when done.
|
||||||
*/
|
*/
|
||||||
protected setEntryIdFromOffset(dataId: number, offset?: number, groupId?: number): Promise<any> {
|
protected setEntryFromOffset(): Promise<any> {
|
||||||
if (typeof offset != 'number') {
|
const emptyOffset = typeof this.offset != 'number';
|
||||||
|
|
||||||
|
if (emptyOffset && typeof this.entryId == 'number') {
|
||||||
// Entry id passed as navigation parameter instead of the offset.
|
// Entry id passed as navigation parameter instead of the offset.
|
||||||
// We don't display next/previous buttons in this case.
|
// We don't display next/previous buttons in this case.
|
||||||
this.nextOffset = null;
|
this.nextOffset = null;
|
||||||
this.previousOffset = 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 perPage = AddonModDataProvider.PER_PAGE;
|
||||||
const page = Math.floor(offset / perPage);
|
const page = !emptyOffset && this.offset >= 0 ? Math.floor(this.offset / perPage) : 0;
|
||||||
const pageOffset = offset % perPage;
|
|
||||||
|
|
||||||
return this.dataProvider.getEntries(dataId, groupId, undefined, undefined, page, perPage).then((entries) => {
|
return this.dataHelper.fetchEntries(this.data, this.fieldsArray, this.selectedGroup, undefined, undefined, '0', 'DESC',
|
||||||
if (!entries || !entries.entries || !entries.entries.length || pageOffset >= entries.entries.length) {
|
page, perPage).then((entries) => {
|
||||||
return Promise.reject(null);
|
|
||||||
|
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.entry = pageEntries[pageIndex];
|
||||||
this.previousOffset = offset > 0 ? offset - 1 : null;
|
this.entryId = this.entry.id;
|
||||||
if (pageOffset + 1 < entries.entries.length) {
|
|
||||||
|
this.previousOffset = page > 0 || pageIndex > 0 ? this.offset - 1 : null;
|
||||||
|
|
||||||
|
let promise;
|
||||||
|
|
||||||
|
if (pageIndex + 1 < pageEntries.length) {
|
||||||
// Not the last entry on the page;
|
// Not the last entry on the page;
|
||||||
this.nextOffset = offset + 1;
|
this.nextOffset = this.offset + 1;
|
||||||
} else if (entries.entries.length < perPage) {
|
} else if (pageEntries.length < perPage) {
|
||||||
// Last entry of the last page.
|
// Last entry of the last page.
|
||||||
this.nextOffset = null;
|
this.nextOffset = null;
|
||||||
} else {
|
} else {
|
||||||
// Last entry of the page, check if there are more pages.
|
// Last entry of the page, check if there are more pages.
|
||||||
return this.dataProvider.getEntries(dataId, groupId, undefined, undefined, page + 1, perPage).then((entries) => {
|
promise = this.dataProvider.getEntries(this.data.id, this.selectedGroup, '0', 'DESC', page + 1, perPage)
|
||||||
this.nextOffset = entries && entries.entries && entries.entries.length > 0 ? offset + 1 : null;
|
.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;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue