1
votes

Full code here: https://plnkr.co/edit/6TTLVcsXLV7C1qXSMQV0?p=preview

I have an angular ui bootstrap accordion here (each repeated panel has a nested accordion):

<uib-accordion close-others="oneAtATime">
    <div ng-repeat="sub in subdivisions">
      <div uib-accordion-group  id="accordion" is-open="status.open"
           class="outercontent" ng-repeat="prov in sub.province"
           heading="{{prov.name}}">

        <!-- INNER ACCORDION -->
        <uib-accordion close-others="oneAtATime">
          <div uib-accordion-group  id="inner-accordion" class="innercontent"
               ng-repeat="dist in sub.district" heading="{{dist.name}}" >
            <!-- ul of communes -->
          </div>  
        </uib-accordion>

      </div>
    </div>
  </uib-accordion> 

And I have a set of links around svg elements here:

<svg height="300" width="425" id="svg"
         xmlns="http://www.w3.org/2000/svg"
         xmlns:xlink="http://www.w3.org/1999/xlink">

      <g id="adrargroup" >  
        <a ng-click="clickOuter($event);" class="svgcircle big"><circle cx="50" cy="50" r="40" id="adrarouter" class="circle"/></a>
          <a ng-click="clickInner($event)" class="svgcircle small" id="adrarinner"><circle cx="50" cy="100" r="10" /></a>
          <a ng-click="click($event)" class="svgcircle small" id="boudainner"><circle cx="50" cy="150" r="10" /></a>
          <a ng-click="click($event)" class="svgcircle small" id="ouledahmedtimmiinner"> <circle cx="50" cy="200" r="10" /></a>
      </g>
      <g id="algiersgroup" > 
        <a ng-click="clickOuter($event)" class="svgcircle big"><circle cx="150" cy="50" r="40" id="algiersouter" class="circle"/></a>
          <a ng-click="clickInner($event)" class="svgcircle small" id="babelouedinner"><circle cx="150" cy="100" r="10" /></a>
          <a ng-click="clickInner($event)" class="svgcircle small" id="barakiinner"><circle cx="150" cy="150" r="10" /></a>
          <a ng-click="clickInner($event)" class="svgcircle small" id="birmouradraisinner"><circle cx="150" cy="200" r="10" /></a>
      </g>
      <g id="aindeflagroup" > 
        <a ng-click="clickOuter($event)" class="svgcircle big" ><circle cx="250" cy="50" r="40" class="circle" id="aindeflaouter" /></a>
          <a ng-click="clickInner($event)" class="svgcircle small" id="aindeflainner"><circle cx="250" cy="100" r="10" /></a>
          <a ng-click="clickInner($event)" class="svgcircle small" id="ainlechiakhinner"><circle cx="250" cy="150" r="10" /></a>
          <a ng-click="clickInner($event)" class="svgcircle small" id="bathiainner"><circle cx="250" cy="200" r="10" /></a>
      </g>
      <g id="chlefgroup" > 
        <a ng-click="clickOuter($event)" class="svgcircle big" ><circle cx="350" cy="50" r="40" id="chlefouter" class="circle"/></a>
          <a ng-click="clickInner($event)" class="svgcircle small" id="abouelhassaninner"><circle cx="350" cy="100" r="10" /></a>
          <a ng-click="clickInner($event)" class="svgcircle small" id="ainmeraneinner"><circle cx="350" cy="150" r="10" /></a>
          <a ng-click="clickInner($event)" class="svgcircle small" id="benihaouainner"><circle cx="350" cy="200" r="10" /></a>
      </g>

</svg>

What i'm trying to achieve is when each link is clicked, the target link's matching div in the accordion opens.

I tried to do this by triggering this function on click:

$scope.clickOuter = function(e) {

    var targetcircle = e.target;

    for(i=0; i<circle.length; i++) {
      circle[i].classList.remove("fff-onfocus");
    }
    targetcircle.classList.toggle("fff-onfocus");

    var targetcontent = document.querySelectorAll('.' + targetcircle.getAttribute("id")); 
    targetcontent = Array.prototype.slice.call(targetcontent);  

    for(i=0; i<targetcontent.length; i++) {
      targetcontent[i].setAttribute("is-open", "!status.open");
    }

};

