rvera / image-picker

Image Picker is a simple jQuery plugin that transforms a select element into a more user friendly graphical interface.
http://rvera.github.com/image-picker
Other
907 stars 216 forks source link

Keyboard usage #108

Closed rvera closed 5 years ago

rvera commented 6 years ago

Add option to control the picker via keyboard

lowegreg commented 6 years ago

I needed to add accessibility to my site and was using the edited version of the control by @AndreasIgelCC. But noticed that it was an older version and had issues with alt image tags (another accessibility feature). I came up with the following solution, but if anyone can improve on it please let me know.

A huge shoutout to @rvera for great work by the way!

ImagePickerOption.prototype.create_node = function() {
      var image, imgAlt, imgClass, thumbnail;
      this.node = jQuery("<li/>");
      //Added tab stop to image
      image = jQuery("<img class='image_picker_image' tabindex='0'/>");
      image.attr("src", this.option.data("img-src"));
      thumbnail = jQuery("<div class='thumbnail'>");
      imgClass = this.option.data("img-class");
      if (imgClass) {
        this.node.addClass(imgClass);
        image.addClass(imgClass);
        thumbnail.addClass(imgClass);
      }
      imgAlt = this.option.data("img-alt");
      if (imgAlt) {
        image.attr('alt', imgAlt);
      }
      thumbnail.on("click", this.clicked);
      //Added keypress for space & enter which calls the above click event
      thumbnail.keypress(function(event) {
        if (event.keyCode === 0 || event.keyCode === 32 || event.keyCode === 13) {
          event.preventDefault();
          thumbnail.click();
        }
      });
      thumbnail.append(image);
      if (this.opts.show_label) {
        thumbnail.append(jQuery("<p/>").html(this.label()));
      }
      this.node.append(thumbnail);
      return this.node;
    };

    return ImagePickerOption;

  })();
rvera commented 6 years ago

You think you can modify the origina coffee script and make a PR for this?

lowegreg commented 6 years ago

Done.

Implemented keyboard navigation for accessibility #109

Humni commented 5 years ago

Resolved in #111