From 2c92758182a82f19e533b2cce75926dfc45f1907 Mon Sep 17 00:00:00 2001 From: Albert Gasset Date: Thu, 10 Jan 2019 13:38:43 +0100 Subject: [PATCH] MOBILE-2795 data: Use default templates as fallback --- src/addon/mod/data/components/index/index.ts | 5 +- src/addon/mod/data/pages/edit/edit.ts | 6 +- src/addon/mod/data/pages/entry/entry.ts | 4 +- src/addon/mod/data/pages/search/search.ts | 9 +-- src/addon/mod/data/providers/helper.ts | 73 ++++++++++++++++++++ 5 files changed, 82 insertions(+), 15 deletions(-) diff --git a/src/addon/mod/data/components/index/index.ts b/src/addon/mod/data/components/index/index.ts index e67b34d2e..e3863a39b 100644 --- a/src/addon/mod/data/components/index/index.ts +++ b/src/addon/mod/data/components/index/index.ts @@ -313,13 +313,14 @@ export class AddonModDataIndexComponent extends CoreCourseModuleMainActivityComp this.firstEntry = entries[0].id; } + const template = this.data.listtemplate || this.dataHelper.getDefaultTemplate('list', this.fieldsArray); + entries.forEach((entry) => { this.entries[entry.id] = entry; const actions = this.dataHelper.getActions(this.data, this.access, entry); - entriesHTML += this.dataHelper.displayShowFields(this.data.listtemplate, this.fieldsArray, entry, 'list', - actions); + entriesHTML += this.dataHelper.displayShowFields(template, this.fieldsArray, entry, 'list', actions); }); entriesHTML += this.data.listtemplatefooter || ''; diff --git a/src/addon/mod/data/pages/edit/edit.ts b/src/addon/mod/data/pages/edit/edit.ts index add927dc3..5520c8e2c 100644 --- a/src/addon/mod/data/pages/edit/edit.ts +++ b/src/addon/mod/data/pages/edit/edit.ts @@ -298,10 +298,6 @@ export class AddonModDataEditPage { * @return {string} Generated HTML. */ protected displayEditFields(): string { - if (!this.data.addtemplate) { - return ''; - } - this.jsData = { fields: this.fields, contents: this.utils.clone(this.entry.contents), @@ -312,7 +308,7 @@ export class AddonModDataEditPage { let replace, render, - template = this.data.addtemplate; + template = this.data.addtemplate || this.dataHelper.getDefaultTemplate('add', this.fieldsArray); // Replace the fields found on template. this.fieldsArray.forEach((field) => { diff --git a/src/addon/mod/data/pages/entry/entry.ts b/src/addon/mod/data/pages/entry/entry.ts index 1b2b721eb..8ea967f72 100644 --- a/src/addon/mod/data/pages/entry/entry.ts +++ b/src/addon/mod/data/pages/entry/entry.ts @@ -176,8 +176,8 @@ export class AddonModDataEntryPage implements OnDestroy { const actions = this.dataHelper.getActions(this.data, this.access, this.entry); - this.entryRendered = this.dataHelper.displayShowFields(this.data.singletemplate, fieldsArray, - this.entry, 'show', actions); + const templte = this.data.singletemplate || this.dataHelper.getDefaultTemplate('single', fieldsArray); + this.entryRendered = this.dataHelper.displayShowFields(templte, fieldsArray, this.entry, 'show', actions); this.showComments = actions.comments; const entries = {}; diff --git a/src/addon/mod/data/pages/search/search.ts b/src/addon/mod/data/pages/search/search.ts index 8bd04314b..721c70ecd 100644 --- a/src/addon/mod/data/pages/search/search.ts +++ b/src/addon/mod/data/pages/search/search.ts @@ -20,6 +20,7 @@ import { CoreDomUtilsProvider } from '@providers/utils/dom'; import { CoreTextUtilsProvider } from '@providers/utils/text'; import { AddonModDataComponentsModule } from '../../components/components.module'; import { AddonModDataFieldsDelegate } from '../../providers/fields-delegate'; +import { AddonModDataHelperProvider } from '../../providers/helper'; /** * Page that displays the search modal. @@ -41,7 +42,7 @@ export class AddonModDataSearchPage { constructor(params: NavParams, private viewCtrl: ViewController, fb: FormBuilder, protected utils: CoreUtilsProvider, protected domUtils: CoreDomUtilsProvider, protected fieldsDelegate: AddonModDataFieldsDelegate, - protected textUtils: CoreTextUtilsProvider) { + protected textUtils: CoreTextUtilsProvider, protected dataHelper: AddonModDataHelperProvider) { this.search = params.get('search'); this.fields = params.get('fields'); this.data = params.get('data'); @@ -82,17 +83,13 @@ export class AddonModDataSearchPage { * @return {string} Generated HTML. */ protected renderAdvancedSearchFields(): string { - if (!this.data.asearchtemplate) { - return ''; - } - this.jsData = { fields: this.fields, form: this.searchForm, search: this.search.advanced }; - let template = this.data.asearchtemplate, + let template = this.data.asearchtemplate || this.dataHelper.getDefaultTemplate('asearch', this.fieldsArray), replace, render; // Replace the fields found on template. diff --git a/src/addon/mod/data/providers/helper.ts b/src/addon/mod/data/providers/helper.ts index 3a592ccfe..97f6ac5e2 100644 --- a/src/addon/mod/data/providers/helper.ts +++ b/src/addon/mod/data/providers/helper.ts @@ -195,6 +195,79 @@ export class AddonModDataHelperProvider { }); } + /** + * Returns the default template of a certain type. + * + * Based on Moodle function data_generate_default_template. + * + * @param {string} type Type of template. + * @param {any[]} fields List of database fields. + * @return {string} Template HTML. + */ + getDefaultTemplate( type: 'add' | 'list' | 'single' | 'asearch', fields: any[]): string { + const html = []; + + if (type == 'list') { + html.push('##delcheck##
'); + } + + html.push( + '
', + '', + '' + ); + + fields.forEach((field) => { + html.push( + '', + '', + '', + '' + ); + }); + + if (type == 'list') { + html.push( + '', + '', + '' + ); + } else if (type == 'single') { + html.push( + '', + '', + '' + ); + } else if (type == 'asearch') { + html.push( + '', + '', + '', + '', + '', + '', + '', + '' + ); + } + + html.push( + '', + '
', field.name, ': [[', field.name, ']]
', + '##edit## ##more## ##delete## ##approve## ##disapprove## ##export##', + '
', + '##edit## ##delete## ##approve## ##disapprove## ##export##', + '
Author first name: ##firstname##
Author surname: ##lastname##
', + '
' + ); + + if (type == 'list') { + html.push('
'); + } + + return html.join(''); + } + /** * Retrieve the entered data in the edit form. * We don't use ng-model because it doesn't detect changes done by JavaScript.