1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5/**
6 * Adds toggle controls to the sidebar list.
7 *
8 * Finds all elements marked as toggleable, and adds an onClick event to
9 * collapse and expand the element's children.
10 */
11(function() {
12  var sidebar = document.getElementById('gc-sidebar');
13  if (!sidebar)
14    return;
15
16  Array.prototype.forEach.call(sidebar.querySelectorAll('[toggleable]'),
17                               function(toggleable) {
18    var button = toggleable.parentNode.querySelector('.button');
19    var toggleIndicator = button.querySelector('.toggleIndicator');
20    var isToggled = false;
21    function toggle() {
22      if (isToggled) {
23        toggleable.classList.add('hidden');
24        toggleIndicator.classList.remove('toggled');
25      } else {
26        toggleable.classList.remove('hidden');
27        toggleIndicator.classList.add('toggled');
28      }
29      isToggled = !isToggled;
30    }
31    button.setAttribute('href', 'javascript:void(0)');
32    button.addEventListener('click', toggle);
33  });
34
35})();
36