Commit 91943b0b authored by KR Moorhouse's avatar KR Moorhouse Committed by GitHub

Merge pull request #3087 from wpninjas/issue#3026

Add field merge tag tracking to HTML fields. 
parents 20a8359f 38477db9
define([], function() {
var controller = Marionette.Object.extend({
htmlFields: [],
trackedMergeTags: [],
initialize: function () {
this.listenTo( Backbone.Radio.channel( 'fields-html' ), 'init:model', this.setupFieldMergeTagTracking );
},
setupFieldMergeTagTracking: function( fieldModel ) {
this.htmlFields.push( fieldModel );
var formID = fieldModel.get( 'formID' );
this.listenTo( nfRadio.channel( 'form-' + formID ), 'init:model', function( formModel ){
var mergeTags = fieldModel.get( 'default' ).match( new RegExp( /{field:(.*?)}/g ) );
if ( ! mergeTags ) return;
_.each( mergeTags, function( mergeTag ) {
var fieldKey = mergeTag.replace( '{field:', '' ).replace( '}', '' );
var fieldModel = formModel.get( 'fields' ).findWhere({ key: fieldKey });
if( 'undefined' == typeof fieldModel ) return;
this.trackedMergeTags.push( fieldModel );
this.listenTo( nfRadio.channel( 'field-' + fieldModel.get( 'id' ) ), 'change:modelValue', this.updateFieldMergeTags );
}, this );
// Let's get this party started!
this.updateFieldMergeTags();
}, this );
},
updateFieldMergeTags: function( fieldModel ) {
_.each( this.htmlFields, function( htmlFieldModel ){
var value = htmlFieldModel.get( 'default' );
_.each( this.trackedMergeTags, function( fieldModel ){
var mergeTag = '{field:' + fieldModel.get( 'key' ) + '}';
value = value.replace( mergeTag, fieldModel.get( 'value' ) );
}, this ) ;
htmlFieldModel.set( 'value', value );
htmlFieldModel.trigger( 'reRender' );
}, this );
}
});
return controller;
});
......@@ -26,6 +26,7 @@ define(
'controllers/calculations',
'controllers/fieldDate',
'controllers/fieldRecaptcha',
'controllers/fieldHTML',
'controllers/helpText',
'controllers/fieldTextareaRTE',
'controllers/fieldStarRating',
......@@ -64,6 +65,7 @@ define(
Calculations,
FieldDate,
FieldRecaptcha,
FieldHTML,
HelpText,
FieldTextareaRTE,
FieldStarRating,
......@@ -97,6 +99,7 @@ define(
new FieldTotal();
new FieldQuantity();
new FieldRecaptcha();
new FieldHTML();
new HelpText();
new FieldTextareaRTE();
new FieldStarRating();
......
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment