Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

97 linhas
2.5 KiB

  1. import $ from 'jquery';
  2. class IHKSurvey {
  3. constructor(section) {
  4. this.section = section.addClass('initiated');
  5. this.formBox = section.find('.form-box');
  6. this.form = section.find('form');
  7. this.initSize();
  8. if (this.form.data('show-result')) {
  9. this.loadResult();
  10. }
  11. else {
  12. this.initOptions();
  13. }
  14. }
  15. initSize() {
  16. const observer = new ResizeObserver((entries) => {
  17. entries.map((entry) => {
  18. const w = entry.borderBoxSize[0].inlineSize;
  19. window.requestAnimationFrame(() => {
  20. if (w < 500) {
  21. this.section.attr('data-size', 'sm');
  22. } else if (w < 1000) {
  23. this.section.attr('data-size', 'md');
  24. } else {
  25. this.section.attr('data-size', 'lg');
  26. }
  27. })
  28. })
  29. })
  30. observer.observe(this.section.get(0));
  31. }
  32. initOptions() {
  33. this.form.find('input[type="radio"]').on('change', (e) => {
  34. if (this.form.find('input[type="radio"]:checked').length === this.form.find('fieldset').length) {
  35. this.loadResult();
  36. }
  37. })
  38. }
  39. loadResult() {
  40. if (this.form.attr('method') === 'get') {
  41. $.get(this.form.attr('action'), this.form.serialize()).done((result) => {
  42. this.insertResult(result);
  43. })
  44. } else {
  45. $.post(this.form.attr('action'), this.form.serialize()).done((result) => {
  46. this.insertResult(result);
  47. })
  48. }
  49. }
  50. insertResult(result) {
  51. const heightDiff = this.formBox.outerHeight() - this.form.outerHeight();
  52. this.formBox.css('height', this.formBox.outerHeight() + 'px');
  53. this.result = $(result).appendTo(this.formBox);
  54. this.formBox.css('height', (this.result.outerHeight() + heightDiff) + 'px');
  55. this.animateResult();
  56. setTimeout(() => {
  57. this.section.addClass('show-result');
  58. },50)
  59. setTimeout(() => {
  60. this.formBox.removeAttr('style');
  61. },800)
  62. }
  63. animateResult() {
  64. const items = this.result.find('.sub-result-wrapper');
  65. items.each((i, el) => {
  66. $('<span class="bar" />').prependTo($(el)).css('transition-delay', (i / 10).toString() + 's');
  67. })
  68. setTimeout(() => {
  69. items.each((i, el) => {
  70. const item = $(el);
  71. item.find('.bar').css('width', parseInt(item.find('.value').text()) + '%');
  72. })
  73. },500)
  74. }
  75. }
  76. export default IHKSurvey;
  77. $('body').on('ihk-init dynamic-component-loaded gfi-dynamic-init', () => {
  78. $('.survey:not(.initiated)').each((i, el) => {
  79. new IHKSurvey($(el));
  80. });
  81. })