]> BookStack Code Mirror - bookstack/blob - resources/js/editor/menu/DialogRadioOptions.js
Started work on details/summary blocks
[bookstack] / resources / js / editor / menu / DialogRadioOptions.js
1 // ::- Represents a submenu wrapping a group of elements that start
2 // hidden and expand to the right when hovered over or tapped.
3 import {prefix, randHtmlId} from "./menu-utils";
4 import crel from "crelt";
5
6 class DialogRadioOptions {
7     /**
8      * Given inputOptions should be keyed by label, with values being values.
9      * Values of empty string will be treated as null.
10      * @param {Object} inputOptions
11      * @param {{label: string, id: string, attrs?: Object, value: function(PmEditorState): string|null}} options
12      */
13     constructor(inputOptions, options) {
14         this.inputOptions = inputOptions;
15         this.options = options || {};
16     }
17
18     // :: (EditorView) → {dom: dom.Node, update: (EditorState) → bool}
19     // Renders the submenu.
20     render(view) {
21
22         const inputs = [];
23         const optionInputLabels = Object.keys(this.inputOptions).map(label => {
24             const inputAttrs = Object.assign({
25                 type: "radio",
26                 name: this.options.id,
27                 value: this.inputOptions[label],
28                 class: prefix + '-dialog-radio-option',
29             }, this.options.attrs || {});
30             const input = crel("input", inputAttrs);
31             inputs.push(input);
32             return crel("label", input, label);
33         });
34
35         const optionInputWrap = crel("div", {class: prefix + '-dialog-radio-option-wrap'}, optionInputLabels);
36
37         const label = crel("label", {}, this.options.label);
38         const rowRap = crel("div", {class: prefix + '-dialog-form-row'}, label, optionInputWrap);
39
40         const update = (state) => {
41             const value = this.options.value(state);
42             for (const input of inputs) {
43                 input.checked = (input.value === value || (value === null && input.value === ""));
44             }
45             return true;
46         }
47
48         return {dom: rowRap, update}
49     }
50
51 }
52
53 export default DialogRadioOptions;