Source

jqueryuihelpers / JQueryUIHelpers / Tabs.cs

Full commit
/* *****************************************
 * Copyright (c) 2012 Attila Losonc
 *
 * This software is subject to the Microsoft Public License (Ms-PL). 
 * A copy of the license can be found in the license.txt file included 
 * in this distribution.
 *
 ***************************************** */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace JQueryUIHelpers
{
    /// <summary>
    /// Represents a tabs widget.
    /// </summary>
    public class Tabs
    {
        private readonly IDictionary<string, object> m_HtmlAttributes;

        internal string InternalTabTemplate { get; private set; }
        internal string InternalPanelTag { get; private set; }

        /// <summary>
        /// Creates a new Tabs.
        /// </summary>
        public Tabs()
            : this(null)
        {

        }

        /// <summary>
        /// Creates a new Tabs with the specified HTML attributes.
        /// </summary>
        /// <param name="htmlAttributes">An object that contains the HTML attributes.</param>
        public Tabs(object htmlAttributes)
        {

            InternalTabTemplate = "<li><a href=\"#{href}\"><span>#{label}</span></a></li>";
            InternalPanelTag = "div";

            m_HtmlAttributes = HtmlAttributesUtility.ObjectToHtmlAttributesDictionary(htmlAttributes);
            m_HtmlAttributes.Add(CommonHtmlAttributeName.JQueryUIType, JQueryUIType.Tabs);
        }

        /// <summary>
        /// Sets the Ajax options for loading tab content.
        /// </summary>
        /// <param name="settings">The Ajax options.</param>
        /// <returns>This tabs.</returns>
        public Tabs AjaxOptions(AjaxSettings settings)
        {
            Guard.ArgumentNotNull(() => settings);
            m_HtmlAttributes[TabsAttributeName.AjaxOptions] = settings.ToJsonString();
            return this;
        }

        /// <summary>
        /// Sets a value indicating whether to cache remote tab content.
        /// Note that to prevent the actual Ajax requests from being cached by the browser you need to set the Cache = false property with the AjaxOptions method.
        /// </summary>
        /// <param name="cache">If true, the remote content is cached.</param>
        /// <returns>This tabs.</returns>
        public Tabs Cache(bool cache)
        {
            m_HtmlAttributes[TabsAttributeName.Cache] = cache;
            return this;
        }

        /// <summary>
        /// Sets a value indicating whether all the tabs can be closed at once.
        /// </summary>
        /// <param name="collapsible">If true, all the tabs can be closed at once.</param>
        /// <returns>This tabs.</returns>
        public Tabs Collapsible(bool collapsible)
        {
            m_HtmlAttributes[TabsAttributeName.Collapsible] = collapsible;
            return this;
        }

        /// <summary>
        /// Enables state tracking in a cookie with the specified name and duration.
        /// </summary>
        /// <param name="name">The name of the cookie.</param>
        /// <param name="duration">The duration of the cookie in days.</param>
        /// <returns>This tabs.</returns>
        public Tabs Cookie(string name, uint duration)
        {
            Guard.ArgumentNotNullOrWhiteSpace(() => name);
            m_HtmlAttributes[TabsAttributeName.Cookie] = String.Format("{{\"expires\":{0},\"name\":\"{1}\"}}", duration, name);
            return this;
        }

        /// <summary>
        /// Disables the specified tabs.
        /// </summary>
        /// <param name="disabledIndexes">An array containing the position of the tabs (zero-based index) that should be disabled on initialization.</param>
        /// <returns>This tabs.</returns>
        public Tabs Disabled(int[] disabledIndexes)
        {
            Guard.ArgumentNotNull(() => disabledIndexes);
            m_HtmlAttributes[TabsAttributeName.Disabled] = String.Join(",", disabledIndexes);
            return this;
        }

        /// <summary>
        /// Sets the name of the event to select a tab.
        /// </summary>
        /// <param name="eventType">The event.</param>
        /// <returns>This tabs.</returns>
        public Tabs Event(Event eventType)
        {
            m_HtmlAttributes[TabsAttributeName.Event] = eventType.ToString().ToLower();
            return this;
        }

        /// <summary>
        /// Enables the fading animation for hiding and showing tab panels with the specified duration.
        /// </summary>
        /// <param name="duration">The duration.</param>
        /// <returns>This tabs.</returns>
        public Tabs FxOpacity(Duration duration)
        {
            m_HtmlAttributes[TabsAttributeName.Fx] = String.Format("{{\"opacity\":\"toggle\",\"duration\":\"{0}\"}}", duration.ToString().ToLower());
            return this;
        }

        /// <summary>
        /// Enables the fading animation for hiding and showing tab panels with the specified duration.
        /// </summary>
        /// <param name="duration">The duration in milliseconds.</param>
        /// <returns>This tabs.</returns>
        public Tabs FxOpacity(uint duration)
        {
            m_HtmlAttributes[TabsAttributeName.Fx] = String.Format("{{\"opacity\":\"toggle\",\"duration\":{0}}}", duration);
            return this;
        }

        /// <summary>
        /// Sets the prefix used for generating ids for remote tabs.
        /// </summary>
        /// <param name="idPrefix">The id prefix.</param>
        /// <returns>This tabs.</returns>
        public Tabs IdPrefix(string idPrefix)
        {
            Guard.ArgumentNotNullOrWhiteSpace(() => idPrefix);
            m_HtmlAttributes[TabsAttributeName.IdPrefix] = idPrefix;
            return this;
        }

        /// <summary>
        /// Sets the HTML tag that surrounds the tab panels.
        /// </summary>
        /// <param name="panelTag">The HTML tag.</param>
        /// <returns>This tabs.</returns>
        public Tabs PanelTag(string panelTag)
        {
            Guard.ArgumentNotNullOrWhiteSpace(() => panelTag);
            InternalPanelTag = panelTag;
            m_HtmlAttributes[TabsAttributeName.PanelTemplate] = String.Format("<{0}></{0}>", panelTag);
            return this;
        }

        /// <summary>
        /// Sets the selected tab.
        /// </summary>
        /// <param name="selectedIndex">The zero-based index of the selected tab, or -1 if notning is selected.</param>
        /// <returns>This tabs.</returns>
        public Tabs Selected(int selectedIndex)
        {
            Guard.ArgumentInRange(() => selectedIndex, -1, Int32.MaxValue);
            m_HtmlAttributes[TabsAttributeName.Selected] = selectedIndex;
            return this;
        }

        /// <summary>
        /// Sets the HTML content shown in the tab title while remote content is loading.
        /// </summary>
        /// <param name="spinner">The content.</param>
        /// <returns>This tabs.</returns>
        public Tabs Spinner(string spinner)
        {
            Guard.ArgumentNotNullOrWhiteSpace(() => spinner);
            m_HtmlAttributes[TabsAttributeName.Spinner] = spinner;
            return this;
        }

        /// <summary>
        /// Sets the HTML template used for creating new tabs.
        /// The placeholders #{href} and #{label} are replaced with the url and tab label.
        /// </summary>
        /// <param name="tabTemplate">The tab template.</param>
        /// <returns>This tabs.</returns>
        public Tabs TabTemplate(string tabTemplate)
        {
            Guard.ArgumentNotNullOrWhiteSpace(() => tabTemplate);
            InternalTabTemplate = tabTemplate;
            m_HtmlAttributes[TabsAttributeName.TabTemplate] = tabTemplate;
            return this;
        }

        /// <summary>
        /// Enables the automatic rotation through tab panels with the specified time.
        /// </summary>
        /// <param name="time">The time in milliseconds a panel is displayed before activating the next.</param>
        /// <returns>This tabs.</returns>
        public Tabs Rotate(uint time)
        {            
            m_HtmlAttributes[TabsAttributeName.RotateTime] = time;
            return this;
        }

        /// <summary>
        /// Enables the automatic rotation through tab panels with the specified time.
        /// </summary>
        /// <param name="time">The time in milliseconds a panel is displayed before activating the next.</param>
        /// <param name="continueAfterSelect">If true, the rotation will continue after the user selected a tab.</param>
        /// <returns>This tabs.</returns>
        public Tabs Rotate(uint time, bool continueAfterSelect)
        {
            m_HtmlAttributes[TabsAttributeName.RotateTime] = time;
            if (continueAfterSelect)
            {
                m_HtmlAttributes[TabsAttributeName.RotateContinue] = continueAfterSelect;
            }
            return this;
        }

        #region Events

        /// <summary>
        /// Sets the name of the JavaScript function that is triggered when a tab is added.
        /// </summary>
        /// <param name="functionName">The name of the function.</param>
        /// <returns>This tabs.</returns>
        public Tabs OnAdd(string functionName)
        {
            Guard.ArgumentNotNullOrWhiteSpace(() => functionName);
            m_HtmlAttributes[TabsAttributeName.OnAdd] = functionName;
            return this;
        }

        /// <summary>
        /// Sets the name of the JavaScript function that is triggered when the tabs is created.
        /// </summary>
        /// <param name="functionName">The name of the function.</param>
        /// <returns>This tabs.</returns>
        public Tabs OnCreate(string functionName)
        {
            Guard.ArgumentNotNullOrWhiteSpace(() => functionName);
            m_HtmlAttributes[TabsAttributeName.OnCreate] = functionName;
            return this;
        }

        /// <summary>
        /// Sets the name of the JavaScript function that is triggered when a tab is disabled.
        /// </summary>
        /// <param name="functionName">The name of the function.</param>
        /// <returns>This tabs.</returns>
        public Tabs OnDisable(string functionName)
        {
            Guard.ArgumentNotNullOrWhiteSpace(() => functionName);
            m_HtmlAttributes[TabsAttributeName.OnDisable] = functionName;
            return this;
        }

        /// <summary>
        /// Sets the name of the JavaScript function that is triggered when a tab is enabled.
        /// </summary>
        /// <param name="functionName">The name of the function.</param>
        /// <returns>This tabs.</returns>
        public Tabs OnEnable(string functionName)
        {
            Guard.ArgumentNotNullOrWhiteSpace(() => functionName);
            m_HtmlAttributes[TabsAttributeName.OnEnable] = functionName;
            return this;
        }

        /// <summary>
        /// Sets the name of the JavaScript function that is triggered after the content of a remote tab has been loaded.
        /// </summary>
        /// <param name="functionName">The name of the function.</param>
        /// <returns>This tabs.</returns>
        public Tabs OnLoad(string functionName)
        {
            Guard.ArgumentNotNullOrWhiteSpace(() => functionName);
            m_HtmlAttributes[TabsAttributeName.OnLoad] = functionName;
            return this;
        }

        /// <summary>
        /// Sets the name of the JavaScript function that is triggered when a tab is removed.
        /// </summary>
        /// <param name="functionName">The name of the function.</param>
        /// <returns>This tabs.</returns>
        public Tabs OnRemove(string functionName)
        {
            Guard.ArgumentNotNullOrWhiteSpace(() => functionName);
            m_HtmlAttributes[TabsAttributeName.OnRemove] = functionName;
            return this;
        }

        /// <summary>
        /// Sets the name of the JavaScript function that is triggered when a tab is selected.
        /// </summary>
        /// <param name="functionName">The name of the function.</param>
        /// <returns>This tabs.</returns>
        public Tabs OnSelect(string functionName)
        {
            Guard.ArgumentNotNullOrWhiteSpace(() => functionName);
            m_HtmlAttributes[TabsAttributeName.OnSelect] = functionName;
            return this;
        }

        /// <summary>
        /// Sets the name of the JavaScript function that is triggered when a tab is shown.
        /// </summary>
        /// <param name="functionName">The name of the function.</param>
        /// <returns>This tabs.</returns>
        public Tabs OnShow(string functionName)
        {
            Guard.ArgumentNotNullOrWhiteSpace(() => functionName);
            m_HtmlAttributes[TabsAttributeName.OnShow] = functionName;
            return this;
        }

        #endregion

        /// <summary>
        /// Gets the start tag of the tabs.
        /// </summary>        
        internal string StartTag
        {
            get
            {
                TagBuilder divBuilder = new TagBuilder("div");
                divBuilder.MergeAttributes(m_HtmlAttributes);
                return divBuilder.ToString(TagRenderMode.StartTag);
            }
        }

        /// <summary>
        /// Gets the end tag of the tabs.
        /// </summary>
        internal string EndTag
        {
            get
            {
                return "</div>";
            }
        }
    }
}