Skip to content

Commit

Permalink
Refactor / Improved target blank links (files, links and so on)
Browse files Browse the repository at this point in the history
  • Loading branch information
boulch committed Feb 24, 2025
1 parent 6675673 commit 747ee43
Show file tree
Hide file tree
Showing 24 changed files with 208 additions and 17 deletions.
7 changes: 7 additions & 0 deletions src/imio/smartweb/core/behaviors/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,11 @@
factory=".subsite.SubsiteCroppingProvider"
/>

<plone:behavior
name="imio.smartweb.new_tab"
title="Open in new tab"
description="Add a field that explicitly allows specifying whether this content (often a link) should open in a new window or not."
provides=".new_tab.INewTab"
/>

</configure>
19 changes: 19 additions & 0 deletions src/imio/smartweb/core/behaviors/new_tab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-

from imio.smartweb.locales import SmartwebMessageFactory as _
from plone.autoform import directives
from plone.autoform.interfaces import IFormFieldProvider
from plone.supermodel import model
from zope import schema
from zope.interface import provider


@provider(IFormFieldProvider)
class INewTab(model.Schema):

directives.order_after(open_in_new_tab="remoteUrl")
open_in_new_tab = schema.Bool(
title=_("Open in a new tab"),
required=False,
default=False,
)
4 changes: 3 additions & 1 deletion src/imio/smartweb/core/browser/faceted/macros.pt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
</tal:if>

<tal:if tal:condition="python: not faceted_view.is_video(item)">
<a tal:attributes="href item/getURL">
<a tal:attributes="href item/getURL;
target python:faceted_view.target(item);
title python:faceted_view.a_tag_item_title(item)">
<tal:if tal:condition="show_images">
<tal:def tal:define="scale_url python: faceted_view.get_scale_url(item, thumb_scale)">
<div class="newsImage card-img-top no-image"
Expand Down
25 changes: 25 additions & 0 deletions src/imio/smartweb/core/browser/faceted/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
from eea.facetednavigation.layout.interfaces import IFacetedLayout
from imio.smartweb.core.interfaces import IViewWithoutLeadImage
from imio.smartweb.core.utils import get_scale_url
from imio.smartweb.locales import SmartwebMessageFactory as _
from plone import api
from plone.app.contenttypes.browser.folder import FolderView
from plone.app.contenttypes.interfaces import IFile
from zope.component import queryAdapter
from zope.i18n import translate
from zope.interface import implementer


Expand All @@ -29,6 +33,27 @@ def show_images(self):
def is_video(self, item):
return item.portal_type == "imio.smartweb.SectionVideo"

def is_target_blank(self, item):
# don't wake up object
if item.portal_type == "File":
return True
# if IFile.providedBy(item.getObject()):
# return True
return False

def target(self, item):
if self.is_target_blank(item):
return "_blank"
return ""

def a_tag_item_title(self, item):
title = item.Title or ""
if self.is_target_blank(item):
current_lang = api.portal.get_current_language()[:2]
new_tab_txt = translate(_("New tab"), target_language=current_lang)
return f"{title} ({new_tab_txt})"
return title

def get_scale_url(self, item, scale="vignette"):
orientation = self.context.orientation
if item.portal_type == "imio.smartweb.SectionGallery":
Expand Down
4 changes: 0 additions & 4 deletions src/imio/smartweb/core/contents/blocks/link/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ class IBlockLink(ILink):

description = schema.Text(title=_("Description"), required=False)

open_in_new_tab = schema.Bool(
title=_("Open in a new tab"), required=False, default=False
)

remoteUrl = LinkField(title=_("URL"), required=True)

image = NamedBlobImage(title=_("Image"), required=False)
Expand Down
8 changes: 6 additions & 2 deletions src/imio/smartweb/core/contents/folder/macros.pt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

<metal:macro define-macro="block">

<a tal:attributes="href item/getURL">
<a tal:attributes="href item/getURL;
title python:view.a_tag_item_title(item);
target python:'_blank' if view.open_in_new_tab(item) else '';">
<tal:if tal:condition="view/show_images">
<div tal:condition="python: not item.has_leadimage or not thumb_scale"
class="newsImage card-img-top no-image" />
Expand All @@ -28,7 +30,9 @@

<metal:macro define-macro="summary">

<a class="row" tal:attributes="href item/getURL">
<a class="row" tal:attributes="href item/getURL;
title python:view.a_tag_item_title(item);
target python:'_blank' if view.open_in_new_tab(item) else '';">
<tal:if tal:condition="view/show_images">
<tal:img tal:condition="python: item.has_leadimage and thumb_scale">
<div class="newsImage card-img-left col-md-2"
Expand Down
14 changes: 14 additions & 0 deletions src/imio/smartweb/core/contents/folder/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from z3c.form.interfaces import IFieldsAndContentProvidersForm
from zope.component import queryMultiAdapter
from zope.contentprovider.provider import ContentProviderBase
from zope.i18n import translate
from zope.interface import implementer


