Computed property in dom-repeat doesn't work - polymer

I try to use computed property in dom-repeat helper but it throws exception immediately after I click button (see code below, packages versions: sdk: 0.12.2, polymer: 1.0.0-rc.5, polymer_interop: 1.0.0-rc.4+1). What might be wrong? When I replace {{computedItems}} with {{items}} everything works fine.
class Item extends JsProxy{
#reflectable
String a="aaa aaa";
#reflectable
String b="bbb bbb";
Item(this.a, this.b);
}
#PolymerRegister('main-app')
class MainApp extends PolymerElement {
#Property(computed:"getItems(items.*)")
List<Item> computedItems;
#property
List<Item> items = [];
#property
String inp = '';
MainApp.created() : super.created();
#reflectable
List<Item> getItems(_) {
return items;
}
#reflectable
void onTap(Event ev, Map details) {
add("items", new Item(inp, 'aa'));
set("inp", "");
}
}
and html file:
<dom-module id="main-app">
<template>
<paper-input value="{{inp}}"> </paper-input>
<paper-button raised on-tap="onTap">add to list</paper-button>
<template is="dom-repeat" items="{{computedItems}}">
<div style="color:red">
<span>[[item.a]]</span>
<span>[[item.b]]</span>
</div>
</template>
</template>
</dom-module>

You could do it this way:
#PolymerRegister('app-element')
class AppElement extends PolymerElement {
AppElement.created() : super.created();
#property
List<Item> computedItems;
#property
List<Item> items = [];
#Observe('items.*')
void computeItems([_, __]) {
set('computedItems', items.toList());
// .toList() creates a copy, otherwise Polymer wouldn't
// recognize it as a change
// this works as well
// set('computedItems', null);
// set('computedItems', items);
}
#property int itemsChangeIndicator = 0;
#property
String inp = '';
#reflectable
List<Item> getItems(_) {
return items;
}
#reflectable
void tapHandler(dom.Event ev, Map details) {
add("items", new Item(inp, 'aa'));
set("inp", "");
}
}
(I renamed onTap because this naming scheme easily collided with event stream getters in the element, like onClick)

Related

Routing to named element in Blazor (use anchor to navigate to specific element)

