Guest User

Untitled

a guest
Jul 17th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. <template>
  2. <input type="text" class="selectize-tag-input">
  3. </template>
  4. <script>
  5. if (!$().selectize) {
  6. require('selectize') // This script requires selectize
  7. }
  8.  
  9. export default
  10. {
  11. name : 'tags-field',
  12. props : ["value", "onAdd"], // The onAdd property must be a function and is used to verify if a new tag being added is valid, returning false will not allow the field to be added
  13. watch : {
  14. value : function() // Here is where we work around the non-reactiveness of Selectize
  15. {
  16. this.$el.value = this.value;
  17. this.$el.setAttribute('value', this.value);
  18.  
  19. this.syncWatcher();
  20. }
  21. },
  22. methods :
  23. {
  24. emitValue()
  25. {
  26. this.$emit('input', this.innerValue);
  27. },
  28. init : function(reinit)
  29. {
  30. var t = this;
  31.  
  32. this.instance = $(this.$el).selectize({
  33. delimiter: ',',
  34. create: function(input) {
  35.  
  36. if ( typeof t.onAdd === "function" )
  37. {
  38. if ( t.onAdd(input) !== true ) {
  39. this.$control_input[0].value = '';
  40. this.$control_input[0].setAttribute('value', '');
  41.  
  42. return false;
  43. }
  44. }
  45.  
  46. return {
  47. value: input,
  48. text: input
  49. }
  50. }
  51. });
  52.  
  53. var inputEl = $(this.$el).siblings().children('.selectize-input').children('input')[0];
  54. var t = this;
  55.  
  56. // After reinitializing, take the user back to the text field if they've typed in it previously
  57.  
  58. inputEl.addEventListener('focus', function()
  59. {
  60. t.userType = true;
  61. });
  62.  
  63. if ( reinit === true && this.userType )
  64. {
  65. inputEl.focus();
  66. }
  67.  
  68. // Restart watcher
  69.  
  70. this.syncWatcher();
  71. },
  72. reinit : function(reinit)
  73. {
  74. this.instance[0].selectize.destroy();
  75. this.init(reinit);
  76. },
  77. syncWatcher : function()
  78. {
  79. if ( this.watcherInstance !== null )
  80. clearInterval(this.watcherInstance); // We need to clear the interval and redefine the "t" variable to ensure that the watcher is up to date with the instance's new information
  81.  
  82. var t = this;
  83.  
  84. this.watcherInstance = setInterval(function()
  85. {
  86. if ( t.$el.getAttribute('value') !== t.innerValue )
  87. {
  88. t.innerValue = t.$el.getAttribute('value');
  89. t.emitValue();
  90. t.reinit(true);
  91. }
  92. }, 100);
  93. }
  94. },
  95. mounted()
  96. {
  97. this.init();
  98. },
  99. data()
  100. {
  101. return {
  102. userType : false,
  103. instance : null,
  104. watcherInstance : null,
  105. innerValue : ''
  106. };
  107. }
  108. }
  109. </script>
Add Comment
Please, Sign In to add comment