Last active
January 17, 2019 13:57
-
-
Save tvaliasek/b0e760317ab7e5418a4c2d425cb38bee to your computer and use it in GitHub Desktop.
Nextras Forms tempus dominus datetime picker init
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This file is part of the forked Nextras community extensions of Nette Framework | |
* | |
* @license MIT | |
* @link https://github.com/nextras/forms | |
* @author Jan Skrasek | |
* @author Tomas Valiasek | |
*/ | |
// main init function, function is appended to $.fn | |
jQuery.fn.extend({ | |
reinitDatetimePickers: function () { | |
$('input.date, input.datetime-local').each(function (i, el) { | |
el = $(el); | |
el.get(0).type = 'text'; | |
var $val = el.attr('value'); | |
var $options = { | |
debug: false, | |
startDate: el.attr('min'), | |
endDate: el.attr('max'), | |
format: el.is('.date') ? 'D.M.YYYY' : 'D.M.YYYY - H:mm', | |
locale: 'cs', | |
sideBySide: true, | |
buttons: { | |
showToday: false, | |
showClose: true, | |
showClear: false | |
}, | |
allowInputToggle: true, | |
ignoreReadonly: true, | |
defaultDate: ($val !== false && $val !== undefined) ? moment($val, moment.ISO_8601) : false | |
}; | |
el.datetimepicker($options); | |
if ($val !== false && $val !== undefined) { | |
el.val(moment($val, moment.ISO_8601).format(el.is('.date') ? 'D.M.YYYY' : 'D.M.YYYY - H:mm')); | |
} | |
$(this).on('focusin', function() { | |
$(this).datetimepicker('show'); | |
}); | |
$(this).on('focusout', function() { | |
$(this).datetimepicker('hide'); | |
}); | |
}); | |
} | |
}); | |
// initialization | |
jQuery(function ($) { | |
$.fn.reinitDatetimePickers(); | |
// nette.ajax.js extension for reinit of date pickers after aplying snippet | |
$.nette.ext('datepickers', { | |
complete: function(payload, status, jqXHR, settings){ | |
if(payload.hasOwnProperty('snippets')){ | |
for (var index in payload.snippets) { | |
var snippet = payload.snippets[index] | |
if ( | |
snippet.indexOf('type="date') > -1 || | |
snippet.indexOf('type="datetime-local') > -1 || | |
snippet.indexOf('type="datetime') > -1 | |
) { | |
$.fn.reinitDatetimePickers(); | |
break; | |
} | |
} | |
} | |
} | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment