spair 0.0.9

A framework for single-page application in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
use super::{HtmlElementUpdater, HtmlElementUpdaterMut};
use crate::{
    component::Component,
    dom::AttributeValueList,
    render::base::{
        AttributeMinMax, BoolAttributeValue, Class, ElementUpdater, ElementUpdaterMut,
        F64AttributeValue, I32AttributeValue, MethodsForEvents, StringAttributeValue,
        U32AttributeValue,
    },
};

macro_rules! make_traits_for_property_values {
    (
        $UpdaterType:ident
        $(
            $TraitName:ident {
                $(
                    $attribute_type:ty,
                    $method_name:ident
                    $ws_method_for_qr:ident $qr_method_name:ident $qrm_method_name:ident $qrmws_method_name:ident,
                )+
            }
        )+
    ) => {
        $(
            pub trait $TraitName<C: Component> {
                fn render(self, element: &mut $UpdaterType<C>);
            }
            $(
                impl<C: Component> $TraitName<C> for $attribute_type {
                    fn render(self, element: &mut $UpdaterType<C>) {
                        element.$method_name(self);
                    }
                }
                make_traits_for_property_values! {
                    @each_queue_render
                    $UpdaterType
                    $TraitName
                    $attribute_type,
                    $ws_method_for_qr $qr_method_name $qrm_method_name $qrmws_method_name
                }
            )+
        )+
    };
    (
        @each_queue_render
        $UpdaterType:ident
        $TraitName:ident
        $attribute_type:ty,
        NO_QUEUE_RENDER NO_QUEUE_RENDER NO_QUEUE_RENDER NO_QUEUE_RENDER
    ) => {
    };
    (
        @each_queue_render
        $UpdaterType:ident
        $TraitName:ident
        $attribute_type:ty,
        $ws_method_for_qr:ident $qr_method_name:ident $qrm_method_name:ident $qrmws_method_name:ident
    ) => {
        #[cfg(feature = "queue-render")]
        impl<C: Component> $TraitName<C> for &crate::queue_render::val::QrVal<$attribute_type> {
            fn render(self, element: &mut $UpdaterType<C>) {
                element.$qr_method_name(crate::dom::WsElement::$ws_method_for_qr, self);
            }
        }
        #[cfg(feature = "queue-render")]
        impl<C: Component, T: 'static> $TraitName<C> for crate::queue_render::val::QrValMap<T, $attribute_type> {
            fn render(self, element: &mut $UpdaterType<C>) {
                element.$qrm_method_name(crate::dom::WsElement::$ws_method_for_qr, self);
            }
        }
        #[cfg(feature = "queue-render")]
        impl<C: Component, T: 'static> $TraitName<C> for crate::queue_render::val::QrValMapWithState<C, T, $attribute_type> {
            fn render(self, element: &mut $UpdaterType<C>) {
                element.$qrmws_method_name(crate::dom::WsElement::$ws_method_for_qr, self);
            }
        }
    };
}

make_traits_for_property_values! {
    HtmlElementUpdater
    PropertyValue {
        &str,           selected_value_str              NO_QUEUE_RENDER NO_QUEUE_RENDER NO_QUEUE_RENDER NO_QUEUE_RENDER,
        Option<&str>,   selected_value_optional_str     NO_QUEUE_RENDER NO_QUEUE_RENDER NO_QUEUE_RENDER NO_QUEUE_RENDER,
        String,         selected_value_string           set_value_for_qr qr_property qrm_property qrmws_property,
        &String,        selected_value_str              NO_QUEUE_RENDER NO_QUEUE_RENDER NO_QUEUE_RENDER NO_QUEUE_RENDER,
        Option<String>, selected_value_optional_string  set_value_for_qr_optional qr_property qrm_property qrmws_property,
    }
    PropertyIndex {
        usize,          selected_index_usize            set_selected_index_ref qr_property qrm_property qrmws_property,
        Option<usize>,  selected_index_optional_usize   set_selected_index_optional qr_property qrm_property qrmws_property,
    }
}

