OpenProject is the leading open source project management software.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openproject/frontend/app/work_packages/models/datepicker.js

179 lines
5.7 KiB

//-- copyright
// OpenProject is a project management system.
// Copyright (C) 2012-2015 the OpenProject Foundation (OPF)
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License version 3.
//
// OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
// Copyright (C) 2006-2013 Jean-Philippe Lang
// Copyright (C) 2010-2013 the ChiliProject Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// See doc/COPYRIGHT.rdoc for more details.
//++
module.exports = function(TimezoneService, ConfigurationService, $timeout) {
10 years ago
var datepickerFormat = 'yy-mm-dd',
addButton = function(appendTo, text, className, callback) {
return jQuery('<button>', {
text: text
})
.addClass('ui-state-default ui-priority-primary ui-corner-all ' + className)
.appendTo(appendTo)
.on('click', callback);
};
function Datepicker(datepickerElem, textboxElem, date) {
this.date = date;
10 years ago
this.prevDate = TimezoneService.formattedISODate(this.date);
this.datepickerCont = angular.element(datepickerElem);
this.textbox = angular.element(textboxElem);
this.editTimerId = 0;
this._disabled = false;
this.initialize();
this.setDate(this.date);
}
Datepicker.prototype.addDoneButton = function() {
var self = this;
setTimeout(function() {
var buttonPane = self.datepickerCont.find('.ui-datepicker-buttonpane');
if (buttonPane.find('.ui-datepicker-done').length !== 0) {
return;
}
addButton(buttonPane, 'Done', 'ui-datepicker-done', function(e) {
self.onDone();
e.preventDefault();
});
}, 1);
};
Datepicker.prototype.setDate = function(date) {
if (date) {
10 years ago
this.datepickerCont.datepicker('option', 'defaultDate', TimezoneService.formattedISODate(date));
this.datepickerCont.datepicker('option', 'setDate', TimezoneService.formattedISODate(date));
this.textbox.val(TimezoneService.formattedISODate(date));
} else {
this.datepickerCont.datepicker('option', 'defaultDate', null);
this.datepickerCont.datepicker('option', 'setDate', null);
this.textbox.val('');
this.textbox.change();
}
};
Datepicker.prototype.initialize = function() {
var self = this,
10 years ago
firstDayOfWeek = ConfigurationService.startOfWeekPresent() ?
ConfigurationService.startOfWeek() :
jQuery.datepicker._defaults.firstDay;
this.datepickerCont.datepicker({
firstDay: firstDayOfWeek,
showWeeks: true,
changeMonth: true,
dateFormat: datepickerFormat,
10 years ago
defaultDate: TimezoneService.formattedISODate(self.date),
inline: true,
showButtonPanel: true,
beforeShow: function() {
self.addDoneButton();
},
onChangeMonthYear: function() {
self.addDoneButton();
},
alterOffset: function(offset) {
var wHeight = angular.element(window).height(),
dpHeight = angular.element('#ui-datepicker-div').height(),
inputTop = self.datepickerCont.offset().top,
inputHeight = self.datepickerCont.innerHeight();
if ((inputTop + inputHeight + dpHeight) > wHeight) {
offset.top -= inputHeight - 4;
}
return offset;
},
onSelect: function(selectedDate) {
10 years ago
if (!selectedDate || selectedDate === self.prevDate) {
return;
}
10 years ago
self.prevDate = TimezoneService.parseISODate(selectedDate);
$timeout(function() {
10 years ago
self.onChange(TimezoneService.formattedISODate(self.prevDate));
});
self.textbox.focus();
self.datepickerCont.hide();
self.addDoneButton();
}
});
};
Datepicker.prototype.hide = function() {
this.datepickerCont.hide();
};
Datepicker.prototype.show = function() {
if(!this._disabled) {
this.datepickerCont.show();
}
};
Datepicker.prototype.focus = function() {
this.textbox.click().focus();
};
Datepicker.prototype.disable = function () {
this._disabled = true;
this.hide();
};
Datepicker.prototype.enable = function () {
this._disabled = false;
};
Datepicker.prototype.setState = function (enable) {
this[enable ? 'enable' : 'disable']();
};
Datepicker.prototype.onChange = angular.noop;
Datepicker.prototype.onDone = angular.noop;
Datepicker.prototype.onEdit = function() {
var self = this;
if (self.textbox.val().trim() === '') {
self.onChange(null);
self.textbox.val('');
$timeout.cancel(self.editTimerId);
return;
}
$timeout.cancel(self.editTimerId);
self.editTimerId = $timeout(function() {
var date = self.textbox.val(),
10 years ago
isValid = TimezoneService.isValidISODate(date);
if (isValid) {
10 years ago
self.prevDate = TimezoneService.parseISODate(date);
self.onChange(TimezoneService.formattedISODate(self.prevDate));
} else {
10 years ago
self.textbox.val(TimezoneService.formattedISODate(self.prevDate));
}
}, 1000);
};
return Datepicker;
};