Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Robert Ghafoor 33 posts 95 karma points
    Nov 23, 2012 @ 21:36
    Robert Ghafoor
    0

    Custom related links macro parameter type not loading in Macro Container but Works in Ritch Text Editor

    Hi

    i done a related links parameter type it loads and works fine in RTE but when i debbug the Value set metod is never called and that results in an empty value...

    I can add items and everyting and when i click on Save and publish it jumbs into the Value Set method and saves the correct information.

     

     

    but when i reopen the page its empty. the code im useing is the following:

    public class RelatedLinksEditor : RelatedLinksControl, IMacroGuiRendering 
        {
            #region IMacroGuiRendering
    
            private string _value = string.Empty;
            public string Value
            {
                get
                {
                    // The get is only used when umbraco saves the value
                    return XmlValue;
                }
                set
                {
            // The set method is used to load the saved value the first time if any.
                    _value = value;
                }
            }
    
            protected override void OnInit(System.EventArgs e)
            {
                base.OnInit(e);
    
                // Check if we have any value to load
                if (string.IsNullOrEmpty(_value))
                {
                    return;
                }
    
                LoadXml(_value);
            }
    
            public bool ShowCaption
            {
                get { return true; }
            }
    
            #endregion
        }

    And the control thats used

    public abstract class RelatedLinksControl : UpdatePanel
        {
            private ListBox _listboxLinks;
            private Button _buttonUp;
            private Button _buttonDown;
            private Button _buttonDelete;
            private TextBox _textboxLinkTitle;
            private CheckBox _checkNewWindow;
            private TextBox _textBoxExtUrl;
            private Button _buttonAddExtUrl;
            private Button _buttonAddIntUrlCp;
            private XmlDocument _xml;
            protected ContentPicker PagePicker;
    
            public string XmlValue { get { return ToXmlDocument().InnerXml; } }
            public ListItemCollection Items { get { return _listboxLinks.Items; } }
    
            protected RelatedLinksControl(string xml)
            {
                if (string.IsNullOrEmpty(xml) == false)
                {
                    LoadXml(xml);
                }
            }
    
            #region EventMappings
    
            public event NewItemAddedHandler OnNewItemAddedHandler;
    
            public delegate void NewItemAddedHandler(ListItem item, EventArgs e);
    
            #endregion
    
            protected RelatedLinksControl()
            {
                OnNewItemAddedHandler += RelatedLinksControl_OnNewItemAddedHandler;
            }
    
            protected override void OnInit(EventArgs e)
            {
                base.OnInit(e);
                EnsureChildControls();
            }
    
            private void RefreshItems()
            {
                EnsureChildControls();
    
                try
                {
                    _xml = ToXmlDocument();
    
                    // Clear items
                    _listboxLinks.Items.Clear();
    
                    if (_xml.DocumentElement != null)
                    {
                        foreach (XmlNode node in _xml.DocumentElement.ChildNodes)
                        {
                            if (node.Attributes != null)
                            {
                                string text = node.Attributes["title"].Value;
                                string value = (node.Attributes["type"].Value.Equals("internal") ? "i" : "e")
                                               + (node.Attributes["newwindow"].Value.Equals("1") ? "n" : "o")
                                               + node.Attributes["link"].Value;
                                _listboxLinks.Items.Add(new ListItem(text, value));
                            }
                        }
                    }
                }
                catch
                {
                    _xml = createBaseXmlDocument();
                }
            }
    
            protected override void CreateChildControls()
            {
                base.CreateChildControls();
    
                _listboxLinks = new ListBox();
                _listboxLinks.ID = "links" + base.ID;
                _listboxLinks.Width = 400;
                _listboxLinks.Height = 140;
    
                _buttonUp = new Button();
                _buttonUp.ID = "btnUp" + base.ID;
                _buttonUp.Text = umbraco.ui.GetText("relatedlinks", "modeUp");
                _buttonUp.Width = 80;
                _buttonUp.Click += buttonUp_Click;
    
    
                _buttonDown = new Button();
                _buttonDown.ID = "btnDown" + base.ID;
                _buttonDown.Attributes.Add("style", "margin-top: 5px;");
                _buttonDown.Text = umbraco.ui.GetText("relatedlinks", "modeDown");
                _buttonDown.Width = 80;
                _buttonDown.Click += buttonDown_Click;
    
                _buttonDelete = new Button();
                _buttonDelete.ID = "btnDel" + base.ID;
                _buttonDelete.Text = umbraco.ui.GetText("relatedlinks", "removeLink");
                _buttonDelete.Width = 80;
                _buttonDelete.Click += buttonDel_Click;
    
                _textboxLinkTitle = new TextBox();
                _textboxLinkTitle.Width = 400;
                _textboxLinkTitle.ID = "linktitle" + base.ID;
    
                _checkNewWindow = new CheckBox();
                _checkNewWindow.ID = "checkNewWindow" + base.ID;
                _checkNewWindow.Checked = false;
                _checkNewWindow.Text = umbraco.ui.GetText("relatedlinks", "newWindow");
    
                _textBoxExtUrl = new TextBox();
                _textBoxExtUrl.Width = 400;
                _textBoxExtUrl.ID = "exturl" + base.ID;
    
                _buttonAddExtUrl = new Button();
                _buttonAddExtUrl.ID = "btnAddExtUrl" + base.ID;
                _buttonAddExtUrl.Text = umbraco.ui.GetText("relatedlinks", "addlink");
                _buttonAddExtUrl.Width = 80;
                _buttonAddExtUrl.Click += buttonAddExt_Click;
    
                _buttonAddIntUrlCp = new Button();
                _buttonAddIntUrlCp.ID = "btnAddIntUrl" + base.ID;
                _buttonAddIntUrlCp.Text = umbraco.ui.GetText("relatedlinks", "addlink");
                _buttonAddIntUrlCp.Width = 80;
                _buttonAddIntUrlCp.Click += buttonAddIntCP_Click;
    
                PagePicker = new ContentPicker();
                PagePicker.ID = "pagePicker" + base.ID;
    
                ContentTemplateContainer.Controls.Add(new LiteralControl("<div class=\"relatedlinksdatatype\" style=\"text-align: left;  padding: 5px;\"><table><tr><td rowspan=\"2\">"));
                ContentTemplateContainer.Controls.Add(_listboxLinks);
                ContentTemplateContainer.Controls.Add(new LiteralControl("</td><td style=\"vertical-align: top\">"));
                ContentTemplateContainer.Controls.Add(_buttonUp);
                ContentTemplateContainer.Controls.Add(new LiteralControl("<br />"));
                ContentTemplateContainer.Controls.Add(_buttonDown);
                ContentTemplateContainer.Controls.Add(new LiteralControl("</td></tr><tr><td style=\"vertical-align: bottom\">"));
                ContentTemplateContainer.Controls.Add(_buttonDelete);
                ContentTemplateContainer.Controls.Add(new LiteralControl("<br />"));
                ContentTemplateContainer.Controls.Add(new LiteralControl("</td></tr></table>"));
    
                // Add related links container
                ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format("<a href=\"javascript:;\" onClick=\"document.getElementById('{0}_addExternalLinkPanel').style.display='none';document.getElementById('{0}_addExternalLinkButton').style.display='none';document.getElementById('{0}_addLinkContainer').style.display='block';document.getElementById('{0}_addInternalLinkPanel').style.display='block';document.getElementById('{0}_addInternalLinkButton').style.display='block';\"><strong>{1}</strong></a>", ClientID, umbraco.ui.GetText("relatedlinks", "addInternal"))));
                ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format(" | <a href=\"javascript:;\" onClick=\"document.getElementById('{0}_addInternalLinkPanel').style.display='none';document.getElementById('{0}_addInternalLinkButton').style.display='none';document.getElementById('{0}_addLinkContainer').style.display='block';document.getElementById('{0}_addExternalLinkPanel').style.display='block';document.getElementById('{0}_addExternalLinkButton').style.display='block';\"><strong>{1}</strong></a>", ClientID, umbraco.ui.GetText("relatedlinks", "addExternal"))));
    
                // All urls
                ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format("<div id=\"{0}_addLinkContainer\" style=\"display: none; padding: 4px; border: 1px solid #ccc; margin-top: 5px;margin-right:10px;\">", ClientID)));
                ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format("<a href=\"javascript:;\" onClick=\"document.getElementById('{0}_addLinkContainer').style.display='none';\" style=\"border: none;\"><img src=\"{1}/images/close.png\" style=\"float: right\" /></a>", ClientID, Page.ResolveUrl(SystemDirectories.Umbraco))));
                ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format("{0}:<br />", umbraco.ui.GetText("relatedlinks", "caption"))));
                ContentTemplateContainer.Controls.Add(_textboxLinkTitle);
                ContentTemplateContainer.Controls.Add(new LiteralControl("<br />"));
                ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format("<div id=\"{0}_addExternalLinkPanel\" style=\"display: none; margin: 3px 0\">", ClientID)));
                ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format("{0}:<br />", umbraco.ui.GetText("relatedlinks", "linkurl"))));
                ContentTemplateContainer.Controls.Add(_textBoxExtUrl);
                ContentTemplateContainer.Controls.Add(new LiteralControl("</div>"));
    
                ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format("<div id=\"{0}_addInternalLinkPanel\" style=\"display: none; margin: 3px 0\">", ClientID)));
                ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format("{0}:<br />", umbraco.ui.GetText("relatedlinks", "internalPage"))));
                ContentTemplateContainer.Controls.Add(PagePicker);
                ContentTemplateContainer.Controls.Add(new LiteralControl("</div>"));
    
                ContentTemplateContainer.Controls.Add(new LiteralControl("<div style=\"margin: 5px 0\">"));
                ContentTemplateContainer.Controls.Add(_checkNewWindow);
                ContentTemplateContainer.Controls.Add(new LiteralControl("</div>"));
    
                ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format("<div id=\"{0}_addInternalLinkButton\" style=\"display: none;\">", ClientID)));
                ContentTemplateContainer.Controls.Add(_buttonAddIntUrlCp);
                ContentTemplateContainer.Controls.Add(new LiteralControl("</div>"));
    
                ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format("<div id=\"{0}_addExternalLinkButton\" style=\"display: none;\">", ClientID)));
                ContentTemplateContainer.Controls.Add(_buttonAddExtUrl);
                ContentTemplateContainer.Controls.Add(new LiteralControl("</div>"));
    
                ContentTemplateContainer.Controls.Add(new LiteralControl("</div>"));
    
                ContentTemplateContainer.Controls.Add(new LiteralControl("</div>"));
    
                ResetInputMedia();
            }
    
            private XmlDocument createBaseXmlDocument()
            {
                var xmlDocument = new XmlDocument();
                var newChild = (XmlNode)xmlDocument.CreateElement("links");
                xmlDocument.AppendChild(newChild);
                return xmlDocument;
            }
    
            private XmlDocument ToXmlDocument()
            {
                EnsureChildControls();
    
                XmlDocument baseXmlDocument = createBaseXmlDocument();
                XmlNode xmlNode = baseXmlDocument.DocumentElement;
    
                foreach (ListItem listItem in _listboxLinks.Items)
                {
                    string str = listItem.Value;
                    var newChild = (XmlNode)baseXmlDocument.CreateElement("link");
                    XmlNode node1 = baseXmlDocument.CreateNode(XmlNodeType.Attribute, "title", null);
                    node1.Value = listItem.Text;
    
                    if (newChild.Attributes != null)
                    {
                        newChild.Attributes.SetNamedItem(node1);
                        XmlNode node2 = baseXmlDocument.CreateNode(XmlNodeType.Attribute, "link", null);
    
                        node2.Value = str.Substring(2);
                        newChild.Attributes.SetNamedItem(node2);
                        XmlNode node3 = baseXmlDocument.CreateNode(XmlNodeType.Attribute, "type", null);
    
                        node3.Value = !str.Substring(0, 1).Equals("i") ? "external" : "internal";
                        newChild.Attributes.SetNamedItem(node3);
    
                        XmlNode node4 = baseXmlDocument.CreateNode(XmlNodeType.Attribute, "newwindow", null);
                        node4.Value = !str.Substring(1, 1).Equals("n") ? "0" : "1";
    
                        newChild.Attributes.SetNamedItem(node4);
    
                        if (xmlNode != null)
                        {
                            xmlNode.AppendChild(newChild);
                        }
                    }
                }
    
                return baseXmlDocument;
            }
    
            private void ResetInputMedia()
            {
                _textBoxExtUrl.Text = "http://";
                _textboxLinkTitle.Text = "";
                PagePicker.Value = "";
            }
    
            void RelatedLinksControl_OnNewItemAddedHandler(ListItem item, EventArgs e)
            {
                RefreshItems();
            }
    
            #region ControlMangament
    
            public void ClearItems()
            {
                _listboxLinks.Items.Clear();
            }
    
            public void AddItem(ListItem item)
            {
                _listboxLinks.Items.Add(item);
            }
    
            public void LoadXml(string xml)
            {
                EnsureChildControls();
    
                try
                {
                    _xml = new XmlDocument();
                    _xml.LoadXml(xml);
    
                    // Clear items
                    _listboxLinks.Items.Clear();
    
                    if (_xml.DocumentElement != null)
                    {
                        foreach (XmlNode node in _xml.DocumentElement.ChildNodes)
                        {
                            if (node.Attributes != null)
                            {
                                string text = node.Attributes["title"].Value;
                                string value = (node.Attributes["type"].Value.Equals("internal") ? "i" : "e")
                                               + (node.Attributes["newwindow"].Value.Equals("1") ? "n" : "o")
                                               + node.Attributes["link"].Value;
                                _listboxLinks.Items.Add(new ListItem(text, value));
                            }
                        }
                    }
                }
                catch
                {
                    _xml = createBaseXmlDocument();
                }
            }
    
            #endregion
    
            #region ButtonEvents
    
            private void buttonUp_Click(Object o, EventArgs ea)
            {
                int index = _listboxLinks.SelectedIndex;
                if (index > 0) //not the first item
                {
                    ListItem temp = _listboxLinks.SelectedItem;
                    _listboxLinks.Items.RemoveAt(index);
                    _listboxLinks.Items.Insert(index - 1, temp);
                    _listboxLinks.SelectedIndex = index - 1;
                }
            }
    
            private void buttonDown_Click(Object o, EventArgs ea)
            {
                int index = _listboxLinks.SelectedIndex;
                if (index > -1 && index < _listboxLinks.Items.Count - 1) //not the last item
                {
                    ListItem temp = _listboxLinks.SelectedItem;
                    _listboxLinks.Items.RemoveAt(index);
                    _listboxLinks.Items.Insert(index + 1, temp);
                    _listboxLinks.SelectedIndex = index + 1;
                }
            }
    
            private void buttonDel_Click(Object o, EventArgs ea)
            {
                int index = _listboxLinks.SelectedIndex;
                if (index > -1)
                {
                    _listboxLinks.Items.RemoveAt(index);
                }
            }
    
            private void buttonAddExt_Click(Object o, EventArgs ea)
            {
                string url = _textBoxExtUrl.Text.Trim();
                if (url.Length > 0 && _textboxLinkTitle.Text.Length > 0)
                {
                    // use default HTTP protocol if no protocol was specified
                    if (!(url.Contains("://")))
                    {
                        url = "http://" + url;
                    }
    
                    string value = "e" + (_checkNewWindow.Checked ? "n" : "o") + url;
                    _listboxLinks.Items.Add(new ListItem(_textboxLinkTitle.Text, value));
                    ResetInputMedia();
                }
            }
    
            private void buttonAddIntCP_Click(Object o, EventArgs ea)
            {
                if (!String.IsNullOrEmpty(_textboxLinkTitle.Text) && !string.IsNullOrEmpty(PagePicker.Value))
                {
                    string value = "i" + (_checkNewWindow.Checked ? "n" : "o") + PagePicker.Value;
                    var listItem = new ListItem(_textboxLinkTitle.Text, value);
    
                    _listboxLinks.Items.Add(listItem);
    
                    if(OnNewItemAddedHandler != null)
                    {
                        OnNewItemAddedHandler(listItem, new EventArgs());
                    }
    
                    ResetInputMedia();
                }
            }
    
            #endregion
        }

    If anyone here could help with this last peace of puzzle i would really appreciate it.

  • Robert Ghafoor 33 posts 95 karma points
    Nov 24, 2012 @ 00:16
    Robert Ghafoor
    0

    i have debuged now in a umbraco fork and i wonder the save format is in xml and as i see the macro container not saving the whole xml i only get this part

     

    so i think its not supported to save in xml format -.- cus it thinks that s> in is the end of UmbracoMacro ......

     

    Edit:

    Confirmed its a bugg it wont format the data to save when its in xml format...

Please Sign in or register to post replies

Write your reply to:

Draft