editlink.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. let checkString = _checkForSpaceString(elem.value,'nospace');
  32. if(saveInput && listBox && elem && newTagTemplate && checkString) {
  33. let toAdd = elem.value;
  34. let newSaveValue = _appendToCommaString(saveInput.value,toAdd);
  35. let newT = newTagTemplate.cloneNode(true);
  36. newT = _fillTagTemplate(newT,toAdd,targetStartString);
  37. listBox.appendChild(newT);
  38. saveInput.value = newSaveValue;
  39. }
  40. elem.value = '';
  41. e.preventDefault();
  42. }
  43. }
  44. /**
  45. * add given string to given existing string seperated with a comma
  46. * @param String theString
  47. * @param String toAdd
  48. * @returns {string}
  49. * @private
  50. */
  51. function _appendToCommaString(theString,toAdd) {
  52. if(theString.length > 0 && toAdd.length > 0) {
  53. let theArray = theString.split(',');
  54. if(!theArray.includes(toAdd)) {
  55. theString = theString + "," + toAdd
  56. }
  57. }
  58. else if (toAdd.length > 0) {
  59. theString = toAdd;
  60. }
  61. return theString;
  62. }
  63. /**
  64. * add given string to given existing string seperated with a comma
  65. * @param String theString
  66. * @param String toAdd
  67. * @returns {string}
  68. * @private
  69. */
  70. function _removeFromCommaString(theString,toRemove) {
  71. if(theString.length > 0 && toRemove.length > 0) {
  72. let theArray = theString.split(',');
  73. if(theArray.includes(toRemove)) {
  74. for( let i = theArray.length-1; i >= 0; i--){
  75. if ( theArray[i] === toRemove) theArray.splice(i, 1);
  76. }
  77. theString = theArray.join(",");
  78. }
  79. }
  80. return theString;
  81. }
  82. /**
  83. * remove from given list the given value if it exists
  84. * @param Object list
  85. * @param String value
  86. * @private
  87. */
  88. function _removeFromDatalist(list, value) {
  89. if(list.options.length > 0 && value && value.length > 0) {
  90. for (i = 0; i < list.options.length; i++) {
  91. if(list.options[i].value == value) {
  92. list.options[i].remove();
  93. }
  94. }
  95. }
  96. }
  97. /**
  98. * fill the tag template with the right data and js calls
  99. * depends on the html template created in the html code
  100. * @param Object el The already cloned node
  101. * @param String newTagString The new tag name
  102. * @param String targetStartString
  103. * @returns Object the cloned el
  104. * @private
  105. */
  106. function _fillTagTemplate(el,newTagString,targetStartString) {
  107. el.removeAttribute('style');
  108. el.setAttribute('id',targetStartString + '-' + newTagString);
  109. let spanEl = el.querySelector('span');
  110. spanEl.innerHTML = newTagString;
  111. let aEl = el.querySelector('a');
  112. aEl.setAttribute('onclick', "removeTag('"+newTagString+"','"+targetStartString+"');");
  113. return el;
  114. }
  115. /**
  116. * simple check if the string is empty or contains whitespace chars
  117. * @param stringTocheck
  118. * @returns {boolean}
  119. * @private
  120. */
  121. function _checkForSpaceString(stringTocheck) {
  122. let check = stringTocheck.replace(/\s/gm,'');
  123. if(check === stringTocheck && check.length > 0) {
  124. return true;
  125. }
  126. return false;
  127. }