I cannot use an HTML anchor to navigate to a specific HTML element of a page in the Blazor Server. For example:
#page "/test"
<nav>
<!-- One version I've tried -->
Section2
<!-- Another version I've tried -->
<NavLink href="#section2">Section2</NavLink>
</nav>
#* ... *#
<h2 id="section2">It's Section2.</h2>
#* ... *#
When I click the link to Section2, I get redirected to the route http://localhost:5000/test#section2, however, will be at the top of the page. In my opinion, the browser should scroll down to the proper element, as specified by the Element Selector, but it can't.
Does it have to be done in a special way in Blazor?
I use Blazor 6 in .Net6 with Visual Studio 2022 (ver:17.0.2).
After loading a page, a browser automatically scrolls to the element identified by its id in the fragment part of the URL. It does the same when you click on an anchor with an href of the kind #element-id.
The page load behavior doesn't work for a Blazor Server because the element doesn't exist yet on page load.
The solution is to manually create a scroller using javascript and a razor component:
First of all, create a razor component like this
#inject IJSRuntime JSRuntime
#inject NavigationManager NavigationManager
#implements IDisposable
#code {
protected override void OnInitialized()
{
NavigationManager.LocationChanged += OnLocationChanged;
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
await ScrollToFragment();
}
public void Dispose()
{
NavigationManager.LocationChanged -= OnLocationChanged;
}
private async void OnLocationChanged(object sender, LocationChangedEventArgs e)
{
await ScrollToFragment();
}
private async Task ScrollToFragment()
{
var uri = new Uri(NavigationManager.Uri, UriKind.Absolute);
var fragment = uri.Fragment;
if (fragment.StartsWith('#'))
{
// Handle text fragment (https://example.org/#test:~:text=foo)
// https://github.com/WICG/scroll-to-text-fragment/
var elementId = fragment.Substring(1);
var index = elementId.IndexOf(":~:", StringComparison.Ordinal);
if (index > 0)
{
elementId = elementId.Substring(0, index);
}
if (!string.IsNullOrEmpty(elementId))
{
await JSRuntime.InvokeVoidAsync("BlazorScrollToId", elementId);
}
}
}
}
Then add this javascript code somewhere before the Blazor script renders. You can wrap it with script tags and place it in the head.
function BlazorScrollToId(id) {
const element = document.getElementById(id);
if (element instanceof HTMLElement) {
element.scrollIntoView({
behavior: "smooth",
block: "start",
inline: "nearest"
});
}
}
Finally implement it in your pages if needed. You can also place it inside your layouts, so it will work for every page you create.
#page "/"
<PageTitle>Index</PageTitle>
<a href="#my-id">
<h1>Hello, world!</h1>
</a>
<SurveyPrompt Title="How is Blazor working for you?" />
<div style="height: 2000px">
</div>
<div id="my-id">
Hello!
</div>
<AnchorNavigation />
Source: link
You can also use an ElementReference and FocusAsync which uses the built in Blazor JS. To use it you need to use a small hack to make the component "Focusable" which is to set a tabindex. I've used a span but you can use what you like. I've used #alessandromanzini's code to get the element from the NavigationManager.
Here's a component:
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Rendering;
using Microsoft.AspNetCore.Components.Routing;
using System.Diagnostics.CodeAnalysis;
namespace SO75358165;
public class Bookmark : ComponentBase, IDisposable
{
private bool _setFocus;
[Inject] private NavigationManager NavManager { get; set; } = default!;
[Parameter] public RenderFragment? ChildContent { get; set; }
[Parameter] public string? BookmarkName { get; set; }
[DisallowNull] public ElementReference? Element { get; private set; }
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
builder.OpenElement(0, "span");
builder.AddAttribute(2, "tabindex", "-1");
builder.AddContent(3, this.ChildContent);
builder.AddElementReferenceCapture(4, this.SetReference);
builder.CloseElement();
}
protected override void OnInitialized()
=> NavManager.LocationChanged += this.OnLocationChanged;
protected override void OnParametersSet()
=> _setFocus = this.IsMe();
private void SetReference(ElementReference reference)
=> this.Element = reference;
private void OnLocationChanged(object? sender, LocationChangedEventArgs e)
{
if (this.IsMe())
{
_setFocus = true;
this.StateHasChanged();
}
}
protected async override Task OnAfterRenderAsync(bool firstRender)
{
if (_setFocus)
await this.Element!.Value.FocusAsync(false);
_setFocus = false;
}
private bool IsMe()
{
string? elementId = null;
var uri = new Uri(this.NavManager.Uri, UriKind.Absolute);
if (uri.Fragment.StartsWith('#'))
{
elementId = uri.Fragment.Substring(1);
return elementId == BookmarkName;
}
return false;
}
public void Dispose()
=> NavManager.LocationChanged -= this.OnLocationChanged;
}
Here's my test page:
#page "/"
<PageTitle>Index</PageTitle>
<NavLink href="#me">To me</NavLink>
<h1>Hello, world!</h1>
<h1>Hello, world!</h1>
<h1>Hello, world!</h1>
//.....
<h1>Hello, world!</h1>
<Bookmark BookmarkName="me" >
<h1 id="me">Focus on Me</h1>
</Bookmark>

Options pattern, configuration, in Legacy .NET Application with Simple Injector