Expand Down Expand Up @@ -87,6 +88,19 @@ def get_scale_url(self, item, scale):
orientation = self.context.orientation
return get_scale_url(item, request, "image", scale, orientation)

def open_in_new_tab(self, item):
if hasattr(item, "open_in_new_tab"):
return item.open_in_new_tab
return False

def a_tag_item_title(self, item):
title = item.title or ""
if self.open_in_new_tab(item):
current_lang = api.portal.get_current_language()[:2]
new_tab_txt = translate(_("New tab"), target_language=current_lang)
return f"{title} ({new_tab_txt})"
return title


@implementer(IViewWithoutLeadImage)
class FolderViewWithImages(FolderView):
Expand Down
4 changes: 4 additions & 0 deletions src/imio/smartweb/core/contents/sections/collection/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ def items(self):
)
results = []
for item in items:
open_in_new_tab = False
if item.portal_type == "File":
open_in_new_tab = True
url = item.getURL()
scale_url = get_scale_url(
item, self.request, "image", image_scale, orientation
Expand All @@ -27,6 +30,7 @@ def items(self):
"effective": item.effective,
"url": url,
"has_image": item.has_leadimage,
"open_in_new_tab": open_in_new_tab,
}
if scale_url == "":
dict_item["bad_scale"] = image_scale
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<tal:def define="item_has_image item/has_image;
show_lead_image python: getattr(context, 'show_items_lead_image', True);
portal context/@@plone_portal_state/portal;
open_in_new_tab item/open_in_new_tab | nothing;">
open_in_new_tab python:view.open_in_new_tab(item);">
<a tal:omit-tag="not: item/url"
tal:attributes="href item/url;
class item/container_id | nothing;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
smartweb_icons python:context.restrictedTraverse('@@smartwebiconresolver');
show_lead_image python:getattr(context, 'show_items_lead_image', True);
portal context/@@plone_portal_state/portal;
open_in_new_tab item/open_in_new_tab | nothing;"
open_in_new_tab python:view.open_in_new_tab(item);"
tal:attributes="class string:table_display ${item/container_id | nothing} ${item/smartweb_type | nothing};"
>
<a tal:attributes="class python:'table_image no-image' if no_icon_no_image else 'table_image';
Expand Down
4 changes: 3 additions & 1 deletion src/imio/smartweb/core/contents/sections/links/view.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-

from imio.smartweb.core.behaviors.new_tab import INewTab
from imio.smartweb.core.contents.sections.views import CarouselOrTableSectionView
from imio.smartweb.core.utils import batch_results
from imio.smartweb.core.utils import get_scale_url
Expand Down Expand Up @@ -30,6 +31,7 @@ def items(self):
scale_url = get_scale_url(
item, self.request, "image", image_scale, orientation
)

results.append(
{
"title": item.title,
Expand All @@ -39,7 +41,7 @@ def items(self):
"has_icon": has_icon,
"image": scale_url,
"has_image": has_image,
"open_in_new_tab": item.open_in_new_tab,
"open_in_new_tab": INewTab(item).open_in_new_tab,
}
)
return batch_results(results, self.context.nb_results_by_batch)
Expand Down
4 changes: 0 additions & 4 deletions src/imio/smartweb/core/contents/sections/slide/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ class ISectionSlide(ISection):

remoteUrl = LinkField(title=_("Link URL"), required=False)

open_in_new_tab = schema.Bool(
title=_("Open in a new tab"), required=False, default=False
)


@implementer(ISectionSlide)
class SectionSlide(Section):
Expand Down
16 changes: 15 additions & 1 deletion src/imio/smartweb/core/contents/sections/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,22 @@ def save_size(self):
)
return json.dumps({"id": section_size, "title": size_txt})

def open_in_new_tab(self, item):
open_in_new_tab = (
item.get("open_in_new_tab")
if isinstance(item, dict)
else getattr(item, "open_in_new_tab", False)
)
return open_in_new_tab

def a_tag_item_title(self, item):
title = item.get("title") or ""
title = (
item.get("title") if isinstance(item, dict) else getattr(item, "Title", "")
)
if self.open_in_new_tab(item):
current_lang = api.portal.get_current_language()[:2]
new_tab_txt = translate(_("New tab"), target_language=current_lang)
return f"{title} ({new_tab_txt})"
return title


Expand Down
2 changes: 1 addition & 1 deletion src/imio/smartweb/core/profiles/default/metadata.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version='1.0' encoding='UTF-8'?>
<metadata>
<version>1062</version>
<version>1064</version>
<dependencies>
<dependency>profile-plone.app.dexterity:default</dependency>
<dependency>profile-plone.app.imagecropping:default</dependency>
Expand Down
13 changes: 13 additions & 0 deletions src/imio/smartweb/core/profiles/default/types/Link.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<object xmlns:i18n="http://xml.zope.org/namespaces/i18n"
meta_type="Dexterity FTI"
name="Link"
i18n:domain="plone"
>

