Skip to content

Commit

Permalink
add a custom format handler for the date picker.
Browse files Browse the repository at this point in the history
  • Loading branch information
robotdan committed Aug 2, 2018
1 parent b1ba12c commit fa62c86
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
2 changes: 1 addition & 1 deletion build.savant
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
savantVersion = "1.0.0"

project(group: "org.inversoft.prime", name: "prime.js", version: "1.2.1", licenses: ["ApacheV2_0"]) {
project(group: "org.inversoft.prime", name: "prime.js", version: "1.3.0", licenses: ["ApacheV2_0"]) {
workflow {
standard()
}
Expand Down
40 changes: 34 additions & 6 deletions src/main/js/Widgets/DateTimePicker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2017, Inversoft Inc., All Rights Reserved
* Copyright (c) 2015-2018, Inversoft Inc., All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -43,6 +43,7 @@ class DateTimePicker {
}

this.callback = null;
this.customFormatHandler = null;
this._setInitialOptions();
};

Expand Down Expand Up @@ -232,8 +233,6 @@ class DateTimePicker {
this.datepicker.queryFirst('header .next').addEventListener('click', this._handleNextMonth);
this.datepicker.queryFirst('header .prev').addEventListener('click', this._handlePreviousMonth);

this.callback = null;

PrimeDocument.addEventListener('click', this._handleGlobalClick);
PrimeDocument.addEventListener('keydown', this._handleGlobalKey);

Expand Down Expand Up @@ -272,9 +271,21 @@ class DateTimePicker {
}.bind(this));

this._rebuild();

if (this.customFormatHandler !== null) {
this.element.setValue(this.customFormatHandler.call(null, this.date));
}

return this;
}

/**
* @returns {Date} Return the current value of the time picker.
*/
getDate() {
return new Date(this.date.getTime());
}

/**
* Moves the DateTimePicker to the next month and redraws the calendar.
*
Expand Down Expand Up @@ -367,10 +378,15 @@ class DateTimePicker {
*/
setDate(newDate) {
this.date = newDate;
if (this.options.dateOnly) {
this.element.setValue(PrimeDate.toDateOnlyISOString(newDate));

if (this.customFormatHandler !== null) {
this.element.setValue(this.customFormatHandler.call(null, this.date));
} else {
this.element.setValue(newDate.toISOString());
if (this.options.dateOnly) {
this.element.setValue(PrimeDate.toDateOnlyISOString(newDate));
} else {
this.element.setValue(newDate.toISOString());
}
}

this._rebuild();
Expand Down Expand Up @@ -449,6 +465,18 @@ class DateTimePicker {
return this;
}

/**
* Sets a custom format handler responsible for formatting the date string that will be set into the input field.
* When not defined the default behavior will be used.
*
* @param formatHandler {Function} The handler function.
* @return {DateTimePicker} This.
*/
withCustomFormatHandler(formatHandler) {
this.customFormatHandler = formatHandler;
return this;
}

/**
* Render the DateTimePicker w/out the time picker. Only the calendar will be displayed and the input field will be updated with date only.
*
Expand Down

0 comments on commit fa62c86

Please sign in to comment.