This article, https://medium.com/#dmitryzaets/legacy-net-applications-configuration-management-net-framework-4-5-1-68220335d9d8, describe how to use Options pattern together with Autofac. I have tried to translate this to use with Simple Injector. But I have no luck.
Here is my IOC code
public class IocBootstrap2
{
private Container Container { get; }
public IocBootstrap2()
{
Container = new Container();
var configurationBuilder = new ConfigurationBuilder()
.SetBasePath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Configuration"))
.AddJsonFile("settings.json", optional: false, reloadOnChange: true);
var configuration = configurationBuilder.Build();
//Register Options
Container.Register(typeof(IOptions<>), typeof(OptionsManager<>));
Container.Register(typeof(IOptionsMonitor<>), typeof(OptionsMonitor<>));
Container.Register(typeof(IOptionsFactory<>), typeof(OptionsFactory<>));
Container.Register(typeof(IOptionsMonitorCache<>), typeof(OptionsCache<>));
// Register ConfigurationOptions
Container.RegisterConfigurationOptions2<MailingOptions>(configuration.GetSection("mailing"));
#if DEBUG
Container.Verify();
#endif
}
}
public static class ConfigurationSetupExtensions2
{
public static void RegisterConfigurationOptions2<TOptions>(this Container container, IConfiguration config)
where TOptions : class
{
container.Register(typeof(IOptionsChangeTokenSource<TOptions>),
() => new ConfigurationChangeTokenSource<TOptions>(config), Lifestyle.Transient);
container.Register(typeof(IConfigureOptions<TOptions>),
() => new ConfigureFromConfigurationOptions<TOptions>(config), Lifestyle.Transient);
}
}
public class MailingOptions
{
public MailingOptions()
{
BatchSize = 1;
}
public int BatchSize { get; set; }
public int BatchDelay { get; set; }
}
settings.json
{
"mailing": {
"batchSize": 15,
"batchDelay": 1
}
}
Then I inject it in a ViewModel:s constructor like this
public class BlockViewModel
{
private readonly MailingOptions _options;
#region Constructor
public BlockViewModel(IOptions<MailingOptions> options)
{
_options = options.Value;
}
#endregion
}
When I run it I get Exceptions in Container.Verify.
The constructor of type OptionsFactory<MailingOptions> contains the parameter with name 'setups' and type IEnumerable<IConfigureOptions<MailingOptions>> that is not registered. Please ensure IEnumerable<IConfigureOptions<MailingOptions>> is registered, or change the constructor of OptionsFactory<MailingOptions>. There is, however, a registration for IConfigureOptions<MailingOptions>; Did you mean to depend on IConfigureOptions<MailingOptions>?
StackTrace:
at SimpleInjector.Container.ThrowParameterTypeMustBeRegistered(InjectionTargetInfo target)
How will I Register an IEnumerable<IConfigureOptions<MailingOptions>>?
Can someone tell my what I'm doing wrong, or more precise, what is it that I don't understand?
The short answer is: don't inject IOptions<T> into your application components. As explained here, that will only complicate your components, their unit tests, and, as you already noticed, your configuration.
Instead, let BlockViewModel depend on MailingOptions directly:
public class BlockViewModel
{
private readonly MailingOptions _options;
public BlockViewModel(MailingOptions options)
{
_options = options ?? throw new ArgumentNullException(nameof(options));
}
}
This allows you to simplify your configuration to the following:
Container = new Container();
var configutation = new ConfigurationBuilder()
.SetBasePath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Configuration"))
.AddJsonFile("settings.json", optional: false);
.Build();
MailingOptions options = configuration.GetSection("mailing").Get<MailingOptions>();
Container.RegisterInstance<MailingOptions>(options);
// Register View Models
Container.Register<BlockViewModel>();
Container.Verify();

Override Primefaces Renderer