/// Elements that have attribute/property `value` are `select`, `input`,
/// `option`, and `textarea`. Apart from `select`, other elements have
/// no issues with the `value` property. Setting `value` on a `select`
/// element expect the corresponding `option` element inside the
/// `select` element got highlighted as an selected item. But setting
/// the value property before adding the children (`option` elements)
/// of the select will not work. This trait provide methods to work
/// with attribute value to help handle the issue. But this trait alone
/// can not sovle the issue. We also need HtmlElementUpdater and
/// HtmlNodesUpdater.
pub trait MethodsForSelectedValueSelectedIndex<C: Component>:
    Sized + HtmlElementUpdaterMut<C>
{
    fn value(mut self, value: impl PropertyValue<C>) -> Self {
        value.render(self.html_element_updater_mut());
        self
    }

    fn selected_value(mut self, value: impl PropertyValue<C>) -> Self {
        value.render(self.html_element_updater_mut());
        self
    }

    fn selected_index(mut self, value: impl PropertyIndex<C>) -> Self {
        value.render(self.html_element_updater_mut());
        self
    }
}

make_traits_for_property_values! {
    ElementUpdater
    PropertyChecked {
        bool, checked checked_ref qr_property qrm_property qrmws_property,
    }
    AttributeEnabled {
        bool, enabled enabled_ref qr_property qrm_property qrmws_property,
    }
    ActionFocus {
        bool, focus focus_ref qr_property qrm_property qrmws_property,
    }
}

pub trait HamsHandMade<C: Component>:
    Sized + ElementUpdaterMut<C> + HamsForDistinctNames<C>
{
    fn done(self) {}

    fn set_attribute_str(mut self, name: &str, value: &str) -> Self {
        self.element_updater_mut().set_str_attribute(name, value);
        self
    }

    /// Only execute `input.set_checked` if the value changed. But it's safer
    /// to use `.checked()` instead.
    fn checked_if_changed(mut self, value: bool) -> Self {
        if self
            .element_updater_mut()
            .must_update_attribute(value, AttributeValueList::check_bool_attribute)
        {
            self.checked(value)
        } else {
            self
        }
    }

    /// The issue describes here does not affect elements in queue render
    /// list or keyed list.
    ///
    /// In incremental mode, this method always execute `input.set_checked`
    /// with the given value. This is useful in situation like in TodoMVC
    /// example. TodoMVC specification requires that when the app in a
    /// filtered mode, for example, 'active' mode just display
    /// active todo-items, if an item is checked (completed) by clicking the
    /// input, the app should hide the todo item. In such a situation, the
    /// DOM item is checked, but Spair DOM is not checked yet. But the
    /// checked item was filtered out (hidden), and only active todos
    /// are displayed, all of them are unchecked which match the state in
    /// Spair DOM, hence Spair skip setting check, leaving the DOM checked
    /// but display an unchecked item. In my understand, this only occurs
    /// with non-keyed list, keyed_list is not affect by this.
    /// I choose to always set checked to avoid surprises for new users.
    /// `checked_if_changed` can be used to reduce interaction with DOM if
    /// it does not bug you.
    fn checked(mut self, value: impl PropertyChecked<C>) -> Self {
        value.render(self.element_updater_mut());
        self
    }

    fn class(mut self, value: impl Class<C>) -> Self {
        value.render(self.element_updater_mut());
        self
    }

    fn class_if(mut self, class_on: bool, class_name: &str) -> Self {
        self.element_updater_mut().class_if(class_on, class_name);
        self
    }

    /// Set the `first_class` if `first` is true, otherwise, set the `second_class`
    fn class_or(mut self, first: bool, first_class: &str, second_class: &str) -> Self {
        self.element_updater_mut()
            .class_or(first, first_class, second_class);
        self
    }

    fn enabled(mut self, value: impl AttributeEnabled<C>) -> Self {
        value.render(self.element_updater_mut());
        self
    }

    fn focus(mut self, value: impl ActionFocus<C>) -> Self {
        value.render(self.element_updater_mut());
        self
    }

    /// This method only accepts a &Route. If you want set `href` with a str, please use `href_str()`.
    /// It is possible to make this method accept either a Route or a str, but I intentionally make
    /// them two separate methods. The purpose is to remind users to use a Route when it's possible.
    fn href(mut self, route: &C::Routes) -> Self {
        self.element_updater_mut().href(route);
        self
    }

    fn id(mut self, id: &str) -> Self {
        self.element_updater_mut().id(id);
        self
    }

    fn scroll_to_top_if(self, need_to_scroll: bool) -> Self {
        if need_to_scroll {
            self.element_updater()
                .element()
                .ws_element()
                .scroll_to_view_with_bool(true);
        }
        self
    }

    fn scroll_to_bottom_if(self, need_to_scroll: bool) -> Self {
        if need_to_scroll {
            self.element_updater()
                .element()
                .ws_element()
                .scroll_to_view_with_bool(false);
        }
        self
    }
}

