]> BookStack Code Mirror - bookstack/blob - dev/docs/components.md
Skip intermediate login page with single provider
[bookstack] / dev / docs / components.md
1 # JavaScript Components
2
3 This document details the format for JavaScript components in BookStack. This is a really simple class-based setup with a few helpers provided.
4
5 #### Defining a Component in JS
6
7 ```js
8 class Dropdown {
9     setup() {
10         this.toggle = this.$refs.toggle;
11         this.menu = this.$refs.menu;
12     
13         this.speed = parseInt(this.$opts.speed);
14     }
15 }
16 ```
17
18 All usage of $refs, $manyRefs and $opts should be done at the top of the `setup` function so any requirements can be easily seen.
19
20 #### Using a Component in HTML
21
22 A component is used like so:
23
24 ```html
25 <div component="dropdown"></div>
26
27 <!-- or, for multiple -->
28
29 <div components="dropdown image-picker"></div>
30 ```
31
32 The names will be parsed and new component instance will be created if a matching name is found in the `components/index.js` componentMapping. 
33
34 #### Element References
35
36 Within a component you'll often need to refer to other element instances. This can be done like so:
37
38 ```html
39 <div component="dropdown">
40     <span refs="dropdown@toggle othercomponent@handle">View more</span>
41 </div>
42 ```
43
44 You can then access the span element as `this.$refs.toggle` in your component.
45
46 #### Component Options
47
48 ```html
49 <div component="dropdown"
50     option:dropdown:delay="500"
51     option:dropdown:show>
52 </div>
53 ```
54
55 Will result with `this.$opts` being:
56
57 ```json
58 {
59     "delay": "500",
60     "show": ""  
61 }
62 ```
63
64 #### Global Helpers
65
66 There are various global helper libraries which can be used in components:
67
68 ```js
69 // HTTP service
70 window.$http.get(url, params);
71 window.$http.post(url, data);
72 window.$http.put(url, data);
73 window.$http.delete(url, data);
74 window.$http.patch(url, data);
75
76 // Global event system
77 // Emit a global event
78 window.$events.emit(eventName, eventData);
79 // Listen to a global event
80 window.$events.listen(eventName, callback);
81 // Show a success message
82 window.$events.success(message);
83 // Show an error message
84 window.$events.error(message);
85 // Show validation errors, if existing, as an error notification
86 window.$events.showValidationErrors(error);
87
88 // Translator
89 // Take the given plural text and count to decide on what plural option
90 // to use, Similar to laravel's trans_choice function but instead
91 // takes the direction directly instead of a translation key.
92 window.trans_plural(translationString, count, replacements);
93
94 // Component System
95 // Parse and initialise any components from the given root el down.
96 window.components.init(rootEl);
97 // Get the first active component of the given name
98 window.components.first(name);
99 ```
Morty Proxy This is a proxified and sanitized view of the page, visit original site.