/
completion.opa
131 lines (113 loc) · 4.72 KB
/
completion.opa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
Copyright © 2011 MLstate
This file is part of OPA.
OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.
OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.
You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Elvis Completions
*
* @category UI
* @author David Rajchenbach-Teller, 2011
* @destination PUBLIC
* @stability EXPERIMENTAL
*/
/**
* An input with auto-completion.
*
* When users type, a popup menu appears, prompting to choose one value in the menu. Only these values can be accepted (note that the popup menu can
* be programmed to display the value currently written by the user). Once the value is accepted, it is displayed as a button and the user can restart
* typing another value, etc.
*/
type ECompletion.options('value) =
{
welcome_text: string
accept_multiple_entries: bool
suggest_completions: string -> list('value)
display_suggestion: 'value -> xhtml
display_choice: 'value -> xhtml
text_suggestion: 'value -> string
}
type ECompletion.sources('value) =
{
added_value: Event.source({new:'value all:list('value)})
//TODO: added_value
//TODO: changed_value
}
@abstract type ECompletion.implem('value) = {}
type ECompletion.elvis('value) = Elvis.elvis(ECompletion.sources('value), ECompletion.implem('value))
ECompletion =
{{
make(options: ECompletion.options('value)): ECompletion.elvis('value) =
(
//Setup events
added_value_net = Network.empty()
//Setup UI
elvis_input = EInput.simple()
elvis_accepted_list = EList.empty()
elvis_accepted_panel = EPanel.make({EPanel.default_options with
children = [Elvis.pack(elvis_accepted_list)]
classes = ["completion", "choices"]
})
elvis_suggestions_list = EList.empty()
elvis_suggestions_panel = EPanel.make({EPanel.default_options with
children = [Elvis.pack(elvis_suggestions_list)]
classes = ["completion", "suggestions"]
is_visible = {false}
})
elvis_panel = EPanel.make({EPanel.default_options with
classes = ["completion", "root"]
children = [Elvis.pack(elvis_input), Elvis.pack(elvis_suggestions_panel), Elvis.pack(elvis_accepted_panel)]
})
//Show/hide suggestions panel
set_suggestions_visible(visible) =
(
EPanel.set_visible(elvis_suggestions_panel, visible)//Note: In the future, we could replace this by an animation
)
//When value is accepted, show it in [elvis_accepted], store it somewhere, clear [elvis_input], hide [elvis_suggestions_panel]
on_value_accepted(value) = (
do set_suggestions_visible({false})
do EInput.set_text(elvis_input, "", {false})
elvis_value_for_display = EClickable.simple(options.display_choice(value))
do EList.add_item(elvis_accepted_list, value, Elvis.pack(elvis_value_for_display))
//TODO: Store value
//value_key = Random.int(10000000) //An arbitrary key, used for storage. Big number to avoid collisions.
//TODO: On single click upon [elvis_value], reselect value/text
//TODO: On double click upon [elvis_value], remove value
//TODO: Trigger event
{}
)
//When text changes, show/hide suggestions
on_changing_text(text) = (
match options.suggest_completions(text) with
[] -> //Hide suggestions
do set_suggestions_visible({false})
void
| suggestions -> //Show suggestions
do set_suggestions_visible({true})
make_suggestion(value) = (
clickable = EClickable.simple(options.display_suggestion(value))
_ = Event.callback(Elvis.sources(clickable).chosen, (_ -> on_value_accepted(value)))
Elvis.pack(clickable)
)
elvis_suggestions = List.map(x -> (x, make_suggestion(x)), suggestions)
do EList.set_items(elvis_suggestions_list, elvis_suggestions)
void
)
_ = Event.callback(Elvis.sources(elvis_input).changing_text, on_changing_text)
//Finish construction
sources = {
added_value = (added_value_net)
}
implem = {}
display = elvis_panel.display
Elvis.make(sources, implem, display)
)
}}