|
1 | | -jQuery(function ($) { |
2 | | - var datepickerDict = {}; |
3 | | - var isBootstrap4 = $.fn.collapse.Constructor.VERSION.split('.').shift() == "4"; |
4 | | - function fixMonthEndDate(e, picker) { |
5 | | - e.date && picker.val().length && picker.val(e.date.endOf('month').format('YYYY-MM-DD')); |
| 1 | +(function (factory) { |
| 2 | + if (typeof define === 'function' && define.amd) |
| 3 | + define(['jquery'], factory) |
| 4 | + else if (typeof module === 'object' && module.exports) |
| 5 | + module.exports = factory(require('jquery')) |
| 6 | + else |
| 7 | + factory(jQuery) |
| 8 | +}(function ($) { |
| 9 | + var datepickerDict = {}; |
| 10 | + const bootstrapVersion = (window.bootstrap?.Collapse||$.fn.collapse.Constructor).VERSION; |
| 11 | + var isBootstrap4 = bootstrapVersion.split('.').shift() === "4"; |
| 12 | + var isBootstrap5 = bootstrapVersion.split('.').shift() === "5"; |
| 13 | + function fixMonthEndDate(e, picker) { |
| 14 | + e.date && picker.val().length && picker.val(e.date.endOf('month').format('YYYY-MM-DD')); |
| 15 | + } |
| 16 | + function initOnePicker(element, options) { |
| 17 | + var $element = $(element), data = {}; |
| 18 | + try { |
| 19 | + data = JSON.parse($element.attr('data-dp-config')); |
6 | 20 | } |
7 | | - $("[dp_config]:not([disabled])").each(function (i, element) { |
8 | | - var $element = $(element), data = {}; |
9 | | - try { |
10 | | - data = JSON.parse($element.attr('dp_config')); |
11 | | - } |
12 | | - catch (x) { } |
13 | | - if (data.id && data.options) { |
14 | | - data.$element = $element.datetimepicker(data.options); |
15 | | - data.datepickerdata = $element.data("DateTimePicker"); |
16 | | - datepickerDict[data.id] = data; |
17 | | - data.$element.next('.input-group-addon').on('click', function(){ |
18 | | - data.datepickerdata.show(); |
19 | | - }); |
20 | | - if(isBootstrap4){ |
21 | | - data.$element.on("dp.show", function (e) { |
22 | | - $('.collapse.in').addClass('show'); |
23 | | - }); |
24 | | - } |
25 | | - } |
| 21 | + catch (x) { } |
| 22 | + $.extend(1, data.options, options); |
| 23 | + if (data.id && data.options) { |
| 24 | + data.$element = $element.datetimepicker(data.options); |
| 25 | + data.datepickerdata = $element.data("DateTimePicker"); |
| 26 | + data.$element.next('.input-group-addon').on('click', function() { |
| 27 | + data.datepickerdata.show(); |
| 28 | + }); |
| 29 | + if (isBootstrap4 || isBootstrap5) { |
| 30 | + data.$element.on("dp.show", function(e) { |
| 31 | + $('.collapse.in').addClass('show'); |
| 32 | + }); |
| 33 | + } |
| 34 | + } |
| 35 | + return data; |
| 36 | + }; |
| 37 | + function initLinkedPickers(to_picker) { |
| 38 | + var from_picker = datepickerDict[to_picker.linked_to]; |
| 39 | + from_picker.datepickerdata.maxDate(to_picker.datepickerdata.date() || false); |
| 40 | + to_picker.datepickerdata.minDate(from_picker.datepickerdata.date() || false); |
| 41 | + from_picker.$element.on("dp.change", function (e) { |
| 42 | + to_picker.datepickerdata.minDate(e.date || false); |
26 | 43 | }); |
27 | | - $.each(datepickerDict, function (id, to_picker) { |
28 | | - if (to_picker.linked_to) { |
29 | | - var from_picker = datepickerDict[to_picker.linked_to]; |
30 | | - from_picker.datepickerdata.maxDate(to_picker.datepickerdata.date() || false); |
31 | | - to_picker.datepickerdata.minDate(from_picker.datepickerdata.date() || false); |
32 | | - from_picker.$element.on("dp.change", function (e) { |
33 | | - to_picker.datepickerdata.minDate(e.date || false); |
34 | | - }); |
35 | | - to_picker.$element.on("dp.change", function (e) { |
36 | | - if (to_picker.picker_type == 'MONTH') fixMonthEndDate(e, to_picker.$element); |
37 | | - from_picker.datepickerdata.maxDate(e.date || false); |
38 | | - }); |
39 | | - if (to_picker.picker_type == 'MONTH') { |
40 | | - to_picker.$element.on("dp.hide", function (e) { |
41 | | - fixMonthEndDate(e, to_picker.$element); |
42 | | - }); |
43 | | - fixMonthEndDate({ date: to_picker.datepickerdata.date() }, to_picker.$element); |
44 | | - } |
45 | | - } |
| 44 | + to_picker.$element.on("dp.change", function (e) { |
| 45 | + if (to_picker.picker_type == 'MONTH') fixMonthEndDate(e, to_picker.$element); |
| 46 | + from_picker.datepickerdata.maxDate(e.date || false); |
46 | 47 | }); |
47 | | - if(isBootstrap4) { |
48 | | - $('body').on('show.bs.collapse','.bootstrap-datetimepicker-widget .collapse',function(e){ |
49 | | - $(e.target).addClass('in'); |
50 | | - }); |
51 | | - $('body').on('hidden.bs.collapse','.bootstrap-datetimepicker-widget .collapse',function(e){ |
52 | | - $(e.target).removeClass('in'); |
53 | | - }); |
| 48 | + if (to_picker.picker_type == 'MONTH') { |
| 49 | + to_picker.$element.on("dp.hide", function (e) { |
| 50 | + fixMonthEndDate(e, to_picker.$element); |
| 51 | + }); |
| 52 | + fixMonthEndDate({ date: to_picker.datepickerdata.date() }, to_picker.$element); |
| 53 | + } |
| 54 | + }; |
| 55 | + $.fn.djangoDatetimePicker = function(options){ |
| 56 | + options = options || {}; |
| 57 | + var newPickers = {}; |
| 58 | + $.each(this, function (i, element) { |
| 59 | + var picker = initOnePicker($(element), options); |
| 60 | + newPickers[picker.id] = picker; |
| 61 | + }); |
| 62 | + $.extend(datepickerDict, newPickers); |
| 63 | + $.each(newPickers, function (i, picker) { |
| 64 | + if (picker.linked_to) |
| 65 | + initLinkedPickers(picker); |
| 66 | + }); |
| 67 | + return this; |
| 68 | + } |
| 69 | + $(function(){ |
| 70 | + $("[data-dp-config]:not([disabled])").djangoDatetimePicker(); |
| 71 | + if (isBootstrap4 || isBootstrap5) { |
| 72 | + $('body').on('show.bs.collapse','.bootstrap-datetimepicker-widget .collapse',function(e){ |
| 73 | + $(e.target).addClass('in'); |
| 74 | + }); |
| 75 | + $('body').on('hidden.bs.collapse','.bootstrap-datetimepicker-widget .collapse',function(e){ |
| 76 | + $(e.target).removeClass('in'); |
| 77 | + }); |
| 78 | + } |
| 79 | + if(isBootstrap5){ |
| 80 | + $('.input-group-addon[data-target="#datetimepicker1"]').each(function (){ |
| 81 | + $(this).attr('data-bs-target','#datetimepicker1').removeAttr('data-target') |
| 82 | + $(this).attr('data-bs-toggle','datetimepickerv').removeAttr('data-toggle') |
| 83 | + }) |
54 | 84 | } |
55 | | -}); |
| 85 | + }); |
| 86 | +})); |
0 commit comments