You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lo...@apache.org on 2021/04/22 10:24:59 UTC

[myfaces-tobago] branch master updated: feat: date min max

This is an automated email from the ASF dual-hosted git repository.

lofwyr pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/myfaces-tobago.git


The following commit(s) were added to refs/heads/master by this push:
     new c10c578  feat: date min max
c10c578 is described below

commit c10c5785b7793b038d0d43fbc07147b0c17ce10c
Author: Udo Schnurpfeil <ud...@irian.eu>
AuthorDate: Thu Apr 22 11:50:27 2021 +0200

    feat: date min max
    
    Inital implementation
    
    issue: TOBAGO-1766
---
 .../tobago/internal/component/AbstractUIDate.java  |  4 +++
 .../internal/renderkit/renderer/DateRenderer.java  | 16 ++++++++++++
 .../taglib/component/DateTagDeclaration.java       | 14 +++++++++++
 .../renderkit/renderer/DateRendererUnitTest.java   | 14 +++++++++++
 .../src/test/resources/renderer/date/minMax.html   | 25 +++++++++++++++++++
 .../tobago/example/demo/DateController.java        | 26 +++++++++++++++++++
 .../20-component/010-input/40-date/Date.xhtml      |  8 ++++++
 .../tobago-theme-standard/src/main/js/tobago.js    | 26 +++++++++++++++----
 .../src/main/js/tobago.js.map                      |  2 +-
 .../src/main/js/tobago.min.js                      |  4 +--
 .../src/main/js/tobago.min.js.map                  |  2 +-
 .../src/main/ts/tobago-date.ts                     | 29 +++++++++++++++++-----
 .../org/apache/myfaces/tobago/apt/component.stg    |  4 +++
 13 files changed, 159 insertions(+), 15 deletions(-)

diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUIDate.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUIDate.java
index ea7fb6c..f3c6db7 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUIDate.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUIDate.java
@@ -46,4 +46,8 @@ public abstract class AbstractUIDate extends AbstractUIInput {
   }
 
   public abstract boolean isTodayButton();
+
+  public abstract Object getMin();
+
+  public abstract Object getMax();
 }
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/DateRenderer.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/DateRenderer.java
index fed828a..41f2011 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/DateRenderer.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/DateRenderer.java
@@ -50,6 +50,7 @@ import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.LocalTime;
 import java.time.ZonedDateTime;
+import java.time.format.DateTimeFormatter;
 import java.util.Calendar;
 import java.util.Date;
 
@@ -154,6 +155,8 @@ public class DateRenderer<T extends AbstractUIDate> extends MessageLayoutRendere
 //    if (!disabled && !readonly) {
 //      writer.writeAttribute(HtmlAttributes.PLACEHOLDER, component.getPlaceholder(), true);
 //    }
+    writer.writeAttribute(HtmlAttributes.MIN, convertToString(component.getMin()), true);
+    writer.writeAttribute(HtmlAttributes.MAX, convertToString(component.getMax()), true);
 
     writer.writeClassAttribute(
         BootstrapClass.borderColor(ComponentUtils.getMaximumSeverity(component)),
@@ -175,6 +178,19 @@ public class DateRenderer<T extends AbstractUIDate> extends MessageLayoutRendere
     }
   }
 
+  private String convertToString(Object value) {
+    if (value == null) {
+      return null;
+    } else if (value instanceof String) {
+      return (String) value;
+    } else if (value instanceof LocalDate) {
+      return ((LocalDate) value).format(DateTimeFormatter.ISO_LOCAL_DATE);
+    } else {
+      LOG.warn("Unknown type for min/max: '{}'", value);
+      return value.toString();
+    }
+  }
+
   private void encodeButton(final FacesContext facesContext, final T component, final FaIcons icon)
       throws IOException {
 
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/taglib/component/DateTagDeclaration.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/taglib/component/DateTagDeclaration.java
index 79e9c27..a4c6255 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/taglib/component/DateTagDeclaration.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/taglib/component/DateTagDeclaration.java
@@ -90,4 +90,18 @@ public interface DateTagDeclaration
   @TagAttribute
   @UIComponentTagAttribute(type = "boolean", defaultValue = "false")
   void setTodayButton(String required);
+
+  /**
+   * Sets the minimum value of the date.
+   */
+  @TagAttribute
+  @UIComponentTagAttribute(type = {"java.time.LocalDate", "java.util.Date"})
+  void setMin(String min);
+
+  /**
+   * Sets the maximum value of the date.
+   */
+  @TagAttribute
+  @UIComponentTagAttribute(type = {"java.time.LocalDate", "java.util.Date"})
+  void setMax(String max);
 }
