]> BookStack Code Mirror - bookstack/blob - dev/docs/javascript-code.md
Merge branch 'development' of github.com:BookStackApp/BookStack into development
[bookstack] / dev / docs / javascript-code.md
1 # BookStack JavaScript Code
2
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.
4
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.
6
7 ## Components
8
9 This section details the format for JavaScript components in BookStack. This is a really simple class-based setup with a few helpers provided.
10
11 ### Defining a Component in JS
12
13 ```js
14 class Dropdown {
15     setup() {
16         this.container = this.$el;
17         this.menu = this.$refs.menu;
18         this.toggles = this.$manyRefs.toggle;
19     
20         this.speed = parseInt(this.$opts.speed);
21     }
22 }
23 ```
24
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.
26
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. 
28
29 ### Using a Component in HTML
30
31 A component is used like so:
32
33 ```html
34 <div component="dropdown"></div>
35
36 <!-- or, for multiple -->
37
38 <div components="dropdown image-picker"></div>
39 ```
40
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. 
42
43 ### Element References
44
45 Within a component you'll often need to refer to other element instances. This can be done like so:
46
47 ```html
48 <div component="dropdown">
49     <span refs="dropdown@toggle othercomponent@handle">View more</span>
50 </div>
51 ```
52
53 You can then access the span element as `this.$refs.toggle` in your component.
54
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`.
56
57 ```html
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>
62 </div>
63 ```
64
65 ### Component Options
66
67 ```html
68 <div component="dropdown"
69     option:dropdown:delay="500"
70     option:dropdown:show>
71 </div>
72 ```
73
74 Will result with `this.$opts` being:
75
76 ```json
77 {
78     "delay": "500",
79     "show": ""  
80 }
81 ```
82
83 #### Component Properties & Methods
84
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.
86
87 ```javascript
88 // The root element that the component has been applied to.
89 this.$el
90
91 // A map of defined element references within the component.
92 // See "Element References" above.
93 this.$refs
94
95 // A map of defined multi-element references within the component.
96 // See "Element References" above.
97 this.$manyRefs
98
99 // Options defined for the component.
100 this.$opts
101
102 // The registered name of the component, usually kebab-case.
103 this.$name
104
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 = {})
110 ```
111
112 ## Global JavaScript Helpers
113
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. 
115
116 ```js
117 // HTTP service
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);
124
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);
136
137 // Translator
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);
142
143 // Component System
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);
157 ```
158
159 ## Public Events
160
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.
162
163 Details on these events can be found in the [JavaScript Public Events file](javascript-public-events.md).
Morty Proxy This is a proxified and sanitized view of the page, visit original site.