awesomplete-multiple-tags.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /** @licstart The following is the entire license notice for the
  2. * JavaScript code in this page.
  3. *
  4. * Copyright: (c) 2011-2015 Sébastien SAUVAGE <sebsauvage@sebsauvage.net>
  5. * (c) 2011-2017 The Shaarli Community, see AUTHORS
  6. *
  7. * This software is provided 'as-is', without any express or implied warranty.
  8. * In no event will the authors be held liable for any damages arising from
  9. * the use of this software.
  10. *
  11. * Permission is granted to anyone to use this software for any purpose,
  12. * including commercial applications, and to alter it and redistribute it
  13. * freely, subject to the following restrictions:
  14. *
  15. * 1. The origin of this software must not be misrepresented; you must not
  16. * claim that you wrote the original software. If you use this software
  17. * in a product, an acknowledgment in the product documentation would
  18. * be appreciated but is not required.
  19. *
  20. * 2. Altered source versions must be plainly marked as such, and must
  21. * not be misrepresented as being the original software.
  22. *
  23. * 3. This notice may not be removed or altered from any source distribution.
  24. *
  25. * @licend The above is the entire license notice
  26. * for the JavaScript code in this page.
  27. */
  28. var awp = Awesomplete.$;
  29. var autocompleteFields = document.querySelectorAll('input[data-multiple]');
  30. [].forEach.call(autocompleteFields, function(autocompleteField) {
  31. awesomplete = new Awesomplete(awp(autocompleteField), {
  32. filter: function (text, input) {
  33. return Awesomplete.FILTER_CONTAINS(text, input.match(/[^ ]*$/)[0]);
  34. },
  35. replace: function (text) {
  36. var before = this.input.value.match(/^.+ \s*|/)[0];
  37. this.input.value = before + text + " ";
  38. },
  39. minChars: 1
  40. })
  41. });
  42. /**
  43. * Remove already selected items from autocompletion list.
  44. * HTML list is never updated, so removing a tag will add it back to awesomplete.
  45. *
  46. * FIXME: This a workaround waiting for awesomplete to handle this.
  47. * https://github.com/LeaVerou/awesomplete/issues/16749
  48. */
  49. function awesompleteUniqueTag(selector) {
  50. var input = document.querySelector(selector);
  51. input.addEventListener('input', function()
  52. {
  53. proposedTags = input.getAttribute('data-list').replace(/,/g, '').split(' ');
  54. reg = /(\w+) /g;
  55. while((match = reg.exec(input.value)) !== null) {
  56. id = proposedTags.indexOf(match[1]);
  57. if(id != -1 ) {
  58. proposedTags.splice(id, 1);
  59. }
  60. }
  61. awesomplete.list = proposedTags;
  62. });
  63. }