TagCloud.cs :  » Bloggers » BlogEngine.NET » Controls » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Bloggers » BlogEngine.NET 
BlogEngine.NET » Controls » TagCloud.cs
#region Using

using System;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Collections.Generic;
using BlogEngine.Core;

#endregion

namespace Controls{
  public class TagCloud : Control
  {

    static TagCloud()
    {
      Post.Saved += delegate { _WeightedList = null; };
    }

    #region Private fields

    private const string LINK = "<a href=\"{0}\" class=\"{1}\" title=\"{2}\">{3}</a> ";
    private static Dictionary<string, string> _WeightedList;
    private static object _SyncRoot = new object();

    #endregion

    private int _MinimumPosts = 1;

    public int MinimumPosts
    {
      get { return _MinimumPosts; }
      set { _MinimumPosts = value; }
    }

    private Dictionary<string, string> WeightedList
    {
      get
      {
        if (_WeightedList == null)
        {
          lock (_SyncRoot)
          {
            if (_WeightedList == null)
            {
              _WeightedList = new Dictionary<string, string>();
              SortList();
            }
          }
        }

        return _WeightedList;
      }
    }

    /// <summary>
    /// Builds a raw list of all tags and the number of times
    /// they have been added to a post.
    /// </summary>
    private static SortedDictionary<string, int> CreateRawList()
    {
      SortedDictionary<string, int> dic = new SortedDictionary<string, int>();
      foreach (Post post in Post.Posts)
      {
        if (post.IsVisibleToPublic)
        {
          foreach (string tag in post.Tags)
          {
            if (dic.ContainsKey(tag))
              dic[tag]++;
            else
              dic[tag] = 1;
          }
        }
      }
      return dic;
    }

    /// <summary>
    /// Sorts the list of tags based on how much they are used.
    /// </summary>
    private void SortList()
    {
      SortedDictionary<string, int> dic = CreateRawList();
      int max = 0;
      foreach (int value in dic.Values)
      {
        if (value > max)
          max = value;
      }

      foreach (string key in dic.Keys)
      {
        if (dic[key] < MinimumPosts)
          continue;

        double weight = ((double)dic[key] / max) * 100;
        if (weight >= 99)
          _WeightedList.Add(key, "biggest");
        else if (weight >= 70)
          _WeightedList.Add(key, "big");
        else if (weight >= 40)
          _WeightedList.Add(key, "medium");
        else if (weight >= 20)
          _WeightedList.Add(key, "small");
        else if (weight >= 3)
          _WeightedList.Add(key, "smallest");
      }
    }

    /// <summary>
    /// Renders the control.
    /// </summary>
    public override void RenderControl(HtmlTextWriter writer)
    {
      if (WeightedList.Keys.Count == 0)
      {
        writer.Write("<p>" + Resources.labels.none + "</p>");
      }

      writer.Write("<ul id=\"tagcloud\" class=\"tagcloud\">");
      foreach (string key in WeightedList.Keys)
      {
        writer.Write("<li>");
        writer.Write(string.Format(LINK, Utils.RelativeWebRoot + "?tag=/" + HttpUtility.UrlEncode(key), WeightedList[key], "Tag: " + key, key));
        writer.Write("</li>");
      }

      writer.Write("</ul>");
      writer.Write(Environment.NewLine);
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.