#[cfg(test)]
use crate::render::html::TestHtmlMethods;

make_trait_for_attribute_methods! {
    TestStructs: (TestHtmlMethods)
    TraitName: HamsForDistinctNames
    attributes:
        i32     tab_index "tabindex"

        // moved to ../attributes_elements_with_ambiguous_names
        // str     abbr

        str     accept
        str     accept_charset "accept-charset"
        str     action
        str     allow
        str     allow_full_screen "allowfullscreen"
        bool    allow_payment_request "allowpaymentrequest"
        str     alt
        bool    auto_play "autoplay"

        // moved to ../attributes_elements_with_ambiguous_names
        // str     cite

        //str     class
        u32     cols
        u32     col_span "colspan"
        bool    content_editable "contenteditable"
        bool    controls
        str     coords

        // moved to ../attributes_elements_with_ambiguous_names
        // str     data

        str     date_time "datetime"
        bool    default
        str     dir_name "dirname"
        bool    disabled
        str     download
        str     r#for "for"

        // moved to ../attributes_elements_with_ambiguous_names
        // str     form

        str     form_action "formaction"
        bool    form_no_validate "formnovalidate"
        str     headers
        u32     height
        bool    hidden
        f64     high
        str     href_str "href" // method named `href` is used for routing
        str     href_lang "hreflang"
        bool    is_map "ismap"

        // moved to ../attributes_elements_with_ambiguous_names
        // str     label

        bool    r#loop "loop"
        f64     low
        AttributeMinMax  max
        i32     max_length "maxlength"
        str     media
        AttributeMinMax  min
        i32     min_length "minlength"
        bool    multiple
        bool    muted
        str     name
        bool    no_validate "novalidate"
        bool    open
        f64     optimum
        str     pattern
        str     ping
        str     placeholder
        str     poster
        bool    plays_inline "playsinline"
        bool    read_only "readonly"
        str     rel
        // ??     rellist
        bool    required
        bool    reversed
        u32     rows
        u32     row_span "rowspan"
        // ?? sandbox
        bool    selected
        u32     size
        str     sizes

        // moved to ../attributes_elements_with_ambiguous_names
        // u32     span

        str     src
        str     src_doc "srcdoc"
        str     src_lang "srclang"
        str     src_set "srcset"
        i32     start
        f64     step
        str     style
        str     title
        str     use_map "usemap"
        u32     width
}

pub struct AttributesOnly<'er, C: Component>(HtmlElementUpdater<'er, C>);
pub struct StaticAttributesOnly<'er, C: Component>(HtmlElementUpdater<'er, C>);
pub struct StaticAttributes<'er, C: Component>(HtmlElementUpdater<'er, C>);

impl<'er, C: Component> AttributesOnly<'er, C> {
    pub(super) fn new(er: HtmlElementUpdater<'er, C>) -> Self {
        Self(er)
    }
    pub(super) fn into_inner(self) -> HtmlElementUpdater<'er, C> {
        self.0
    }

    pub fn static_attributes_only(self) -> StaticAttributesOnly<'er, C> {
        StaticAttributesOnly::new(self.0)
    }
}

