opal-vite 0.2.7 → 0.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/opal/vite/version.rb +1 -1
- data/opal/opal_vite/concerns/dom_helpers.rb +4 -86
- data/opal/opal_vite/concerns/js_proxy_ex.rb +4 -411
- data/opal/opal_vite/concerns/react_helpers.rb +4 -312
- data/opal/opal_vite/concerns/stimulus_helpers.rb +4 -1303
- data/opal/opal_vite/concerns/storable.rb +4 -28
- data/opal/opal_vite/concerns/toastable.rb +4 -33
- data/opal/opal_vite/concerns/v1/dom_helpers.rb +89 -0
- data/opal/opal_vite/concerns/v1/js_proxy_ex.rb +414 -0
- data/opal/opal_vite/concerns/v1/react_helpers.rb +315 -0
- data/opal/opal_vite/concerns/v1/stimulus_helpers.rb +1306 -0
- data/opal/opal_vite/concerns/v1/storable.rb +31 -0
- data/opal/opal_vite/concerns/v1/toastable.rb +36 -0
- data/opal/opal_vite/concerns/v1/vue_helpers.rb +249 -0
- data/opal/opal_vite/concerns/v1.rb +8 -0
- data/opal/opal_vite/concerns/vue_helpers.rb +4 -246
- data/opal/opal_vite/concerns.rb +6 -6
- metadata +9 -1
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
# backtick_javascript: true
|
|
2
|
+
|
|
3
|
+
# ReactHelpers - DSL helpers for React applications with Opal
|
|
4
|
+
# Reduces backtick JavaScript usage in React components
|
|
5
|
+
module ReactHelpers
|
|
6
|
+
# ===================
|
|
7
|
+
# React Access
|
|
8
|
+
# ===================
|
|
9
|
+
|
|
10
|
+
# Get React from window
|
|
11
|
+
def react
|
|
12
|
+
Native(`window.React`)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# Get ReactDOM from window
|
|
16
|
+
def react_dom
|
|
17
|
+
Native(`window.ReactDOM`)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# ===================
|
|
21
|
+
# Window/Global Access
|
|
22
|
+
# ===================
|
|
23
|
+
|
|
24
|
+
# Get a property from window
|
|
25
|
+
def window_get(key)
|
|
26
|
+
`window[#{key}]`
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Set a property on window
|
|
30
|
+
def window_set(key, value)
|
|
31
|
+
`window[#{key}] = #{value}`
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Delete a property from window
|
|
35
|
+
def window_delete(key)
|
|
36
|
+
`delete window[#{key}]`
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# ===================
|
|
40
|
+
# Console
|
|
41
|
+
# ===================
|
|
42
|
+
|
|
43
|
+
# Console log
|
|
44
|
+
def console_log(*args)
|
|
45
|
+
`console.log(...#{args})`
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Console warn
|
|
49
|
+
def console_warn(*args)
|
|
50
|
+
`console.warn(...#{args})`
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Console error
|
|
54
|
+
def console_error(*args)
|
|
55
|
+
`console.error(...#{args})`
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# ===================
|
|
59
|
+
# Alerts/Dialogs
|
|
60
|
+
# ===================
|
|
61
|
+
|
|
62
|
+
# Show alert dialog
|
|
63
|
+
def alert_message(message)
|
|
64
|
+
`alert(#{message})`
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Show confirm dialog
|
|
68
|
+
def confirm_message(message)
|
|
69
|
+
`confirm(#{message})`
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Show prompt dialog
|
|
73
|
+
def prompt_message(message, default_value = '')
|
|
74
|
+
`prompt(#{message}, #{default_value})`
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# ===================
|
|
78
|
+
# DOM Events
|
|
79
|
+
# ===================
|
|
80
|
+
|
|
81
|
+
# Execute block when DOM is ready
|
|
82
|
+
def on_dom_ready(&block)
|
|
83
|
+
`document.addEventListener('DOMContentLoaded', #{block})`
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Add event listener to window
|
|
87
|
+
def on_window_event(event_name, &block)
|
|
88
|
+
`window.addEventListener(#{event_name}, #{block})`
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Remove event listener from window
|
|
92
|
+
def off_window_event(event_name, handler)
|
|
93
|
+
`window.removeEventListener(#{event_name}, #{handler})`
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# ===================
|
|
97
|
+
# DOM Query
|
|
98
|
+
# ===================
|
|
99
|
+
|
|
100
|
+
# Query single element
|
|
101
|
+
def query(selector)
|
|
102
|
+
`document.querySelector(#{selector})`
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# Query all elements
|
|
106
|
+
def query_all(selector)
|
|
107
|
+
`Array.from(document.querySelectorAll(#{selector}))`
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# Get element by ID
|
|
111
|
+
def get_element_by_id(id)
|
|
112
|
+
`document.getElementById(#{id})`
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# ===================
|
|
116
|
+
# DOM Manipulation
|
|
117
|
+
# ===================
|
|
118
|
+
|
|
119
|
+
# Create element
|
|
120
|
+
def create_element(tag)
|
|
121
|
+
`document.createElement(#{tag})`
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# Set innerHTML
|
|
125
|
+
def set_html(element, html)
|
|
126
|
+
`#{element}.innerHTML = #{html}`
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# Set textContent
|
|
130
|
+
def set_text(element, text)
|
|
131
|
+
`#{element}.textContent = #{text}`
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# Add class to element
|
|
135
|
+
def add_class(element, *classes)
|
|
136
|
+
`#{element}.classList.add(...#{classes})`
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# Remove class from element
|
|
140
|
+
def remove_class(element, *classes)
|
|
141
|
+
`#{element}.classList.remove(...#{classes})`
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# ===================
|
|
145
|
+
# React Element Creation Helpers
|
|
146
|
+
# ===================
|
|
147
|
+
|
|
148
|
+
# Create React element (shorthand)
|
|
149
|
+
def el(type, props = nil, *children)
|
|
150
|
+
react.createElement(type, props, *children)
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# Create div element
|
|
154
|
+
def div(props = nil, *children, &block)
|
|
155
|
+
if block_given?
|
|
156
|
+
react.createElement('div', props, block.call)
|
|
157
|
+
else
|
|
158
|
+
react.createElement('div', props, *children)
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
# Create span element
|
|
163
|
+
def span(props = nil, *children, &block)
|
|
164
|
+
if block_given?
|
|
165
|
+
react.createElement('span', props, block.call)
|
|
166
|
+
else
|
|
167
|
+
react.createElement('span', props, *children)
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
# Create button element
|
|
172
|
+
def button(props = nil, *children, &block)
|
|
173
|
+
if block_given?
|
|
174
|
+
react.createElement('button', props, block.call)
|
|
175
|
+
else
|
|
176
|
+
react.createElement('button', props, *children)
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# Create p element
|
|
181
|
+
def paragraph(props = nil, *children, &block)
|
|
182
|
+
if block_given?
|
|
183
|
+
react.createElement('p', props, block.call)
|
|
184
|
+
else
|
|
185
|
+
react.createElement('p', props, *children)
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
# Create heading elements
|
|
190
|
+
def h1(props = nil, *children)
|
|
191
|
+
react.createElement('h1', props, *children)
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
def h2(props = nil, *children)
|
|
195
|
+
react.createElement('h2', props, *children)
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
def h3(props = nil, *children)
|
|
199
|
+
react.createElement('h3', props, *children)
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
# ===================
|
|
203
|
+
# Timing
|
|
204
|
+
# ===================
|
|
205
|
+
|
|
206
|
+
# Set timeout
|
|
207
|
+
def set_timeout(delay_ms, &block)
|
|
208
|
+
`setTimeout(#{block}, #{delay_ms})`
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
# Set interval
|
|
212
|
+
def set_interval(interval_ms, &block)
|
|
213
|
+
`setInterval(#{block}, #{interval_ms})`
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
# Clear timeout
|
|
217
|
+
def clear_timeout(timeout_id)
|
|
218
|
+
`clearTimeout(#{timeout_id})`
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
# Clear interval
|
|
222
|
+
def clear_interval(interval_id)
|
|
223
|
+
`clearInterval(#{interval_id})`
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
# ===================
|
|
227
|
+
# LocalStorage
|
|
228
|
+
# ===================
|
|
229
|
+
|
|
230
|
+
# Get from localStorage
|
|
231
|
+
def storage_get(key)
|
|
232
|
+
`localStorage.getItem(#{key})`
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
# Set to localStorage
|
|
236
|
+
def storage_set(key, value)
|
|
237
|
+
`localStorage.setItem(#{key}, #{value})`
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
# Remove from localStorage
|
|
241
|
+
def storage_remove(key)
|
|
242
|
+
`localStorage.removeItem(#{key})`
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
# ===================
|
|
246
|
+
# Fetch API
|
|
247
|
+
# ===================
|
|
248
|
+
|
|
249
|
+
# Fetch with promise (returns Native promise)
|
|
250
|
+
def fetch_url(url, options = nil)
|
|
251
|
+
if options
|
|
252
|
+
Native(`fetch(#{url}, #{options.to_n})`)
|
|
253
|
+
else
|
|
254
|
+
Native(`fetch(#{url})`)
|
|
255
|
+
end
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
# ===================
|
|
259
|
+
# JSON
|
|
260
|
+
# ===================
|
|
261
|
+
|
|
262
|
+
# Parse JSON string
|
|
263
|
+
def parse_json(json_string)
|
|
264
|
+
`JSON.parse(#{json_string})`
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
# Stringify to JSON
|
|
268
|
+
def to_json(object)
|
|
269
|
+
`JSON.stringify(#{object})`
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
# ===================
|
|
273
|
+
# Type Conversion
|
|
274
|
+
# ===================
|
|
275
|
+
|
|
276
|
+
# Parse string to integer (wrapper for JavaScript parseInt)
|
|
277
|
+
# @param value [String, Number] Value to parse
|
|
278
|
+
# @param radix [Integer] Radix (default: 10)
|
|
279
|
+
# @return [Integer, NaN] Parsed integer
|
|
280
|
+
def parse_int(value, radix = 10)
|
|
281
|
+
`parseInt(#{value}, #{radix})`
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
# Parse string to float (wrapper for JavaScript parseFloat)
|
|
285
|
+
# @param value [String, Number] Value to parse
|
|
286
|
+
# @return [Float, NaN] Parsed float
|
|
287
|
+
def parse_float(value)
|
|
288
|
+
`parseFloat(#{value})`
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
# Check if value is NaN
|
|
292
|
+
# @param value [Number] Value to check
|
|
293
|
+
# @return [Boolean] true if NaN
|
|
294
|
+
def is_nan?(value)
|
|
295
|
+
`Number.isNaN(#{value})`
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
# Parse integer with default value (returns default if NaN)
|
|
299
|
+
# @param value [String, Number] Value to parse
|
|
300
|
+
# @param default_value [Integer] Default value if parsing fails
|
|
301
|
+
# @return [Integer] Parsed integer or default
|
|
302
|
+
def parse_int_or(value, default_value = 0)
|
|
303
|
+
result = parse_int(value)
|
|
304
|
+
is_nan?(result) ? default_value : result
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
# Parse float with default value (returns default if NaN)
|
|
308
|
+
# @param value [String, Number] Value to parse
|
|
309
|
+
# @param default_value [Float] Default value if parsing fails
|
|
310
|
+
# @return [Float] Parsed float or default
|
|
311
|
+
def parse_float_or(value, default_value = 0.0)
|
|
312
|
+
result = parse_float(value)
|
|
313
|
+
is_nan?(result) ? default_value : result
|
|
314
|
+
end
|
|
315
|
+
end
|