示例#1
0
        /// <summary>
        /// Validates a URL Picker state against the settings, to ensure there has been no trickery
        /// client-side
        /// </summary>
        /// <param name="state">The state DTO to validate against</param>
        /// <returns>Whether the state is valid</returns>
        public bool ValidateState(UrlPickerState state)
        {
            // Invalid if:
            // 1. If the mode is not allowed (or)
            // 2. The state fails its own validation
            if (!AllowedModes.Contains(state.Mode) || !state.Validates())
            {
                return(false);
            }

            return(true);
        }
示例#2
0
        /// <summary>
        /// Returns a UrlPickerState based on a serialized string.
        ///
        /// Tries to infer the format of the serialized data based on known types.  Will throw exceptions
        /// if it fails to parse.
        /// </summary>
        /// <param name="serializedState">An instance of UrlPickerState as a serialized string</param>
        /// <returns>The state</returns>
        public static UrlPickerState Deserialize(string serializedState)
        {
            // Can't deserialize an empty whatever
            if (string.IsNullOrEmpty(serializedState))
            {
                return(null);
            }

            // Default
            var state = new UrlPickerState();

            // Imply data format from the formatting of the serialized state
            UrlPickerDataFormat impliedDataFormat;

            if (serializedState.StartsWith("<"))
            {
                impliedDataFormat = UrlPickerDataFormat.Xml;
            }
            else if (serializedState.StartsWith("{"))
            {
                impliedDataFormat = UrlPickerDataFormat.Json;
            }
            else
            {
                impliedDataFormat = UrlPickerDataFormat.Csv;
            }

            // Try to deserialize the string
            try
            {
                switch (impliedDataFormat)
                {
                case UrlPickerDataFormat.Xml:
                    var dataNode = XElement.Parse(serializedState);

                    // Carefully try to get values out.  This is in case new versions add
                    // to the XML
                    var modeAttribute = dataNode.Attribute("mode");
                    if (modeAttribute != null)
                    {
                        state.Mode = (UrlPickerMode)Enum.Parse(typeof(UrlPickerMode), modeAttribute.Value, false);
                    }

                    var newWindowElement = dataNode.Element("new-window");
                    if (newWindowElement != null)
                    {
                        state.NewWindow = bool.Parse(newWindowElement.Value);
                    }

                    var nodeIdElement = dataNode.Element("node-id");
                    if (nodeIdElement != null)
                    {
                        int nodeId;
                        if (int.TryParse(nodeIdElement.Value, out nodeId))
                        {
                            state.NodeId = nodeId;
                        }
                    }

                    var urlElement = dataNode.Element("url");
                    if (urlElement != null)
                    {
                        state.Url = urlElement.Value;
                    }

                    var linkTitleElement = dataNode.Element("link-title");
                    if (linkTitleElement != null && !string.IsNullOrEmpty(linkTitleElement.Value))
                    {
                        state.Title = linkTitleElement.Value;
                    }

                    break;

                case UrlPickerDataFormat.Csv:

                    var parameters = serializedState.Split(',');

                    if (parameters.Length > 0)
                    {
                        state.Mode = (UrlPickerMode)Enum.Parse(typeof(UrlPickerMode), parameters[0], false);
                    }
                    if (parameters.Length > 1)
                    {
                        state.NewWindow = bool.Parse(parameters[1]);
                    }
                    if (parameters.Length > 2)
                    {
                        int nodeId;
                        if (int.TryParse(parameters[2], out nodeId))
                        {
                            state.NodeId = nodeId;
                        }
                    }
                    if (parameters.Length > 3)
                    {
                        state.Url = parameters[3].Replace("&#45;", ",");
                    }
                    if (parameters.Length > 4)
                    {
                        if (!string.IsNullOrEmpty(parameters[4]))
                        {
                            state.Title = parameters[4].Replace("&#45;", ",");
                        }
                    }

                    break;

                case UrlPickerDataFormat.Json:

                    var jss = new JavaScriptSerializer();
                    state = jss.Deserialize <UrlPickerState>(serializedState);

                    // Check for old states
                    var untypedState = jss.DeserializeObject(serializedState);
                    if (untypedState is Dictionary <string, object> )
                    {
                        var dictState = (Dictionary <string, object>)untypedState;

                        if (dictState.ContainsKey("LinkTitle"))
                        {
                            state.Title = (string)dictState["LinkTitle"];

                            if (dictState.ContainsKey("NewWindow"))
                            {
                                // There was a short period where the UrlPickerMode values were
                                // integers starting with zero, instead of one.  This period only
                                // existed when both the "LinkTitle" and "NewWindow" keys were
                                // used.
                                state.Mode = (UrlPickerMode)((int)dictState["Mode"] + 1);
                            }
                        }
                    }

                    break;

                default:
                    throw new NotImplementedException();
                }

                // If the mode is a content node, get the url for the node
                if (state.Mode == UrlPickerMode.Content && state.NodeId.HasValue && UmbracoContext.Current != null)
                {
                    var n   = uQuery.GetNode(state.NodeId.Value);
                    var url = n != null ? n.Url : "#";

                    if (!string.IsNullOrWhiteSpace(url))
                    {
                        state.Url = url;
                    }

                    if (string.IsNullOrWhiteSpace(state.Title) && n != null)
                    {
                        state.Title = n.Name;
                    }
                }
            }
            catch (Exception)
            {
                // Could not be deserialised, return null
                state = null;
            }

            return(state);
        }
 /// <summary>
 /// Sets defaults
 /// </summary>
 public MultiUrlPickerSettings()
 {
     UrlPickerSettings = new UrlPickerSettings();
     UrlPickerDefaultState = new UrlPickerState(UrlPickerSettings);
     Standalone = true;
 }
