forked from evgenyvas/bootstrap-editable-datetimepicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap-editable-datetimepicker.js
executable file
·160 lines (130 loc) · 4.67 KB
/
bootstrap-editable-datetimepicker.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
Bootstrap-datetimepicker.
Description and examples: http://eonasdan.github.io/bootstrap-datetimepicker/
@class datetime2
@extends abstractinput
@final
@example
**/
(function ($) {
"use strict";
var Date = function (options) {
this.init('datetime2', options, Date.defaults);
this.initPicker(options, Date.defaults);
};
$.fn.editableutils.inherit(Date, $.fn.editabletypes.abstractinput);
$.extend(Date.prototype, {
dateValue: null,
initPicker: function (options, defaults) {
//'format' is set directly from settings or data-* attributes
//try parse datetimepicker config defined as json string in data-datetimepicker
options.datetimepicker = $.fn.editableutils.tryParseJson(options.datetimepicker, true);
//overriding datetimepicker config (as by default jQuery extend() is not recursive)
this.options.datetimepicker = $.extend({}, defaults.datetimepicker, options.datetimepicker, {
format: this.options.format
});
},
render: function () {
var self = this;
this.$input.datetimepicker(this.options.datetimepicker);
//"clear" link
if (this.options.clear) {
this.$clear = $('<a href="#"></a>').html(this.options.clear).click($.proxy(function (e) {
e.preventDefault();
e.stopPropagation();
this.clear();
}, this));
this.$tpl.parent().append($('<div class="editable-clear">').append(this.$clear));
}
},
value2html: function (value, element) {
var text = this.value2str(value);
Date.superclass.value2html.call(this, text, element);
},
html2value: function (html) {
return this.parseDate(html, this.options.format);
},
value2str: function (value) {
return value ? moment(value, this.options.format).format(this.options.format) : '';
},
str2value: function (str) {
return this.parseDate(str, this.options.format);
},
value2submit: function (value) {
return this.value2str(value);
},
value2input: function (value) {
if (!value) {
value = moment();
}
var new_date = moment(value, this.options.format);
this.$input.data("DateTimePicker").date(new_date);
},
input2value: function () {
var date = this.$input.data("DateTimePicker").date();
return date ? date.format(this.options.format) : '';
},
activate: function () {
},
clear: function () {
this.$input.data("DateTimePicker").clear();
},
autosubmit: function () {
// not implemented yet
},
/*
For incorrect date bootstrap-datepicker returns current date that is not suitable
for datefield.
This function returns null for incorrect date.
*/
parseDate: function (str, format) {
var date = null, formattedBack;
if (str) {
date = moment(str, format).format(format);
if (typeof str === 'string') {
formattedBack = moment(date, format).format(format);
if (str !== formattedBack) {
date = null;
}
}
}
return date;
}
});
Date.defaults = $.extend({}, $.fn.editabletypes.abstractinput.defaults, {
/**
@property tpl
@default <div></div>
**/
tpl: '<div class="editable-date well"></div>',
/**
Format used for sending value to server. Also applied when converting date from <code>data-value</code> attribute.<br>
more info: http://momentjs.com/docs/#/displaying/format/
@property format
@type string
@default YYYY-MM-DD
**/
format: 'YYYY-MM-DD',
/**
Configuration of datetimepicker.
Full list of options: http://eonasdan.github.io/bootstrap-datetimepicker/Options/
@property datetimepicker
@type object
@default {
inline: true,
}
**/
datetimepicker: {
inline: true,
},
/**
Text shown as clear date button.
If <code>false</code> clear button will not be rendered.
@property clear
@type boolean|string
@default 'x clear'
**/
clear: '× clear'
});
$.fn.editabletypes.datetime2 = Date;
}(window.jQuery));