editlink.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * remove a tag from the tag "cloud"
  3. * @param String tagString
  4. * @param String targetStartString
  5. */
  6. function removeTag(tagString,targetStartString) {
  7. let toRemove = document.getElementById(targetStartString + '-' + tagString);
  8. let saveInput = document.getElementById(targetStartString + '-save');
  9. if(toRemove && saveInput) {
  10. let newSaveValue = _removeFromCommaString(saveInput.value,tagString);
  11. saveInput.value = newSaveValue;
  12. toRemove.remove();
  13. }
  14. else {
  15. console.log("Delete element not found");
  16. }
  17. }
  18. /**
  19. * add a tag to the visible tag "cloud" and hidden form input
  20. * used in the form for saving
  21. * @param Object e
  22. * @param String targetStartString
  23. */
  24. function addTag(e,targetStartString) {
  25. e = e || window.event;
  26. if(e.keyCode === 13) {
  27. let elem = e.srcElement || e.target;
  28. let saveInput = document.getElementById(targetStartString + '-save');
  29. let listBox = document.getElementById(targetStartString + '-listbox');
  30. let newTagTemplate = document.getElementById(targetStartString + '-template');
  31. if(saveInput && listBox && elem && newTagTemplate) {
  32. let toAdd = elem.value;
  33. let newSaveValue = _appendToCommaString(saveInput.value,toAdd);
  34. let newT = newTagTemplate.cloneNode(true);
  35. newT = _fillTagTemplate(newT,toAdd,targetStartString);
  36. listBox.appendChild(newT);
  37. saveInput.value = newSaveValue;
  38. }
  39. elem.value = '';
  40. e.preventDefault();
  41. }
  42. }
  43. /**
  44. * add given string to given existing string seperated with a comma
  45. * @param String theString
  46. * @param String toAdd
  47. * @returns {string}
  48. * @private
  49. */
  50. function _appendToCommaString(theString,toAdd) {
  51. if(theString.length > 0 && toAdd.length > 0) {
  52. let theArray = theString.split(',');
  53. if(!theArray.includes(toAdd)) {
  54. theString = theString + "," + toAdd
  55. }
  56. }
  57. else if (toAdd.length > 0) {
  58. theString = toAdd;
  59. }
  60. return theString;
  61. }
  62. /**
  63. * add given string to given existing string seperated with a comma
  64. * @param String theString
  65. * @param String toAdd
  66. * @returns {string}
  67. * @private
  68. */
  69. function _removeFromCommaString(theString,toRemove) {
  70. if(theString.length > 0 && toRemove.length > 0) {
  71. let theArray = theString.split(',');
  72. if(theArray.includes(toRemove)) {
  73. for( let i = theArray.length-1; i >= 0; i--){
  74. if ( theArray[i] === toRemove) theArray.splice(i, 1);
  75. }
  76. theString = theArray.join(",");
  77. }
  78. }
  79. return theString;
  80. }
  81. /**
  82. * remove from given list the given value if it exists
  83. * @param Object list
  84. * @param String value
  85. * @private
  86. */
  87. function _removeFromDatalist(list, value) {
  88. if(list.options.length > 0 && value && value.length > 0) {
  89. for (i = 0; i < list.options.length; i++) {
  90. if(list.options[i].value == value) {
  91. list.options[i].remove();
  92. }
  93. }
  94. }
  95. }
  96. /**
  97. * fill the tag template with the right data and js calls
  98. * depends on the html template created in the html code
  99. * @param Object el The already cloned node
  100. * @param String newTagString The new tag name
  101. * @param String targetStartString
  102. * @returns Object the cloned el
  103. * @private
  104. */
  105. function _fillTagTemplate(el,newTagString,targetStartString) {
  106. el.removeAttribute('style');
  107. el.setAttribute('id',targetStartString + '-' + newTagString);
  108. let spanEl = el.querySelector('span');
  109. spanEl.innerHTML = newTagString;
  110. let aEl = el.querySelector('a');
  111. aEl.setAttribute('onclick', "removeTag('"+newTagString+"','"+targetStartString+"');");
  112. return el;
  113. }