示例#4
0
        /// <summary>
        /// Returns a UrlPickerState based on a serialized string.
        /// 
        /// Tries to infer the format of the serialized data based on known types.  Will throw exceptions
        /// if it fails to parse.
        /// </summary>
        /// <param name="serializedState">An instance of UrlPickerState as a serialized string</param>
        /// <returns>The state</returns>
        public static UrlPickerState Deserialize(string serializedState)
        {
            // Can't deserialize an empty whatever
            if (string.IsNullOrEmpty(serializedState))
            {
                return null;
            }

            // Default
            var state = new UrlPickerState();

            // Imply data format from the formatting of the serialized state
            UrlPickerDataFormat impliedDataFormat;
            if (serializedState.StartsWith("<"))
            {
                impliedDataFormat = UrlPickerDataFormat.Xml;
            }
            else if (serializedState.StartsWith("{"))
            {
                impliedDataFormat = UrlPickerDataFormat.Json;
            }
            else
            {
                impliedDataFormat = UrlPickerDataFormat.Csv;
            }

            // Try to deserialize the string
            try
            {
                switch (impliedDataFormat)
                {
                    case UrlPickerDataFormat.Xml:
                        var dataNode = XElement.Parse(serializedState);

                        // Carefully try to get values out.  This is in case new versions add
                        // to the XML
                        var modeAttribute = dataNode.Attribute("mode");
                        if (modeAttribute != null)
                        {
                            state.Mode = (UrlPickerMode)Enum.Parse(typeof(UrlPickerMode), modeAttribute.Value, false);
                        }

                        var newWindowElement = dataNode.Element("new-window");
                        if (newWindowElement != null)
                        {
                            state.NewWindow = bool.Parse(newWindowElement.Value);
                        }

                        var nodeIdElement = dataNode.Element("node-id");
                        if (nodeIdElement != null)
                        {
                            int nodeId;
                            if (int.TryParse(nodeIdElement.Value, out nodeId))
                            {
                                state.NodeId = nodeId;
                            }
                        }

                        var urlElement = dataNode.Element("url");
                        if (urlElement != null)
                        {
                            state.Url = urlElement.Value;
                        }

                        var linkTitleElement = dataNode.Element("link-title");
                        if (linkTitleElement != null && !string.IsNullOrEmpty(linkTitleElement.Value))
                        {
                            state.Title = linkTitleElement.Value;
                        }

                        break;
                    case UrlPickerDataFormat.Csv:

                        var parameters = serializedState.Split(',');

                        if (parameters.Length > 0)
                        {
                            state.Mode = (UrlPickerMode)Enum.Parse(typeof(UrlPickerMode), parameters[0], false);
                        }
                        if (parameters.Length > 1)
                        {
                            state.NewWindow = bool.Parse(parameters[1]);
                        }
                        if (parameters.Length > 2)
                        {
                            int nodeId;
                            if (int.TryParse(parameters[2], out nodeId))
                            {
                                state.NodeId = nodeId;
                            }
                        }
                        if (parameters.Length > 3)
                        {
                            state.Url = parameters[3].Replace("&#45;", ",");
                        }
                        if (parameters.Length > 4)
                        {
                            if (!string.IsNullOrEmpty(parameters[4]))
                            {
                                state.Title = parameters[4].Replace("&#45;", ",");
                            }
                        }

                        break;
                    case UrlPickerDataFormat.Json:

                        var jss = new JavaScriptSerializer();
                        state = jss.Deserialize<UrlPickerState>(serializedState);

                        // Check for old states
                        var untypedState = jss.DeserializeObject(serializedState);
                        if (untypedState is Dictionary<string, object>)
                        {
                            var dictState = (Dictionary<string, object>)untypedState;

                            if (dictState.ContainsKey("LinkTitle"))
                            {
                                state.Title = (string)dictState["LinkTitle"];

                                if (dictState.ContainsKey("NewWindow"))
                                {
                                    // There was a short period where the UrlPickerMode values were
                                    // integers starting with zero, instead of one.  This period only
                                    // existed when both the "LinkTitle" and "NewWindow" keys were
                                    // used.
                                    state.Mode = (UrlPickerMode)((int)dictState["Mode"] + 1);
                                }
                            }
                        }

                        break;
                    default:
                        throw new NotImplementedException();
                }

                 // If the mode is a content node, get the url for the node
                 if (state.Mode == UrlPickerMode.Content && state.NodeId.HasValue && UmbracoContext.Current != null)
                 {
                     var n = uQuery.GetNode(state.NodeId.Value);
                     var url = n != null ? n.Url : "#";

                     if (!string.IsNullOrWhiteSpace(url))
                     {
                         state.Url = url;
                     }

                     if (string.IsNullOrWhiteSpace(state.Title) && n != null)
                     {
                         state.Title = n.Name;
                     }
                 }
            }
            catch (Exception)
            {
                // Could not be deserialised, return null
                state = null;
            }

            return state;
        }
        /// <summary>
        /// Validates a URL Picker state against the settings, to ensure there has been no trickery
        /// client-side
        /// </summary>
        /// <param name="state">The state DTO to validate against</param>
        /// <returns>Whether the state is valid</returns>
        public bool ValidateState(UrlPickerState state)
        {
            // Invalid if:
            // 1. If the mode is not allowed (or)
            // 2. The state fails its own validation
            if (!AllowedModes.Contains(state.Mode) || !state.Validates())
            {
                return false;
            }

            return true;
        }
        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.Load"/> event.
        /// </summary>
        /// <param name="e">The <see cref="T:System.EventArgs"/> object that contains the event data.</param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            //add the js/css required
            this.AddAllUrlPickerClientDependencies();

            // If this.State was not set, create a default state
            if (State == null)
            {
                State = new UrlPickerState(Settings);
            }
        }