using System.Collections.Generic; using System.Linq; using BTCPayServer.Services.Rates; using Microsoft.AspNetCore.Razor.TagHelpers; namespace BTCPayServer.TagHelpers { [HtmlTargetElement("input", Attributes = "currency-selection")] public class CurrenciesSuggestionsTagHelper : TagHelper { private readonly CurrencyNameTable _currencies; public CurrenciesSuggestionsTagHelper(CurrencyNameTable currencies) { _currencies = currencies; } public override void Process(TagHelperContext context, TagHelperOutput output) { output.Attributes.RemoveAll("currency-selection"); output.PostElement.AppendHtml(""); var currencies = _currencies.Currencies .Where(c => !c.Crypto) .OrderBy(c => c.Code).ToList(); // insert btc at the front output.PostElement.AppendHtml(""); output.PostElement.AppendHtml(""); // move most often used currencies up int pos = 0; InsertAt(currencies, "USD", pos++); InsertAt(currencies, "EUR", pos++); InsertAt(currencies, "JPY", pos++); InsertAt(currencies, "CNY", pos++); // add options foreach (var c in currencies) { output.PostElement.AppendHtml($""); } output.PostElement.AppendHtml(""); output.Attributes.Add("list", "currency-selection-suggestion"); base.Process(context, output); } private void InsertAt(List currencies, string code, int idx) { var curr = currencies.FirstOrDefault(c => c.Code == code); currencies.Remove(curr); currencies.Insert(idx, curr); } } }