Sha256: 3ca697cee9cc4b16d5f4300e946fec9ff1b80e1f7c036c11914c52626a941b42

Contents?: true

Size: 1.16 KB

Versions: 16

Compression:

Stored size: 1.16 KB

Contents

$(function() {
  const COUNT_KEY = "%count%";

  $.fn.remainingCharacters = function() {
    return $(this).each(

      /**
       * @this HTMLElement
       * @returns {void}
       */
      function() {
        const $input = $(this);
        const $target = $($input.data("remaining-characters"));
        const maxCharacters = parseInt($(this).attr("maxlength"), 10);

        if ($target.length > 0 && maxCharacters > 0) {
          const messagesJson = $target.data("remaining-characters-messages");
          const messages = $.extend({
            one: `${COUNT_KEY} character left`,
            many: `${COUNT_KEY} characters left`
          }, messagesJson);

          const updateStatus = function() {
            const numCharacters = $input.val().length;
            const remaining = maxCharacters - numCharacters;
            let message = messages.many;
            if (remaining === 1) {
              message = messages.one;
            }

            $target.text(message.replace(COUNT_KEY, remaining));
          };

          $input.on("keyup focus", function() {
            updateStatus();
          });
          updateStatus();
        }
      }
    );
  };
});

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
decidim-plans-0.18.2 app/assets/javascripts/decidim/plans/remaining_characters.es6
decidim-plans-0.18.1 app/assets/javascripts/decidim/plans/remaining_characters.es6
decidim-plans-0.18.0 app/assets/javascripts/decidim/plans/remaining_characters.es6
decidim-plans-0.17.0 app/assets/javascripts/decidim/plans/remaining_characters.es6
decidim-plans-0.16.9 app/assets/javascripts/decidim/plans/remaining_characters.es6
decidim-plans-0.16.8 app/assets/javascripts/decidim/plans/remaining_characters.es6
decidim-plans-0.16.7 app/assets/javascripts/decidim/plans/remaining_characters.es6
decidim-plans-0.16.6 app/assets/javascripts/decidim/plans/remaining_characters.es6
decidim-plans-0.16.5 app/assets/javascripts/decidim/plans/remaining_characters.es6
decidim-plans-0.16.4 app/assets/javascripts/decidim/plans/remaining_characters.es6
decidim-plans-0.16.3 app/assets/javascripts/decidim/plans/remaining_characters.es6
decidim-plans-0.16.2 app/assets/javascripts/decidim/plans/remaining_characters.es6
decidim-plans-0.16.1 app/assets/javascripts/decidim/plans/remaining_characters.es6
decidim-plans-0.16.0 app/assets/javascripts/decidim/plans/remaining_characters.es6
decidim-plans-0.15.1 app/assets/javascripts/decidim/plans/remaining_characters.es6
decidim-plans-0.15.0 app/assets/javascripts/decidim/plans/remaining_characters.es6