Skip to content

Commit

Permalink
ui: select menu items by Cypress through ids
Browse files Browse the repository at this point in the history
In Cypress it's difficult to choose a specific item in the menu if you
don't know in which language you are. Each element can be in French,
English, etc.
To facilitate this work we can add id= on each element on the menu.
With a specific word. This way you don't need to know in which language
you are.

* Adds Id on all menus ('My Account' menu, languages menu, help menu,
logout, etc.) for testing purposes.

Co-Authored-by: Olivier DOSSMANN <git@dossmann.net>
  • Loading branch information
blankoworld committed Jul 20, 2020
1 parent cdf6c67 commit 3891f73
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 23 deletions.
3 changes: 2 additions & 1 deletion rero_ils/modules/patrons/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ def logged_user():
blueprint,
'main.profile.profile',
_('%(icon)s Profile', icon='<i class="fa fa-user fa-fw"></i>'),
visible_when=user_has_patron
visible_when=user_has_patron,
id="my-account-menu"
)
def profile(viewcode):
"""Patron Profile Page."""
Expand Down
11 changes: 6 additions & 5 deletions rero_ils/templates/rero_ils/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,15 @@
<a class="nav-link collapsed" data-toggle="collapse" role="button"
aria-controls="collapseExample" aria-expanded="false"
href="#{{ item.name }}"
{{ "id={}".format(item.id) if item.id }}
>
{{ item.text|safe }}
<i class="fa fa-caret-down" aria-hidden="true"></i>
</a>
</li>
{%- else %}
<li class="nav-item{{ ' active' if item.active else ''}}">
<a href="{{ item.url }}">{{ item.text|safe }}</a>
<a href="{{ item.url }}" {{ "id={}".format(item.id) if item.id }}>{{ item.text|safe }}</a>
</li>
{%- endif %}
{%- endfor %}
Expand All @@ -86,7 +87,7 @@
<ul class="navbar-nav justify-content-end">
{%- for child in item.children if child.visible %}
<li class="nav-item {{class_name if class_name}}">
<a class="nav-link" href="{{ child.url }}">{{ child.text|safe }}</a>
<a class="nav-link" href="{{ child.url }}" {{ "id={}".format(child.id) if child.id }}>{{ child.text|safe }}</a>
</li>
{%- endfor %}
</ul>
Expand All @@ -103,18 +104,18 @@
{%- for item in current_menu.submenu('main').children|sort(attribute='order') if item.visible %}
{%- if item.children %}
<li class="nav-item{{ ' active' if item.active else ''}} w-100 list-group-item bg-light">
<a class="nav-link collapsed" href="#{{ item.name }}" data-toggle="collapse" role="button" aria-controls="collapseExample" aria-expanded="false">{{ item.text|safe }} <i class="fa fa-caret-down float-right" aria-hidden="true"></i></a>
<a class="nav-link collapsed" href="#{{ item.name }}" data-toggle="collapse" role="button" aria-controls="collapseExample" aria-expanded="false" {{ "id={}".format(item.id) if item.id }}>{{ item.text|safe }} <i class="fa fa-caret-down float-right" aria-hidden="true"></i></a>
<ul class="nav collapse pl-2" id="{{ item.name }}" data-parent="#mobileHide">
{%- for child in item.children if child.visible %}
<li class="nav-item {{class_name if class_name}} w-100">
<a class="nav-link" href="{{ child.url }}">{{ child.text|safe }}</a>
<a class="nav-link" href="{{ child.url }}" {{ "id={}".format(child.id) if child.id }}>{{ child.text|safe }}</a>
</li>
{%- endfor %}
</ul>
</li>
{%- else %}
<li class="nav-item{{ ' active' if item.active else ''}}">
<a href="{{ item.url }}" class="nav-link">{{ item.text|safe }}</a>
<a href="{{ item.url }}" class="nav-link" {{ "id={}".format(item.id) if item.id }}>{{ item.text|safe }}</a>
</li>
{%- endif %}
{%- endfor %}
Expand Down
89 changes: 72 additions & 17 deletions rero_ils/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,57 @@
)


