Skip to content

DIVE-131 Add Gutenberg support for dynamic fields #717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
3 changes: 2 additions & 1 deletion credits.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Contributors:
* Matthew Johnson, Co-founder, Alley Interactive LLC
* Will Gladstone, Alley Interactive LLC
* Daniel Bachhuber, Digital First Media
* Kevin Fodness, Alley Interactive LLC

Open Source components consulted or used:

Expand All @@ -30,4 +31,4 @@ Open Source components consulted or used:
Developed at Twitter by @mdo and @fat
* [4/8/13] Consulted Simon Wheatley's demo plugin to support using the
term_order field to sort core taxonomies.
(http://simonwheatley.co.uk/2012/07/ordering-terms-in-wordpress-taxonomies/)
(http://simonwheatley.co.uk/2012/07/ordering-terms-in-wordpress-taxonomies/)
8 changes: 7 additions & 1 deletion js/fieldmanager-autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ fm.autocomplete = {
},

enable_autocomplete: function() {
$( 'input.fm-autocomplete:visible' ).each( function() {
$( 'input.fm-autocomplete' ).each( function() {
// Do not apply to a field in a prototype block.
if ( fm.is_proto_field( this ) ) {
return;
}

// Do not apply if the autocomplete has already been bound.
if ( !$( this ).hasClass( 'fm-autocomplete-enabled' ) ) {
var ac_params = {};
var $el = $( this );
Expand Down
9 changes: 8 additions & 1 deletion js/fieldmanager-colorpicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@

fm.colorpicker = {
init: function() {
$( '.fm-colorpicker-popup:visible' ).wpColorPicker();
$( '.fm-colorpicker-popup' ).each( function() {
// Do not apply to a field in a prototype block.
if ( fm.is_proto_field( this ) ) {
return;
}

$( this ).wpColorPicker();
} );
}
}

Expand Down
10 changes: 8 additions & 2 deletions js/fieldmanager-datepicker.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
( function( $ ) {
fm.datepicker = {
add_datepicker: function() {
$( '.fm-datepicker-popup:visible' ).each( function() {
$( '.fm-datepicker-popup' ).each( function() {
// Do not apply to a field in a prototype block.
if ( fm.is_proto_field( this ) ) {
return;
}

// Do not apply if the datepicker has already been bound.
if ( !$( this ).hasClass( 'fm-has-date-picker' ) ) {
var opts = $( this ).data( 'datepicker-opts' );
$( this ).datepicker( opts ).addClass( 'fm-has-date-picker' );
}
} );
}
}
};

$( document ).ready( fm.datepicker.add_datepicker );
$( document ).on( 'fm_collapsible_toggle fm_added_element fm_displayif_toggle fm_activate_tab', fm.datepicker.add_datepicker );
Expand Down
38 changes: 38 additions & 0 deletions js/fieldmanager.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
var fm = {};

/**
* Polyfill Element.matches, if necessary.
* @see https://developer.mozilla.org/en-US/docs/Web/API/Element/closest#Polyfill
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The link should be https://developer.mozilla.org/en-US/docs/Web/API/Element/matches#Polyfill

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This specific matches polyfill was taken from the closest polyfill page, which is slightly different from the one present on the matches polyfill page you linked above. I'm only including it because closest depends on it, so I'm pulling both polyfills from the same location. Good catch, though!

*/
if ( ! Element.prototype.matches ) {
Element.prototype.matches = Element.prototype.msMatchesSelector ||
Element.prototype.webkitMatchesSelector;
}

/**
* Polyfill Element.closest, if necessary.
* @see https://developer.mozilla.org/en-US/docs/Web/API/Element/closest#Polyfill
*/
if ( ! Element.prototype.closest ) {
Element.prototype.closest = function ( s ) {
var el = this;
if ( ! document.documentElement.contains( el ) ) {
return null;
}
do {
if ( el.matches( s ) ) {
return el;
}
el = el.parentElement || el.parentNode;
} while ( el !== null && el.nodeType === 1 );
return null;
};
}

( function( $ ) {

var dynamic_seq = 0;
Expand Down Expand Up @@ -272,6 +301,15 @@ $( document ).ready( function () {
};
$( document ).on( 'change', '.display-trigger', fm.trigger_display_if );

/**
* A helper function to determine if a node is a field in a proto block.
* @param {Element} elem - The field element to check.
* @returns {boolean} - True if part of a proto block, false if not.
*/
fm.is_proto_field = function ( elem ) {
return ( null !== elem.closest( '.fmjs-proto' ) );
};

init_label_macros();
init_sortable();

Expand Down
18 changes: 13 additions & 5 deletions js/grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ $.fm_grid_init = function() {

$.fn.fm_grid_serialize = function() {
var rows = [], row_counter = 0, self = this, bottom_row_with_data = 0;
if ( this.find( 'tbody:visible' ).length === 0 ) {
return;
}
this.find( 'tbody tr:visible' ).each( function() {
this.find( 'tbody tr' ).each( function() {

// Do not apply to a field in a prototype block.
if ( fm.is_proto_field( this ) ) {
return;
}

var row = [];
$( this ).find( 'td' ).each( function() {
var to_save = {
Expand Down Expand Up @@ -63,7 +66,12 @@ $.fn.fm_grid = function( opts ) {
}
}
hot.setDataAtCell( data_to_set );
this.find( 'tbody tr:visible' ).each( function() {
this.find( 'tbody tr' ).each( function() {
// Do not apply to a field in a prototype block.
if ( fm.is_proto_field( this ) ) {
return;
}

var base_name = self.data( 'fm-grid-name' );
var col = 0;
if ( !rows || typeof( rows[row] ) === 'undefined' ) return false;
Expand Down
7 changes: 6 additions & 1 deletion js/richtext.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
( function( $ ) {
fm.richtextarea = {
add_rte_to_visible_textareas: function() {
$( 'textarea.fm-richtext:visible' ).each( function() {
$( 'textarea.fm-richtext' ).each( function() {
// Do not apply to a field in a prototype block.
if ( fm.is_proto_field( this ) ) {
return;
}

if ( ! $( this ).hasClass( 'fm-tinymce' ) ) {
var init, ed_id, mce_options, qt_options, proto_id;
$( this ).addClass( 'fm-tinymce' );
Expand Down
Loading