暂无描述

main.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. ///// !!! GLOBAL FUNCTIONS !!! /////
  2. // CHECKS THAT STRINGS ARE JSON
  3. function isJSON(item) {
  4. // item = (typeof item !== "string")
  5. // ? JSON.stringify(item)
  6. // : item;
  7. try {
  8. item = JSON.parse(item);
  9. } catch (e) {
  10. return false;
  11. }
  12. return (typeof item === "object" && item !== null);
  13. }
  14. // MODAL SWITCHING FUNCTION (home.php & viewExperience.php)
  15. function nextModal(name) {
  16. // $('[data-toggle="tooltip"]').tooltip('hide');
  17. setTimeout(() => $(name).modal('show'), 100);
  18. }
  19. // Disable 'enter' key from submitting forms
  20. // $('form').keypress(function(event){ if (event.which == '13') { event.preventDefault(); } } );
  21. // $('[form]:not(button[form])').keypress(function(event){ if (event.which == '13') { event.preventDefault(); } } );
  22. // NUMBER INPUT SANITIZATION (home.php & viewExperience.php)
  23. $('input[type="number"]').on('change keyup', () => {
  24. // Remove invalid characters
  25. var sanitized = $(this).val().replace(/[^0-9]/g, '');
  26. // Update value
  27. $(this).val(sanitized);
  28. });
  29. // DATE INPUT DATEPICKER (home.php & viewExperience.php)
  30. // Taken from: http://www.javascriptkit.com/javatutors/createelementcheck2.shtml
  31. // If browser doesn't support input type="date", load files for jQuery UI Date Picker
  32. let testDateField = document.createElement("input");
  33. testDateField.setAttribute("type", "date");
  34. if(testDateField.type != "date") {
  35. // console.log('Browser does not support input of type date, loading jQuery UI...');
  36. // First load the CSS ("base" theme) for the calendar
  37. document.write('<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css">\n');
  38. // let objectContainer = document.createElement('div');
  39. // objectContainer.innerHTML = '<link href="//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css" rel="stylesheet">';
  40. // document.body.appendChild(objectContainer);
  41. //<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css">
  42. // Then load the calendar functionality
  43. document.write('<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>\n');
  44. // objectContainer = document.createElement('div');
  45. // objectContainer.innerHTML = '<script src="//code.jquery.com/jquery-1.12.4.js"></script><script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script>';
  46. // document.body.appendChild(objectContainer);
  47. //<script src="https://code.jquery.com/jquery-1.12.4.js"></script><script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>
  48. // Initialize date picker widget (on document.ready)
  49. // Help: https://jqueryui.com/datepicker/#min-max
  50. window.onload = () => {
  51. if($('input[type="date"]').length !== 0) {
  52. // jQuery(($) => $('input[type="date"]').datepicker({ minDate: 0, maxDate: "+1Y"}));
  53. // console.log('Done!');
  54. }
  55. }
  56. }
  57. // !!! FROM HERE ON DOWN, THERE'S ONLY OLD CODE !!! //
  58. // Declaration and initialization of the "naming counter"
  59. var selectionCounter = 2;
  60. // WHEN WANTING TO ADD MULTIPLE QUESTIONNAIRES TO AN EXPERIENCE (newExperience.php and editExperience.php)
  61. function AddQuestionnaire() {
  62. // We add two linebreaks so that there is space between each dropdown list
  63. var br = document.createElement('br');
  64. document.getElementById('addQuestionnaires').appendChild(br);
  65. br = document.createElement('br');
  66. document.getElementById('addQuestionnaires').appendChild(br);
  67. // We create a copy of the dropdown list we already have, so multiple questions can be added at the same time
  68. var select = document.getElementById('questionnaire');
  69. var clone = select.cloneNode(true);
  70. // The clones will have the same name as the original but with a number that will distinguish them
  71. var name = select.getAttribute('name') + selectionCounter++;
  72. clone.id = name;
  73. clone.setAttribute('name', name);
  74. document.getElementById('addQuestionnaires').appendChild(clone);
  75. //document.getElementById("demo").innerHTML = selectionCounter;
  76. }
  77. // Reinitialization of the "naming counter"
  78. selectionCounter = 2;
  79. // WHEN WANTING TO ADD MULTIPLE QUESTIONS TO A QUESTIONNAIRE (newQuestionnaire.php and editQuestionnaire.php)
  80. function AddQuestion() {
  81. // We add a linebreak so that there is space between each dropdown list
  82. var br = document.createElement("br");
  83. document.getElementById("addQuestions").appendChild(br)
  84. var br2 = document.createElement("br");
  85. document.getElementById("addQuestions").appendChild(br2)
  86. // We create a copy of the dropdown list we already have, so multiple questions can be added at the same time.
  87. var select = document.getElementById("question");
  88. var clone = select.cloneNode(true);
  89. // The clones will have the same name as the original but with a number that will distinguish them
  90. var name = select.getAttribute("name") + selectionCounter++;
  91. clone.id = name;
  92. clone.setAttribute("name", name)
  93. document.getElementById("addQuestions").appendChild(clone)
  94. //document.getElementById("demo").innerHTML = selectionCounter;
  95. }