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.

128 lines
5.0KB

  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. options.placeholder = "";
  19. // @TODO : génère une erreur
  20. if ($select.find('option[value=""]').length) {
  21. options.placeholder = $select.find('option[value=""]').html()
  22. }
  23. var myselect = $select.select2(options);
  24. myselect.on('select2:select', function (e) {
  25. var event = new Event('change');
  26. e.target.dispatchEvent(event);
  27. });
  28. myselect.on('select2:unselect', function (e) {
  29. var event = new Event('change');
  30. e.target.dispatchEvent(event);
  31. });
  32. myselect.off('select2:open')
  33. return myselect;
  34. }
  35. }
  36. static setDateRange() {
  37. $('.date-time-range, .date-range').each(function (i, picker) {
  38. let options = {
  39. autoUpdateInput: false,
  40. locale: {
  41. "format": "DD/MM/YY",
  42. "separator": " - ",
  43. "applyLabel": "Appliquer",
  44. "cancelLabel": "Annuler",
  45. "fromLabel": "Du",
  46. "toLabel": "au",
  47. "customRangeLabel": "Custom",
  48. "daysOfWeek": ["Di", "Lu", "Ma", "Me", "Je", "Ve", "Sa"],
  49. "monthNames": ["Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre"],
  50. "firstDay": 1
  51. }
  52. };
  53. if ($(picker).hasClass('date-time-range')) {
  54. options = Object.assign(options, {
  55. timePicker: true,
  56. timePickerIncrement: 30,
  57. timePicker24Hour: true,
  58. locale: {
  59. "format": "DD/MM/YYYY HH:mm",
  60. }
  61. });
  62. }
  63. if ($(picker).nextAll('.date-time-range-fields').find('.date-start').val()) {
  64. options.startDate = new Date($(picker).nextAll('.date-time-range-fields').find('.date-start').val());
  65. options.autoUpdateInput = true;
  66. }
  67. if ($(picker).nextAll('.date-time-range-fields').find('.date-end').val()) {
  68. options.endDate = new Date($(picker).nextAll('.date-time-range-fields').find('.date-end').val());
  69. options.autoUpdateInput = true;
  70. }
  71. $(picker).daterangepicker(options);
  72. $(picker).on('apply.daterangepicker', function (ev, pickerElm) {
  73. if ($(picker).hasClass('date-time-range')) {
  74. $(this).val(pickerElm.startDate.format('DD/MM/YY HH:mm') + ' - ' + pickerElm.endDate.format(options.locale.format));
  75. } else {
  76. $(this).val(pickerElm.startDate.format('DD/MM/YY') + ' - ' + pickerElm.endDate.format(options.locale.format));
  77. }
  78. if ($(picker).hasClass('date-time-range')) {
  79. $(picker).nextAll('.date-time-range-fields').find('.date-start').val(pickerElm.startDate.format('YYYY-MM-DD HH:mm'));
  80. $(picker).nextAll('.date-time-range-fields').find('.date-end').val(pickerElm.endDate.format('YYYY-MM-DD HH:mm'));
  81. } else {
  82. $(picker).nextAll('.date-time-range-fields').find('.date-start').val(pickerElm.startDate.format('YYYY-MM-DD'));
  83. $(picker).nextAll('.date-time-range-fields').find('.date-end').val(pickerElm.endDate.format('YYYY-MM-DD'));
  84. }
  85. });
  86. });
  87. }
  88. static setAutoCompleteField() {
  89. var autocompleteFields = $('[data-lc-autocomplete-url]');
  90. autocompleteFields.each(function () {
  91. var $this = $(this),
  92. url = $this.data('lc-autocomplete-url');
  93. $this.autoComplete({
  94. //bootstrapVersion: 4,
  95. //noResultsText: 'Aucun résultat n\'a été trouvé.',
  96. resolverSettings: {
  97. url: url
  98. }
  99. });
  100. });
  101. }
  102. static setCollectionWidget() {
  103. $('.field-collection[data-prototype]').each(function (i, collectionWidget) {
  104. SovWidgetCollection.setCollectionWidgetSortable($(collectionWidget));
  105. SovWidgetCollection.reindexKeyCollectionWidget($(collectionWidget));
  106. SovWidgetCollection.setCollectionWidgetAdd($(collectionWidget));
  107. SovWidgetCollection.setCollectionWidgetDelete($(collectionWidget));
  108. });
  109. }
  110. }