2015-01-23 13:09:30 +00:00
|
|
|
|
2016-09-26 09:28:47 +00:00
|
|
|
var Calendar = require ('./calendar');
|
|
|
|
|
|
|
|
module.exports = new Class
|
2015-01-23 13:09:30 +00:00
|
|
|
({
|
|
|
|
Extends: Htk.Field
|
|
|
|
,Tag: 'htk-date-chooser'
|
|
|
|
|
|
|
|
,format: _('%a, %e %b %Y')
|
|
|
|
,calendar: null
|
|
|
|
,ignoreCalendarChange: false
|
|
|
|
|
2016-10-16 14:16:08 +00:00
|
|
|
,render: function ()
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2016-10-16 14:16:08 +00:00
|
|
|
var node = this.createRoot ('div');
|
|
|
|
node.className = 'htk-date-chooser';
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2016-10-16 14:16:08 +00:00
|
|
|
this.label = this.createElement ('span');
|
2015-01-23 13:09:30 +00:00
|
|
|
this.node.appendChild (this.label);
|
|
|
|
|
|
|
|
this.setEditable (this._editable);
|
|
|
|
}
|
|
|
|
|
|
|
|
,putValue: function (value)
|
|
|
|
{
|
|
|
|
var dateString;
|
|
|
|
|
|
|
|
if (value instanceof Date)
|
|
|
|
dateString = Vn.Date.strftime (value, this.format);
|
|
|
|
else
|
|
|
|
dateString = '';
|
|
|
|
|
|
|
|
Vn.Node.setText (this.label, dateString);
|
|
|
|
}
|
|
|
|
|
|
|
|
,setEditable: function (editable)
|
|
|
|
{
|
2015-12-10 13:48:43 +00:00
|
|
|
Vn.Node.remove (this.label);
|
|
|
|
|
|
|
|
if (this.calendar)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2015-12-10 13:48:43 +00:00
|
|
|
this.node.removeChild (this.button);
|
|
|
|
|
|
|
|
this.calendar.disconnectByInstance (this);
|
|
|
|
this.calendar = null;
|
|
|
|
}
|
2015-02-08 15:38:38 +00:00
|
|
|
|
2015-12-10 13:48:43 +00:00
|
|
|
if (editable && !this.calendar)
|
|
|
|
{
|
2016-10-16 14:16:08 +00:00
|
|
|
this.button = this.createElement ('button');
|
2015-10-23 23:23:19 +00:00
|
|
|
this.button.className = 'input';
|
2015-01-23 13:09:30 +00:00
|
|
|
this.button.title = _('ChangeDate');
|
2015-12-10 13:48:43 +00:00
|
|
|
this.button.addEventListener ('click', this._onClick.bind (this));
|
2015-02-08 15:38:38 +00:00
|
|
|
this.button.appendChild (this.label);
|
|
|
|
this.node.appendChild (this.button);
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2016-09-26 09:28:47 +00:00
|
|
|
this.calendar = new Calendar ();
|
2015-12-10 13:48:43 +00:00
|
|
|
this.calendar.on ('changed', this._onCalendarChange.bind (this));
|
2015-03-06 23:33:54 +00:00
|
|
|
|
|
|
|
this.popup = new Htk.Popup ();
|
|
|
|
this.popup.setChild (this.calendar);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
else if (!editable)
|
2015-02-08 15:38:38 +00:00
|
|
|
this.node.appendChild (this.label);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 13:48:43 +00:00
|
|
|
,_onCalendarChange: function (calendar)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
|
|
|
if (this.ignoreCalendarChange)
|
|
|
|
return;
|
|
|
|
|
2015-03-06 23:33:54 +00:00
|
|
|
this.popup.hide ();
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
var newDate = calendar.value;
|
|
|
|
this.putValue (newDate);
|
|
|
|
this.valueChanged (newDate);
|
|
|
|
}
|
|
|
|
|
2015-12-10 13:48:43 +00:00
|
|
|
,_onClick: function (event)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
|
|
|
this.ignoreCalendarChange = true;
|
|
|
|
this.calendar.value = this._value;
|
|
|
|
this.calendar.goToSelectedMonth ();
|
|
|
|
this.ignoreCalendarChange = false;
|
2015-03-06 23:33:54 +00:00
|
|
|
|
|
|
|
this.popup.show (this.button);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|