I'm developing a dynamic menu using Primefaces and JSF 2.2.
The problem is that it's not inserting the menu id. Looking primefaces' code it hits a code that will always be false:
BaseMenuRenderer:
protected boolean shouldRenderId(MenuElement element) {
if(element instanceof UIComponent)
return shouldWriteId((UIComponent) element);
else
return false;
}
TieredMenuRenderer:
writer.startElement("li", null);
if(shouldRenderId(submenu)) {
writer.writeAttribute("id", submenu.getClientId(), null);
}
So, I decided to override primefaces' TieredMenuRenderer, but my override constructor classe is called but the override method is never called.
Here's how I set my facesconfig.xml
<render-kit>
<renderer>
<component-family>org.primefaces.component</component-family>
<renderer-type>org.primefaces.component.TieredMenuRenderer</renderer-type>
<renderer-class>ui.jsf.TieredMenuRenderer</renderer-class>
</renderer>
</render-kit>
My override class:
public class TieredMenuRenderer extends org.primefaces.component.tieredmenu.TieredMenuRenderer {
#Override
protected void encodeElements(FacesContext context, AbstractMenu menu, List<MenuElement> elements) throws IOException {
System.out.println("----------- TEST --------------");
super.encodeElements(context, menu, elements);
}
Sysout is never print.
Does anyone know what i'm doing wrong?
Thanks!
Edit:
Add ID to DefaultMenuItem:
DefaultMenuItem item = new DefaultMenuItem();
item.setId(menuItem.getMenuId());// just return a string value.
Adding menu xhtml, the "menucontroller.model" is a primefaces MenuModel which I use a DefaultMenuModel :
Iterating over renderer kit, When I execute the following command, returns the correct renderer-type org.primefaces.component.TieredMenuRenderer
Iterator<String> renderKit = kit.getRendererTypes("org.primefaces.component");
When I execute the following code returns my qualified classname ui.jsf.TieredMenuRenderer#64baec0e:
Renderer renderer = kit.getRenderer("org.primefaces.component", "org.primefaces.component.TieredMenuRenderer");
First of all, My renderer was not overriding the correct renderer. I was overriding org.primefaces.component.tieredmenu.TieredMenuRenderer when I should override org.primefaces.component.MenubarRenderer.
Then, to correct the primefaces id problem I did the following in my rendered class:
#Override
protected boolean shouldRenderId(MenuElement element) {
return true;
}
#Override
public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
AbstractMenu menu = (AbstractMenu) component;
encodeMarkup(context, menu);
encodeScript(context, menu);
}
EncodedEnd was calling "generateIds()" from primefaces which override my ids.
Full rendered class:
import java.io.IOException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import org.primefaces.component.menu.AbstractMenu;
import org.primefaces.model.menu.MenuElement;
public class MenuRenderer extends org.primefaces.component.menubar.MenubarRenderer {
#Override
protected boolean shouldRenderId(MenuElement element) {
return true;
}
#Override
public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
AbstractMenu menu = (AbstractMenu) component;
encodeMarkup(context, menu);
encodeScript(context, menu);
}
}

Writing Junits test class for a DoubleClickListener

I need to write a Junit test class for my DoubleClickImplementation class. Here is the code :
DoubleClickImplementation.java
public class DoubleClickImplementation implements IDoubleClickListener {
private TreeViewer treeViewer;
public DoubleClickImplementation(TreeViewer viewer) {
this.treeViewer = viewer;
}
/**
* Expands and Collapses the tree items.
*
* #param event
*
*/
#Override
public void doubleClick(DoubleClickEvent event) {
IStructuredSelection selection = (IStructuredSelection) treeViewer.getSelection();
if (selection == null || selection.isEmpty()) {
return;
}
Object item = selection.getFirstElement();
ITreeContentProvider provider = (ITreeContentProvider) treeViewer.getContentProvider();
if (!provider.hasChildren(item)) {
return;
}
if (treeViewer.getExpandedState(item)) {
treeViewer.collapseToLevel(item, AbstractTreeViewer.ALL_LEVELS);
} else {
treeViewer.expandToLevel(item, 1);
}
}
}
And this is how I instantiate DoubleClickImplementation class from another class named RepoView
private TreeViewer browseTreeViewer;
private DoubleClickImplementation doubleClickExpansionListener;
doubleClickExpansionListener = new DoubleClickImplementation(browseTreeViewer);
browseTreeViewer.addDoubleClickListener(doubleClickExpansionListener);
Please give me a Junit test class for the above mentioned DoubleClickImplementation class's doubleClick() method. I'm completely new to JUnits.

Adding custom attribute (HTML5) support to JSF 2.0 UIInput component

