1 | # Flourish controls
|
2 |
|
3 | Add interchangeable controls (dropdown, buttons or slider) to a page.
|
4 |
|
5 | ## How to install
|
6 |
|
7 | `npm install -s @flourish/controls`
|
8 |
|
9 | Add one or more control objects to your template state:
|
10 |
|
11 | ``` js
|
12 | export var state = {
|
13 | controls: {},
|
14 | ...
|
15 | }
|
16 | ```
|
17 |
|
18 | Import `@flourish/controls` into your `template.yml` settings.
|
19 |
|
20 |
|
21 | ## Basics
|
22 |
|
23 | Initialise the control outside any function:
|
24 |
|
25 | ``` js
|
26 | import initControls from "@flourish/controls";
|
27 | var control = initControls(state.controls);
|
28 | ```
|
29 |
|
30 | You can also pass in optional functions for getting parsing and formatting functions. The former is required if you want to use user input and the user is using "," as the decimal separator. The latter is required if you want to format numbers to use "," as a decimal separator. For example:
|
31 |
|
32 | ``` js
|
33 | import initLocalization from "@flourish/number-localization";
|
34 | import initFormatter from "@flourish/number-formatter";
|
35 | import state from "../core/state";
|
36 |
|
37 | localization = initLocalization(state.localization);
|
38 | formatter = initFormatter(state.formatting);
|
39 |
|
40 | function getParser() {
|
41 | return localization.getParser();
|
42 | }
|
43 |
|
44 | function getFormatter() {
|
45 | return formatter(localization.getFormatterFunction());
|
46 | }
|
47 |
|
48 | var control = init(state.controls, getParser, getFormatter);
|
49 | ```
|
50 |
|
51 | Append the control to a container and add an on change handler: `controls.appendTo(container).on("change", update);`. This is usually done in the `draw` function.
|
52 |
|
53 | In `update` you typically want to update the set of `options` and then call the controls own `update` method: `controls.options(options).update();`.
|
54 |
|
55 | ## Methods
|
56 |
|
57 | ### `appendTo(parent_container, [bounding_container])`
|
58 |
|
59 | Appends the control to the `parent_container` DOM node and injects CSS into the `head` if necessary. The optional `bounding_container` makes sure the dropdown list will never overflow outside that container.
|
60 |
|
61 | Returns the control object.
|
62 |
|
63 | ### `getSortedIndex()`
|
64 |
|
65 | Returns the index of the currently chosen value in a sorted version of the options array.
|
66 |
|
67 | ### `index([i])`
|
68 |
|
69 | With no argument returns the (unsorted) index of the currently selected option. If `i` is specified and corresponds to an index into the `options` array, sets the currently selected option to `i`. Returns the control object.
|
70 |
|
71 | ### `n_options()`
|
72 |
|
73 | Returns the current number of options.
|
74 |
|
75 | ### `on(event, callback)`
|
76 |
|
77 | Add `callback` to the list of `event` handlers. Currently the only supported `event` is "change". Returns the control object.
|
78 |
|
79 | ### `options([arr])`
|
80 |
|
81 | With no argument returns a copy of the current list of options displayed by the current control. With an array, replaces the current options with a copy of `arr` and returns the control object.
|
82 |
|
83 | ### `remove()`
|
84 |
|
85 | Removes the control (and its resize event handler) from the DOM. Returns the control object.
|
86 |
|
87 | ### `trigger(event)`
|
88 |
|
89 | Calls all the handlers assigned to `event` and returns the control object. Currently the only supported `event` type is "change".
|
90 |
|
91 | ### `update()`
|
92 |
|
93 | Rebuilds the control with latest settings. Returns the control object.
|
94 |
|
95 | ### `value([val])`
|
96 |
|
97 | With no argument returns the string value of the currently selected option. With `val` present changes the current index to match that of `val` in the options array and returns the control object. If `val` is not in the options array then nothing is changed but the control object is still returned.
|
98 |
|
99 | ### getNode()
|
100 |
|
101 | Returns the container of the control that has been created. It will have a class of `"fl-controls-container"`
|
102 |
|
103 |
|
104 | ## Styling the controls
|
105 |
|
106 | The controls have very basic styling. You can overwrite these styles with your own css and styling. We recommend using [@flourish/ui-styles](https://github.com/kiln/flourish-ui-styles) to style the controls.
|
107 |
|
108 |
|