Developer forum

Forum » Development » Item in item list and access parent item when available

Item in item list and access parent item when available

Remi Muller
Reply

I have an item in an item list and like to access the parent item using a custom editor field.

Is this possible? See following code sample to get an idea.

[Editor("My custom editor")]
    public class SimpleTextboxEditor : Editor
    {
        public override Type DataType
        {
            get { return typeof(string); }
        }

        public override void BeginEdit(EditorContext context)
        {
            if (context != null && context.Output != null)
            {
                //if in an item list determine what the parent item is and read it's properties
                //Is it possible something like this?
                //context.ItemType.GetParentItem()
                
                var v = context.Value != null ?
                  context.Value.ToString() : string.Empty;

                context.Output.WriteLine(string.Format("<input type=\"text\" value=\"{0}\" />",
                  System.Web.HttpUtility.HtmlAttributeEncode(v)));
            }
        }

    }
 

Replies

 
Mikkel Ricky
Reply

This is currently not possible and it may be tricky to implement. Can you share more details on exactly what you're trying to achieve?

Best regards,
Mikkel

 
Remi Muller
Reply

Idea is to create an imagemap editor. See below for a simplistic view of the item types.

Item ImageMap:
Title
Image
AreaLink[]

Item AreaLink:
Coords
Link


For the field "Coords" i want to create an image map editor and need access to the parent item "ImageMap" for the Image url. This way i can show the image, the user draws a pologon and store the coords.

Are there other options to achieve a similar result?

 
Mikkel Ricky
Reply
This post has been marked as an answer

Right after asking you for further details I got an idea for how to do something like what you're trying to do (I think).

Create a custom item field editor like this:

using Dynamicweb.Content.Items.Annotations;
using Dynamicweb.Content.Items.Editors;
using System;
using System.Web;

namespace CustomHead.ItemFieldEditors
{
    [Editor("Forum thread 38790")]
    public class ForumThread_38790 : Editor
    {
        public override Type DataType
        {
            get { return typeof(string); }
        }

        public override void BeginEdit(EditorContext context)
        {
            if (context != null && context.Output != null)
            {
                var value = context.Value != null ? context.Value.ToString() : string.Empty;

                var content = new System.Text.StringBuilder();
                content.AppendFormat(@"<textarea id='{0}' name='{0}' rows='20' cols='80'>{1}</textarea>", System.Net.WebUtility.HtmlEncode(Key), System.Net.WebUtility.HtmlEncode(value));
                content.AppendFormat("<script src='/Files/System/ItemFieldEditors/{0}.js'></script>", GetType().Name);
                content.AppendFormat("<script>try {{ new {0}({1}) }} catch (ex) {{}}</script>", GetType().Name, Newtonsoft.Json.JsonConvert.SerializeObject(new
                {
                    id = Key
                }));
                context.Output.Write(content);
            }
        }

        public override object EndEdit()
        {
            return HttpContext.Request[Key];
        }
    }
}

Then in /Files/System/ItemFieldEditors/ForumThread_38790.js, i.e. the JavaScript library included by the this field editor, you can add something like

if (typeof ForumThread_38790 == 'undefined') {
  var parentData = (function() {
                      var parentData = null;
                      if (parent != self) {
                        parentData = {
                          page: parent.Dynamicweb.Items.ItemEdit.get_current().get_page(),
                          item: parent.Dynamicweb.Items.ItemEdit.get_current().get_item()
                        };
                        var i, el, elements = parent.document.querySelectorAll('.item-field');
                        for (i = 0; el = elements[i]; i++) {
                          parentData[el.name] = el.value;
                        }
                      }
                      return parentData;
                    }()),

  ForumThread_38790 = function(config) {
    config || (config = {});
    var el, data = {}, id = config.id;
    if (id) {
      el = document.getElementById(id);
      if (el) {
        try {
          data = JSON.parse(el.value);
        } catch (ex) {}
        el.value = JSON.stringify({ id : id, parentData: parentData }, null, 2);

        // @TODO Build UI for editing data and put it back into el.value
        alert(JSON.stringify({
          id: id,
          value: el.value,
          data: data
        }, null, 2));
      }
    }
  };
}

Then you just – and that's the tricky part – have to create the UI for adding/editing an image map area and serialize the data back into the actual control.

This is just a sketch, but I hope it makes sense and is useful.

Best regards,
Mikkel

Votes for this answer: 1
 
Remi Muller
Reply

Thanks! I have a rough implementation now. This is going to work.

 

You must be logged in to post in the forum