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 22, 2012 @ 12:14
    Robert Ghafoor
    0

    Createing RelatedLinksEditor form Macro parameter "IMacroGuiRendering"

    Hi

    i have the following code thats a clone of the relatedLinksDataEditor

    i need to get this working with a macro parameter, whats working now is adding items to the list like in related links data editor but when i reopen the macro in rte the values is loaded but then if i add a new item to the list my old entries dissapear...

     

    also the page picker wont reset after i added one item to the list.

    ScriptManager.RegisterClientScriptBlock(_pagePicker, _pagePicker.GetType(), "clearPagePicker", _pagePicker.ClientID + "_clear();", true);

    it outputs this in console:
    Uncaught ReferenceError: pagePickerPushCollection_clear is not defined /umbraco/plugins/tinymce3/insertMacro.aspx?editor=trueurl&umbPageId=1188&umbVersionId=80056b55-a7db-4242-a4d0-1b54e71dff81 (1):1

    public class RelatedLinksEditor : UpdatePanel, IMacroGuiRendering 
        {
            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;
            private pagePicker _pagePicker;
            private PagePickerDataExtractor _pagePickerExtractor;
    
            private string _value = string.Empty;
    
            public bool ShowCaption
            {
                get { return true; }
            }
    
            protected override void OnInit(EventArgs e)
            {
                base.OnInit(e);
    
                try
                {
                    _xml = new XmlDocument();
                    _xml.LoadXml(_value);
    
                }
                catch
                {
                    _xml = createBaseXmlDocument();
                }
    
                _listboxLinks = new ListBox();
                _listboxLinks.ID = "links" + base.ID;
                _listboxLinks.Width = 400;
                _listboxLinks.Height = 140;
    
                foreach (XmlNode node in _xml.DocumentElement.ChildNodes)
                {
                    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));
                }
    
                _buttonUp = new Button();
                _buttonUp.ID = "btnUp" + base.ID;
                _buttonUp.Text = umbraco.ui.GetText("relatedlinks", "modeUp");
                _buttonUp.Width = 80;
                _buttonUp.Click += new EventHandler(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 += new EventHandler(buttonDown_Click);
    
                _buttonDelete = new Button();
                _buttonDelete.ID = "btnDel" + base.ID;
                _buttonDelete.Text = umbraco.ui.GetText("relatedlinks", "removeLink");
                _buttonDelete.Width = 80;
                _buttonDelete.Click += new EventHandler(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 += new EventHandler(this.buttonAddExt_Click);
    
                _buttonAddIntUrlCP = new Button();
                _buttonAddIntUrlCP.ID = "btnAddIntUrl" + base.ID;
                _buttonAddIntUrlCP.Text = umbraco.ui.GetText("relatedlinks", "addlink");
                _buttonAddIntUrlCP.Width = 80;
                _buttonAddIntUrlCP.Click += new EventHandler(this.buttonAddIntCP_Click);
    
                _pagePickerExtractor = new PagePickerDataExtractor();
                _pagePicker = new pagePicker(_pagePickerExtractor);
                //_pagePicker = new pagePicker(null);
                _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, this.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();
            }
    
            public string Value
            {
                get
                {
                    if (string.IsNullOrEmpty(_value))
                    {
                        // Try to save it
                        Save();
                    }
    
                    return _value;
                }
                set
                {
                    _value = value;
                }
            }
    
            private XmlDocument createBaseXmlDocument()
            {
                var xmlDocument = new XmlDocument();
                var newChild = (XmlNode)xmlDocument.CreateElement("links");
                xmlDocument.AppendChild(newChild);
                return xmlDocument;
            }
    
            public void Save()
            {
                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;
    
                    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);
    
                    xmlNode.AppendChild(newChild);
                }
    
                _value = baseXmlDocument.InnerXml;
            }
    
            private void resetInputMedia()
            {
                _textBoxExtUrl.Text = "http://";
                _textboxLinkTitle.Text = "";
                _pagePickerExtractor.Value = "";
            }
    
            #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)
            {
                _pagePicker.Save();
    
                if (!String.IsNullOrEmpty(_textboxLinkTitle.Text) && _pagePickerExtractor.Value != null && _pagePickerExtractor.Value.ToString() != "")
                {
                    string value = "i" + (_checkNewWindow.Checked ? "n" : "o") + _pagePickerExtractor.Value;
                    _listboxLinks.Items.Add(new ListItem(_textboxLinkTitle.Text, value));
                    resetInputMedia();
                    ScriptManager.RegisterClientScriptBlock(_pagePicker, _pagePicker.GetType(), "clearPagePicker", _pagePicker.ClientID + "_clear();", true);
                }
            }
    
            #endregion
    /// <summary>
        /// Allows for the extraction of the link ID for the selected node of the
        /// PagePicker (aka content picker) class for integration of the PagePicker
        /// in another datatype.
        /// This class replaces the database linkup that normally holds the data and
        /// stores the data locally in memory and allows for easy access to the
        /// data (after the IDataEditor has performed a save()).
        /// This class was not designed for, but might work equally well for other datatypes.
        /// </summary>
        public class PagePickerDataExtractor : IData
        {
            private object _value;
    
            public PagePickerDataExtractor() { }
            public PagePickerDataExtractor(object o)
            {
                Value = o;
            }
    
            #region IData Members
    
            public void Delete()
            {
                throw new NotImplementedException();
            }
    
            public void MakeNew(int PropertyId)
            {
                throw new NotImplementedException();
            }
    
            public int PropertyId
            {
                set { throw new NotImplementedException(); }
            }
    
            public System.Xml.XmlNode ToXMl(System.Xml.XmlDocument d)
            {
                throw new NotImplementedException();
            }
    
            public object Value
            {
                get
                {
                    return _value;
                }
                set
                {
                    _value = value;
                }
            }
    
            #endregion
        }
  • Robert Ghafoor 33 posts 95 karma points
    Nov 22, 2012 @ 15:03
    Robert Ghafoor
    0

    I have now discoveded that i have to make my own table in the database to make this work way?

    well as i see properties that exist on a page is saved in the databse but not macors -.-

    so when i try to save my related link data it do work when its in a rte but not in a macro container...

    and the Value proeprty will get nulled when i do a edit on the type way is that?

    way wont the IMacroRender... save Value...

     

    Edit:

    Anyone know way Value wont save on edit ?

    I have now fixed this. the value field was used the wrong way... Update here: http://our.umbraco.org/forum/developers/extending-umbraco/36417-Custom-related-links-macro-parameter-type-not-loading-in-Macro-Container-but-Works-in-Ritch-Text-Editor

Please Sign in or register to post replies

Write your reply to:

Draft