UNPKG

5.22 kBMarkdownView Raw
1# Flourish controls
2
3Add interchangeable controls (dropdown, buttons or slider) and controls container to a page.
4
5## How to install
6
7`npm install -s @flourish/controls`
8
9Add one or more control objects to your template state as well an object for the controls container
10
11``` js
12export var state = {
13 controls_container: {},
14 buttons_control: {
15 control_type: "buttons"
16 },
17 dropdown_control: {
18 control_type: "dropdown"
19 },
20 ...
21}
22```
23
24Add settings for the controls container in `template.yml`.
25
26``` yaml
27 - property: controls_container
28 import: "@flourish/controls/container"
29```
30
31This will add a controls container alignment and spacing options in the settings.
32
33Then import `@flourish/controls` for each controls instance.
34
35``` yaml
36- property: buttons_control
37 import: "@flourish/controls"
38- property: dropdown_control
39 import: "@flourish/controls"
40```
41
42## Basics
43
44Initialise the controls container and individual controls outside any function:
45
46``` js
47import { createControlsContainer, createControls } from "@flourish/controls";
48
49var buttons_control = initControls(state.buttons_control);
50var dropdown_control = initControls(state.dropdown_control)
51
52var controls_container = createControlsContainer(state.controls_container)
53 .appendTo(layout.getSection("controls"))
54 .add([buttons_control, dropdown_control]);
55
56```
57
58You 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:
59
60``` js
61import initLocalization from "@flourish/number-localization";
62import initFormatter from "@flourish/number-formatter";
63import state from "../core/state";
64
65localization = initLocalization(state.localization);
66formatter = initFormatter(state.formatting);
67
68function getParser() {
69 return localization.getParser();
70}
71
72function getFormatter() {
73 return formatter(localization.getFormatterFunction());
74}
75
76var buttons_control = init(state.buttons_control, getParser, getFormatter);
77```
78
79Add an on change handler to each controls instance: `buttons_control.on("change", update);`. This is usually done in the `draw` function.
80
81In `update` you typically want to update the set of `options` and then call the controls own `update` method: `buttons_control.options(options).update();`. If you're using this module alongside `@flourish/ui-styles` you should call the update method from the ui-styles object first (this will ensure the dropdown icon correctly matches the size and color styles set in the ui-styles panel).
82
83## Controls container methods
84
85### `appendTo(parent_container)`
86
87Appends the controls container to the `parent_container` DOM node (this should usually be `layout.getSection("controls")`). Returns the controls container object.
88
89### `add([controls_instances])`
90
91Takes an array of controls instances and appends these to the controls container. Returns the controls container object.
92
93### `update()`
94
95This updates the controls container and all the appended controls instances (calling their individual `update()` methods). Returns the controls container object.
96
97## Controls methods
98
99### `appendTo(parent_container)`
100
101Appends the control to the `parent_container` DOM node and injects CSS into the `head` if necessary.
102
103Returns the control object.
104
105### `getSortedIndex()`
106
107Returns the index of the currently chosen value in a sorted version of the options array.
108
109### `index([i])`
110
111With 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.
112
113### `n_options()`
114
115Returns the current number of options.
116
117### `on(event, callback)`
118
119Add `callback` to the list of `event` handlers. Currently the only supported `event` is "change". Returns the control object.
120
121### `options([arr])`
122
123With 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.
124
125### `remove()`
126
127Removes the control (and its resize event handler) from the DOM. Returns the control object.
128
129### `trigger(event)`
130
131Calls all the handlers assigned to `event` and returns the control object. Currently the only supported `event` type is "change".
132
133### `update()`
134
135Rebuilds the control with latest settings. Returns the control object.
136
137### `value([val])`
138
139With 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.
140
141### getNode()
142
143Returns the container of the control that has been created. It will have a class of `"fl-controls-container"`
144
145
146## Styling the controls
147
148The 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.
149
150