I am trying to write a renderer which would process the placeholder attribute on an <h:inputText> component.
I headed to this path after reading JSF 2.0 strips out needed HTML5 attributes and it seems correct. Here's my custom renderer
public class InputRenderer extends com.sun.faces.renderkit.html_basic.TextRenderer{
#Override
public void encodeBegin(FacesContext context, UIComponent component)
throws IOException {
System.out.println("Rendering :"+component.getClientId());
String placeholder = (String)component.getAttributes().get("placeholder");
if(placeholder != null) {
ResponseWriter writer = context.getResponseWriter();
writer.writeAttribute("placeholder", placeholder, "placeholder");
}
super.encodeBegin(context, component);
}
#Override
public void decode(FacesContext context, UIComponent component) {
super.decode(context, component);
}
#Override
public void encodeEnd(FacesContext context, UIComponent component)
throws IOException {
super.encodeEnd(context, component);
}
}
And this renderer is registered in faces config as
<render-kit>
<renderer>
<component-family>javax.faces.Input</component-family>
<renderer-type>javax.faces.Text</renderer-type>
<renderer-class>com.example.renderer.InputRenderer</renderer-class>
</renderer>
</render-kit>
This gets registered fine, no issues there.
My intention is to process the placeholder attribute, insert it, and then delegate the processing to super. My above code doesn't work because I'm inserting the attribute at a wrong place. It must be inserted after writer.startElement('input') has executed. However, the startElement must be happening somewhere in the super's encodeBegin() method. So how do I insert a custom attribute ('placeholder' in this case) and then continue the execution flow?
NB: The above code does add a placeholder attribute but not to the input component that I intend to, It writes it to the parent of the Input (since I'm trying to write an attribute before the component itself is actually written in the stream, it applies the attribute to the current component)
This is my way. I added placeholder and data-theme attributes. If you want to add more attributes, you should just add its name to attributes array.
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import com.sun.faces.renderkit.html_basic.TextRenderer;
public class InputRender extends TextRenderer {
#Override
protected void getEndTextToRender(FacesContext context,
UIComponent component,
String currentValue)
throws java.io.IOException{
String [] attributes = {"placeholder","data-theme"};
ResponseWriter writer = context.getResponseWriter();
for(String attribute : attributes)
{
String value = (String)component.getAttributes().get(attribute);
if(value != null) {
writer.writeAttribute(attribute, value, attribute);
}
}
super.getEndTextToRender(context, component, currentValue);
}
}
You should add this to faces-config.xml file.
<render-kit>
<renderer>
<component-family>javax.faces.Input</component-family>
<renderer-type>javax.faces.Text</renderer-type>
<renderer-class>your.package.InputRenderer</renderer-class>
</renderer>
</render-kit>
You can just override ResponseWriters startElement method, that method is only called once and then you can restore to the original responsewriter object.
import javax.faces.context.*;
import java.io.IOException;
public class InputRenderer extends com.sun.faces.renderkit.html_basic.TextRenderer{
// Put all of the attributes you want to render here...
private static final String[] ATTRIBUTES = {"required","placeholder"};
#Override
protected void getEndTextToRender(FacesContext context,
UIComponent component, String currentValue) throws IOException {
final ResponseWriter originalResponseWriter = context.getResponseWriter();
context.setResponseWriter(new ResponseWriterWrapper() {
#Override
// As of JSF 1.2 this method is now public.
public ResponseWriter getWrapped() {
return originalResponseWriter;
}
#Override
public void startElement(String name, UIComponent component)
throws IOException {
super.startElement(name, component);
if ("input".equals(name)) {
for (String attribute : ATTRIBUTES)
{
Object value = component.getAttributes().get(attribute);
if (value != null)
{
super.writeAttribute(attribute,value,attribute);
}
}
}
});
super.getEndTextToRender(context, component, currentValue);
context.setResponseWriter(originalResponseWriter); // Restore original writer.
}
}
And to override for MyFaces 2.0.8+
package com.hsop.abc.eld;
import java.io.IOException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import org.apache.myfaces.renderkit.html.HtmlTextRenderer;
public class InputRenderer extends HtmlTextRenderer
{
#Override
protected void renderInputBegin(FacesContext context, UIComponent component)
throws IOException
{
// TODO Auto-generated method stub
super.renderInputBegin(context, component);
Object placeholder = component.getAttributes().get("placeholder");
if(placeholder != null) {
ResponseWriter writer = context.getResponseWriter();
writer.writeAttribute("placeholder", placeholder, "placeholder");
}
}
}