{"size": 2879, "ext": "js", "max_stars_repo_path": "admin/webapp/root/app_src/js/infra/layout/sidebar/sidebar.controller.js", "max_stars_repo_name": "rushk014/manager", "max_stars_repo_stars_event_min_datetime": "2022-01-17T02:48:03.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-29T09:16:08.000Z", "max_issues_repo_path": "admin/webapp/root/app_src/js/infra/layout/sidebar/sidebar.controller.js", "max_issues_repo_name": "rushk014/manager", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "admin/webapp/root/app_src/js/infra/layout/sidebar/sidebar.controller.js", "max_forks_repo_name": "rushk014/manager", "max_forks_repo_forks_event_min_datetime": "2022-01-14T18:12:28.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-28T04:58:14.000Z"}
{"max_stars_count": 19, "max_issues_count": null, "max_forks_count": 16, "avg_line_length": 26.1727272727, "max_line_length": 116, "alphanum_fraction": 0.5349079542}
(function() {
  "use strict";

  angular
    .module("app.sidebar")
    .controller("SidebarController", SidebarController);

  SidebarController.$inject = [
    "$rootScope",
    "$scope",
    "$state",
    "SidebarService",
    "Utils"
  ];
  function SidebarController(
    $rootScope,
    $scope,
    $state,
    SidebarService,
    Utils
  ) {
    const activate = () => {

      const hasAdminAccess = roles => {
        for (let key in roles) {
          if (roles[key] === "2" || roles[key] === "4") return true;
        }
        return false;
      };

      const openCurrOnly = index => {
        index += "";
        for (let i in submenuGroupList) {
          if (index < 0 || index.indexOf(i) < 0) submenuGroupList[i] = true;
        }
      };

      const isSelected = item => {
        if (item) {
          if (!item.stateName || item.stateName === "_") {
            let foundSelected = false;
            item.submenu.forEach(function (value) {
              if (isSelected(value)) foundSelected = true;
            });
            return foundSelected;
          } else {
            return $state.is(item.stateName) || $state.includes(item.stateName);
          }
        }
      };

      const isChild = index => typeof index === "string" && !(index.indexOf("-") < 0);

      const isSidebarRendered = items => {
        $scope.menuItems = items;
        $rootScope.sidebarDone = true;
      };

      let submenuGroupList = [];
      if ($scope.user) {
        let isAdmin = hasAdminAccess($scope.user.roles);

        $rootScope.$watch("app.layout.asideHover", (oldVal, newVal) => {
          if (newVal === false && oldVal === true) {
            openCurrOnly(-1);
          }
        });

        SidebarService.getMenu(isAdmin, isSidebarRendered);
      }

      $scope.getMenuItemClass = item => `${item.heading ? "nav-heading" : ""} ${isSelected(item) ? " active" : ""}`;

      $scope.addSubmenu = (index, item) => {
        submenuGroupList[index] = $rootScope.app.layout.asideHover
            ? true
            : !isSelected(item);
      };

      $scope.isCollapse = index => submenuGroupList[index];

      $scope.toggleSubmenuCollapse = (index, isUpperLevelItem) => {
        if (Utils.isSubmenuCollapsed() || $rootScope.app.layout.asideHover)
          return true;

        if (typeof submenuGroupList[index] !== "undefined") {
          if (!$scope.isLastEventFromSubmenu) {
            submenuGroupList[index] = !submenuGroupList[index];
            openCurrOnly(index);
          }
        } else if (isUpperLevelItem) {
          openCurrOnly(-1);
        }

        $scope.isLastEventFromSubmenu = isChild(index);

        return true;
      };
    };

    activate();

    $rootScope.$watch('app.layout.isCollapsed', newValue => {
      if (newValue === false)
        $rootScope.$broadcast('closeSidebarMenu');
    });

  }
})();
