|
| 1 | +# WARP.md |
| 2 | + |
| 3 | +This is a vanilla js, css and html project. It uses rspack to build the files. |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +Please use dataroom-js for all custom html elements, and use the above features in dataroom-js. https://github.com/DATAROOM-NETWORK/dataroom.js |
| 8 | + |
| 9 | +Basic Usage |
| 10 | +To use DataroomElement, you can either use it directly in your HTML or extend it to create your own custom components. |
| 11 | + |
| 12 | +Extending DataroomElement |
| 13 | +import DataroomElement from 'dataroom-js'; |
| 14 | + |
| 15 | +class MyComponent extends DataroomElement { |
| 16 | + async initialize() { |
| 17 | + // Your initialization logic here |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +customElements.define('my-component', MyComponent); |
| 22 | +Features |
| 23 | +create(type, attributes, target_el) |
| 24 | +Creates a new HTML element and appends it to the component or a specified target. |
| 25 | + |
| 26 | +Example: |
| 27 | + |
| 28 | +class MyComponent extends DataroomElement { |
| 29 | + async initialize() { |
| 30 | + const container = this.create('div', { class: 'container' }); |
| 31 | + this.create('p', { content: 'Hello, World!' }, container); |
| 32 | + } |
| 33 | +} |
| 34 | +event(name, detail) |
| 35 | +Emits a custom event from the component. |
| 36 | + |
| 37 | +Example: |
| 38 | + |
| 39 | +class MyComponent extends DataroomElement { |
| 40 | + async initialize() { |
| 41 | + this.event('my-event', { foo: 'bar' }); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +const myComponent = document.querySelector('my-component'); |
| 46 | +myComponent.addEventListener('my-event', (e) => { |
| 47 | + console.log(e.detail); // { foo: 'bar' } |
| 48 | +}); |
| 49 | +on(name, cb) and once(name, cb) |
| 50 | +Attaches an event listener to the component. once is a variant that fires the listener only one time. |
| 51 | + |
| 52 | +on Example: |
| 53 | + |
| 54 | +class MyComponent extends DataroomElement { |
| 55 | + async initialize() { |
| 56 | + this.on('my-event', (detail) => { |
| 57 | + console.log('This will be logged every time:', detail); |
| 58 | + }); |
| 59 | + |
| 60 | + this.event('my-event', { foo: 'bar' }); |
| 61 | + this.event('my-event', { foo: 'baz' }); |
| 62 | + } |
| 63 | +} |
| 64 | +once Example: |
| 65 | + |
| 66 | +class MyComponent extends DataroomElement { |
| 67 | + async initialize() { |
| 68 | + this.once('one-time-event', (detail) => { |
| 69 | + console.log('This will only be logged once:', detail); |
| 70 | + }); |
| 71 | + |
| 72 | + // Firing the event multiple times |
| 73 | + this.event('one-time-event', { attempt: 1 }); |
| 74 | + this.event('one-time-event', { attempt: 2 }); |
| 75 | + } |
| 76 | +} |
| 77 | +call(endpoint, body) |
| 78 | +A helper for making fetch requests. It includes features for handling different security schemes and request timeouts. |
| 79 | + |
| 80 | +security-scheme Attribute |
| 81 | +Determines the authentication method: |
| 82 | + |
| 83 | +localstorage: (Default) Sends a bearer token from localStorage. |
| 84 | +cookie: Relies on the browser to send cookies automatically. |
| 85 | +Example: |
| 86 | + |
| 87 | +<my-component security-scheme="localstorage"></my-component> |
| 88 | +// In your component |
| 89 | +const data = await this.call('/api/data'); |
| 90 | +call-timeout Attribute |
| 91 | +Sets a timeout for the request in milliseconds. |
| 92 | + |
| 93 | +Example: |
| 94 | + |
| 95 | +<my-component call-timeout="5000"></my-component> |
| 96 | +getJSON(url) |
| 97 | +Fetches a JSON file from a URL, parses it, and returns it as a JavaScript object. It includes robust error handling for network issues, bad HTTP statuses, and JSON parsing errors. |
| 98 | + |
| 99 | +Example: |
| 100 | + |
| 101 | +class JsonComponent extends DataroomElement { |
| 102 | + async initialize() { |
| 103 | + try { |
| 104 | + // Public APIs are great for examples |
| 105 | + const data = await this.getJSON('https://jsonplaceholder.typicode.com/users/1'); |
| 106 | + this.log(`Fetched user: ${data.name}`); |
| 107 | + this.innerHTML = `Hello, ${data.name}!`; |
| 108 | + } catch (error) { |
| 109 | + console.error(error); |
| 110 | + this.innerHTML = `Failed to fetch data: ${error.message}`; |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | +log(message) |
| 115 | +Logs a message to the console if the verbose attribute is set. |
| 116 | + |
| 117 | +Example: |
| 118 | + |
| 119 | +<my-component verbose="true"></my-component> |
| 120 | +class MyComponent extends DataroomElement { |
| 121 | + async initialize() { |
| 122 | + this.log('Initializing component...'); |
| 123 | + } |
| 124 | +} |
| 125 | +Lifecycle Methods |
| 126 | +DataroomElement provides several lifecycle methods that you can override to control the component's behavior. |
| 127 | + |
| 128 | +initialize(): Called after the component is connected to the DOM and its attributes are available. |
| 129 | +disconnect(): Called when the component is removed from the DOM. |
| 130 | +Example: |
| 131 | + |
| 132 | +class MyComponent extends DataroomElement { |
| 133 | + async initialize() { |
| 134 | + console.log('Component initialized!'); |
| 135 | + } |
| 136 | + |
| 137 | + async disconnect() { |
| 138 | + console.log('Component disconnected!'); |
| 139 | + } |
| 140 | +} |
| 141 | +Attribute Observation |
| 142 | +DataroomElement automatically observes attribute changes and fires a NODE-CHANGED event. |
| 143 | + |
| 144 | +Example: |
| 145 | + |
| 146 | +class MyComponent extends DataroomElement { |
| 147 | + async initialize() { |
| 148 | + this.on('NODE-CHANGED', (detail) => { |
| 149 | + console.log(detail.attribute, detail.newValue); |
| 150 | + }); |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +const myComponent = document.querySelector('my-component'); |
| 155 | +myComponent.setAttribute('foo', 'bar'); // Logs: foo bar |
| 156 | + |
| 157 | +We do not use shadowdom. |
| 158 | + |
| 159 | +We do not embedd CSS in our Javascript -- we have separate CSS for that. If you think you need a CSS for a component, create a new CSS file in the styles/ folder with the name of the component and import it via the index.css file. |
| 160 | + |
| 161 | +We use DockBlock style comments for all code. |
0 commit comments