public void SelectorHelpers_GetText() { Assert.AreEqual("X1", SelectorHelpers.GetText <int>(i => "X" + i, 1)); // use text selector Assert.AreEqual("1", SelectorHelpers.GetText <int>(null, 1)); // text selector is null, use fallback (1.ToString()) Assert.AreEqual("", SelectorHelpers.GetText <int?>(null, null)); // no text selector, no item, use empty string Assert.AreEqual("", SelectorHelpers.GetText <int>(i => null, 1)); // text selector is not null, do not use fallback (1.ToString()) }
/// <inheritdoc/> protected override void BuildRenderInput(RenderTreeBuilder builder) { RefreshState(); if (itemsToRender.Count > 0) { UglyHack uglyHack = new UglyHack(); // see comment below foreach (var item in itemsToRender) { TValue value = SelectorHelpers.GetValue <TItem, TValue>(ValueSelector, item); builder.OpenComponent(1, typeof(HxInputCheckbox)); builder.AddAttribute(2, nameof(HxInputCheckbox.Label), SelectorHelpers.GetText(TextSelector, item)); builder.AddAttribute(3, nameof(HxInputCheckbox.Value), Value?.Contains(value) ?? false); builder.AddAttribute(4, nameof(HxInputCheckbox.ValueChanged), EventCallback.Factory.Create <bool>(this, @checked => HandleValueChanged(@checked, item))); builder.AddAttribute(5, nameof(HxInputCheckbox.Enabled), EnabledEffective); // We need ValueExpression. Ehm, HxInputCheckbox needs ValueExpression. Because it is InputBase<T> which needs ValueExpression. // We have nothing to give the HxInputCheckbox. So we make own class with property which we assign to the ValueExpression. // Impacts? Unknown. Maybe none. builder.AddAttribute(6, nameof(HxInputCheckbox.ValueExpression), (Expression <Func <bool> >)(() => uglyHack.HackProperty)); builder.AddAttribute(7, nameof(HxInputCheckbox.ShowValidationMessage), false); builder.AddAttribute(8, nameof(HxInputCheckbox.Inline), this.Inline); builder.AddMultipleAttributes(100, this.AdditionalAttributes); builder.CloseComponent(); } } }
protected void cancelEditAnswer_Click(object sender, EventArgs e) { Button btn = sender as Button; Panel editAnswer = SelectorHelpers.FindFirstByCssClass <Panel>(btn.Parent, "editAnswer"); new EffectRollUp(editAnswer, 500).Render(); }
protected void ViewComments(object sender, EventArgs e) { LinkButton btn = sender as LinkButton; int id = GetIdOfAnswer(btn); QuizItem answer = QuizItem.Find(id); Panel tmp = SelectorHelpers.FindFirstByCssClass <Panel>(btn.Parent, "viewComments"); if (!tmp.Visible || tmp.Style["display"] == "none") { btn.Text = "Hide " + btn.Text; tmp.Visible = true; tmp.ReRender(); System.Web.UI.WebControls.Repeater rep = Selector.SelectFirst <System.Web.UI.WebControls.Repeater>(tmp); rep.DataSource = answer.Children; rep.DataBind(); TextArea txt = Selector.SelectFirst <TextArea>(tmp); txt.Text = "write your comment here..."; new EffectRollDown(tmp, 500) .Render(); } else { btn.Text = btn.Text.Replace("Hide ", ""); new EffectRollUp(tmp, 500) .Render(); } }
/// <inheritdoc/> protected override bool TryParseValueFromString(string value, out TValue result, out string validationErrorMessage) { int index = int.Parse(value); result = (index == -1) ? default(TValue) : SelectorHelpers.GetValue <TItem, TValue>(ValueSelectorImpl, itemsToRender[index]); validationErrorMessage = null; return(true); }
protected override void RenderChipValue(RenderTreeBuilder builder) { if ((chipValue is null) && (Value != null) && (DataImpl != null)) { // fallback for initial rendering without chipValue // does not help when DataImpl is not set yet (loaded asynchronously) var item = DataImpl.FirstOrDefault(item => comparer.Equals(Value, SelectorHelpers.GetValue <TItem, TValue>(ValueSelectorImpl, item))); chipValue = SelectorHelpers.GetText(TextSelectorImpl, item); } builder.AddContent(0, chipValue); }
/// <summary> /// Selects the entity whose key member value is equal to the request's key member value: /// ie: Single(entity => request.key == entity.key) /// </summary> public static RequestEntityConfigBuilder <TRequest, TEntity> SelectBy <TRequest, TEntity>( this RequestEntityConfigBuilder <TRequest, TEntity> config, string key) where TEntity : class { var kRequest = Key.MakeKey <TRequest>(key); var kEntity = Key.MakeKey <TEntity>(key); var selector = SelectorHelpers.BuildSingle <TRequest, TEntity>(kRequest, kEntity); return(config.SetSelector(selector)); }
/// <summary> /// Selects entities whose entityKey member is contained within the request's requestItems collection /// ie: Where(entity => request.requestItems.Contains(entity.entityKey)) /// </summary> public static BulkRequestEntityConfigBuilder <TRequest, TItem, TEntity> SelectBy <TRequest, TItem, TEntity>( this BulkRequestEntityConfigBuilder <TRequest, TItem, TEntity> config, Expression <Func <TRequest, ICollection <TItem> > > requestItems, string entityKey) where TEntity : class { var kEntity = Key.MakeKey <TEntity>(entityKey); var kItem = Key.Identity <TItem>(); var selector = SelectorHelpers.BuildCollection <TRequest, TItem, TEntity>(requestItems, kEntity, kItem); return(config.SetSelector(selector)); }
protected void BuildRenderInput_RenderRadioItem(RenderTreeBuilder builder, int index) { var item = itemsToRender[index]; if (item != null) { bool selected = (index == selectedItemIndex); if (selected) { chipValue = SelectorHelpers.GetText(TextSelectorImpl, item); } string inputId = GroupName + "_" + index.ToString(); builder.OpenElement(100, "div"); // TODO CoreCssClass builder.AddAttribute(101, "class", CssClassHelper.Combine("form-check", this.Inline ? "form-check-inline" : null)); builder.OpenElement(200, "input"); builder.AddAttribute(201, "class", "form-check-input"); builder.AddAttribute(202, "type", "radio"); builder.AddAttribute(203, "name", GroupName); builder.AddAttribute(204, "id", inputId); builder.AddAttribute(205, "value", index.ToString()); builder.AddAttribute(206, "checked", selected); builder.AddAttribute(207, "disabled", !CascadeEnabledComponent.EnabledEffective(this)); int j = index; builder.AddAttribute(208, "onclick", EventCallback.Factory.Create(this, () => HandleInputClick(j))); builder.AddEventStopPropagationAttribute(209, "onclick", true); builder.AddMultipleAttributes(250, this.AdditionalAttributes); builder.CloseElement(); // input builder.OpenElement(300, "label"); builder.AddAttribute(301, "class", "form-check-label"); builder.AddAttribute(302, "for", inputId); if (ItemTemplateImpl != null) { builder.AddContent(303, ItemTemplateImpl(item)); } else { builder.AddContent(304, SelectorHelpers.GetText(TextSelectorImpl, item)); } builder.CloseElement(); // label builder.CloseElement(); // div } }
/// <inheritdoc /> protected override string FormatValueAsString(List <TValue> value) { // Used for CurrentValueAsString (which is used for the input element and for the chip generator). // Thats why we do not use NullDataText here. if ((!value?.Any() ?? true) || (Data == null)) { // don't care about chip generator, it does not call this method for null/empty value return(EmptyText); } // Take itemsToRender because they are sorted. List <TItem> selectedItems = itemsToRender.Where(item => value.Contains(SelectorHelpers.GetValue <TItem, TValue>(ValueSelector, item))).ToList(); return(String.Join(", ", selectedItems.Select(TextSelector))); }
private void HandleItemSelectionChanged(bool @checked, TItem item) { var newValue = Value == null ? new List <TValue>() : new List <TValue>(Value); TValue value = SelectorHelpers.GetValue <TItem, TValue>(ValueSelector, item); if (@checked) { newValue.Add(value); } else { newValue.Remove(value); } CurrentValue = newValue; // setter includes ValueChanged + NotifyFieldChanged }
protected void EditAnswerBtnClick(object sender, EventArgs e) { LinkButton btn = sender as LinkButton; Panel editAnswer = SelectorHelpers.FindFirstByCssClass <Panel>(btn.Parent, "editAnswer"); if (!editAnswer.Visible || editAnswer.Style["display"] == "none") { TextArea text = Selector.SelectFirst <TextArea>(editAnswer); int id = GetIdOfAnswer(btn); text.Text = QuizItem.Find(id).Body; editAnswer.Visible = true; new EffectRollDown(editAnswer, 500) .ChainThese(new EffectFocusAndSelect(text)) .Render(); } }
/// <summary> /// Selects entities whose entityKey member is contained within the request's requestItems collection /// ie: Where(entity => request.requestItems.Contains(entity.entityKey)) /// </summary> public static BulkRequestEntityConfigBuilder <TRequest, TItem, TEntity> SelectBy <TRequest, TItem, TEntity>( this BulkRequestEntityConfigBuilder <TRequest, TItem, TEntity> config, Expression <Func <TRequest, ICollection <TItem> > > requestItems, Expression <Func <TEntity, TItem> > entityKey) where TEntity : class { var kEntity = Key.MakeKeys(entityKey); var kItem = Key.Identity <TItem>(); if (kEntity.Length > 1) { throw new BadConfigurationException($"Composite keys are not supported for bulk requests"); } var selector = SelectorHelpers.BuildCollection <TRequest, TItem, TEntity>(requestItems, kEntity[0], kItem); return(config.SetSelector(selector)); }
protected void SaveComment(object sender, EventArgs e) { Button btn = sender as Button; int id = GetIdOfAnswer(btn.Parent); QuizItem q = QuizItem.Find(id); TextArea tmp = Selector.SelectFirst <TextArea>(btn.Parent); QuizItem n = new QuizItem(); n.Body = tmp.Text; n.CreatedBy = Operator.Current; n.Parent = q; n.Save(); q.Refresh(); new EffectRollUp(btn.Parent, 500) .Render(); LinkButton viewComments = SelectorHelpers.FindFirstByCssClass <LinkButton>(btn.Parent.Parent, "comments"); viewComments.Text = "Comments [" + q.Children.Count + "]"; }
/// <summary> /// Selects the entity whose entityKey member value is equal to the request's requestKey member value: /// ie: Single(entity => request.requestKey == entity.entityKey) /// </summary> public static RequestEntityConfigBuilder <TRequest, TEntity> SelectBy <TRequest, TEntity, TRequestKey>( this RequestEntityConfigBuilder <TRequest, TEntity> config, Expression <Func <TRequest, TRequestKey> > requestKey, string entityKey) where TEntity : class { var kRequest = Key.MakeKeys(requestKey); var kEntity = Key.MakeKey <TEntity>(entityKey); if (kRequest == null || kRequest.Length == 0 || kEntity == null) { return(config); } if (kRequest.Length != 1) { throw new IncompatibleKeysException(typeof(TRequest), typeof(TEntity)); } var selector = SelectorHelpers.BuildSingle <TRequest, TEntity>(kRequest[0], kEntity); return(config.SetSelector(selector)); }
private void BuildDefaultSelector <TCompatibleRequest>(RequestConfig <TCompatibleRequest> config) { var kItem = config.GetRequestKeys(); var kEntity = config.GetKeysFor <TEntity>(); if (kItem != null && kItem.Length > 0 && kEntity != null && kItem.Length > 0) { if (kItem.Length != kEntity.Length) { throw new IncompatibleKeysException(typeof(TCompatibleRequest), typeof(TEntity)); } if (kItem.Length > 1) { throw new BadConfigurationException($"Composite keys are not supported for bulk requests"); } var selector = SelectorHelpers.BuildCollection <TRequest, TItem, TEntity>( _getRequestItems, kEntity[0], kItem[0]); config.SetEntitySelector <TEntity>(selector); } }
public async Task Handle_DefaultWithResponse_CreatesUserAndReturnsDto() { var request = new CreateRequest <User, UserDto, UserGetDto>(new UserDto { Name = "TestUser" }); var response = await Mediator.HandleAsync(request); Assert.IsFalse(response.HasErrors); Assert.AreEqual(1, Context.Set <User>().Count()); Assert.IsNotNull(response.Result); Assert.AreEqual("TestUser", response.Result.Name); Assert.AreEqual(response.Result.Id, Context.Set <User>().First().Id); var requestTest = new CreateUserWithoutResponseRequest(); var kRequest = Key.MakeKeys <CreateUserWithoutResponseRequest, string>(x => x.User.Name); var kEntity = Key.MakeKeys <User, string>(x => x.Name); var selector = SelectorHelpers.BuildSingle <CreateUserWithoutResponseRequest, User>(kRequest[0], kEntity[0]); var test1 = selector.Get <User>()(new CreateUserWithoutResponseRequest()); var test2 = Test(request => user => request.User.Name == user.Name, requestTest); int n = 0; }
private void HandleInputClick(int index) { CurrentValue = SelectorHelpers.GetValue <TItem, TValue>(ValueSelectorImpl, itemsToRender[index]); }
/// <inheritdoc/> protected override void BuildRenderInput(RenderTreeBuilder builder) { chipValue = null; RefreshState(); bool enabledEffective = this.EnabledEffective; builder.OpenElement(0, "select"); BuildRenderInput_AddCommonAttributes(builder, null); builder.AddAttribute(1000, "onchange", EventCallback.Factory.CreateBinder <string>(this, value => CurrentValueAsString = value, CurrentValueAsString)); builder.AddEventStopPropagationAttribute(1001, "onclick", true); builder.AddElementReferenceCapture(1002, elementReferece => InputElement = elementReferece); if (itemsToRender != null) { if ((NullableEffective && enabledEffective) || (selectedItemIndex == -1)) { builder.OpenElement(2000, "option"); builder.AddAttribute(2001, "value", -1); builder.AddAttribute(2002, "selected", selectedItemIndex == -1); builder.AddContent(2003, NullTextImpl); builder.CloseElement(); } for (int i = 0; i < itemsToRender.Count; i++) { var item = itemsToRender[i]; if (item != null) { bool selected = (i == selectedItemIndex); if (enabledEffective || selected) /* when not enabled only selected item is rendered */ { string text = SelectorHelpers.GetText(TextSelectorImpl, item); builder.OpenElement(3000, "option"); builder.SetKey(i.ToString()); builder.AddAttribute(3001, "value", i.ToString()); builder.AddAttribute(3002, "selected", selected); builder.AddContent(3003, text); builder.CloseElement(); if (selected) { chipValue = text; } } } } } else { if (!String.IsNullOrEmpty(NullDataTextImpl)) { builder.OpenElement(4000, "option"); builder.AddAttribute(4001, "selected", true); builder.AddContent(4002, NullDataTextImpl); builder.CloseElement(); } } builder.CloseElement(); }
private void RefreshState() { if (DataImpl != null) { itemsToRender = DataImpl.ToList(); // AutoSort if (AutoSortImpl && (itemsToRender.Count > 1)) { if (SortKeySelectorImpl != null) { itemsToRender = itemsToRender.OrderBy(this.SortKeySelectorImpl).ToList(); } else if (TextSelectorImpl != null) { itemsToRender = itemsToRender.OrderBy(this.TextSelectorImpl).ToList(); } else { itemsToRender = itemsToRender.OrderBy(i => i.ToString()).ToList(); } } // set next properties for rendering selectedItemIndex = itemsToRender.FindIndex(item => comparer.Equals(Value, SelectorHelpers.GetValue <TItem, TValue>(ValueSelectorImpl, item))); if ((Value != null) && (selectedItemIndex == -1)) { throw new InvalidOperationException($"Data does not contain item for current value '{Value}'."); } } else { itemsToRender = null; selectedItemIndex = -1; } }
public void SelectorHelpers_GetValue() { Assert.AreEqual(null, SelectorHelpers.GetValue <int?, string>(i => "X" + i, null)); // use default Assert.AreEqual("X1", SelectorHelpers.GetValue <int?, string>(i => "X" + i, 1)); // use value selector Assert.AreEqual(1, SelectorHelpers.GetValue <int?, int?>(null, 1)); // use value selector }