sparnatural
Version:
Visual client-side SPARQL query builder and knowledge graph exploration tool
152 lines • 8.6 kB
JavaScript
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _TimeDatePickerWidget_instances, _TimeDatePickerWidget_addValueBtnClicked, _TimeDatePickerWidget_getFirstDayYear, _TimeDatePickerWidget_getLastDayOfYear, _TimeDatePickerWidget_getValueLabel, _TimeDatePickerWidget_isValidDate;
import { AddUserInputBtn } from "../../buttons/AddUserInputBtn";
import { InfoBtn } from "../../buttons/InfoBtn";
import { AbstractWidget, ValueRepetition } from "../AbstractWidget";
import "@chenfengyuan/datepicker";
import { DataFactory } from 'rdf-data-factory';
import { I18n } from "../../../settings/I18n";
import { TOOLTIP_CONFIG } from "../../../settings/defaultSettings";
const factory = new DataFactory();
export class DateTimePickerValue {
key() {
return JSON.stringify(this.value.start).replace(/["]+/g, '') + " - " + JSON.stringify(this.value.stop).replace(/["]+/g, '');
}
constructor(v) {
this.value = v;
}
}
export class TimeDatePickerWidget extends AbstractWidget {
constructor(parentComponent, dateFormat, startClassCal, objectPropVal, endClassVal, specProvider) {
super("timedatepicker-widget", parentComponent, null, startClassCal, objectPropVal, endClassVal, ValueRepetition.SINGLE);
_TimeDatePickerWidget_instances.add(this);
_TimeDatePickerWidget_addValueBtnClicked.set(this, () => {
// fix for negative years
// set a minus in front of the date if there was one in the value
let startDate;
if (this.inputStart.val() != '') {
startDate = this.inputStart.datepicker("getDate");
if (this.inputStart.val().startsWith("-") && !startDate.toISOString().startsWith("-")) {
startDate.setFullYear(parseInt(this.inputStart.val().toString()));
}
}
let endDate;
if (this.inputEnd.val() != '') {
endDate = this.inputEnd.datepicker("getDate");
if (this.inputEnd.val().startsWith("-") && !endDate.toISOString().startsWith("-")) {
endDate.setFullYear(parseInt(this.inputEnd.val().toString()));
}
}
let stringDateTimeVal = {
label: null,
start: (startDate) ? startDate.toISOString() : null,
stop: (endDate) ? endDate.toISOString() : null,
};
let widgetVal = this.parseInput(stringDateTimeVal);
if (!widgetVal)
return;
this.triggerRenderWidgetVal(widgetVal);
});
_TimeDatePickerWidget_getValueLabel.set(this, function (startLabel, stopLabel) {
let valueLabel = "";
if ((startLabel != "") && (stopLabel != "")) {
valueLabel = I18n.labels.LabelDateFrom + ' ' + startLabel + ' ' + I18n.labels.LabelDateTo + ' ' + stopLabel;
}
else if (startLabel != "") {
valueLabel = I18n.labels.DisplayValueDateFrom + ' ' + startLabel;
}
else if (stopLabel != "") {
valueLabel = I18n.labels.DisplayValueDateTo + ' ' + stopLabel;
}
return valueLabel;
});
this.dateFormat = dateFormat;
this.specProvider = specProvider;
}
render() {
super.render();
this.html.append($(`<span>${I18n.labels.LabelDateFrom} </span>`));
this.inputStart = $(`<input id="input-start" placeholder="${I18n.labels.TimeWidgetDateFrom}" autocomplete="off" class="${this.dateFormat}" />`);
this.inputEnd = $(`<input id="input-end" placeholder="${I18n.labels.TimeWidgetDateTo}" autocomplete="off" class="${this.dateFormat}" />`);
this.inputValue = $(`<input id="input-value" type="hidden"/>`);
let span = $(`<span> ${I18n.labels.LabelDateTo} </span>`);
this.html
.append(this.inputStart)
.append(span)
.append(this.inputEnd)
.append(this.inputValue);
// Build datatippy info
let datatippy = this.dateFormat == "day"
? I18n.labels.TimeWidgetDateHelp
: I18n.labels.TimeWidgetYearHelp;
// set a tooltip on the info circle
var tippySettings = Object.assign({}, TOOLTIP_CONFIG);
tippySettings.placement = "left";
tippySettings.trigger = "click";
tippySettings.offset = [this.dateFormat == "day" ? 75 : 50, -20];
tippySettings.delay = [0, 0];
this.infoBtn = new InfoBtn(this, datatippy, tippySettings).render();
//finish datatippy
this.addValueBtn = new AddUserInputBtn(this, I18n.labels.ButtonAdd, __classPrivateFieldGet(this, _TimeDatePickerWidget_addValueBtnClicked, "f")).render();
let calendarFormat = (this.dateFormat === "day")
? I18n.labels.TimeWidgetDateFormat
: I18n.labels.TimeWidgetYearFormat;
var options = {
language: I18n.labels.LangCodeTimeDate,
autoHide: true,
format: calendarFormat,
date: null,
startView: 2,
};
this.inputStart.datepicker(options);
this.inputEnd.datepicker(options);
return this;
}
parseInput(input) {
if (!__classPrivateFieldGet(this, _TimeDatePickerWidget_instances, "m", _TimeDatePickerWidget_isValidDate).call(this, input.start) && !__classPrivateFieldGet(this, _TimeDatePickerWidget_instances, "m", _TimeDatePickerWidget_isValidDate).call(this, input.stop))
throw Error('No valid Date received');
let startValue = (__classPrivateFieldGet(this, _TimeDatePickerWidget_instances, "m", _TimeDatePickerWidget_isValidDate).call(this, input.start)) ? new Date(input.start) : null;
let endValue = (__classPrivateFieldGet(this, _TimeDatePickerWidget_instances, "m", _TimeDatePickerWidget_isValidDate).call(this, input.stop)) ? new Date(input.stop) : null;
if (startValue && endValue && (startValue > endValue))
throw Error('StartDate is bigger than Enddate!');
let tmpValue;
if (this.dateFormat == "day") {
tmpValue = {
start: (startValue) ? new Date(startValue.setHours(0, 0, 0, 0)) : null,
stop: (endValue) ? new Date(endValue.setHours(23, 59, 59, 59)) : null,
startLabel: startValue ? startValue.toLocaleDateString() : "",
endLabel: endValue ? endValue.toLocaleDateString() : ""
};
}
else {
tmpValue = {
start: __classPrivateFieldGet(this, _TimeDatePickerWidget_instances, "m", _TimeDatePickerWidget_getFirstDayYear).call(this, startValue),
stop: __classPrivateFieldGet(this, _TimeDatePickerWidget_instances, "m", _TimeDatePickerWidget_getLastDayOfYear).call(this, endValue),
startLabel: startValue ? startValue.getFullYear().toString() : "",
endLabel: endValue ? endValue.getFullYear().toString() : ""
};
}
let dateTimePickerVal = new DateTimePickerValue({
label: __classPrivateFieldGet(this, _TimeDatePickerWidget_getValueLabel, "f").call(this, tmpValue.startLabel, tmpValue.endLabel),
start: tmpValue.start,
stop: tmpValue.stop,
});
return dateTimePickerVal;
}
}
_TimeDatePickerWidget_addValueBtnClicked = new WeakMap(), _TimeDatePickerWidget_getValueLabel = new WeakMap(), _TimeDatePickerWidget_instances = new WeakSet(), _TimeDatePickerWidget_getFirstDayYear = function _TimeDatePickerWidget_getFirstDayYear(startValue) {
return startValue ?
new Date(startValue.getFullYear(), 0, 1, 0, 0, 1, 0)
: null;
}, _TimeDatePickerWidget_getLastDayOfYear = function _TimeDatePickerWidget_getLastDayOfYear(endValue) {
return endValue ?
new Date(endValue.getFullYear(), 11, 31, 23, 59, 59)
: null;
}, _TimeDatePickerWidget_isValidDate = function _TimeDatePickerWidget_isValidDate(dateString) {
return (new Date(dateString).toString() !== "Invalid Date") && !isNaN(Date.parse(dateString));
};
//# sourceMappingURL=TimeDatePickerWidget.js.map