def rero_register(
item,
endpoint=None,
text=None,
order=0,
external_url=None,
endpoint_arguments_constructor=None,
dynamic_list_constructor=None,
active_when=None,
visible_when=None,
expected_args=None,
**kwargs):
"""Take care each element in kwargs doesn't already exists in item."""
# Check which option in kwargs already exists in `item`.
to_delete = []
for option in kwargs.keys():
if hasattr(item, option):
to_delete.append(option)
# Delete all existing options in kwargs
for element in to_delete:
del kwargs[element]

return item.register(
endpoint,
text,
order,
external_url,
endpoint_arguments_constructor,
dynamic_list_constructor,
active_when,
visible_when,
expected_args,
**kwargs)


def init_menu_lang():
"""Create the header language menu."""
item = current_menu.submenu('main.menu')
item.register(
# Bug: when you reload the page with register(**kwargs), it failed
# We so check that 'id' already exists. If yes, do not create again
# the item.
rero_register(
item,
endpoint=None,
text='{icon} <span class="{visible}">{menu}'.format(
icon='<i class="fa fa-bars"></i>',
visible='visible-md-inline visible-lg-inline',
menu=_('Menu')
),
order=0
order=0,
id='language-menu'
)

order = 10
Expand All @@ -72,7 +112,8 @@ def hide_language(lang):
'main.menu.lang_{language}'.format(
language=language_item.language))
ui_language = 'ui_language_{lang}'.format(lang=language_item.language)
item.register(
rero_register(
item,
endpoint='invenio_i18n.set_lang',
endpoint_arguments_constructor=partial(
return_language, language_item.language),
Expand All @@ -81,18 +122,21 @@ def hide_language(lang):
language=_(ui_language)
),
visible_when=partial(hide_language, language_item.language),
order=order
)
order=order,
id='language-menu-{language}'.format(language=ui_language))
order += 1

item = current_menu.submenu('main.menu.help')
item.register(

rero_register(
item,
endpoint='wiki.index',
text='{icon} {help}'.format(
icon='<i class="fa fa-info"></i>',
help=_('Help')
),
order=100
order=100,
id='help-menu'
)


Expand All @@ -109,18 +153,22 @@ def init_menu_profile():
session.pop('user_initials', None)
account = session.get('user_initials', _('My Account'))

item.register(
rero_register(
item,
endpoint=None,
text='{icon} <span class="{visible}">{account}</span>'.format(
icon='<i class="fa fa-user"></i>',
visible='visible-md-inline visible-lg-inline',
account=account
),
order=1
order=1,
id='my-account-menu',
)

item = current_menu.submenu('main.profile.login')
item.register(

rero_register(
item,
endpoint='security.login',
endpoint_arguments_constructor=lambda: dict(
next=request.full_path
Expand All @@ -130,22 +178,26 @@ def init_menu_profile():
icon='<i class="fa fa-sign-in"></i>',
login=_('Login')
),
order=1
order=1,
id='login-menu',
)

item = current_menu.submenu('main.profile.professional')
item.register(
rero_register(
item,
endpoint='rero_ils.professional',
visible_when=lambda: current_patron.is_librarian,
text='{icon} {professional}'.format(
icon='<i class="fa fa-briefcase"></i>',
professional=_('Professional interface')
),
order=1
order=1,
id='professional-interface-menu',
)

item = current_menu.submenu('main.profile.logout')
item.register(
rero_register(
item,
endpoint='security.logout',
endpoint_arguments_constructor=lambda: dict(
next='/{viewcode}'.format(viewcode=request.view_args.get(
Expand All @@ -158,18 +210,21 @@ def init_menu_profile():
icon='<i class="fa fa-sign-out"></i>',
logout=_('Logout')
),
order=1
order=1,
id='logout-menu',
)

item = current_menu.submenu('main.profile.signup')
item.register(
rero_register(
item,
endpoint='security.register',
visible_when=lambda: not current_user.is_authenticated,
text='{icon} {signup}'.format(
icon='<i class="fa fa-user-plus"></i>',
signup=_('Sign Up')
),
order=2
order=2,
id='signup-menu',
)


Expand Down

0 comments on commit 3891f73

Please sign in to comment.