<!-- Enabled behaviors -->
<property name="behaviors" purge="false">
<element value="imio.smartweb.new_tab" />
</property>

</object>
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

<!-- Enabled behaviors -->
<property name="behaviors" purge="false">
<element value="imio.smartweb.new_tab" />
<element value="plone.namefromtitle"/>
<element value="plone.locking"/>
<element value="plone.shortname"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

<!-- Enabled behaviors -->
<property name="behaviors" purge="false">
<element value="imio.smartweb.new_tab" />
<element value="plone.namefromtitle"/>
<element value="plone.locking"/>
<element value="plone.shortname"/>
Expand Down
3 changes: 2 additions & 1 deletion src/imio/smartweb/core/tests/test_sections.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from bs4 import BeautifulSoup
from collective.geolocationbehavior.geolocation import IGeolocatable
from functools import reduce
from imio.smartweb.core.behaviors.new_tab import INewTab
from imio.smartweb.core.interfaces import IImioSmartwebCoreLayer
from imio.smartweb.core.testing import IMIO_SMARTWEB_CORE_INTEGRATION_TESTING
from imio.smartweb.core.testing import ImioSmartwebTestCase
Expand Down Expand Up @@ -551,7 +552,7 @@ def test_link_section(self):
'<a class="table_image" title="My link" href="http://nohost/plone/page/section-links/my-link" target="">',
view(),
)
link.open_in_new_tab = True
INewTab(link).open_in_new_tab = True
self.assertNotIn(
'<a class="table_image" href="http://nohost/plone/page/section-links/my-link" target="_blank">',
view(),
Expand Down
44 changes: 44 additions & 0 deletions src/imio/smartweb/core/upgrades/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,22 @@
provides="Products.GenericSetup.interfaces.EXTENSION"
/>

<genericsetup:registerProfile
name="upgrade_1062_to_1063"
title="Upgrade core from 1062 to 1063"
directory="profiles/1062_to_1063"
description="Rollback from upgrade step 1027 to 1028. The agent must always be explicit for its content to open in a new window"
provides="Products.GenericSetup.interfaces.EXTENSION"
/>

<genericsetup:registerProfile
name="upgrade_1063_to_1064"
title="Upgrade core from 1063 to 1064"
directory="profiles/1063_to_1064"
description="Add new behavior (open in new tab) to content type Link and content type imio.smartweb.BlockLink"
provides="Products.GenericSetup.interfaces.EXTENSION"
/>

<genericsetup:upgradeStep
title="Configure first official release"
description="Run needed profiles steps and reindex catalog"
Expand Down Expand Up @@ -909,5 +925,33 @@
/>
</genericsetup:upgradeSteps>

<genericsetup:upgradeSteps
source="1062"
destination="1063"
profile="imio.smartweb.core:default">
<genericsetup:upgradeDepends
title="Rollback from upgrade step 1027 to 1028. The agent must always be explicit for its content to open in a new window"
import_profile="imio.smartweb.core.upgrades:upgrade_1062_to_1063"
/>
</genericsetup:upgradeSteps>

<genericsetup:upgradeSteps
source="1063"
destination="1064"
profile="imio.smartweb.core:default">
<genericsetup:upgradeDepends
title="Add new behavior (open in new tab) to content type Link and content type imio.smartweb.BlockLink"
import_profile="imio.smartweb.core.upgrades:upgrade_1063_to_1064"
/>
</genericsetup:upgradeSteps>

<!--genericsetup:upgradeStep
title="Check contact itinerary if address was checked in visible blocks"
description=""
source="1064"
destination="1065"
handler=".upgrades.migrate_external_links"
profile="imio.smartweb.core:default"
/-->

</configure>
11 changes: 11 additions & 0 deletions src/imio/smartweb/core/upgrades/profiles/1062_to_1063/registry.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<registry
xmlns:i18n="http://xml.zope.org/namespaces/i18n"
i18n:domain="imio.smartweb">

<records interface="Products.CMFPlone.interfaces.controlpanel.ILinkSchema"
prefix="plone">
<value key="external_links_open_new_window">False</value>
</records>

</registry>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version='1.0' encoding='UTF-8'?>
<object name="portal_types" meta_type="Plone Types Tool">
<object meta_type="Dexterity FTI" name="imio.smartweb.SectionTimestampedPublications"/>
</object>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<object xmlns:i18n="http://xml.zope.org/namespaces/i18n"
name="Link"
meta_type="Dexterity FTI"
i18n:domain="plone">

<property name="behaviors" purge="false">
<element value="imio.smartweb.new_tab" />
</property>

</object>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<object xmlns:i18n="http://xml.zope.org/namespaces/i18n"
name="imio.smartweb.BlockLink"
meta_type="Dexterity FTI"
i18n:domain="imio.smartweb">

<property name="behaviors" purge="false">
<element value="imio.smartweb.new_tab" />
</property>

</object>
Loading

0 comments on commit 747ee43

Please sign in to comment.