You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
5.8KB

  1. export class SovWidgets {
  2. static setSelect2($select) {
  3. if (typeof $select.data('select2-id') === 'undefined') {
  4. $select.data('init', 'set')
  5. var options = {
  6. width: "100%",
  7. theme: "bootstrap4",
  8. dropdownAutoWidth: false,
  9. allowClear: true,
  10. minimumResultsForSearch: 8
  11. };
  12. if ($select.data('allow-clear') == 'false') {
  13. options.allowClear = false;
  14. }
  15. if ($select.data('width')) {
  16. options.width = 'auto'
  17. }
  18. if ($select.find('option[value=""]')) {
  19. options.placeholder = $select.find('option[value=""]').html()
  20. }
  21. var myselect = $select.select2(options);
  22. myselect.on('select2:select', function (e) {
  23. var event = new Event('change');
  24. e.target.dispatchEvent(event);
  25. });
  26. myselect.on('select2:unselect', function (e) {
  27. var event = new Event('change');
  28. e.target.dispatchEvent(event);
  29. });
  30. return myselect;
  31. }
  32. }
  33. static setDateRange() {
  34. $('.date-time-range, .date-range').each(function (i, picker) {
  35. let options = {
  36. autoUpdateInput: false,
  37. locale: {
  38. "format": "DD/MM/YY",
  39. "separator": " - ",
  40. "applyLabel": "Appliquer",
  41. "cancelLabel": "Annuler",
  42. "fromLabel": "Du",
  43. "toLabel": "au",
  44. "customRangeLabel": "Custom",
  45. "daysOfWeek": ["Di", "Lu", "Ma", "Me", "Je", "Ve", "Sa"],
  46. "monthNames": ["Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre"],
  47. "firstDay": 1
  48. }
  49. };
  50. if ($(picker).hasClass('date-time-range')) {
  51. options = Object.assign(options, {
  52. timePicker: true,
  53. timePickerIncrement: 30,
  54. timePicker24Hour: true,
  55. locale: {
  56. "format": "DD/MM/YYYY HH:mm",
  57. }
  58. });
  59. }
  60. if ($(picker).nextAll('.date-time-range-fields').find('.date-start').val()) {
  61. options.startDate = new Date($(picker).nextAll('.date-time-range-fields').find('.date-start').val());
  62. options.autoUpdateInput = true;
  63. }
  64. if ($(picker).nextAll('.date-time-range-fields').find('.date-end').val()) {
  65. options.endDate = new Date($(picker).nextAll('.date-time-range-fields').find('.date-end').val());
  66. options.autoUpdateInput = true;
  67. }
  68. $(picker).daterangepicker(options);
  69. $(picker).on('apply.daterangepicker', function (ev, pickerElm) {
  70. if ($(picker).hasClass('date-time-range')) {
  71. $(this).val(pickerElm.startDate.format('DD/MM/YY HH:mm') + ' - ' + pickerElm.endDate.format(options.locale.format));
  72. } else {
  73. $(this).val(pickerElm.startDate.format('DD/MM/YY') + ' - ' + pickerElm.endDate.format(options.locale.format));
  74. }
  75. if ($(picker).hasClass('date-time-range')) {
  76. $(picker).nextAll('.date-time-range-fields').find('.date-start').val(pickerElm.startDate.format('YYYY-MM-DD HH:mm'));
  77. $(picker).nextAll('.date-time-range-fields').find('.date-end').val(pickerElm.endDate.format('YYYY-MM-DD HH:mm'));
  78. } else {
  79. $(picker).nextAll('.date-time-range-fields').find('.date-start').val(pickerElm.startDate.format('YYYY-MM-DD'));
  80. $(picker).nextAll('.date-time-range-fields').find('.date-end').val(pickerElm.endDate.format('YYYY-MM-DD'));
  81. }
  82. });
  83. });
  84. }
  85. static setAutoCompleteField() {
  86. var autocompleteFields = $('[data-lc-autocomplete-url]');
  87. autocompleteFields.each(function () {
  88. var $this = $(this),
  89. url = $this.data('lc-autocomplete-url');
  90. SovTools.log($this);
  91. $this.autoComplete({
  92. noResultsText: 'Aucun résultat n\'a été trouvé.',
  93. resolverSettings: {
  94. url: url
  95. }
  96. });
  97. });
  98. }
  99. }