Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Sliders! #109

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ version = "0.7.9"
[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
HypertextLiteral = "ac1192a8-f4b3-4bfe-ba22-af5b92cd3ab2"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
Expand All @@ -15,10 +16,11 @@ Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"

[compat]
JSON = "^0.21.1"
Reexport = "^1"
Suppressor = "^0.2.0"
julia = "^1"
JSON = "^0.21.1"
HypertextLiteral = "^0.7"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
54 changes: 30 additions & 24 deletions src/Builtins.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ import Dates

export Slider, NumberField, Button, CheckBox, TextField, PasswordField, Select, MultiSelect, Radio, FilePicker, DateField, TimeField, ColorStringPicker

struct Slider
range::AbstractRange
default::Number
show_value::Bool
end

"""A Slider on the given `range`.

## Examples
Expand All @@ -19,24 +13,36 @@ end
`@bind x Slider(1:10; default=8, show_value=true)`

"""
Slider(range::AbstractRange; default=missing, show_value=false) = Slider(range, (default === missing) ? first(range) : default, show_value)

function show(io::IO, ::MIME"text/html", slider::Slider)
print(io, """<input
type="range"
min="$(first(slider.range))"
step="$(step(slider.range))"
max="$(last(slider.range))"
value="$(slider.default)"
$(slider.show_value ? "oninput=\"this.nextElementSibling.value=this.value\"" : "")
>""")

if slider.show_value
print(io, """<output>$(slider.default)</output>""")
end
end

get(slider::Slider) = slider.default
Slider(range::AbstractRange; default=first(range), show_value=false) = @htl("""
<div>
<input type="range" min="$(first(range))" max="$(last(range))" step="$(step(range))" value="$(default)">
$(HTML(show_value ? "<output>$(default)</output>" : ""))

<script>
let div = currentScript.parentElement
let slider = div.querySelector("input")

slider.addEventListener("input", e => {
div.value = +slider.value
$(JavaScript(show_value ? "div.querySelector(\"output\").value = +slider.value" : ""))
div.dispatchEvent(new CustomEvent("input"))
e.preventDefault()
})

div.value = $(default)
let localVal = div.value
delete div.value
SyxP marked this conversation as resolved.
Show resolved Hide resolved
Object.defineProperty(div, "value",
{configurable: false,
enumerable: false,
get: () => {return localVal},
set: (newVal) => {
slider.value = newVal
$(JavaScript(show_value ? "div.querySelector(\"output\").value = +newVal" : ""))
localVal = newVal
}})
</script>
</div>""")

"""A box where you can type in a number, within a specific range.

Expand Down
1 change: 1 addition & 0 deletions src/PlutoUI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module PlutoUI

import Base: show, get
import Markdown: htmlesc, withtag
import HypertextLiteral: @htl, JavaScript

using Reexport

Expand Down