diff --git a/pom.xml b/pom.xml index 2aa5258..8da6b5e 100644 --- a/pom.xml +++ b/pom.xml @@ -4,12 +4,12 @@ com.flowingcode.addons simple-timer - 2.2.1-SNAPSHOT + 3.0.0-SNAPSHOT Simple Timer Addon Simple Timer for Vaadin Flow - 14.8.20 + 14.11.12 1.8 1.8 UTF-8 diff --git a/src/main/java/com/flowingcode/vaadin/addons/simpletimer/SimpleTimer.java b/src/main/java/com/flowingcode/vaadin/addons/simpletimer/SimpleTimer.java index 033d1b3..f1bb167 100644 --- a/src/main/java/com/flowingcode/vaadin/addons/simpletimer/SimpleTimer.java +++ b/src/main/java/com/flowingcode/vaadin/addons/simpletimer/SimpleTimer.java @@ -41,44 +41,35 @@ public class SimpleTimer extends Component implements HasSize, HasStyle, Serializable { private static final long serialVersionUID = 1L; - private static final int START_TIME_S = 60; private static final String DISPLAY = "display"; private static final String INLINE = "inline"; private static final String CURRENT_TIME = "currentTime"; - /** Creates a timer with a start time of 60 */ + /** Creates a timer */ public SimpleTimer() { - this(START_TIME_S); - } - - /** - * Creates a timer using the start time passed in the constructor - * - * @param startTime value in seconds for the start time - */ - public SimpleTimer(final Number startTime) { getElement().getStyle().set(DISPLAY, INLINE); - setStartTime(startTime); } /** - * Sets the start time + * Sets the start time, for countdown mode. * * @param startTime value in seconds for the start time */ public void setStartTime(final Number startTime) { + getElement().setProperty("countUp", false); getElement().setProperty("startTime", startTime.doubleValue()); getElement().setProperty(CURRENT_TIME, startTime.doubleValue()); reset(); } /** - * Changes the behavior to count up or down Default is false for count down + * Sets the end time, for countup mode. * - * @param countUp + * @param endTime value in seconds for the end time */ - public void setCountUp(final boolean countUp) { - getElement().setProperty("countUp", countUp); + public void setEndTime(final Number endTime) { + getElement().setProperty("countUp", true); + getElement().setProperty("endTime", endTime.doubleValue()); reset(); } @@ -168,7 +159,7 @@ public CompletableFuture getCurrentTimeAsync() { /** * Adds a property change listener for the {@code currentTime} property - * + * * @param listener the property change listener * @param period the minimum period between listener invocations, or 0 to disable throttling * @param periodUnit time duration of throttling period diff --git a/src/main/resources/META-INF/frontend/simple-timer/simple-timer.js b/src/main/resources/META-INF/frontend/simple-timer/simple-timer.js index f801941..4965118 100644 --- a/src/main/resources/META-INF/frontend/simple-timer/simple-timer.js +++ b/src/main/resources/META-INF/frontend/simple-timer/simple-timer.js @@ -41,11 +41,17 @@ Polymer({ properties: { /** * Start time for the timer in seconds - * @default 60 */ startTime: { type: Number, - value: 60 + value: 0 + }, + /** + * End time for the timer in seconds + */ + endTime: { + type: Number, + value: 0 }, /** * Current time of the timer, in seconds @@ -121,21 +127,21 @@ Polymer({ ready: function() { if (this.countUp) { this.set('currentTime', 0); - this.set('_formattedTime', '0'); } else { this.set('currentTime', this.startTime); - this.set('_formattedTime', this.startTime.toString()); } + this.set('_formattedTime', this._formatTime(this.currentTime.toString())); }, start: function() { if ((this.currentTime <= 0 && !this.countUp) - || (this.currentTime >= this.startTime && this.countUp) ) { + || (this.currentTime >= this.endTime && this.countUp) ) { // timer is over - this.currentTime = this.countUp ? this.startTime : 0; + this.currentTime = this.countUp ? this.endTime : 0; + this._formattedTime = this._formatTime(this.currentTime); } - if (!this.startTime || this.isRunning) { + if (this.countUp && !this.endTime || !this.countUp && !this.startTime || this.isRunning) { this.pause(); return; } @@ -153,9 +159,10 @@ Polymer({ return; } if ((this.currentTime <= 0 && !this.countUp) - || (this.currentTime >= this.startTime && this.countUp) ) { + || (this.currentTime >= this.endTime && this.countUp) ) { // timer is over - this.currentTime = this.countUp ? this.startTime : 0; + this.currentTime = this.countUp ? this.endTime : 0; + this._formattedTime = this._formatTime(this.currentTime); this.pause(); this.fire('simple-timer-end'); return; @@ -175,6 +182,9 @@ Polymer({ if (timeString[0].indexOf('-') === 0) { return 0; } + if (timeString.length==1) { + timeString.push("00"); + } var seconds = timeString[0]; var minutes = seconds / 60 | 0; var hours = minutes / 60 | 0; diff --git a/src/test/java/com/flowingcode/vaadin/addons/simpletimer/SimpletimerDemo.java b/src/test/java/com/flowingcode/vaadin/addons/simpletimer/SimpletimerDemo.java index c93e395..6ca9a14 100644 --- a/src/test/java/com/flowingcode/vaadin/addons/simpletimer/SimpletimerDemo.java +++ b/src/test/java/com/flowingcode/vaadin/addons/simpletimer/SimpletimerDemo.java @@ -19,7 +19,6 @@ */ package com.flowingcode.vaadin.addons.simpletimer; -import java.math.BigDecimal; import com.flowingcode.vaadin.addons.demo.DemoSource; import com.vaadin.flow.component.button.Button; import com.vaadin.flow.component.checkbox.Checkbox; @@ -32,6 +31,7 @@ import com.vaadin.flow.component.textfield.TextField; import com.vaadin.flow.router.PageTitle; import com.vaadin.flow.router.Route; +import java.math.BigDecimal; @SuppressWarnings("serial") @PageTitle("Simple Timer Demo") @@ -39,28 +39,37 @@ @Route(value = "simple-timer/simple-timer", layout = SimpletimerDemoView.class) public class SimpletimerDemo extends Div { + private final SimpleTimer timer = new SimpleTimer(); + + private boolean countUpMode; + private BigDecimal time = new BigDecimal(60); + public SimpletimerDemo() { - this.setSizeFull(); - final SimpleTimer timer = new SimpleTimer(); + setSizeFull(); timer.setWidth("100px"); timer.setHeight("50px"); timer.getStyle().set("font-size", "40px"); + timer.setStartTime(60); Span timerTitle = new Span("Simple Count Up Timer"); final TextField startTime = - new TextField("Start Time", e -> timer.setStartTime(new BigDecimal(e.getValue()))); + new TextField("Start Time", e -> { + time = new BigDecimal(e.getValue()); + update(); + }); final Checkbox countUp = new Checkbox("Count Up", false); countUp.addValueChangeListener( e -> { - timer.setCountUp(countUp.getValue()); - if (e.getValue()) { + countUpMode = countUp.getValue(); + if (countUpMode) { startTime.setLabel("End Time"); timerTitle.setText("Simple Count Up Timer"); } else { startTime.setLabel("Start Time"); timerTitle.setText("Simple Countdown Timer"); } + update(); }); final Button start = new Button("Start/Stop", e -> timer.start()); final Button stop = new Button("Stop", e -> timer.pause()); @@ -91,7 +100,9 @@ public SimpletimerDemo() { new Checkbox( "Visible", e -> { - if (e.isFromClient()) timer.setVisible(!timer.isVisible()); + if (e.isFromClient()) { + timer.setVisible(!timer.isVisible()); + } }); visible.setValue(true); @@ -110,4 +121,12 @@ public SimpletimerDemo() { add(new VerticalLayout(topLayout, startTime, options, bottomLayout)); } + + private void update() { + if (countUpMode) { + timer.setEndTime(time); + } else { + timer.setStartTime(time); + } + } }