<%
# Copyright (C) 2014 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<% granted_permissions = ContextExternalTool.global_navigation_granted_permissions(root_account: @domain_root_account,
    user: @current_user, context: @context || @domain_root_account, session: session) %>

<% cache([ContextExternalTool.global_navigation_menu_render_cache_key(@domain_root_account, granted_permissions)]) do %>
  <% tools = ContextExternalTool.filtered_global_navigation_tools(@domain_root_account, granted_permissions) %>
  <% tools.each do |tool|
      tool_display = tool.extension_setting('global_navigation', 'windowTarget') == '_blank' ? 'borderless' : nil
      tool_path = account_external_tool_path(@domain_root_account, tool, :launch_type => 'global_navigation', display: tool_display)
  %>
    <li id="<%= tool.asset_string %>_menu_item" class="globalNavExternalTool menu-item ic-app-header__menu-list-item<% if current_page?(tool_path)%> ic-app-header__menu-list-item--active<% end %>">
      <a class='ic-app-header__menu-list-link' href="<%= tool_path %>">
        <% svg_path = tool.extension_setting(:global_navigation, :icon_svg_path_64) %>
        <% if svg_path %>
          <svg version="1.1" class="ic-icon-svg ic-icon-svg--lti menu-item__icon" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 64 64">
            <path d="<%= svg_path %>"/>
          </svg>
        <% else %>
          <img src="<%= tool.extension_setting(:global_navigation, :icon_url) %>" alt="" />
        <% end %>
        <div class="menu-item__text">
          <%= tool.label_for(:global_navigation) %>
        </div>
      </a>
    </li>
  <% end %>
<% end %>
