From 5958feb1c31d0a791079a384f421c443dbd7c6b8 Mon Sep 17 00:00:00 2001 From: nathangray Date: Tue, 30 Jun 2020 10:43:19 -0600 Subject: [PATCH] Etemplate: add customfield prefix as an explicit attribute, importexport filter was using it --- .../etemplate/et2_extension_customfields.js | 38 +++++++++++-------- .../etemplate/et2_extension_customfields.ts | 38 +++++++++++-------- 2 files changed, 46 insertions(+), 30 deletions(-) diff --git a/api/js/etemplate/et2_extension_customfields.js b/api/js/etemplate/et2_extension_customfields.js index f65c60addc..cc19eb0c27 100644 --- a/api/js/etemplate/et2_extension_customfields.js +++ b/api/js/etemplate/et2_extension_customfields.js @@ -135,10 +135,10 @@ var et2_customfields_list = /** @class */ (function (_super) { if (this.getType() != 'customfields-list' && !jQuery.isEmptyObject(this.options.fields) && !this.options.fields[field_name]) continue; var field = this.options.customfields[field_name]; - var id = et2_customfields_list.PREFIX + field_name; + var id = this.options.prefix + field_name; // Need curlies around ID for nm row expansion if (this.id == '$row') { - id = "{" + this.id + "}" + "[" + et2_customfields_list.PREFIX + field_name + "]"; + id = "{" + this.id + "}" + "[" + this.options.prefix + field_name + "]"; } else if (this.id != et2_customfields_list.DEFAULT_ID) { // Prefix this ID to avoid potential ID collisions @@ -159,7 +159,7 @@ var et2_customfields_list = /** @class */ (function (_super) { 'statustext': field.help, 'needed': field.needed, 'readonly': this.getArrayMgr("readonlys").isReadOnly(id, null, this.options.readonly), - 'value': this.options.value[et2_customfields_list.PREFIX + field_name] + 'value': this.options.value[this.options.prefix + field_name] }); // Can't have a required readonly, it will warn & be removed later, so avoid the warning if (attrs.readonly === true) @@ -243,14 +243,15 @@ var et2_customfields_list = /** @class */ (function (_super) { if (contentMgr != null) { var val = contentMgr.getEntry(this.id); _attrs["value"] = {}; + var prefix = _attrs["prefix"] || et2_customfields_list.PREFIX; if (val !== null) { - if (this.id.indexOf(et2_customfields_list.PREFIX) === 0 && typeof data.fields != 'undefined' && data.fields[this.id.replace(et2_customfields_list.PREFIX, '')] === true) { + if (this.id.indexOf(prefix) === 0 && typeof data.fields != 'undefined' && data.fields[this.id.replace(prefix, '')] === true) { _attrs['value'][this.id] = val; } else { // Only set the values that match desired custom fields for (var key_3 in val) { - if (key_3.indexOf(et2_customfields_list.PREFIX) == 0) { + if (key_3.indexOf(prefix) === 0) { _attrs["value"][key_3] = val[key_3]; } } @@ -260,7 +261,7 @@ var et2_customfields_list = /** @class */ (function (_super) { else { // Check for custom fields directly in record for (var key in _attrs.customfields) { - _attrs["value"][et2_customfields_list.PREFIX + key] = contentMgr.getEntry(et2_customfields_list.PREFIX + key); + _attrs["value"][prefix + key] = contentMgr.getEntry(prefix + key); } } } @@ -281,10 +282,10 @@ var et2_customfields_list = /** @class */ (function (_super) { // Make sure widget is created, and has the needed function if (!this.widgets[field_name] || !this.widgets[field_name].set_value) continue; - var value = _value[et2_customfields_list.PREFIX + field_name] ? _value[et2_customfields_list.PREFIX + field_name] : null; + var value = _value[this.options.prefix + field_name] ? _value[et2_customfields_list.PREFIX + field_name] : null; // Check if ID was missing - if (value == null && this.id == et2_customfields_list.DEFAULT_ID && this.getArrayMgr("content").getEntry(et2_customfields_list.PREFIX + field_name)) { - value = this.getArrayMgr("content").getEntry(et2_customfields_list.PREFIX + field_name); + if (value == null && this.id == et2_customfields_list.DEFAULT_ID && this.getArrayMgr("content").getEntry(this.options.prefix + field_name)) { + value = this.getArrayMgr("content").getEntry(this.options.prefix + field_name); } switch (this.options.customfields[field_name].type) { case 'date': @@ -310,7 +311,7 @@ var et2_customfields_list = /** @class */ (function (_super) { var value = {}; for (var field_name in this.widgets) { if (this.widgets[field_name].getValue && !this.widgets[field_name].options.readonly) { - value[et2_customfields_list.PREFIX + field_name] = this.widgets[field_name].getValue(); + value[this.options.prefix + field_name] = this.widgets[field_name].getValue(); } } return value; @@ -413,7 +414,7 @@ var et2_customfields_list = /** @class */ (function (_super) { attrs.config.height = (((field.rows > 0 && field.rows != 'undefined') ? field.rows : 5) * 16) + 'px'; // We have to push the config modifications into the modifications array, or they'll // be overwritten by the site config from the server - var data = this.getArrayMgr("modifications").getEntry(et2_customfields_list.PREFIX + field_name); + var data = this.getArrayMgr("modifications").getEntry(this.options.prefix + field_name); if (data) jQuery.extend(data.config, attrs.config); return true; @@ -540,12 +541,12 @@ var et2_customfields_list = /** @class */ (function (_super) { */ et2_customfields_list.prototype.set_visible = function (_fields) { for (var name_1 in _fields) { - if (this.rows[et2_customfields_list.PREFIX + name_1]) { + if (this.rows[this.options.prefix + name_1]) { if (_fields[name_1]) { - jQuery(this.rows[et2_customfields_list.PREFIX + name_1]).show(); + jQuery(this.rows[this.options.prefix + name_1]).show(); } else { - jQuery(this.rows[et2_customfields_list.PREFIX + name_1]).hide(); + jQuery(this.rows[this.options.prefix + name_1]).hide(); } } this.options.fields[name_1] = _fields[name_1]; @@ -566,7 +567,7 @@ var et2_customfields_list = /** @class */ (function (_super) { for (var i = 0; i < _nodes.length; i++) { // toggle() needs a boolean to do what we want var key = _nodes[i].getAttribute('data-field'); - jQuery(_nodes[i]).toggle(_values.fields[key] && _values.value[et2_customfields_list.PREFIX + key] ? true : false); + jQuery(_nodes[i]).toggle(_values.fields[key] && _values.value[this.options.prefix + key] ? true : false); } }; et2_customfields_list._attributes = { @@ -606,6 +607,13 @@ var et2_customfields_list = /** @class */ (function (_super) { "type": "string", "default": et2_no_init, "description": "JS code which is executed when the value changes." + }, + // Allow changing the field prefix. Normally it's the constant but importexport filter changes it. + "prefix": { + name: "prefix", + type: "string", + default: et2_no_init, + description: "Custom prefix for custom fields. Default #" } }; et2_customfields_list.legacyOptions = ["type_filter", "private", "fields"]; // Field restriction & private done server-side diff --git a/api/js/etemplate/et2_extension_customfields.ts b/api/js/etemplate/et2_extension_customfields.ts index a5a1bd4d93..6072ce8e6d 100644 --- a/api/js/etemplate/et2_extension_customfields.ts +++ b/api/js/etemplate/et2_extension_customfields.ts @@ -61,6 +61,13 @@ export class et2_customfields_list extends et2_valueWidget implements et2_IDetac "type": "string", "default": et2_no_init, "description": "JS code which is executed when the value changes." + }, + // Allow changing the field prefix. Normally it's the constant but importexport filter changes it. + "prefix": { + name: "prefix", + type: "string", + default: et2_no_init, + description: "Custom prefix for custom fields. Default #" } }; @@ -200,12 +207,12 @@ export class et2_customfields_list extends et2_valueWidget implements et2_IDetac const field = this.options.customfields[field_name]; - let id = et2_customfields_list.PREFIX + field_name; + let id = this.options.prefix + field_name; // Need curlies around ID for nm row expansion if(this.id == '$row') { - id = "{" + this.id + "}" + "["+et2_customfields_list.PREFIX + field_name+"]"; + id = "{" + this.id + "}" + "["+this.options.prefix + field_name+"]"; } else if (this.id != et2_customfields_list.DEFAULT_ID) { @@ -230,7 +237,7 @@ export class et2_customfields_list extends et2_valueWidget implements et2_IDetac 'statustext': field.help, 'needed': field.needed, 'readonly': this.getArrayMgr("readonlys").isReadOnly(id, null, this.options.readonly), - 'value': this.options.value[et2_customfields_list.PREFIX + field_name] + 'value': this.options.value[this.options.prefix + field_name] }); // Can't have a required readonly, it will warn & be removed later, so avoid the warning if(attrs.readonly === true) delete attrs.needed; @@ -335,9 +342,10 @@ export class et2_customfields_list extends et2_valueWidget implements et2_IDetac if (contentMgr != null) { const val = contentMgr.getEntry(this.id); _attrs["value"] = {}; + let prefix = _attrs["prefix"] || et2_customfields_list.PREFIX; if (val !== null) { - if(this.id.indexOf(et2_customfields_list.PREFIX) === 0 && typeof data.fields != 'undefined' && data.fields[this.id.replace(et2_customfields_list.PREFIX,'')] === true) + if(this.id.indexOf(prefix) === 0 && typeof data.fields != 'undefined' && data.fields[this.id.replace(prefix,'')] === true) { _attrs['value'][this.id] = val; } @@ -346,7 +354,7 @@ export class et2_customfields_list extends et2_valueWidget implements et2_IDetac // Only set the values that match desired custom fields for(let key in val) { - if(key.indexOf(et2_customfields_list.PREFIX) == 0) { + if(key.indexOf(prefix) === 0) { _attrs["value"][key] = val[key]; } } @@ -358,7 +366,7 @@ export class et2_customfields_list extends et2_valueWidget implements et2_IDetac // Check for custom fields directly in record for(var key in _attrs.customfields) { - _attrs["value"][et2_customfields_list.PREFIX + key] = contentMgr.getEntry(et2_customfields_list.PREFIX + key); + _attrs["value"][prefix + key] = contentMgr.getEntry(prefix + key); } } } @@ -383,12 +391,12 @@ export class et2_customfields_list extends et2_valueWidget implements et2_IDetac // Make sure widget is created, and has the needed function if(!this.widgets[field_name] || !this.widgets[field_name].set_value) continue; - let value = _value[et2_customfields_list.PREFIX + field_name] ? _value[et2_customfields_list.PREFIX + field_name] : null; + let value = _value[this.options.prefix + field_name] ? _value[et2_customfields_list.PREFIX + field_name] : null; // Check if ID was missing - if(value == null && this.id == et2_customfields_list.DEFAULT_ID && this.getArrayMgr("content").getEntry(et2_customfields_list.PREFIX + field_name)) + if(value == null && this.id == et2_customfields_list.DEFAULT_ID && this.getArrayMgr("content").getEntry(this.options.prefix + field_name)) { - value = this.getArrayMgr("content").getEntry(et2_customfields_list.PREFIX + field_name); + value = this.getArrayMgr("content").getEntry(this.options.prefix + field_name); } switch(this.options.customfields[field_name].type) @@ -422,7 +430,7 @@ export class et2_customfields_list extends et2_valueWidget implements et2_IDetac { if(this.widgets[field_name].getValue && !this.widgets[field_name].options.readonly) { - value[et2_customfields_list.PREFIX + field_name] = this.widgets[field_name].getValue(); + value[this.options.prefix + field_name] = this.widgets[field_name].getValue(); } } return value; @@ -559,7 +567,7 @@ export class et2_customfields_list extends et2_valueWidget implements et2_IDetac // We have to push the config modifications into the modifications array, or they'll // be overwritten by the site config from the server - const data = this.getArrayMgr("modifications").getEntry(et2_customfields_list.PREFIX + field_name); + const data = this.getArrayMgr("modifications").getEntry(this.options.prefix + field_name); if(data) jQuery.extend(data.config, attrs.config); return true; @@ -724,15 +732,15 @@ export class et2_customfields_list extends et2_valueWidget implements et2_IDetac { for(let name in _fields) { - if(this.rows[et2_customfields_list.PREFIX + name]) + if(this.rows[this.options.prefix + name]) { if(_fields[name]) { - jQuery(this.rows[et2_customfields_list.PREFIX+name]).show(); + jQuery(this.rows[this.options.prefix+name]).show(); } else { - jQuery(this.rows[et2_customfields_list.PREFIX+name]).hide(); + jQuery(this.rows[this.options.prefix+name]).hide(); } } this.options.fields[name] = _fields[name]; @@ -761,7 +769,7 @@ export class et2_customfields_list extends et2_valueWidget implements et2_IDetac { // toggle() needs a boolean to do what we want const key = _nodes[i].getAttribute('data-field'); - jQuery(_nodes[i]).toggle(_values.fields[key] && _values.value[et2_customfields_list.PREFIX + key]?true:false); + jQuery(_nodes[i]).toggle(_values.fields[key] && _values.value[this.options.prefix + key]?true:false); } } }