1 # BookStack JavaScript Code
3 BookStack is primarily server-side-rendered, but it uses JavaScript sparingly to drive any required dynamic elements. Most JavaScript is applied via a custom, and very thin, component interface to keep code organised and somewhat reusable.
5 JavaScript source code can be found in the `resources/js` directory. This gets bundled and transformed by `esbuild`, ending up in the `public/dist` folder for browser use. Read the [Development > "Building CSS & JavaScript Assets"](development.md#building-css-&-javascript-assets) documentation for details on this process.
9 This section details the format for JavaScript components in BookStack. This is a really simple class-based setup with a few helpers provided.
11 ### Defining a Component in JS
16 this.container = this.$el;
17 this.menu = this.$refs.menu;
18 this.toggles = this.$manyRefs.toggle;
20 this.speed = parseInt(this.$opts.speed);
25 All usage of $refs, $manyRefs and $opts should be done at the top of the `setup` function so any requirements can be easily seen.
27 Once defined, the component has to be registered for use. This is done in the `resources/js/components/index.js` file by defining an additional export, following the pattern of other components.
29 ### Using a Component in HTML
31 A component is used like so:
34 <div component="dropdown"></div>
36 <!-- or, for multiple -->
38 <div components="dropdown image-picker"></div>
41 The names will be parsed and new component instance will be created if a matching name is found in the `components/index.js` componentMapping.
43 ### Element References
45 Within a component you'll often need to refer to other element instances. This can be done like so:
48 <div component="dropdown">
49 <span refs="dropdown@toggle othercomponent@handle">View more</span>
53 You can then access the span element as `this.$refs.toggle` in your component.
55 Multiple elements of the same reference name can be accessed via a `this.$manyRefs` property within your component. For example, all the buttons in the below example could be accessed via `this.$manyRefs.buttons`.
58 <div component="list">
59 <button refs="list@button">Click here</button>
60 <button refs="list@button">No, Click here</button>
61 <button refs="list@button">This button is better</button>
68 <div component="dropdown"
69 option:dropdown:delay="500"
74 Will result with `this.$opts` being:
83 #### Component Properties & Methods
85 A component has the below shown properties & methods available for use. As mentioned above, most of these should be used within the `setup()` function to make the requirements/dependencies of the component clear.
88 // The root element that the component has been applied to.
91 // A map of defined element references within the component.
92 // See "Element References" above.
95 // A map of defined multi-element references within the component.
96 // See "Element References" above.
99 // Options defined for the component.
102 // The registered name of the component, usually kebab-case.
105 // Emit a custom event from this component.
106 // Will be bubbled up from the dom element this is registered on,
107 // as a custom event with the name `<elementName>-<eventName>`,
108 // with the provided data in the event detail.
109 this.$emit(eventName, data = {})
112 ## Global JavaScript Helpers
114 There are various global helper libraries in BookStack which can be accessed via the `window`. The below provides an overview of what's available.
118 // Relative URLs will be resolved against the instance BASE_URL
119 window.$http.get(url, params);
120 window.$http.post(url, data);
121 window.$http.put(url, data);
122 window.$http.delete(url, data);
123 window.$http.patch(url, data);
125 // Global event system
126 // Emit a global event
127 window.$events.emit(eventName, eventData);
128 // Listen to a global event
129 window.$events.listen(eventName, callback);
130 // Show a success message
131 window.$events.success(message);
132 // Show an error message
133 window.$events.error(message);
134 // Show validation errors, if existing, as an error notification
135 window.$events.showValidationErrors(error);
138 // Take the given plural text and count to decide on what plural option
139 // to use, Similar to laravel's trans_choice function but instead
140 // takes the translation text directly instead of a translation key.
141 window.$trans.choice(translationString, count, replacements);
144 // Parse and initialise any components from the given root el down.
145 window.$components.init(rootEl);
146 // Register component models to be used by the component system.
147 // Takes a mapping of classes/constructors keyed by component names.
148 // Names will be converted to kebab-case.
149 window.$components.register(mapping);
150 // Get the first active component of the given name.
151 window.$components.first(name);
152 // Get all the active components of the given name.
153 window.$components.get(name);
154 // Get the first active component of the given name that's been
155 // created on the given element.
156 window.$components.firstOnElement(element, name);
161 There are a range of available events that are emitted as part of a public & supported API for accessing or extending JavaScript libraries & components used in the system.
163 Details on these events can be found in the [JavaScript Public Events file](javascript-public-events.md).