main.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. function submitForm() {
  2. var $form = $("form");
  3. if (validate($form)) {
  4. // https://github.com/marioizquierdo/jquery.serializeJSON
  5. var data = $form.serializeJSON({
  6. customTypes: {
  7. csv: function(str) {
  8. if (str === "") {
  9. return null;
  10. }
  11. return $.csv.toArray(str);
  12. },
  13. }
  14. });
  15. // Remove empty nodes
  16. data = trimObject(data);
  17. var dumped = jsyaml.safeDump(data, {
  18. noArrayIndent: true,
  19. });
  20. console.log(dumped);
  21. $('#add-game-yaml-body').val(dumped);
  22. $('#yaml-modal').modal();
  23. return false;
  24. } else {
  25. // Form is invalid; let the browser's built in validation show
  26. return true;
  27. }
  28. }
  29. function validate($form) {
  30. // Check required for multiple checkboxes
  31. var valid = true;
  32. $form.find('.checkbox-group-required').each(function(i, elem) {
  33. $(elem).find(':checkbox').removeAttr('required');
  34. if ($(elem).find(':checkbox:checked').length === 0) {
  35. // Set one of them to required to use the browser's own validation message
  36. $(elem).find(':checkbox').attr('required', 'required');
  37. valid = false;
  38. }
  39. });
  40. valid = valid && $form.get()[0].checkValidity();
  41. return valid;
  42. }
  43. function trimObject(o) {
  44. if (typeof o === 'object') {
  45. for (var a in o) {
  46. o[a] = trimObject(o[a]);
  47. if (!o[a] || (o[a].constructor === Object && Object.keys(o[a]).length === 0)) {
  48. delete o[a];
  49. }
  50. }
  51. } else if (Array.isArray(o)) {
  52. o = o.map(function(e) {
  53. return trimObject(e);
  54. });
  55. }
  56. return o;
  57. }
  58. function copy() {
  59. $("#add-game-yaml-body").select();
  60. document.execCommand("copy");
  61. }
  62. $("input.tagsinput").each(function() {
  63. $(this).tagsinput({
  64. allowDuplicates: this.hasAttribute("data-allow-duplicates")
  65. });
  66. });
  67. $("input.datepicker").datepicker({
  68. format: "yyyy-mm-dd",
  69. autoclose: true,
  70. todayBtn: "linked",
  71. todayHighlight: true
  72. });