|
| 1 | +# Helper Functions |
| 2 | + |
| 3 | +OpenScript provides a set of global helper functions and utilities to simplify common tasks, DOM manipulation, and framework interaction. |
| 4 | + |
| 5 | +## Logic Helpers |
| 6 | + |
| 7 | +These helpers are available globally (e.g., `window.ifElse`) and can be used directly in your code or templates. |
| 8 | + |
| 9 | +### `ifElse(condition, trueValue, falseValue)` |
| 10 | + |
| 11 | +Evaluates a condition and returns one of two values. If the values are functions, they are executed. |
| 12 | + |
| 13 | +```javascript |
| 14 | +// Basic usage |
| 15 | +const status = ifElse(isOnline, "Online", "Offline"); |
| 16 | + |
| 17 | +// With functions (lazy evaluation) |
| 18 | +const result = ifElse( |
| 19 | + isValid, |
| 20 | + () => complexCalculation(), |
| 21 | + () => "Invalid", |
| 22 | +); |
| 23 | +``` |
| 24 | + |
| 25 | +### `coalesce(value1, value2)` |
| 26 | + |
| 27 | +Returns the first non-null/undefined value. Handy for defaults. |
| 28 | + |
| 29 | +```javascript |
| 30 | +const name = coalesce(userInput, "Guest"); |
| 31 | +``` |
| 32 | + |
| 33 | +### `each(iterable, callback)` |
| 34 | + |
| 35 | +Safely iterates over arrays or objects. Returns an array of results. |
| 36 | + |
| 37 | +```javascript |
| 38 | +// Array |
| 39 | +each([1, 2, 3], (num) => console.log(num)); |
| 40 | + |
| 41 | +// Object |
| 42 | +each({ a: 1, b: 2 }, (val, key) => console.log(key, val)); |
| 43 | +``` |
| 44 | + |
| 45 | +### `lazyFor(array, callback)` |
| 46 | + |
| 47 | +Iterates over an array asynchronously using `setTimeout(..., 0)` to avoid blocking the main thread during large operations. |
| 48 | + |
| 49 | +```javascript |
| 50 | +lazyFor(hugeArray, (item) => { |
| 51 | + // Process item without freezing UI |
| 52 | +}); |
| 53 | +``` |
| 54 | + |
| 55 | +--- |
| 56 | + |
| 57 | +## DOM Utilities (`dom`) |
| 58 | + |
| 59 | +The `dom` object provides shortcuts for common DOM operations. |
| 60 | + |
| 61 | +### Selection |
| 62 | + |
| 63 | +- **`dom.id(id)`**: wrapper for `document.getElementById`. |
| 64 | +- **`dom.get(selector, parent?)`**: wrapper for `querySelector`. |
| 65 | +- **`dom.all(selector, parent?)`**: wrapper for `querySelectorAll`. |
| 66 | +- **`dom.byClass(className, parent?)`**: wrapper for `getElementsByClassName`. |
| 67 | + |
| 68 | +### Manipulation |
| 69 | + |
| 70 | +- **`dom.create(type)`**: wrapper for `document.createElement`. |
| 71 | +- **`dom.put(html, element, append = false)`**: Sets `innerHTML`. |
| 72 | +- **`dom.clear(element)`**: Clears `innerHTML`. |
| 73 | +- **`dom.disable(element)` / `dom.enable(element)`**: Toggles `disabled` attribute. |
| 74 | + |
| 75 | +### Positioning |
| 76 | + |
| 77 | +- **`dom.centerInside(container, element)`**: Centers an absolutely positioned element within a container. |
| 78 | + |
| 79 | +--- |
| 80 | + |
| 81 | +## Framework Helpers |
| 82 | + |
| 83 | +### `app(serviceName?)` |
| 84 | + |
| 85 | +Access the IoC Container. |
| 86 | + |
| 87 | +- `app("router")`: Get Router. |
| 88 | +- `app("broker")`: Get Event Broker. |
| 89 | +- `app()`: Get the Container instance itself. |
| 90 | + |
| 91 | +### `component(id)` |
| 92 | + |
| 93 | +Retrieves a Component instance by its unique ID (UID). |
| 94 | +Useful in event listeners where you have the UID but not the instance. |
| 95 | + |
| 96 | +```javascript |
| 97 | +const myComp = component(123); |
| 98 | +myComp?.setState(newState); |
| 99 | +``` |
| 100 | +
|
| 101 | +### `context(name)` & `putContext(name, value)` |
| 102 | +
|
| 103 | +Shortcuts for the Context API. |
| 104 | +
|
| 105 | +- `context("theme")`: Get the "theme" context. |
| 106 | +- `putContext("theme", "dark")`: Define/Update the "theme" context. |
| 107 | +
|
| 108 | +### `state(initialValue)` |
| 109 | +
|
| 110 | +Creates a new State object. |
| 111 | +
|
| 112 | +```javascript |
| 113 | +const count = state(0); |
| 114 | +``` |
| 115 | +
|
| 116 | +### `v(state, callback)` |
| 117 | +
|
| 118 | +Creates an "Anonymous Component" that updates when the bound state changes. |
| 119 | +
|
| 120 | +```javascript |
| 121 | +// Renders text that updates automatically |
| 122 | +h.div( |
| 123 | + {}, |
| 124 | + v(countState, (val) => `Count is: ${val}`), |
| 125 | +); |
| 126 | +``` |
| 127 | +
|
| 128 | +### `ojs(...classes)` |
| 129 | +
|
| 130 | +The main entry point to run the application runner. |
| 131 | +
|
| 132 | +```javascript |
| 133 | +ojs(AppClass); |
| 134 | +``` |
0 commit comments