This part of the function:

var targetcontent = document.querySelectorAll('.' + targetcircle.getAttribute("id")); 
targetcontent = Array.prototype.slice.call(targetcontent);  

for(i=0; i<targetcontent.length; i++) {
  targetcontent[i].setAttribute("is-open", "!status.open");
}

Gets the target circle's id and retrieves the matching div. It then changes the is-open (default value = false) attribute's value to !status.open (true). However this is not working fully when I test it. I click the link, it returns the matching div and changes the value of is-open attribute but no divs in the accordion are actually opening or closing.

(in this https://plnkr.co/edit/M6ZjYSlzgmDQYXFzOurD?p=preview plunker example the last panel can be toggled through making status.open true )

My second problem is that this part:

for(i=0; i<circle.length; i++) {
      circle[i].classList.remove("fff-onfocus");
    }
    targetcircle.classList.toggle("fff-onfocus");

is removing the class from one circle when another is clicked but won't let you toggle the one selected when clicked again.

So 2 problems:

  1. target div isn't being toggled open/close when matching link/circle is clicked.
  2. fff-onfocus class isn't being toggled.

Any help would be greatly appreciated. Thanks.

Edit Tried this and still not working:

$scope.state = {};
$scope.state.isclosed = false;
$scope.state.isopen = true;

div attribute: is-open="state.isclosed", inside function: targetcontent[i].setAttribute("is-open", "state.isopen");

1
Can u fix the link mentioned as (in this https://plnkr.co/edit/?p=preview plunker example in your question with the one which is working - Shashank Vivek
Apologies, missed that. Added a working link now. - Gryffin

1 Answers

1
votes

It took sometime but I was able to make it work with AngularJS way rather than the hack which you were trying (^_^)

Take a look at this plunkr

    <svg height="300" width="425" id="svg"
         xmlns="http://www.w3.org/2000/svg"
         xmlns:xlink="http://www.w3.org/1999/xlink">

      <g id="adrargroup-{{$index}}" ng-repeat="sub in subdivisions">
        <a ng-click="clickOuter(prov);" ng-repeat="prov in sub.province" class="svgcircle big">
          <circle ng-attr-cx="{{prov.cx}}" ng-attr-cy="{{prov.cy}}" r="40" class="circle"/>
        </a>
          <a ng-repeat="dist in sub.district" ng-click="clickInner(dist)" class="svgcircle small" >
            <circle  ng-attr-cx="{{dist.cx}}" ng-attr-cy="{{dist.cy}}" r="10" class="circle"/>
          </a>
      </g>
  </svg>

I have modified the $scope.subdivisions as below:

{
    province: [{
        name: 'Adrar',
        namealt: 'adrarouter',
        population: '123',
        open: false,
        cx: 50,
        cy: 50
    }],
    district: [{
            name: 'Adrar',
            namealt: 'adrarinner',
            population: '1234',
            open: false,
            cx: 50,
            cy: 100
        },
        {
            name: 'Bouda',
            namealt: 'boudainner',
            population: '1234',
            open: false,
            cx: 50,
            cy: 150
        },
        {
            name: 'Ouled Ahmed Timmi',
            namealt: 'ouledahmedtimmiinner',
            population: '1234',
            open: false,
            cx: 50,
            cy: 200
        }
    ]
}

What you need to do next:

  1. Write a function that assigns open, cx and cy value to every object
  2. You need to improve below logic to accommodate array of province and not how I did as quick fix $scope.subdivisions[index].province[0]
    $scope.clickOuter = function(e) {
        var index = $scope.subdivisions.findIndex(function(obj){
          return obj.province[0].name === e.name;
        })
        $scope.subdivisions[index].province[0].open = !$scope.subdivisions[index].province[0].open;
    };
  1. Write similar logic in $scope.clickInner = function(e) {};