Samples / Learn / Event

Event

A Link whose default browser action is suppressed (check_prevent_default), so the app handles the click instead of the href.

Class
z2ui5_cl_smp_app_472
Repository
abap2UI5/samples
UI5 libraries
sap.m, sap.ui.core, sap.ui.layout
Lowest UI5 release
1.71
In the browser
Runs in the playground
Controls it builds
sap.m.Label, sap.m.Link, sap.m.MessageStrip, sap.m.Page, sap.m.Shell, sap.m.Switch, sap.m.Text, sap.ui.core.mvc.View, sap.ui.layout.form.SimpleForm
Documentation
cookbook/event_navigation/frontend ↗

The class

" @keywords link href default action check_prevent_default
" @summary A Link whose default browser action is suppressed (check_prevent_default), so the app handles the click instead of the href.
" @docs https://abap2ui5.github.io/docs/cookbook/event_navigation/frontend
CLASS z2ui5_cl_smp_app_472 DEFINITION PUBLIC.

  PUBLIC SECTION.
    INTERFACES z2ui5_if_app.

    DATA block_navigation TYPE abap_bool.
    DATA last_press TYPE string.

  PROTECTED SECTION.
    DATA client TYPE REF TO z2ui5_if_client.

    METHODS on_event.
    METHODS view_display.

  PRIVATE SECTION.
ENDCLASS.


CLASS z2ui5_cl_smp_app_472 IMPLEMENTATION.

  METHOD z2ui5_if_app~main.

    me->client = client.
    IF client->check_on_init( ).
      block_navigation = abap_true.
      view_display( ).
    ELSEIF client->check_on_navigated( ).
      view_display( ).
    ELSEIF client->check_on_event( ).
      on_event( ).
    ENDIF.

  ENDMETHOD.


  METHOD on_event.

    CASE client->get_event( ).

      WHEN `LINK_PRESS`.

        IF block_navigation = abap_true.
          last_press = `Link pressed - the browser did NOT follow the href, the backend decides what happens.`.
        ELSE.
          last_press = `Link pressed - the href was followed by the browser as usual.`.
        ENDIF.

      WHEN `TOGGLE`.
        " the flag is part of the event registration, so the view has to be
        " rebuilt for the change to reach the frontend
        view_display( ).

    ENDCASE.

  ENDMETHOD.


  METHOD view_display.

    DATA(view) = z2ui5_cl_ui5_view_builder=>factory(
        )->ele( n = `View` ns = `mvc`
            )->a( n = `displayBlock` v = `true`
            )->a( n = `height`       v = `100%`
            )->a( n = `xmlns`        v = `sap.m`
            )->a( n = `xmlns:mvc`    v = `sap.ui.core.mvc`
            )->a( n = `xmlns:form`   v = `sap.ui.layout.form` ).

    DATA(page) = view->ele( `Shell`
        )->ele( `Page`
            )->a( n = `title`          v = `abap2UI5 - Event - Link with preventDefault`
            )->a( n = `showNavButton`  b = client->check_app_prev_stack( )
            )->a( n = `navButtonPress` v = client->_event_nav_app_leave( ) ).

    page->tag( `MessageStrip`
        )->a( n = `text`     v = `A sap.m.Link normally follows its href when pressed. Registered with ` &&
                   `s_ctrl-check_prevent_default the event cancels that built-in default ` &&
                   `(oEvent.preventDefault()) before the roundtrip - the event still reaches the ` &&
                   `backend, so the app decides what happens instead. Flip the switch to compare.`
        )->a( n = `type`     v = `Information`
        )->a( n = `showIcon` b = abap_true
        )->a( n = `class`    v = `sapUiSmallMargin` ).

    DATA(form) = page->ele( n = `SimpleForm` ns = `form`
        )->a( n = `title`    v = `Link with a cancelled default`
        )->a( n = `editable` b = abap_true
        )->ele( n = `content` ns = `form` ).

    form->tag( `Label`
        )->a( n = `text` v = `Cancel the browser navigation` ).

    form->tag( `Switch`
        )->a( n = `state`  v = client->_bind( block_navigation )
        )->a( n = `change` v = client->_event( `TOGGLE` ) ).

    form->tag( `Label`
        )->a( n = `text` v = `Link` ).

    form->tag( `Link`
        )->a( n = `text`   v = `Open abap2ui5.org`
        )->a( n = `target` v = `_blank`
        )->a( n = `href`   v = `https://abap2ui5.org`
        )->a( n = `press`  v = client->_event(
            val    = `LINK_PRESS`
            s_ctrl = VALUE #( check_prevent_default = block_navigation ) ) ).

    form->tag( `Label`
        )->a( n = `text` v = `Result` ).

    form->tag( `Text`
        )->a( n = `text` v = client->_bind( last_press ) ).

    client->view_display( view->stringify( ) ).

  ENDMETHOD.

ENDCLASS.