diff --git a/js/fivestars.js b/js/fivestars.js index 64e0a1d..34dcc7f 100644 --- a/js/fivestars.js +++ b/js/fivestars.js @@ -3,42 +3,46 @@ * Attaches fivestar rating. */ -(function ($, Drupal) { +(($) => { + + /** + * @type {{attach: Drupal.behaviors.fiveStarRating.attach}} + */ Drupal.behaviors.fiveStarRating = { - attach: function (context, settings) { - $('body').find('.fivestar').each(function () { - var $this = $(this); - var $select = $this.find('select'); - var value = Math.round($select.data('result-value')); - var vote_own_value = $select.data('vote-value'); - var isEdit = $select.data('is-edit'); - var show_own_vote = $select.data('show-own-vote'); - if (isEdit) { - value = $select.val(); - } - if (!value) { - value = -1; - } - var options = { - theme: ($select.data('style') == 'default') ? 'css-stars' : $select.data('style'), - initialRating: show_own_vote ? (vote_own_value ? vote_own_value : -1) : value, - allowEmpty: true, - emptyValue: '', - readonly: ($select.attr('disabled')) ? true : false, - onSelect: function (value, text) { - if (isEdit) { - return; - } - $this.find('select').barrating('readonly', true); - $this.find('[type=submit]').trigger('click'); - $this.find('a').addClass('disabled'); - $this.find('.vote-result').html(); - }, - }; + attach: () => { + once('five-star-rating', '.fivestar', document.body).forEach((item) => { + var $this = $(item); + var $select = $this.find('select'); + var value = Math.round($select.data('result-value')); + var vote_own_value = $select.data('vote-value'); + var isEdit = $select.data('is-edit'); + var show_own_vote = $select.data('show-own-vote'); + if (isEdit) { + value = $select.val(); + } + if (!value) { + value = -1; + } + var options = { + theme: ($select.data('style') == 'default') ? 'css-stars' : $select.data('style'), + initialRating: show_own_vote ? (vote_own_value ? vote_own_value : -1) : value, + allowEmpty: true, + emptyValue: '', + readonly: ($select.attr('disabled')) ? true : false, + onSelect: () => { + if (isEdit) { + return; + } + $select.barrating('readonly', true); + $this.find('[type=submit]').trigger('click'); + $this.find('a').addClass('disabled'); + $this.find('.vote-result').html(); + }, + }; - $this.find('select').once('processed').barrating('show', options); - $this.find('[type=submit]').hide(); - }); + $select.barrating('show', options); + $this.find('[type=submit]').hide(); + }); } }; -})(jQuery, Drupal); +})(jQuery); diff --git a/js/like.js b/js/like.js index 35aaa19..07d4a03 100644 --- a/js/like.js +++ b/js/like.js @@ -3,29 +3,33 @@ * Attaches like rating. */ -(function ($, Drupal) { +(($) => { + + /** + * @type {{attach: Drupal.behaviors.likeRating.attach}} + */ Drupal.behaviors.likeRating = { - attach: function (context, settings) { - $('body').find('.like').each(function () { - var $this = $(this); - $(this).find('select').once('processed').each(function () { - $this.find('[type=submit]').hide(); - var $select = $(this); - var isPreview = $select.data('is-edit'); - $select.after('
').hide(); - $this.find('.like-rating a').eq(0).each(function () { - $(this).bind('click',function (e) { - if (isPreview) { - return; - } - e.preventDefault(); - $select.get(0).selectedIndex = 0; - $this.find('[type=submit]').trigger('click'); - $this.find('a').addClass('disabled'); - }) - }) - }) - }); + attach: () => { + once('like-rating', '.like', document.body).forEach((item) => { + var $this = $(item); + $(this).find('select').once('processed').each(() => { + $this.find('[type=submit]').hide(); + var $select = $(this); + var isPreview = $select.data('is-edit'); + $select.after('
').hide(); + $this.find('.like-rating a').eq(0).each(() =>{ + $(this).bind('click', (e) => { + if (isPreview) { + return; + } + e.preventDefault(); + $select.get(0).selectedIndex = 0; + $this.find('[type=submit]').trigger('click'); + $this.find('a').addClass('disabled'); + }) + }) + }) + }); } }; -})(jQuery, Drupal); +})(jQuery); diff --git a/js/useful.js b/js/useful.js index 9a6edcd..95f8fd8 100644 --- a/js/useful.js +++ b/js/useful.js @@ -3,42 +3,46 @@ * Attaches is useful rating. */ -(function ($, Drupal) { +(($) => { + + /** + * @type {{attach: Drupal.behaviors.usefulRating.attach}} + */ Drupal.behaviors.usefulRating = { - attach: function (context, settings) { - $('body').find('.useful').each(function () { - var $this = $(this); - $(this).find('select').once('processed').each(function () { - $this.find('[type=submit]').hide(); - var $select = $(this); - var isPreview = $select.data('is-edit'); - $select.after('
').hide(); - $this.find('.useful-rating a').eq(0).each(function () { - $(this).bind('click',function (e) { - if (isPreview) { - return; - } - e.preventDefault(); - $select.get(0).selectedIndex = 0; - $this.find('[type=submit]').trigger('click'); - $this.find('a').addClass('disabled'); - $this.find('.vote-result').html(); - }) - }) - $this.find('.useful-rating a').eq(1).each(function () { - $(this).bind('click',function (e) { - if (isPreview) { - return; - } - e.preventDefault(); - $select.get(0).selectedIndex = 1; - $this.find('[type=submit]').trigger('click'); - $this.find('a').addClass('disabled'); - $this.find('.vote-result').html(); - }) - }) - }) - }); + attach: (context, settings) => { + once('useful-rating', '.useful', document.body).forEach((item) => { + var $this = $(item); + $(this).find('select').once('processed').each(() => { + $this.find('[type=submit]').hide(); + var $select = $(this); + var isPreview = $select.data('is-edit'); + $select.after('
').hide(); + $this.find('.useful-rating a').eq(0).each(() => { + $(this).bind('click', (e) => { + if (isPreview) { + return; + } + e.preventDefault(); + $select.get(0).selectedIndex = 0; + $this.find('[type=submit]').trigger('click'); + $this.find('a').addClass('disabled'); + $this.find('.vote-result').html(); + }) + }) + $this.find('.useful-rating a').eq(1).each(() => { + $(this).bind('click',(e) => { + if (isPreview) { + return; + } + e.preventDefault(); + $select.get(0).selectedIndex = 1; + $this.find('[type=submit]').trigger('click'); + $this.find('a').addClass('disabled'); + $this.find('.vote-result').html(); + }) + }) + }) + }); } }; -})(jQuery, Drupal); +})(jQuery); diff --git a/votingapi_widgets.info.yml b/votingapi_widgets.info.yml index 9bf5c10..e4ccd90 100644 --- a/votingapi_widgets.info.yml +++ b/votingapi_widgets.info.yml @@ -2,7 +2,7 @@ name: Votingapi Widgets type: module description: 'Voting API Widgets.' package: Voting -core_version_requirement: ^8.8.2 || ^9 +core_version_requirement: ^8.8.2 || ^9 || ^10 || ^11 dependencies: - votingapi:votingapi diff --git a/votingapi_widgets.libraries.yml b/votingapi_widgets.libraries.yml index 8fdfb91..f74ac49 100644 --- a/votingapi_widgets.libraries.yml +++ b/votingapi_widgets.libraries.yml @@ -7,6 +7,7 @@ fivestar: https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css: {} css/fivestar.css: {} dependencies: + - core/once - core/jquery - core/drupal - votingapi_widgets/fontawesome @@ -40,6 +41,7 @@ useful: theme: css/useful.css: {} dependencies: + - core/once - core/jquery - core/drupal - votingapi_widgets/fontawesome @@ -52,6 +54,7 @@ like: theme: css/like.css: {} dependencies: + - core/once - core/jquery - core/drupal - votingapi_widgets/fontawesome diff --git a/votingapi_widgets.module b/votingapi_widgets.module index 5b2091e..9f49d3a 100644 --- a/votingapi_widgets.module +++ b/votingapi_widgets.module @@ -11,6 +11,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; +use Drupal\Core\StringTranslation\TranslatableMarkup; /** * Implements hook_help(). @@ -20,8 +21,8 @@ function votingapi_widgets_help($route_name, RouteMatchInterface $route_match) { // Main module help for the votingapi_widgets module. case 'help.page.votingapi_widgets': $output = ''; - $output .= '

' . t('About') . '

'; - $output .= '

' . t('Voting API Widgets') . '

'; + $output .= '

' . new TranslatableMarkup('About') . '

'; + $output .= '

' . new TranslatableMarkup('Voting API Widgets') . '

'; return $output; default: @@ -39,11 +40,11 @@ function votingapi_widgets_entity_base_field_info(EntityTypeInterface $entity_ty $fields = []; $fields['field_name'] = BaseFieldDefinition::create('string') - ->setLabel(t('Field name')) + ->setLabel(new TranslatableMarkup('Field name')) ->setName('field_name') ->setRevisionable(FALSE) ->setRequired(FALSE) - ->setDescription(t('Holds the field name.')) + ->setDescription(new TranslatableMarkup('Holds the field name.')) ->setPropertyConstraints('value', ['Length' => ['max' => FieldStorageConfig::NAME_MAX_LENGTH]]); return $fields;