using FastReport.Utils;
using System;
using System.IO;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using static System.String;
namespace FastReport.Web.Toolbar
{
public abstract class ToolbarElement
{
public ToolbarElement()
{
Name = ID.ToString();
}
internal Guid ID { get; } = Guid.NewGuid();
///
/// Name of button required to interact with the element list
///
public string Name { get; set; }
///
/// Defines the visibility of the button on the toolbar
///
public bool Enabled { get; set; } = true;
///
/// Element styles to be specified in the style tag
///
public string ElementCustomStyle { get; set; } = Empty;
///
/// Classes to be specified in the class tag for the element
///
public string ElementClasses { get; set; } = Empty;
///
/// Text to be displayed when the element is hovered over
///
public string Title { get; set; } = Empty;
///
/// Property by which the buttons in the toolbar will be sorted
///
public int Position { get; set; } = -1;
internal abstract string Render(string template_FR);
}
///
/// Action that is triggered when the element is clicked
///
public interface IClickAction
{
}
///
/// Action that is triggered when the element is changed
///
public interface IChangeAction
{
}
///
/// A script that is called when you click on element
///
public class ElementScript : IClickAction, IChangeAction
{
///
/// A script that is called when you click on element
///
public string Script { get; set; }
}
///
/// Action that is called when you click on an item
///
public class ElementClickAction : IClickAction
{
///
/// Action that is called when you click on an item. WebReport - WebReport for which the button is made
///
public Func OnClickAction { get; set; }
}
///
/// Action that is called when item changed
///
public class ElementChangeAction : IChangeAction
{
///
/// Action that is called when item changed. WebReport - WebReport for which the button is mad
///
public Func OnChangeAction { get; set; }
}
///
/// The image that will be displayed in the toolbar
///
public class ToolbarElementImage
{
private string defaultButtonImage;
private string base64;
internal string RenderedImage => IsNullOrEmpty(Url) ? $"data:image/svg+xml;base64,{Base64}" : Url;
///
/// Toolbar element image in Base64 format
///
public string Base64
{
get
{
if (!IsNullOrWhiteSpace(base64))
return base64;
if (IsNullOrWhiteSpace(defaultButtonImage))
LoadDefaultImage();
return defaultButtonImage;
}
set => base64 = value;
}
///
/// Link to the image of the toolbar element
///
public string Url { get; set; }
private void LoadDefaultImage()
{
var assembly = Assembly.GetExecutingAssembly();
var resourceStream =
assembly.GetManifestResourceStream($"{assembly.GetName().Name}.Resources.default-custom-button.svg");
string resource;
using (var reader = new StreamReader(resourceStream, Encoding.UTF8))
{
resource = reader.ReadToEnd();
}
var svgBytes = System.Text.Encoding.UTF8.GetBytes(resource);
var base64String = Convert.ToBase64String(svgBytes);
defaultButtonImage = base64String;
}
}
}