awesomplete-multiple-tags.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. var awp = Awesomplete.$;
  2. awesomplete = new Awesomplete(awp('input[data-multiple]'), {
  3. filter: function(text, input) {
  4. return Awesomplete.FILTER_CONTAINS(text, input.match(/[^ ]*$/)[0]);
  5. },
  6. replace: function(text) {
  7. var before = this.input.value.match(/^.+ \s*|/)[0];
  8. this.input.value = before + text + " ";
  9. },
  10. minChars: 1
  11. });
  12. /**
  13. * Remove already selected items from autocompletion list.
  14. * HTML list is never updated, so removing a tag will add it back to awesomplete.
  15. *
  16. * FIXME: This a workaround waiting for awesomplete to handle this.
  17. * https://github.com/LeaVerou/awesomplete/issues/16749
  18. */
  19. function awesompleteUniqueTag(selector) {
  20. var input = document.querySelector(selector);
  21. input.addEventListener('input', function()
  22. {
  23. proposedTags = input.getAttribute('data-list').replace(/,/g, '').split(' ');
  24. reg = /(\w+) /g;
  25. while((match = reg.exec(input.value)) !== null) {
  26. id = proposedTags.indexOf(match[1]);
  27. if(id != -1 ) {
  28. proposedTags.splice(id, 1);
  29. }
  30. }
  31. awesomplete.list = proposedTags;
  32. });
  33. }