post.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Editor toggle
  2. function switchMenu(obj) {
  3. var el = document.getElementById(obj);
  4. if ( el === null ) {
  5. console.log('no such element '+el);
  6. return;
  7. }
  8. if ( el.style.display != 'none' ) {
  9. el.style.display = 'none';
  10. } else {
  11. el.style.display = 'inline-block';
  12. }
  13. }
  14. //Add items to the tags forms
  15. function add2tags(id) {
  16. var select = document.getElementById( id + '-tags');
  17. var input = document.getElementById( id + '-customtag');
  18. var newOption = document.createElement('option');
  19. newOption.value = input.value;
  20. newOption.innerText = input.value;
  21. newOption.selected = true;
  22. select.appendChild(newOption);
  23. input.value= '';
  24. }
  25. //Ditto for aliases
  26. function add2aliases(id) {
  27. var select = document.getElementById( id + '-alias');
  28. var input = document.getElementById( id + '-customalias');
  29. var newOption = document.createElement('option');
  30. newOption.value = input.value;
  31. newOption.innerText = input.value;
  32. newOption.selected = true;
  33. select.appendChild(newOption);
  34. input.value= '';
  35. }
  36. // Multi-page data functions
  37. function add2data(id, page) {
  38. var select = document.getElementById( id + '-pages');
  39. var input = document.getElementById( id + '-' + page + '-page');
  40. var thisPage = document.getElementById(id+'-'+page+'-page-option');
  41. if (thisPage) {
  42. thisPage.value = input.value;
  43. return false;
  44. }
  45. var newOption = document.createElement('option');
  46. newOption.value = input.value;
  47. newOption.innerText = "Page " + page;
  48. newOption.selected = true;
  49. newOption.id = id+'-'+page+"-page-option";
  50. select.appendChild(newOption);
  51. }
  52. function addPage(id) {
  53. var pageContainer = document.getElementById( 'content-pages-' + id );
  54. var lastPage = document.querySelector('#content-pages-'+id+' > textarea.data-page:nth-last-of-type(1)');
  55. var curPage;
  56. if (!lastPage) {
  57. curPage = 0;
  58. } else {
  59. var matches = /^[a-f0-9\-]*-(\d+)-page$/.exec(lastPage.id);
  60. // Go ahead and add this page in case we forgot.
  61. add2data(id, matches[1]);
  62. curPage = parseInt(matches[1]) + 1;
  63. }
  64. var newSpan = document.createTextNode('Page '+ curPage);
  65. pageContainer.appendChild(newSpan);
  66. var newButan = document.createElement('button');
  67. newButan.type = "button";
  68. newButan.className = "coolbutton emojiPicker";
  69. newButan.innerText = "😎";
  70. pageContainer.appendChild(newButan);
  71. var newBr = document.createElement('br');
  72. pageContainer.appendChild(newBr);
  73. var newTextArea = document.createElement('textarea');
  74. newTextArea.id = id + "-" + curPage + "-page";
  75. newTextArea.className = 'cooltext data-page';
  76. pageContainer.appendChild(newTextArea);
  77. }
  78. function addAllPages(id) {
  79. var pages = document.querySelectorAll('#content-pages-'+id+' textarea');
  80. for (page of pages) {
  81. var matches = /^[a-f0-9\-]*-(\d+)-page$/.exec(page.id);
  82. // Go ahead and add this page in case we forgot.
  83. add2data(id, matches[1]);
  84. }
  85. return true;
  86. }
  87. // Post attachment forms
  88. function add2attachments(id, page) {
  89. var select = document.getElementById( id + '-attachments');
  90. var input = document.getElementById( id + '-' + page + '-attachment');
  91. var thisPage = document.getElementById(id+'-'+page+'-attachment-option');
  92. if (thisPage) {
  93. thisPage.value = input.value;
  94. return false;
  95. }
  96. var newOption = document.createElement('option');
  97. newOption.value = input.value;
  98. newOption.innerText = "Attachment " + page;
  99. newOption.selected = true;
  100. newOption.id = id+'-'+page+"-attachment-option";
  101. select.appendChild(newOption);
  102. }
  103. function addAttachment(id) {
  104. var pageContainer = document.getElementById( 'content-attachments-' + id );
  105. var lastPage = document.querySelector('#content-attachments-'+id+' > input.attachment:nth-last-of-type(1)');
  106. var curPage;
  107. if (!lastPage) {
  108. curPage = 0;
  109. } else {
  110. var matches = /^[a-f0-9\-]*-(\d+)-attachment$/.exec(lastPage.id);
  111. // Go ahead and add this page in case we forgot.
  112. add2attachments(id, matches[1]);
  113. curPage = parseInt(matches[1]) + 1;
  114. }
  115. var newSpan = document.createTextNode('Attachment '+ curPage);
  116. pageContainer.appendChild(newSpan);
  117. var newBr = document.createElement('br');
  118. pageContainer.appendChild(newBr);
  119. var newFilePicker = document.createElement('input');
  120. newFilePicker.id = id + "-" + curPage + "-attachment";
  121. newFilePicker.className = 'cooltext attachment';
  122. newFilePicker.type = "file";
  123. pageContainer.appendChild(newFilePicker);
  124. }
  125. function addAllAttachments(id) {
  126. var pages = document.querySelectorAll('#content-attachments-'+id+' input');
  127. for (page of pages) {
  128. var matches = /^[a-f0-9\-]*-(\d+)-attachment$/.exec(page.id);
  129. // Go ahead and add this page in case we forgot.
  130. add2attachments(id, matches[1]);
  131. }
  132. return true;
  133. }