Skip to content

Commit 494eb12

Browse files
authored
Clock: repeat option (#271)
1 parent 1b06470 commit 494eb12

File tree

3 files changed

+46
-29
lines changed

3 files changed

+46
-29
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "PlutoUI"
22
uuid = "7f904dfe-b85e-4ff6-b463-dae2292396a8"
33
authors = ["Fons van der Plas <[email protected]>"]
4-
version = "0.7.52"
4+
version = "0.7.53"
55

66
[deps]
77
AbstractPlutoDingetjes = "6e696c72-6542-2067-7265-42206c756150"

assets/clock.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ const unit = clock.querySelector("span#unit")
77
const button = clock.querySelector("button")
88

99
const max_value = +clock.dataset.maxValue
10+
const repeat = clock.dataset.repeat === "true"
1011

11-
var t = clock.value = 1
12+
var t = (clock.value = 1)
1213
var starttime = null
1314
var dt = 1
1415

@@ -29,16 +30,21 @@ analogfront.onanimationiteration = (e) => {
2930
const running_time = (Date.now() - starttime) / 1000
3031
t = Math.max(t + 1, Math.floor(running_time / dt))
3132
if (!isNaN(max_value)) {
32-
t = Math.min(t, max_value)
33+
if (repeat) {
34+
if(t > max_value) {
35+
t = 1
36+
starttime = Date.now()
37+
}
38+
} else {
39+
if (t >= max_value) {
40+
clock.classList.add("stopped")
41+
t = max_value
42+
}
43+
}
3344
}
3445
clock.value = t
3546
clock.dispatchEvent(new CustomEvent("input"))
3647
}
37-
38-
if (t >= max_value) {
39-
clock.classList.add("stopped")
40-
t = 0
41-
}
4248
}
4349
unit.onclick = (e) => {
4450
clock.classList.toggle("inverted")
@@ -48,6 +54,6 @@ button.onclick = (e) => {
4854
starttime = Date.now()
4955
clock.classList.toggle("stopped")
5056
if (!clock.classList.contains("stopped")) {
51-
t = 1 - 1
57+
t = 1
5258
}
5359
}

src/Clock.jl

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
11
### A Pluto.jl notebook ###
2-
# v0.19.12
2+
# v0.19.27
33

44
using Markdown
55
using InteractiveUtils
66

7-
# This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error).
8-
macro bind(def, element)
9-
quote
10-
local iv = try Base.loaded_modules[Base.PkgId(Base.UUID("6e696c72-6542-2067-7265-42206c756150"), "AbstractPlutoDingetjes")].Bonds.initial_value catch; b -> missing; end
11-
local el = $(esc(element))
12-
global $(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : iv(el)
13-
el
14-
end
15-
end
16-
177
# ╔═╡ 3689bb1b-23f8-41ae-a392-fb2ee2ec40d7
188
# ╠═╡ skip_as_script = true
199
#=╠═╡
@@ -24,12 +14,6 @@ begin
2414
end
2515
╠═╡ =#
2616

27-
# ╔═╡ e7a070ab-67e7-444b-88d8-87c14aaef046
28-
# ╠═╡ skip_as_script = true
29-
#=╠═╡
30-
names(@__MODULE__)
31-
╠═╡ =#
32-
3317
# ╔═╡ fed9022f-1e8e-4f47-92d8-f99065023d29
3418
import AbstractPlutoDingetjes.Bonds
3519

@@ -47,14 +31,15 @@ begin
4731
fixed::Bool = false
4832
start_running::Bool = false
4933
max_value::Union{Int64,Nothing} = nothing
34+
repeat::Bool = false
5035

5136
# Clock(interval, fixed, start_running, max_value) = interval >= 0 ? new(interval, fixed, start_running, max_value) : error("interval must be non-negative")
5237
end
5338

5439
# for backwards compat
55-
Clock(interval; kwargs...) = Clock(interval=interval; kwargs...)
40+
Clock(interval; kwargs...) = Clock(; interval=interval, kwargs...)
5641

57-
Clock(interval, fixed, start_running=false) = Clock(interval, fixed, start_running, nothing)
42+
Clock(interval, fixed, start_running=false, max_value=nothing) = Clock(; interval=interval, fixed=fixed, start_running=start_running, max_value=max_value)
5843

5944
# We split the HTML string into multiple files, but you could also write all of this into a single (long) string 🎈
6045
const cb = read(joinpath(@__DIR__, "..", "assets", "clock_back.svg"), String)
@@ -67,7 +52,7 @@ begin
6752
clock.interval < 0 && error("interval must be non-negative")
6853

6954
result = """
70-
<plutoui-clock class='$(clock.fixed ? " fixed" : "")$(clock.start_running ? "" : " stopped")' data-max-value=$(repr(clock.max_value))>
55+
<plutoui-clock class='$(clock.fixed ? " fixed" : "")$(clock.start_running ? "" : " stopped")' data-max-value=$(repr(clock.max_value)) data-repeat=$(repr(clock.repeat))>
7156
<plutoui-analog>
7257
<plutoui-back>$(cb)</plutoui-back>
7358
<plutoui-front>$(cf)</plutoui-front>
@@ -105,6 +90,11 @@ end
10590
@bind tick Clock()
10691
╠═╡ =#
10792

93+
# ╔═╡ 4930b73b-14e1-4b1d-8efe-686e31d69070
94+
#=╠═╡
95+
tick
96+
╠═╡ =#
97+
10898
# ╔═╡ 9ecd95f0-d7a5-4ee9-9e18-9d87e5d43ab7
10999
#=╠═╡
110100
tick; rand()
@@ -126,6 +116,18 @@ tick
126116
fasttick
127117
╠═╡ =#
128118

119+
# ╔═╡ b45005b3-822b-4b72-88ef-3f9fe865de6a
120+
# ╠═╡ skip_as_script = true
121+
#=╠═╡
122+
@bind loopy Clock(0.5, max_value=4, repeat=true)
123+
╠═╡ =#
124+
125+
# ╔═╡ e7a070ab-67e7-444b-88d8-87c14aaef046
126+
# ╠═╡ skip_as_script = true
127+
#=╠═╡
128+
loopy
129+
╠═╡ =#
130+
129131
# ╔═╡ a5f8ed96-136c-4ff4-8275-bd569f0dae40
130132
md"""
131133
## Different constructors
@@ -155,6 +157,12 @@ Clock(3.0, true, true)
155157
Clock(3.0, true, true, 5)
156158
╠═╡ =#
157159

160+
# ╔═╡ f9f1e6db-d4a6-40dc-908e-51ed5833011c
161+
# ╠═╡ skip_as_script = true
162+
#=╠═╡
163+
Clock(3.0, true, true, 5, true)
164+
╠═╡ =#
165+
158166
# ╔═╡ 9115fbcd-1550-4439-a830-c69b83b774b3
159167
# ╠═╡ skip_as_script = true
160168
#=╠═╡
@@ -194,10 +202,12 @@ a
194202

195203
# ╔═╡ Cell order:
196204
# ╠═06289ad2-9e2f-45b3-9d15-7c5a4167e138
205+
# ╠═4930b73b-14e1-4b1d-8efe-686e31d69070
197206
# ╠═9ecd95f0-d7a5-4ee9-9e18-9d87e5d43ab7
198207
# ╠═d82dae11-b2c6-42b5-8c52-67fbb6cc236a
199208
# ╠═80c6e80e-077a-4e31-9467-788a8c437bfc
200209
# ╠═63854404-e6a5-4dc6-a40e-b09b9f531465
210+
# ╠═b45005b3-822b-4b72-88ef-3f9fe865de6a
201211
# ╠═e7a070ab-67e7-444b-88d8-87c14aaef046
202212
# ╠═3689bb1b-23f8-41ae-a392-fb2ee2ec40d7
203213
# ╠═fed9022f-1e8e-4f47-92d8-f99065023d29
@@ -208,6 +218,7 @@ a
208218
# ╠═83a021ab-7cca-47c7-a560-9cbf58b35ab7
209219
# ╠═c96dfd13-ddd4-443f-ab09-30e15ea76785
210220
# ╠═78ee5465-ce3b-45f6-acec-aa69175807f5
221+
# ╠═f9f1e6db-d4a6-40dc-908e-51ed5833011c
211222
# ╠═9115fbcd-1550-4439-a830-c69b83b774b3
212223
# ╠═f4104cb3-7c07-4814-99f9-a00764ebadf6
213224
# ╠═21cba3fb-7bb0-43ae-b4c4-5c1eb7241fec

0 commit comments

Comments
 (0)