impl<'er, C: Component> StaticAttributesOnly<'er, C> {
    pub(super) fn new(mut er: HtmlElementUpdater<'er, C>) -> Self {
        er.element_updater_mut().set_static_mode();
        Self(er)
    }
    pub(super) fn into_inner(self) -> HtmlElementUpdater<'er, C> {
        self.0
    }
}

impl<'er, C: Component> StaticAttributes<'er, C> {
    pub(super) fn new(mut er: HtmlElementUpdater<'er, C>) -> Self {
        er.element_updater_mut().set_static_mode();
        Self(er)
    }
    pub(super) fn into_inner(self) -> HtmlElementUpdater<'er, C> {
        self.0
    }
    pub fn static_attributes_only(self) -> StaticAttributesOnly<'er, C> {
        StaticAttributesOnly::new(self.0)
    }
}

impl<'er, C: Component> ElementUpdaterMut<C> for AttributesOnly<'er, C> {
    fn element_updater(&self) -> &ElementUpdater<C> {
        self.0.element_updater()
    }
    fn element_updater_mut(&mut self) -> &mut ElementUpdater<C> {
        self.0.element_updater_mut()
    }
}
impl<'er, C: Component> HtmlElementUpdaterMut<C> for AttributesOnly<'er, C> {
    fn html_element_updater_mut(&mut self) -> &'er mut HtmlElementUpdater<C> {
        &mut self.0
    }
}

impl<'er, C: Component> ElementUpdaterMut<C> for StaticAttributesOnly<'er, C> {
    fn element_updater(&self) -> &ElementUpdater<C> {
        self.0.element_updater()
    }
    fn element_updater_mut(&mut self) -> &mut ElementUpdater<C> {
        self.0.element_updater_mut()
    }
}
impl<'er, C: Component> HtmlElementUpdaterMut<C> for StaticAttributesOnly<'er, C> {
    fn html_element_updater_mut(&mut self) -> &'er mut HtmlElementUpdater<C> {
        &mut self.0
    }
}

impl<'er, C: Component> ElementUpdaterMut<C> for StaticAttributes<'er, C> {
    fn element_updater(&self) -> &ElementUpdater<C> {
        self.0.element_updater()
    }
    fn element_updater_mut(&mut self) -> &mut ElementUpdater<C> {
        self.0.element_updater_mut()
    }
}
impl<'er, C: Component> HtmlElementUpdaterMut<C> for StaticAttributes<'er, C> {
    fn html_element_updater_mut(&mut self) -> &'er mut HtmlElementUpdater<C> {
        &mut self.0
    }
}

impl<'er, C: Component> MethodsForSelectedValueSelectedIndex<C> for HtmlElementUpdater<'er, C> {}
impl<'er, C: Component> HamsHandMade<C> for HtmlElementUpdater<'er, C> {}
impl<'er, C: Component> HamsForDistinctNames<C> for HtmlElementUpdater<'er, C> {}

impl<'er, C: Component> MethodsForSelectedValueSelectedIndex<C> for StaticAttributes<'er, C> {}
impl<'er, C: Component> HamsHandMade<C> for StaticAttributes<'er, C> {}
impl<'er, C: Component> HamsForDistinctNames<C> for StaticAttributes<'er, C> {}

impl<'er, C: Component> MethodsForSelectedValueSelectedIndex<C> for AttributesOnly<'er, C> {}
impl<'er, C: Component> HamsHandMade<C> for AttributesOnly<'er, C> {}
impl<'er, C: Component> HamsForDistinctNames<C> for AttributesOnly<'er, C> {}

impl<'er, C: Component> MethodsForSelectedValueSelectedIndex<C> for StaticAttributesOnly<'er, C> {}
impl<'er, C: Component> HamsHandMade<C> for StaticAttributesOnly<'er, C> {}
impl<'er, C: Component> HamsForDistinctNames<C> for StaticAttributesOnly<'er, C> {}

impl<'er, C: Component> MethodsForEvents<C> for StaticAttributes<'er, C> {}
impl<'er, C: Component> MethodsForEvents<C> for StaticAttributesOnly<'er, C> {}
impl<'er, C: Component> MethodsForEvents<C> for AttributesOnly<'er, C> {}