Merge pull request #1720 from albertgasset/MOBILE-2795

MOBILE-2795 data: Use default templates as fallback
main
Juan Leyva 2019-01-10 14:54:42 +01:00 committed by GitHub
commit fee1dde1dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 82 additions and 15 deletions

View File

@ -313,13 +313,14 @@ export class AddonModDataIndexComponent extends CoreCourseModuleMainActivityComp
this.firstEntry = entries[0].id; this.firstEntry = entries[0].id;
} }
const template = this.data.listtemplate || this.dataHelper.getDefaultTemplate('list', this.fieldsArray);
entries.forEach((entry) => { entries.forEach((entry) => {
this.entries[entry.id] = entry; this.entries[entry.id] = entry;
const actions = this.dataHelper.getActions(this.data, this.access, entry); const actions = this.dataHelper.getActions(this.data, this.access, entry);
entriesHTML += this.dataHelper.displayShowFields(this.data.listtemplate, this.fieldsArray, entry, 'list', entriesHTML += this.dataHelper.displayShowFields(template, this.fieldsArray, entry, 'list', actions);
actions);
}); });
entriesHTML += this.data.listtemplatefooter || ''; entriesHTML += this.data.listtemplatefooter || '';

View File

@ -298,10 +298,6 @@ export class AddonModDataEditPage {
* @return {string} Generated HTML. * @return {string} Generated HTML.
*/ */
protected displayEditFields(): string { protected displayEditFields(): string {
if (!this.data.addtemplate) {
return '';
}
this.jsData = { this.jsData = {
fields: this.fields, fields: this.fields,
contents: this.utils.clone(this.entry.contents), contents: this.utils.clone(this.entry.contents),
@ -312,7 +308,7 @@ export class AddonModDataEditPage {
let replace, let replace,
render, render,
template = this.data.addtemplate; template = this.data.addtemplate || this.dataHelper.getDefaultTemplate('add', this.fieldsArray);
// Replace the fields found on template. // Replace the fields found on template.
this.fieldsArray.forEach((field) => { this.fieldsArray.forEach((field) => {

View File

@ -176,8 +176,8 @@ export class AddonModDataEntryPage implements OnDestroy {
const actions = this.dataHelper.getActions(this.data, this.access, this.entry); const actions = this.dataHelper.getActions(this.data, this.access, this.entry);
this.entryRendered = this.dataHelper.displayShowFields(this.data.singletemplate, fieldsArray, const templte = this.data.singletemplate || this.dataHelper.getDefaultTemplate('single', fieldsArray);
this.entry, 'show', actions); this.entryRendered = this.dataHelper.displayShowFields(templte, fieldsArray, this.entry, 'show', actions);
this.showComments = actions.comments; this.showComments = actions.comments;
const entries = {}; const entries = {};

View File

@ -20,6 +20,7 @@ import { CoreDomUtilsProvider } from '@providers/utils/dom';
import { CoreTextUtilsProvider } from '@providers/utils/text'; import { CoreTextUtilsProvider } from '@providers/utils/text';
import { AddonModDataComponentsModule } from '../../components/components.module'; import { AddonModDataComponentsModule } from '../../components/components.module';
import { AddonModDataFieldsDelegate } from '../../providers/fields-delegate'; import { AddonModDataFieldsDelegate } from '../../providers/fields-delegate';
import { AddonModDataHelperProvider } from '../../providers/helper';
/** /**
* Page that displays the search modal. * Page that displays the search modal.
@ -41,7 +42,7 @@ export class AddonModDataSearchPage {
constructor(params: NavParams, private viewCtrl: ViewController, fb: FormBuilder, protected utils: CoreUtilsProvider, constructor(params: NavParams, private viewCtrl: ViewController, fb: FormBuilder, protected utils: CoreUtilsProvider,
protected domUtils: CoreDomUtilsProvider, protected fieldsDelegate: AddonModDataFieldsDelegate, protected domUtils: CoreDomUtilsProvider, protected fieldsDelegate: AddonModDataFieldsDelegate,
protected textUtils: CoreTextUtilsProvider) { protected textUtils: CoreTextUtilsProvider, protected dataHelper: AddonModDataHelperProvider) {
this.search = params.get('search'); this.search = params.get('search');
this.fields = params.get('fields'); this.fields = params.get('fields');
this.data = params.get('data'); this.data = params.get('data');
@ -82,17 +83,13 @@ export class AddonModDataSearchPage {
* @return {string} Generated HTML. * @return {string} Generated HTML.
*/ */
protected renderAdvancedSearchFields(): string { protected renderAdvancedSearchFields(): string {
if (!this.data.asearchtemplate) {
return '';
}
this.jsData = { this.jsData = {
fields: this.fields, fields: this.fields,
form: this.searchForm, form: this.searchForm,
search: this.search.advanced search: this.search.advanced
}; };
let template = this.data.asearchtemplate, let template = this.data.asearchtemplate || this.dataHelper.getDefaultTemplate('asearch', this.fieldsArray),
replace, render; replace, render;
// Replace the fields found on template. // Replace the fields found on template.

View File

@ -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##<br />');
}
html.push(
'<div class="defaulttemplate">',
'<table class="mod-data-default-template ##approvalstatus##">',
'<tbody>'
);
fields.forEach((field) => {
html.push(
'<tr class="">',
'<td class="template-field cell c0" style="">', field.name, ': </td>',
'<td class="template-token cell c1 lastcol" style="">[[', field.name, ']]</td>',
'</tr>'
);
});
if (type == 'list') {
html.push(
'<tr class="lastrow">',
'<td class="controls template-field cell c0 lastcol" style="" colspan="2">',
'##edit## ##more## ##delete## ##approve## ##disapprove## ##export##',
'</td>',
'</tr>'
);
} else if (type == 'single') {
html.push(
'<tr class="lastrow">',
'<td class="controls template-field cell c0 lastcol" style="" colspan="2">',
'##edit## ##delete## ##approve## ##disapprove## ##export##',
'</td>',
'</tr>'
);
} else if (type == 'asearch') {
html.push(
'<tr class="searchcontrols">',
'<td class="template-field cell c0" style="">Author first name: </td>',
'<td class="template-token cell c1 lastcol" style="">##firstname##</td>',
'</tr>',
'<tr class="searchcontrols lastrow">',
'<td class="template-field cell c0" style="">Author surname: </td>',
'<td class="template-token cell c1 lastcol" style="">##lastname##</td>',
'</tr>'
);
}
html.push(
'</tbody>',
'</table>',
'</div>'
);
if (type == 'list') {
html.push('<hr />');
}
return html.join('');
}
/** /**
* Retrieve the entered data in the edit form. * Retrieve the entered data in the edit form.
* We don't use ng-model because it doesn't detect changes done by JavaScript. * We don't use ng-model because it doesn't detect changes done by JavaScript.