<?php
/**
 * COmanage Match jQuery onload JavaScript
 * Applies jQuery widgets and flash messages
 *
 * Portions licensed to the University Corporation for Advanced Internet
 * Development, Inc. ("UCAID") under one or more contributor license agreements.
 * See the NOTICE file distributed with this work for additional information
 * regarding copyright ownership.
 *
 * UCAID licenses this file to you under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * @link          http://www.internet2.edu/comanage COmanage Project
 * @package       match
 * @since         COmanage Match v1.0.0
 * @license       Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
 */
?>

<script>
  $(function() {
    // Focus any designated form element
    $('.focusFirst').focus();

    // DESKTOP MENU DRAWER BEHAVIOR
    // Check the drawer half-closed cookie on first load and set the drawer state appropriately
    if (Cookies.get("desktop-drawer-state") == "half-closed") {
      $("#navigation-drawer").addClass("half-closed");
      $("#main").addClass("drawer-half-closed");
    } else {
      // Preserve the state of the most recently selected menu item if it is expandable (a "menuTop" item)
      // (we only use this behavior when the the drawer is fully-open)
      var mainMenuSelectedParentId = Cookies.get("main-menu-selected-parent-id");
      if(mainMenuSelectedParentId != undefined && mainMenuSelectedParentId != "") {
        $("#" + mainMenuSelectedParentId).addClass("active");
        $("#" + mainMenuSelectedParentId + " > a.menuTop").attr("aria-expanded","true");
        $("#" + mainMenuSelectedParentId + " > ul").addClass("in");
      }
    }

    // Hamburger menu-drawer toggle
    $('#co-hamburger').click(function () {
      if($(window).width() < 768) {
        // Mobile mode
        $("#navigation-drawer").removeClass("half-closed").toggle();
      } else {
        // Desktop mode
        if ($("#navigation-drawer").hasClass("half-closed")) {
          $("#navigation-drawer").removeClass("half-closed");
          $("#main").removeClass("drawer-half-closed");
          // set a cookie to hold drawer half-open state between requests
          Cookies.set("desktop-drawer-state", "open");
        } else {
          $("#navigation-drawer").addClass("half-closed");
          $("#main").addClass("drawer-half-closed");
          // set a cookie to hold drawer half-open state between requests
          Cookies.set("desktop-drawer-state", "half-closed");
        }
      }
    });

    // Catch the edge-case of browser resize causing menu-drawer
    // to remain hidden and vice versa.
    $(window).resize(function() {
      if($( window ).width() > 767) {
        $("#navigation-drawer").show();
      } else {
        $("#navigation-drawer").hide();
      }
    });

    // Desktop half-closed drawer behavior & expandable menu items
    $('#navigation-drawer a.menuTop').click(function () {
      if (Cookies.get("desktop-drawer-state") == "half-closed") {
        $("#navigation-drawer").toggleClass("half-closed");
      }

      // Save the ID of the most recently expanded menuTop item for use on reload
      if ($(this).attr("aria-expanded") == "true") {
        var parentId = $(this).parent().attr("id");
        Cookies.set("main-menu-selected-parent-id", parentId);
      } else {
        Cookies.set("main-menu-selected-parent-id", "");
      }
    });
    // END DESKTOP MENU DRAWER BEHAVIOR
    
    // Click outside behaviors
    $(document).on('click', function (e) {
      // Hide popovers on click outside but don't close current popover when interacting with content inside it
      $('#content [data-bs-toggle="popover"]').each(function () {
        if (!$(this).is(e.target) && $('.popover.show').has(e.target).length === 0) {
          $(this).popover('hide');
        }
      });
    });

    // TOP SEARCH FILTER FORM
    // Send only non-empty fields in the form
    $("#top-search-form").submit(function() {
      $("#top-search-form *").filter(':input').each(function () {
        if($(this).val() == '') {
          $(this).prop('disabled',true);
        }
      });
    });

      // Toggle the top search filter box
    $("#top-search-toggle, #top-search-toggle button.cm-toggle").click(function(e) {
      e.preventDefault();
      e.stopPropagation();
      if ($("#top-search-fields").is(":visible")) {
        $("#top-search-fields").hide();
        $("#top-search-toggle button.cm-toggle").attr("aria-expanded","false");
        $("#top-search-toggle button.cm-toggle .drop-arrow").text("arrow_drop_down");
      } else {
        $("#top-search-fields").show();
        $("#top-search-toggle button.cm-toggle").attr("aria-expanded","true");
        $("#top-search-toggle button.cm-toggle .drop-arrow").text("arrow_drop_up");
      }
    });

    // Clear a specific top search filter by clicking the filter button
    $("#top-search-toggle button.top-search-active-filter").click(function(e) {
      e.preventDefault();
      e.stopPropagation();
      $(this).hide();
      filterId = '#' + $(this).attr("aria-controls");
      $(filterId).val("");
      $(this).closest('form').submit();
    });

    // Clear all top filters from the filter bar
    $("#top-search-clear-all-button").click(function(e) {
      e.preventDefault();
      e.stopPropagation();
      $(this).hide();
      $("#top-search-toggle .top-search-active-filter").hide();
      $("#top-search-clear").click();
    });

    // Make all submit buttons pretty (Bootstrap)
    $("input:submit").addClass("spin submit-button btn btn-primary");

    // Make all select form controls Bootstrappy
    $("select").addClass("form-select");
    
    // Enable Bootstrap Popovers. Unless needed elsewhere, constrain this to #content
    $('#content [data-bs-toggle="popover"]').popover();

    // Generic row click handling for div-based rows
    $('div.linked-row').click(function (e) {
      location.href = $(this).find('a.row-link').attr('href');
    });
    
    // Generic row click handling for table rows
    $('td.row-link').each(function (e) {
      url = $(this).find('a').attr('href');
      if(url != undefined && url != '') {
        $(this).closest('tr').addClass('linked-row').attr('data-cm-target',url).click(function (e) {
          location.href = $(this).attr('data-cm-target');
        }).find('a').on('click',function(e){
          // don't propagate on other links to avoid redirecting dialog boxes
          e.stopPropagation();
        });  
      }
    });    

    // Add loading animation when a form is submitted, when any item with a "spin" class is clicked, 
    // or on any button or anchor tag lacking the .nospin class.
    $("input[type='submit'], button:not(.nospin), a:not(.nospin), .spin").click(function(e) {

      // Start a spinner only if CTRL, CMD, or SHIFT is not pressed (which loads a new tab or window).
      if(!(e.ctrlKey || e.metaKey || e.shiftKey)) {
        displaySpinner();

        // Test for invalid fields (HTML5) and turn off spinner explicitly if found
        if (document.querySelectorAll(":invalid").length) {
          stopSpinner();
        }
      }

    });

  });

  // Define default text for confirm dialog
  var defaultConfirmOk = "<?php print __('op.ok'); ?>";
  var defaultConfirmCancel = "<?php print __('op.cancel'); ?>";
  var defaultConfirmTitle = "<?php print __('op.confirm'); ?>";

</script>

<?= $this->Flash->render() ?>