0

Now I'm doing it like:

NodeList* elementsUsingTagG = [third_floor.DOMDocument getElementsByTagName:@"g"];
auds = [elementsUsingTagG.internalArray objectAtIndex:1];
SVGElement *elem = (SVGElement*)[third_floor.DOMDocument getElementById:hitLayer.name];
if ([auds.childNodes.internalArray containsObject:elem])
{
    [self deselectTappedLayer];
    lastTappedLayer = hitLayer;

    if (lastTappedLayer != nil)
    {
        lastTappedLayer.opacity = 1.0f;
        NSLog(@"Clicked on auditory %@, highlight it!", hitLayer.name);
    }
}

Can I get some property of SVGElement/CALayer/Node, which identifies group, which this elem belongs to? Thanks

Mattias Wadman
  • 11,172
  • 2
  • 42
  • 57
Aft3rmath
  • 669
  • 2
  • 12
  • 21
  • You mean traversing upwards to the first parent node that is a `g`-tag? – Mattias Wadman Mar 17 '13 at 17:41
  • I mean is there any way to get element's group name, if it's contained by any group. So, in other words, yes, traverse upwards until any g-tag will be found or root node will be gotten. – Aft3rmath Mar 17 '13 at 17:50

1 Answers1

1

Something like this i guess. I haven't tested the code but i hope it can help you.

Iterative version:

- (Element *)findFirstParentElementFromNode:(Node *)node
                                withTagName:(NSString *)tagName {
    for (node = node.parentNode; node != nil; node = node.parentNode) {
        if (![node isKindOfClass:[Element class]]) {
            continue;
        }

        Element *element = (Element *)node;
        if([element.tagName isEqualToString:tagName]) {
            return element;
        }
    }

    return nil;
}

Recursive version:

- (Element *)findFirstParentElementFromNode:(Node *)node
                                withTagName:(NSString *)tagName {
    if (node.parentNode == nil) {
        return nil;
    }

    if ([node.parentNode isKindOfClass:[Element class]]) {
        Element *element = (Element *)node.parentNode;
        if([element.tagName isEqualToString:tagName]) {
            return element;
        }
    }

    return [self findFirstParentElementFrom:node.parentNode
                                withTagName:tagName];
}

And use one of them like this in your case:

Element *groupElement = [self findFirstParentElementFromNode:elem
                                                 withTagName:@"g"];
Mattias Wadman
  • 11,172
  • 2
  • 42
  • 57
  • and then just get `name` property of `Element`? – Aft3rmath Mar 18 '13 at 17:54
  • Hmm not sure. By `name` you mean some kind of group name? maybe your looking for the `id` attribute? E.g. `NSString *groupId = [groupElement getAttribute:@"id"]` – Mattias Wadman Mar 18 '13 at 20:29
  • If not, can you post a sample SVG somewhere and describe which svg group you want to find when which svg element is tapped. – Mattias Wadman Mar 18 '13 at 20:30
  • http://imgh.us/test_15.svg here it is. By `name` I mean determine by 1 line of code which `group` (name of group) clicked element belongs to. In this example the `group` is set of auditories. – Aft3rmath Mar 19 '13 at 07:32
  • In that file i see two groups (``-tags), one with id "layer1" and one with id "g6474". But i guess you want to get the id:s named "aud-15-3-5" etc? but those seems to be on rects and paths. Is that correct? – Mattias Wadman Mar 19 '13 at 08:41
  • No, my fault, i forgot to rename "g6474" into "audsgroup" for example, but it doesn't matter. So according to this .svg, I want to get "g6474" groupname from element, which is included in this group. So in other words - does any element know which group (or not) it included in? – Aft3rmath Mar 19 '13 at 17:46
  • Ok, but you also want to find "aud-15-3-5" etc when touching that rect or path? that is do you want only id:s from groups? (g-tags) or also id:s from rect and paths? – Mattias Wadman Mar 19 '13 at 18:50
  • I can get "aud-15-3-5" from `name` property of tapped rect/path, because it's the same that `id` in my case. So I need to get `id`/`name` value from group, which my tapped element incuded in. – Aft3rmath Mar 19 '13 at 19:49
  • So `elem` in that case would be the rect with id "aud-15-3-5"? then i think my suggested code should work to traverse upwards to the closest group which as id "g6474". – Mattias Wadman Mar 20 '13 at 08:40
  • Not yet, I'll accept your answer as soon as I'll do it, don't worry – Aft3rmath Mar 25 '13 at 19:06
  • Just wanted to know if you made any progress :) but please let me know if you make it work so we can make the answer more useful. – Mattias Wadman Mar 26 '13 at 10:32