Skip to content

Commit

Permalink
Paragraphs Actions JS for closing the toggle.
Browse files Browse the repository at this point in the history
  • Loading branch information
laryn committed Jun 25, 2024
1 parent 21527f5 commit 92c4806
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 29 deletions.
1 change: 1 addition & 0 deletions css/paragraphs.css
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ html.dialog-open {
border-radius: 4px;
border: 2px solid #ccc;
margin-top: 5px;
box-shadow: 0 0 4px #ddd;
}
.paragraphs-actions-more .details-child-wrapper:before {
content: "";
Expand Down
46 changes: 46 additions & 0 deletions js/paragraphs_actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @file
* Paragraphs actions JS code for paragraphs actions button.
*/

(function ($, Backdrop) {

'use strict';

/**
* Process paragraph_actions elements.
*
* @type {Backdrop~behavior}
*
* @prop {Backdrop~behaviorAttach} attach
* Attaches paragraphsActions behaviors.
*/
Backdrop.behaviors.paragraphsActions = {
attach: function (context, settings) {
var $actionsElement = $('.paragraphs-actions-more').once('paragraphs-actions-more', context);
// Attach event handlers to toggle button.
$actionsElement.each(function () {
var $this = $(this);

$this.on('focusout', function (e) {
setTimeout(function () {
if ($this.has(document.activeElement).length == 0) {
// The focus left the action button group, hide actions.
$this.removeAttr('open');
}
}, 1);
});
});
$(document).on('keydown.paragraphsActions', function (event) {
if (event.key === 'Escape') {
$('.paragraphs-actions-more').removeAttr('open');
}
});

},
detach: function (context) {
$(document).off('keydown.paragraphsActions');
}
};

})(jQuery, Backdrop);
3 changes: 2 additions & 1 deletion paragraphs.field_widget.inc
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ function paragraphs_field_multiple_value_form($field, $instance, $langcode, $ite
if (module_exists('file')) {
// file.js triggers uploads when the main Submit button is clicked.
$field_elements['#attached']['js'][] = backdrop_get_path('module', 'file') . '/js/file.js';
$field_elements['#attached']['js'][] = backdrop_get_path('module', 'paragraphs') . '/paragraphs.js';

This comment has been minimized.

Copy link
@herbdool

herbdool Jun 25, 2024

Contributor

@laryn we could probably move this file to the js folder as well and rename it paragraphs-file.js or something like that.

This comment has been minimized.

Copy link
@laryn

laryn Jun 25, 2024

Author Member

Yes, I think we should. I almost did it here but thought a separate issue might be cleaner. There's also a few jQuery deprecations in there that could be fixed then. .bind/.unbind

I filed #186

$form_state['has_file_element'] = TRUE;
}
$field_elements['#attached']['js'][] = backdrop_get_path('module', 'paragraphs') . '/paragraphs.js';

return $field_elements;
}
Expand Down Expand Up @@ -336,6 +336,7 @@ function _paragraphs_add_more_buttons($instance, $langcode, $form_state, $field_
);
}
}
$field_elements['#attached']['js'][] = backdrop_get_path('module', 'paragraphs') . '/js/paragraphs_actions.js';

This comment has been minimized.

Copy link
@herbdool

herbdool Jun 25, 2024

Contributor

@laryn do you know if this gets called just once in this location? It did in the other.

This comment has been minimized.

Copy link
@laryn

laryn Jun 25, 2024

Author Member

Oh, did I drop it in the wrong spot? Not sure!

This comment has been minimized.

Copy link
@laryn

laryn Jun 25, 2024

Author Member

Moved.

}
else {
$field_elements['add_more']['add_more'] = array(
Expand Down
28 changes: 0 additions & 28 deletions paragraphs.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,4 @@
}
};

/**
* Close any open "more actions" menus.
*/
Backdrop.behaviors.closeMoreActions = {
attach: function (context) {
$('body', context).on('click', Backdrop.paragraphs.closeActionMenu);
},
detach: function (context) {
$('body', context).on('click', Backdrop.paragraphs.closeActionMenu);
}
};

Backdrop.paragraphs = Backdrop.paragraphs || {

/**
* Close any open actions menus if the click is outside of the menu.
*/
closeActionMenu: function (event) {
var $moreactions = $('.paragraphs-actions-more');
var $body = $('body');
var insideMoreActions = $.contains(event.target, $body);
if (!insideMoreActions) {
$moreactions.removeAttr('open');
}
}

};

})(jQuery);

0 comments on commit 92c4806

Please sign in to comment.