diff --git a/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/renderkit/renderer/DateRendererUnitTest.java b/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/renderkit/renderer/DateRendererUnitTest.java
index c72c948..c54a7a4 100644
--- a/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/renderkit/renderer/DateRendererUnitTest.java
+++ b/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/renderkit/renderer/DateRendererUnitTest.java
@@ -147,6 +147,20 @@ public class DateRendererUnitTest extends RendererTestBase {
   }
 
   @Test
+  public void minMax() throws IOException {
+
+    final UIDate d = (UIDate) ComponentUtils.createComponent(
+        facesContext, Tags.date.componentType(), RendererTypes.Date, "id");
+    d.setValue(SPUTNIK_LOCAL_DATE);
+    d.setMin(SPUTNIK_LOCAL_DATE.minusDays(30));
+    d.setMax(SPUTNIK_LOCAL_DATE.plusDays(30));
+
+    d.encodeAll(facesContext);
+
+    Assertions.assertEquals(loadHtml("renderer/date/minMax.html"), formattedResult());
+  }
+
+  @Test
   public void localDateTimeAuto() throws IOException {
 
     final UIDate d = (UIDate) ComponentUtils.createComponent(
diff --git a/tobago-core/src/test/resources/renderer/date/minMax.html b/tobago-core/src/test/resources/renderer/date/minMax.html
new file mode 100644
index 0000000..569606a
--- /dev/null
+++ b/tobago-core/src/test/resources/renderer/date/minMax.html
@@ -0,0 +1,25 @@
+<!--
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+-->
+
+<tobago-date id='id' class='tobago-auto-spacing' i18n='{"months":["January","February","March","April","May","June","July","August","September","October","November","December"],"monthsShort":["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],"days":["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],"daysShort":["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],"daysMin":["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],"firstDay":0,"minDays":1,"today":"Tod [...]
+  <div class='tobago-input-group-outer'>
+    <div class='input-group'>
+      <input type='date' name='id' id='id::field' value='1957-10-05' min='1957-09-05' max='1957-11-04' class='form-control'>
+      <button class='btn btn-secondary tobago-date-picker' type='button' title='Date Picker'><i class='fa fa-calendar'></i></button>
+    </div>
+  </div>
+</tobago-date>
\ No newline at end of file
diff --git a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/DateController.java b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/DateController.java
index 38740cf..8662fbc 100644
--- a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/DateController.java
+++ b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/DateController.java
@@ -50,6 +50,12 @@ public class DateController implements Serializable {
   private LocalDateTime sputnikLdt = SPUTNIK_LOCAL_DATE_TIME;
   private LocalDate sputnikLd = APOLLO11_LOCAL_DATE_TIME.toLocalDate();
 
+  private final LocalDate today = LocalDate.now();
+
+  private LocalDate party;
+  private final LocalDate partyMin = today.plusDays(3);
+  private final LocalDate partyMax = today.plusDays(10);
+
   public DateController() {
     once = new Date();
     final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
@@ -103,4 +109,24 @@ public class DateController implements Serializable {
   public void setSputnikLd(LocalDate sputnikLd) {
     this.sputnikLd = sputnikLd;
   }
+
+  public LocalDate getToday() {
+    return today;
+  }
+
+  public LocalDate getParty() {
+    return party;
+  }
+
+  public void setParty(LocalDate party) {
+    this.party = party;
+  }
+
+  public LocalDate getPartyMin() {
+    return partyMin;
+  }
+
+  public LocalDate getPartyMax() {
+    return partyMax;
+  }
 }
diff --git a/tobago-example/tobago-example-demo/src/main/webapp/content/20-component/010-input/40-date/Date.xhtml b/tobago-example/tobago-example-demo/src/main/webapp/content/20-component/010-input/40-date/Date.xhtml
index df69b7e..2b04906 100644
--- a/tobago-example/tobago-example-demo/src/main/webapp/content/20-component/010-input/40-date/Date.xhtml
+++ b/tobago-example/tobago-example-demo/src/main/webapp/content/20-component/010-input/40-date/Date.xhtml
@@ -163,6 +163,14 @@
       <f:convertDateTime dateStyle="full" timeStyle="full" type="both"/>
     </tc:date>
   </tc:section>
+  <tc:section label="Min and Max">
+    <p>
+      To define the earliest or latest acceptable date, the attributes <code>min</code>
+      and <code>max</code> can be used.
+    </p>
+      <tc:date id="minmax" label="Party" value="#{dateController.party}" tip="Pick a date in the next 3 to 10 days"
+      min="#{dateController.partyMin}" max="#{dateController.partyMax}"/>
+  </tc:section>
   <tc:section label="Submit">
     <tc:form id="formSubmit">
       <p>Press the button to submit the date to the server. The output field show the current value.
diff --git a/tobago-theme/tobago-theme-standard/src/main/js/tobago.js b/tobago-theme/tobago-theme-standard/src/main/js/tobago.js
index 4838d97..3a6ce33 100644
--- a/tobago-theme/tobago-theme-standard/src/main/js/tobago.js
+++ b/tobago-theme/tobago-theme-standard/src/main/js/tobago.js
@@ -10321,10 +10321,12 @@
           super();
       }
       connectedCallback() {
-          console.debug("input type=date support", DatePicker.SUPPORTS_INPUT_TYPE_DATE);
-          if (!DatePicker.SUPPORTS_INPUT_TYPE_DATE) {
-              this.setAttribute("type", "text");
-              this.initVanillaDatePicker();
+          if (this.type == "date") {
+              console.debug("check input type=date support", DatePicker.SUPPORTS_INPUT_TYPE_DATE);
+              if (!DatePicker.SUPPORTS_INPUT_TYPE_DATE) {
+                  this.setAttribute("type", "text");
+                  this.initVanillaDatePicker();
+              }
           }
       }
       initVanillaDatePicker() {
@@ -10341,7 +10343,9 @@
               autohide: true,
               language: locale,
               todayBtn: this.todayButton,
-              todayBtnMode: 1
+              todayBtnMode: 1,
+              minDate: this.min,
+              maxDate: this.max,
               // todo readonly
               // todo show week numbers
           };
@@ -10396,6 +10400,18 @@
               this.removeAttribute("today-button");
           }
       }
+      get type() {
+          var _a;
+          return (_a = this.field) === null || _a === void 0 ? void 0 : _a.getAttribute("type");
+      }
+      get min() {
+          var _a;
+          return (_a = this.field) === null || _a === void 0 ? void 0 : _a.getAttribute("min");
+      }
+      get max() {
+          var _a;
+          return (_a = this.field) === null || _a === void 0 ? void 0 : _a.getAttribute("max");
+      }
       get pattern() {
           let pattern = this.getAttribute("pattern");
           return pattern ? pattern : "yyyy-mm-dd";
diff --git a/tobago-theme/tobago-theme-standard/src/main/js/tobago.js.map b/tobago-theme/tobago-theme-standard/src/main/js/tobago.js.map
index f4cbe0c..968172a 100644
--- a/tobago-theme/tobago-theme-standard/src/main/js/tobago.js.map
+++ b/tobago-theme/tobago-theme-standard/src/main/js/tobago.js.map
@@ -1 +1 @@
-{"version":3,"file":"tobago.js","sources":["../ts/tobago-listener.ts","../ts/tobago-utils.ts","../ts/tobago-bar.ts","../ts/tobago-dropdown.ts","../../../../node_modules/vanillajs-datepicker/js/lib/utils.js","../../../../node_modules/vanillajs-datepicker/js/lib/date.js","../../../../node_modules/vanillajs-datepicker/js/lib/date-format.js","../../../../node_modules/vanillajs-datepicker/js/lib/event.js","../../../../node_modules/vanillajs-datepicker/js/i18n/base-locales.js","../../../../nod [...]
\ No newline at end of file
+{"version":3,"file":"tobago.js","sources":["../ts/tobago-listener.ts","../ts/tobago-utils.ts","../ts/tobago-bar.ts","../ts/tobago-dropdown.ts","../../../../node_modules/vanillajs-datepicker/js/lib/utils.js","../../../../node_modules/vanillajs-datepicker/js/lib/date.js","../../../../node_modules/vanillajs-datepicker/js/lib/date-format.js","../../../../node_modules/vanillajs-datepicker/js/lib/event.js","../../../../node_modules/vanillajs-datepicker/js/i18n/base-locales.js","../../../../nod [...]
\ No newline at end of file
diff --git a/tobago-theme/tobago-theme-standard/src/main/js/tobago.min.js b/tobago-theme/tobago-theme-standard/src/main/js/tobago.min.js
index 319fcc5..c8fd08d 100644
--- a/tobago-theme/tobago-theme-standard/src/main/js/tobago.min.js
+++ b/tobago-theme/tobago-theme-standard/src/main/js/tobago.min.js
@@ -1,8 +1,8 @@
-!function(e){"function"==typeof define&&define.amd?define(e):e()}((function(){"use strict";var e,t;!function(e){e[e.DOCUMENT_READY=0]="DOCUMENT_READY",e[e.WINDOW_LOAD=1]="WINDOW_LOAD",e[e.BEFORE_SUBMIT=2]="BEFORE_SUBMIT",e[e.AFTER_UPDATE=3]="AFTER_UPDATE",e[e.BEFORE_UNLOAD=4]="BEFORE_UNLOAD",e[e.BEFORE_EXIT=5]="BEFORE_EXIT"}(e||(e={})),function(e){e[e.EARLIER=0]="EARLIER",e[e.EARLY=1]="EARLY",e[e.NORMAL=2]="NORMAL",e[e.LATE=3]="LATE",e[e.LATER=4]="LATER"}(t||(t={}));class s{constructor() [...]
+!function(e){"function"==typeof define&&define.amd?define(e):e()}((function(){"use strict";var e,t;!function(e){e[e.DOCUMENT_READY=0]="DOCUMENT_READY",e[e.WINDOW_LOAD=1]="WINDOW_LOAD",e[e.BEFORE_SUBMIT=2]="BEFORE_SUBMIT",e[e.AFTER_UPDATE=3]="AFTER_UPDATE",e[e.BEFORE_UNLOAD=4]="BEFORE_UNLOAD",e[e.BEFORE_EXIT=5]="BEFORE_EXIT"}(e||(e={})),function(e){e[e.EARLIER=0]="EARLIER",e[e.EARLY=1]="EARLY",e[e.NORMAL=2]="NORMAL",e[e.LATE=3]="LATE",e[e.LATER=4]="LATER"}(t||(t={}));class s{constructor() [...]
 /*!
     * Bootstrap v5.0.0-beta3 (https://getbootstrap.com/)
     * Copyright 2011-2021 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
     * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
     */
-const Es="transitionend",Ls=e=>{do{e+=Math.floor(1e6*Math.random())}while(document.getElementById(e));return e},Ss=e=>{let t=e.getAttribute("data-bs-target");if(!t||"#"===t){let s=e.getAttribute("href");if(!s||!s.includes("#")&&!s.startsWith("."))return null;s.includes("#")&&!s.startsWith("#")&&(s="#"+s.split("#")[1]),t=s&&"#"!==s?s.trim():null}return t},xs=e=>{const t=Ss(e);return t&&document.querySelector(t)?t:null},ks=e=>{const t=Ss(e);return t?document.querySelector(t):null},As=e=>{i [...]
+const Es="transitionend",Ss=e=>{do{e+=Math.floor(1e6*Math.random())}while(document.getElementById(e));return e},Ls=e=>{let t=e.getAttribute("data-bs-target");if(!t||"#"===t){let s=e.getAttribute("href");if(!s||!s.includes("#")&&!s.startsWith("."))return null;s.includes("#")&&!s.startsWith("#")&&(s="#"+s.split("#")[1]),t=s&&"#"!==s?s.trim():null}return t},xs=e=>{const t=Ls(e);return t&&document.querySelector(t)?t:null},ks=e=>{const t=Ls(e);return t?document.querySelector(t):null},As=e=>{i [...]
 //# sourceMappingURL=tobago.min.js.map
diff --git a/tobago-theme/tobago-theme-standard/src/main/js/tobago.min.js.map b/tobago-theme/tobago-theme-standard/src/main/js/tobago.min.js.map
index badc0aa..42dfba6 100644
--- a/tobago-theme/tobago-theme-standard/src/main/js/tobago.min.js.map
+++ b/tobago-theme/tobago-theme-standard/src/main/js/tobago.min.js.map
@@ -1 +1 @@
-{"version":3,"file":"tobago.min.js","sources":["../ts/tobago-listener.ts","../ts/tobago-utils.ts","../ts/tobago-bar.ts","../ts/tobago-dropdown.ts","../../../../node_modules/vanillajs-datepicker/js/lib/utils.js","../../../../node_modules/vanillajs-datepicker/js/lib/date.js","../../../../node_modules/vanillajs-datepicker/js/lib/date-format.js","../../../../node_modules/vanillajs-datepicker/js/lib/event.js","../../../../node_modules/vanillajs-datepicker/js/i18n/base-locales.js","../../../.. [...]
\ No newline at end of file
+{"version":3,"file":"tobago.min.js","sources":["../ts/tobago-listener.ts","../ts/tobago-utils.ts","../ts/tobago-bar.ts","../ts/tobago-dropdown.ts","../../../../node_modules/vanillajs-datepicker/js/lib/utils.js","../../../../node_modules/vanillajs-datepicker/js/lib/date.js","../../../../node_modules/vanillajs-datepicker/js/lib/date-format.js","../../../../node_modules/vanillajs-datepicker/js/lib/event.js","../../../../node_modules/vanillajs-datepicker/js/i18n/base-locales.js","../../../.. [...]
\ No newline at end of file
diff --git a/tobago-theme/tobago-theme-standard/src/main/ts/tobago-date.ts b/tobago-theme/tobago-theme-standard/src/main/ts/tobago-date.ts
index 83edcd9..92fbaaa 100644
--- a/tobago-theme/tobago-theme-standard/src/main/ts/tobago-date.ts
+++ b/tobago-theme/tobago-theme-standard/src/main/ts/tobago-date.ts
@@ -40,6 +40,8 @@ interface DatePickerOptions {
   language: string;
   todayBtn: boolean;
   todayBtnMode: number;
+  minDate: string;
+  maxDate: string;
 }
 
 class DatePicker extends HTMLElement {
@@ -59,11 +61,12 @@ class DatePicker extends HTMLElement {
   }
 
   connectedCallback(): void {
-    console.debug("input type=date support", DatePicker.SUPPORTS_INPUT_TYPE_DATE);
-
-    if (!DatePicker.SUPPORTS_INPUT_TYPE_DATE) {
-      this.setAttribute("type", "text");
-      this.initVanillaDatePicker();
+    if (this.type == "date") {
+      console.debug("check input type=date support", DatePicker.SUPPORTS_INPUT_TYPE_DATE);
+      if (!DatePicker.SUPPORTS_INPUT_TYPE_DATE) {
+        this.setAttribute("type", "text");
+        this.initVanillaDatePicker();
+      }
     }
   }
 
@@ -82,7 +85,9 @@ class DatePicker extends HTMLElement {
       autohide: true,
       language: locale,
       todayBtn: this.todayButton,
-      todayBtnMode: 1
+      todayBtnMode: 1,
+      minDate: this.min,
+      maxDate: this.max,
       // todo readonly
       // todo show week numbers
     };
@@ -148,6 +153,18 @@ class DatePicker extends HTMLElement {
     }
   }
 
+  get type(): string {
+    return this.field?.getAttribute("type");
+  }
+
+  get min(): string {
+    return this.field?.getAttribute("min");
+  }
+
+  get max(): string {
+    return this.field?.getAttribute("max");
+  }
+
   get pattern(): string {
     let pattern = this.getAttribute("pattern");
     return pattern ? pattern : "yyyy-mm-dd";
diff --git a/tobago-tool/tobago-tool-apt/src/main/resources/org/apache/myfaces/tobago/apt/component.stg b/tobago-tool/tobago-tool-apt/src/main/resources/org/apache/myfaces/tobago/apt/component.stg
index 18d5116..45c59c4 100644
--- a/tobago-tool/tobago-tool-apt/src/main/resources/org/apache/myfaces/tobago/apt/component.stg
+++ b/tobago-tool/tobago-tool-apt/src/main/resources/org/apache/myfaces/tobago/apt/component.stg
@@ -395,6 +395,10 @@ AlignItemsProperty(property) ::= <<
 <NormalProperty(property)>
 >>
 
+LocalDateProperty(property) ::= <<
+<NormalProperty(property)>
+>>
+
 JustifyContentProperty(property) ::= <<
 <NormalProperty(property)>
 >>