diff --git a/Plan/common/src/main/resources/assets/plan/web/js/query.js b/Plan/common/src/main/resources/assets/plan/web/js/query.js index 9cd05e6b1..0b2efaeae 100644 --- a/Plan/common/src/main/resources/assets/plan/web/js/query.js +++ b/Plan/common/src/main/resources/assets/plan/web/js/query.js @@ -1,26 +1,41 @@ var filterCount = 0; +/* { + id: "DOM id", + options... +}*/ +var filterQuery = []; + function addFilter(parentSelector, filterIndex) { - $(parentSelector).append(createElement(filters[filterIndex])); + const id = "f" + filterCount; + $(parentSelector).append(createElement(filters[filterIndex], id)); filterCount++; } -function createElement(filter) { +function createElement(filter, id) { switch (filter.kind) { case "activityIndexNow": - return createMultipleChoiceSelector(`are in Activity Groups`, filter.options); + return createMultipleChoiceSelector( + id, + `are in Activity Groups`, + filter.options + ); case "banned": - return createMultipleChoiceSelector(`are`, filter.options); + return createMultipleChoiceSelector(id, `are`, filter.options); case "operators": - return createMultipleChoiceSelector(`are`, filter.options); + return createMultipleChoiceSelector(id, `are`, filter.options); case "pluginGroups": - return createMultipleChoiceSelector(`are in ${filter.options.plugin} Groups`, filter.options); + return createMultipleChoiceSelector( + id, + `are in ${filter.options.plugin} Groups`, + filter.options + ); case "playedBetween": - return createBetweenSelector("Played between", filter.options); + return createBetweenSelector(id, "Played between", filter.options); case "registeredBetween": - return createBetweenSelector("Registered between", filter.options); + return createBetweenSelector(id, "Registered between", filter.options); default: - throw new Error("Unsupported filter kind: '" + filter.kind + "'") + throw new Error("Unsupported filter kind: '" + filter.kind + "'"); } } @@ -28,46 +43,120 @@ function createFilterSelector(parent, index, filter) { return `${filter.kind}`; } -function createBetweenSelector(label, options) { - var select = filterCount === 0 ? "of Players who " : "and "; - return `` + - `
` + +function createBetweenSelector(id, label, options) { + const query = { + id: id, + afterDate: options.after[0], + afterTime: options.after[1], + beforeDate: options.before[0], + beforeTime: options.before[1], + }; + filterQuery.push(query); + const select = filterCount === 0 ? "of Players who " : "and "; + return ( + `` + + `
` + `
` + `
` + - `` + + `` + `
` + - `
` + `
` + - `` + + `` + `
` + - `
` + - `
` + `
` + - `` + + `` + `
` + - `
` + `
` + - `` + + `` + `
` + - `
`; + `
` + ); } -function createMultipleChoiceSelector(label, options) { +function isValidDate(value) { + if (!value) return true; + const date = value.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4,5})$/); + return date ? new Date(date[3], date[2] - 1, date[1]) : null; +} + +function correctDate(value) { + const d = value.match( + /^(0\d{1}|\d{2})[\/|\-]?(0\d{1}|\d{2})[\/|\-]?(\d{4,5})$/ + ); + if (!d) return value; + + const date = d ? new Date(d[3], d[2] - 1, d[1]) : null; + const day = "" + (date.getUTCDate() + 1); + const month = "" + (date.getUTCMonth() + 1); + const year = "" + date.getUTCFullYear(); + return ( + (day.length === 1 ? "0" + day : day) + + "/" + + (month.length === 1 ? "0" + month : month) + + "/" + + year + ); +} + +function isValidTime(value) { + if (!value) return true; + const regex = /^[0-2][0-9]\:[0-5][0-9]$/; + return regex.test(value); +} + +function correctTime(value) { + const d = value.match(/^(\d{2})\:?(\d{2})$/); + if (!d) return value; + let hour = d[1]; + while (hour > 23) hour--; + let minute = d[2]; + while (minute > 59) minute--; + return hour + ":" + minute; +} + +function setFilterOption( + id, + elementId, + propertyName, + isValidFunction, + correctionFunction +) { + const query = filterQuery.find(function (f) { + return f.id === id; + }); + const element = $(`#${elementId}`); + let value = element.val(); + + console.log(element, value, id, elementId); + + value = window[correctionFunction].apply(element, [value]); + element.val(value); + + const isValid = window[isValidFunction].apply(element, [value]); + if (isValid) { + element.removeClass("is-invalid"); + query[propertyName] = value; + } else { + element.addClass("is-invalid"); + } +} + +function createMultipleChoiceSelector(id, label, options) { var select = filterCount === 0 ? "of Players who " : "and "; - var html = `
` + - `
- ` + + var html = + `
` + + `
` + `
`; return html; -} \ No newline at end of file +} diff --git a/Plan/common/src/main/resources/assets/plan/web/query.html b/Plan/common/src/main/resources/assets/plan/web/query.html index 26adb7a5e..7a0c4dbff 100644 --- a/Plan/common/src/main/resources/assets/plan/web/query.html +++ b/Plan/common/src/main/resources/assets/plan/web/query.html @@ -319,8 +319,6 @@