logo

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

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)
    };
}
Note
To apply this extension point to the menu item, you must specify the correct value in the method public bool HasCountEvaluator (MenuItemNode item), for example, Code (item code) or Name (the name of the menu item). The most reliable way is to specify the value using the Code, because this will prevent possible problems with different locales or identical names of the menu items. To find the Code menu, you need to investigate the element in the markup and find its Code, an example is shown in Figure 2.

Fig. 2. Code of the menu item "Projects"

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

Fig. 3. The display style of the menu counter

Links to API elements

IMenuItemCountEvaluator