/
jquery.jeditable.datepicker.js
74 lines (64 loc) · 2.17 KB
/
jquery.jeditable.datepicker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* Datepicker for Jeditable
*
* Copyright (c) 2011 Piotr 'Qertoip' Włodarek
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
* Depends on jQuery UI Datepicker
*
* Project home:
* http://github.com/qertoip/jeditable-datepicker
*
*/
// add :focus selector
jQuery.expr[':'].focus = function( elem ) {
return elem === document.activeElement && ( elem.type || elem.href );
};
$.editable.addInputType( 'datepicker', {
/* create input element */
element: function( settings, original ) {
var form = $( this ),
input = $( '<input />' );
input.attr( 'autocomplete','off' );
form.append( input );
return input;
},
/* attach jquery.ui.datepicker to the input element */
plugin: function( settings, original ) {
var form = this,
input = form.find( "input" );
// Don't cancel inline editing onblur to allow clicking datepicker
settings.onblur = 'nothing';
datepicker = {
onSelect: function() {
// clicking specific day in the calendar should
// submit the form and close the input field
form.submit();
},
onClose: function() {
setTimeout( function() {
if ( !input.is( ':focus' ) ) {
// input has NO focus after 150ms which means
// calendar was closed due to click outside of it
// so let's close the input field without saving
original.reset( form );
} else {
// input still HAS focus after 150ms which means
// calendar was closed due to Enter in the input field
// so lets submit the form and close the input field
form.submit();
}
// the delay is necessary; calendar must be already
// closed for the above :focus checking to work properly;
// without a delay the form is submitted in all scenarios, which is wrong
}, 150 );
}
};
if (settings.datepicker) {
jQuery.extend(datepicker, settings.datepicker);
}
input.datepicker(datepicker);
}
} );