/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library 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 Lesser General Public License for more
 * details.
 */
AUI.add('liferay-calendar-simple-menu', function (A) {
  var Lang = A.Lang;
  var getClassName = A.getClassName;
  var isArray = Lang.isArray;
  var CSS_SIMPLE_MENU_ITEM = getClassName('simple-menu', 'item');
  var CSS_SIMPLE_MENU_ITEM_HIDDEN = getClassName('simple-menu', 'item', 'hidden');
  var CSS_SIMPLE_MENU_SEPARATOR = getClassName('simple-menu', 'separator');
  var DEFAULT_ALIGN_POINTS = [A.WidgetPositionAlign.TL, A.WidgetPositionAlign.BL];
  var STR_BLANK = '';
  var STR_DASH = '-';
  var STR_DOT = '.';
  var STR_SPACE = ' ';
  var TPL_ICON = '<i class="{iconClass}"></i>';
  var TPL_SIMPLE_MENU_ITEM = '<li class="{cssClass}" data-id="{id}" role="menuitem" tabindex="-1"></li>';
  var getItemHandler = A.cached(function (id, items) {
    var found = null;
    items.some(function (item) {
      if (item.id === id) {
        found = item;
      }

      return !!found;
    });
    return found && found.fn;
  });
  var SimpleMenu = A.Component.create({
    ATTRS: {
      alignNode: {
        value: null
      },
      hiddenItems: {
        validator: isArray,
        value: []
      },
      host: {
        value: null
      },
      items: {
        validator: isArray,
        value: []
      },
      toggler: {
        setter: A.one,
        value: null
      }
    },
    AUGMENTS: [A.WidgetModality, A.WidgetPosition, A.WidgetPositionAlign, A.WidgetPositionConstrain, A.WidgetStack, A.WidgetStdMod],
    NAME: 'simple-menu',
    UI_ATTRS: ['hiddenItems', 'items'],
    prototype: {
      _closeMenu: function _closeMenu() {
        var instance = this;

        instance._focusItem();

        instance.hide();

        instance._insideHandler.detach();

        instance._insideHandler = null;

        instance._outsideHandler.detach();

        instance._outsideHandler = null;
      },
      _focusItem: function _focusItem(index) {
        var instance = this;
        var visibleItems = instance.items.filter(':not(.' + CSS_SIMPLE_MENU_ITEM_HIDDEN + ')');

        if (index !== undefined) {
          index = (index + visibleItems.size()) % visibleItems.size();
          var item = visibleItems.item(index);
          item.setAttribute('tabindex', 0);
          item.getDOMNode().focus();
        }

        for (var i = 0; i < visibleItems.size(); i++) {
          visibleItems.item(i).setAttribute('tabindex', i === index ? 0 : -1);
        }
      },
      _onClickItems: function _onClickItems(event) {
        var instance = this;
        var items = instance.get('items');
        var id = event.currentTarget.attr('data-id');
        var handler = getItemHandler(id, items);

        if (handler) {
          instance._closeMenu();

          handler.apply(instance, arguments);
        }
      },
      _onClickOutside: function _onClickOutside(event) {
        var instance = this;
        var toggler = instance.get('toggler');

        if (!toggler || !toggler.contains(event.target)) {
          instance._closeMenu();
        }
      },
      _onKeyDown: function _onKeyDown(event) {
        var instance = this;

        if (event.keyCode === A.Event.KeyMap.ESC || event.keyCode === A.Event.KeyMap.TAB) {
          instance._closeMenu();

          return;
        }

        var activeElement = document.activeElement;
        var visibleItems = instance.items.filter(':not(.' + CSS_SIMPLE_MENU_ITEM_HIDDEN + ')');

        for (var i = 0; i < visibleItems.size(); i++) {
          var item = visibleItems.item(i);

          if (item.getDOMNode() !== activeElement) {
            continue;
          }

          if (event.keyCode === A.Event.KeyMap.UP) {
            instance._focusItem(i - 1);
          } else if (event.keyCode === A.Event.KeyMap.DOWN) {
            instance._focusItem(i + 1);
          } else if (event.keyCode === A.Event.KeyMap.ENTER) {
            visibleItems.item(i).simulate('click');
          }

          break;
        }
      },
      _onVisibleChange: function _onVisibleChange(event) {
        var instance = this;

        if (event.newVal) {
          var contentBox = instance.get('contentBox');
          instance._insideHandler = contentBox.on(['keydown'], instance._onKeyDown, instance);
          instance._outsideHandler = contentBox.on(['mouseupoutside', 'touchendoutside'], instance._onClickOutside, instance);

          instance._positionMenu();
        }
      },
      _positionMenu: function _positionMenu() {
        var instance = this;

        if (instance.items.size()) {
          var Util = Liferay.Util;
          var align = {
            node: instance.get('alignNode'),
            points: DEFAULT_ALIGN_POINTS
          };
          var centered = false;
          var modal = false;
          var width = 222;

          if (Util.isPhone() || Util.isTablet()) {
            align = null;
            centered = true;
            modal = true;
            width = '90%';
          }

          instance.setAttrs({
            align: align,
            centered: centered,
            modal: modal,
            width: width
          });
          var contentBox = instance.get('contentBox');
          contentBox.getDOMNode().focus();

          this._focusItem(0);
        }
      },
      _renderItems: function _renderItems(items) {
        var instance = this;
        var contentBox = instance.get('contentBox');
        var hiddenItems = instance.get('hiddenItems');
        instance.items = A.NodeList.create();
        items.forEach(function (item) {
          var caption = item.caption;

          if (!Object.prototype.hasOwnProperty.call(item, 'id')) {
            item.id = A.guid();
          }

          var id = item.id;
          var cssClass = CSS_SIMPLE_MENU_ITEM;

          if (caption == STR_DASH) {
            cssClass = CSS_SIMPLE_MENU_SEPARATOR;
          }

          if (hiddenItems.indexOf(id) > -1) {
            cssClass += STR_SPACE + CSS_SIMPLE_MENU_ITEM_HIDDEN;
          }

          if (item.cssClass) {
            cssClass += STR_SPACE + item.cssClass;
          }

          var icon = STR_BLANK;

          if (item.icon) {
            icon = Lang.sub(TPL_ICON, {
              iconClass: item.icon
            });
            caption = [icon, caption].join(STR_SPACE);
          }

          var li = A.Node.create(Lang.sub(TPL_SIMPLE_MENU_ITEM, {
            cssClass: cssClass,
            icon: icon,
            id: id
          }));
          li.setContent(caption);
          instance.items.push(li);
        });
        contentBox.setContent(instance.items);
      },
      _uiSetHiddenItems: function _uiSetHiddenItems(val) {
        var instance = this;

        if (instance.get('rendered')) {
          instance.items.each(function (item) {
            var id = item.attr('data-id');
            item.toggleClass(CSS_SIMPLE_MENU_ITEM_HIDDEN, val.indexOf(id) > -1);
          });
        }
      },
      _uiSetItems: function _uiSetItems(val) {
        var instance = this;

        if (instance.get('rendered')) {
          instance._renderItems(val);
        }
      },
      CONTENT_TEMPLATE: '<ul aria-live="polite" role="menu" tabindex="0"></ul>',
      bindUI: function bindUI() {
        var instance = this;
        A.Event.defineOutside('touchend');
        var contentBox = instance.get('contentBox');
        instance._eventHandlers = [A.getWin().on('resize', A.debounce(instance._positionMenu, 200, instance)), contentBox.delegate('click', instance._onClickItems, STR_DOT + CSS_SIMPLE_MENU_ITEM, instance), instance.after('visibleChange', instance._onVisibleChange, instance)];
      },
      destructor: function destructor() {
        var instance = this;
        new A.EventHandle(instance._eventHandlers).detach();
      },
      renderUI: function renderUI() {
        var instance = this;
        instance.get('boundingBox').unselectable();

        instance._renderItems(instance.get('items'));
      }
    }
  });
  Liferay.SimpleMenu = SimpleMenu;
}, '', {
  requires: ['aui-base', 'aui-event-base', 'aui-template-deprecated', 'event-outside', 'event-touch', 'widget-modality', 'widget-position', 'widget-position-align', 'widget-position-constrain', 'widget-stack', 'widget-stdmod']
});
//# sourceMappingURL=simple_menu.js.map