Creating custom data counter for the menu
The article describes how to create your own data counter in the "Projects" section. This counter displays the number of current projects. This extension point can only be applied to simple menus (without additional markup, for example, without filters under the menu item).
Example of the Data Display
Fig. 1. Counter of current projects for the "Projects" menu item
Extension methods (interface)
An extension point (interface) IMenuItemCountEvaluator has the following methods:
/// <summary>
/// Whether there is a menu item number
/// </summary>
/// <param name="item"> menu item for creating a tree</param>
/// <returns></returns>
bool HasCountEvaluator(MenuItemNode item);
/// <summary>
/// Indicates whether to periodically update the data of the client application
/// </summary>
/// <param name="item">menu item for creating a tree</param>
/// <returns></returns>
bool NeedUpdate(MenuItemNode item);
/// <summary>
/// Returns the menu item number
/// </summary>
/// <param name="item">menu item for creating a tree</param>
/// <returns></returns>
MenuItemCount Evaluate(MenuItemNode item);
Example of the extension point class
[Component]
public class ProjectMenuItemEvaluator : IMenuItemCountEvaluator
{
public bool HasCountEvaluator(MenuItemNode item)
{
return item.Code == "ProjectsModule";
}
public bool NeedUpdate(MenuItemNode item)
{
return false;
}
public MenuItemCount Evaluate(MenuItemNode item)
{
var filter = InterfaceActivator.Create<IProjectFilter>();
filter.Status = ProjectStatus.Active;
var count = ProjectManager.Instance.Find(filter, null).Count(a => !a.IsTemplate());
return new MenuItemCount
{
Count = count.ToString(CultureInfo.InvariantCulture)
};
}
Fig. 2. Code of the menu item "Projects"
The counter can be displayed in different styles, for example, you can make the counter red if there is a need. This can be done as follows:
public MenuItemCount Evaluate(MenuItemNode item)
{
var filter = InterfaceActivator.Create<IProjectFilter>();
filter.Status = ProjectStatus.Active;
var count = ProjectManager.Instance.Find(filter, null).Count(a => !a.IsTemplate());
return new MenuItemCount
{
Count = count.ToString(CultureInfo.InvariantCulture),
CssClass = (count > 5) ? "important" : ""
};
}
In this case, the counter will be red if the number of current projects is greater than five. See the display style in the Fig. 3.
Fig. 3. The display style of the menu counter