script.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**
  2. * Validates the form fields.
  3. *
  4. * @return true if fields are OK, flase otherwise.
  5. */
  6. function validate_post(){
  7. var text_es = document.getElementById('text_es').value;
  8. var text_eu = document.getElementById('text_eu').value;
  9. var text_en = document.getElementById('text_en').value;
  10. var title_es = document.getElementById('title_es').value;
  11. var title_eu = document.getElementById('title_eu').value;
  12. var title_en = document.getElementById('title_en').value;
  13. result = true;
  14. //Spanish fields empty
  15. if (title_es.length < 1){
  16. result = false;
  17. alert("Introduce un titulo en castellano");
  18. return false;
  19. }
  20. if (text_es.length < 1){
  21. result = false;
  22. alert("Introduce un texto en castellano");
  23. return false;
  24. }
  25. //Titles without texts
  26. if (title_eu.length > 0 && text_eu.length < 1){
  27. result = false;
  28. alert("Introduce un texto o borra el titulo en euskera")
  29. return false;
  30. }
  31. if (title_en.length > 0 && text_en.length < 1){
  32. result = false;
  33. alert("Introduce un texto o borra el titulo en ingles")
  34. return false;
  35. }
  36. //Text without titles
  37. if (text_eu.length > 0 && title_eu.length < 1){
  38. result = false;
  39. alert("Introduce un titulo o borra el texto en euskera")
  40. return false;
  41. }
  42. if (text_en.length > 0 && title_en.length < 1){
  43. result = false;
  44. alert("Introduce un titulo o borra el texto en ingles")
  45. return false;
  46. }
  47. return true;
  48. }
  49. /**
  50. * Adds HTML tags to format the text in a a textarea.
  51. *
  52. * @param textarea The textarea to select the text from.
  53. * @param format Format tag: 'italic', 'bold', 'underline', 'stroke', 'link', 'color-[red|green|blue]'
  54. * @return Formatted text.
  55. */
  56. function formatText(textarea, format){
  57. var text = textarea.value;
  58. var selectedText= window.getSelection().toString();
  59. event.preventDefault();
  60. //IE
  61. if (document.selection != undefined){
  62. textarea.focus();
  63. var selection = document.selection.createRange();
  64. selectedText = selection.text;
  65. }
  66. //Mozilla
  67. else if (textarea.selectionStart != undefined){
  68. var startPos = textarea.selectionStart;
  69. var endPos = textarea.selectionEnd;
  70. selectedText = textarea.value.substring(startPos, endPos);
  71. }
  72. var replace;
  73. switch (format){
  74. case "italic":
  75. replace = "<span style='font-style:italic;'>" + selectedText + "</span>";
  76. break;
  77. case "bold":
  78. replace = "<span style='font-weight:bold;'>" + selectedText + "</span>";
  79. break;
  80. case "underline":
  81. replace = "<span style='text-decoration:underline;'>" + selectedText + "</span>";
  82. break;
  83. case "stroke":
  84. replace = "<span style='text-decoration:line-through;'>" + selectedText + "</span>";
  85. break;
  86. case "link":
  87. replace = "<a href='" + selectedText + "'>" + selectedText + "</a>";
  88. break;
  89. case "color-red":
  90. replace = "<span style='color:#f00;'>" + selectedText + "</span>";
  91. break;
  92. case "color-green":
  93. replace = "<span style='color:#0f0;'>" + selectedText + "</span>";
  94. break;
  95. case "color-blue":
  96. replace = "<span style='color:#00f;'>" + selectedText + "</span>";
  97. break;
  98. default:
  99. replace = selectedText;
  100. }
  101. if (selectedText.length >0 && text != replace){
  102. text = text.replace(selectedText, replace);
  103. textarea.value = text;
  104. }
  105. }
  106. /**
  107. * Previews an image that has been just uploaded to the server.
  108. * @param id ID of the image.
  109. * @param source Image resource.
  110. * @param target img tag to preview the photo in.
  111. */
  112. function preview_image(id, source, target) {
  113. var reader = new FileReader();
  114. reader.readAsDataURL(source.files[0]);
  115. reader.onload = function (reader_event) {
  116. target.src = reader_event.target.result;
  117. target.style.border = "2px solid black";
  118. target.style.marginTop = 10 + "px";
  119. target.style.marginBottom = 10 + "px";
  120. target.style.display = "block";
  121. document.getElementById("delete_image_" + id).value = "no";
  122. };
  123. };
  124. /**
  125. * Deletes an uploaded image and it's preview.
  126. * @param id ID of the image.
  127. * @param target img tag to preview the photo in.
  128. * @param button Button used to clear the image.
  129. */
  130. function delete_image(id, target, button){
  131. target.src = "/img/misc/alpha.png";
  132. target.style.display = "none";
  133. button.style.display = "none";
  134. document.getElementById("delete_image_" + id).value = "yes";
  135. }