Sha256: cfd3a4b7b4e6e3f543b460aee4033c5128b7c57d4842e0f53bd142c6808a456a

Contents?: true

Size: 1.79 KB

Versions: 12

Compression:

Stored size: 1.79 KB

Contents

// Autocomplete for linked data elements
// After selecting something, the seleted item is immutable
export default class LinkedData {
  constructor(element, url) {
    this.url = url
    this.element = element
    this.activate()
  }

  activate() {
    this.element
      .select2(this.options(this.element))
      .on("change", (e) => { this.selected(e) })
  }

  // Called when a choice is made
  selected(e) {
    let result = this.element.select2("data")
    this.element.select2("destroy")
    this.element.val(result.label).attr("readonly", "readonly")
    this.setIdentifier(result.id)
  }

  // Store the uri in the associated hidden id field
  setIdentifier(uri) {
    this.element.closest('.field-wrapper').find('[data-id]').val(uri);
  }

  options(element) {
    return {
      // placeholder: $(this).attr("value") || "Search for a location",
      minimumInputLength: 2,
      id: function(object) {
        return object.id;
      },
      text: function(object) {
        return object.label;
      },
      initSelection: function(element, callback) {
        // Called when Select2 is created to allow the user to initialize the
        // selection based on the value of the element select2 is attached to.
        // Essentially this is an id->object mapping function.

        // TODO: Presently we're just showing a URI, but we should show the label.
        var data = {
          id: element.val(),
          label: element.val()
        };
        callback(data);
      },
      ajax: { // Use the jQuery.ajax wrapper provided by Select2
        url: this.url,
        dataType: "json",
        data: function (term, page) {
          return {
            q: term // Search term
          };
        },
        results: function(data, page) {
          return { results: data };
        }
      }
    }
  }
}

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
hyrax-2.0.3 app/assets/javascripts/hyrax/autocomplete/linked_data.es6
hyrax-2.0.2 app/assets/javascripts/hyrax/autocomplete/linked_data.es6
hyrax-2.0.1 app/assets/javascripts/hyrax/autocomplete/linked_data.es6
hyrax-2.0.0 app/assets/javascripts/hyrax/autocomplete/linked_data.es6
hyrax-2.0.0.rc3 app/assets/javascripts/hyrax/autocomplete/linked_data.es6
hyrax-2.0.0.rc2 app/assets/javascripts/hyrax/autocomplete/linked_data.es6
hyrax-2.0.0.rc1 app/assets/javascripts/hyrax/autocomplete/linked_data.es6
hyrax-2.0.0.beta5 app/assets/javascripts/hyrax/autocomplete/linked_data.es6
hyrax-2.0.0.beta4 app/assets/javascripts/hyrax/autocomplete/linked_data.es6
hyrax-2.0.0.beta3 app/assets/javascripts/hyrax/autocomplete/linked_data.es6
hyrax-2.0.0.beta2 app/assets/javascripts/hyrax/autocomplete/linked_data.es6
hyrax-2.0.0.beta1 app/assets/javascripts/hyrax/autocomplete/linked_data.es6