<!--
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->

<!--

`paper-dropdown-menu` is a control where the user can choose from an array of
options in a drop-down menu. The currently selected option is displayed in
the control.

Example:

    <paper-dropdown-menu selected="Financier" valueattr="label">
        <paper-item label="Croissant"></paper-item>
        <paper-item label="Donut"></paper-item>
        <paper-item label="Financier"></paper-item>
        <paper-item label="Madeleine"></paper-item>
    </paper-dropdown-menu>

This example renders a drop-down menu with 4 options, with the option
`Financier` pre-selected.

Theming
-------

Style the drop-down menu with the `paper-dropdown-menu::shadow #menu` selector.

Example:

    paper-dropdown-menu::shadow #dropdown {
        background-color: #eee;
        border: 1px solid #ccc;
    }

@group Paper Elements
@element paper-dropdown-menu
@status beta
@homepage github.io
-->

<!--
Fired when an item's selection state is changed. This event is fired both
when an item is selected or deselected. The `isSelected` detail property
contains the selection state.

@event core-select
@param {Object} detail
  @param {boolean} detail.isSelected true for selection and false for deselection
  @param {Object} detail.item the item element
-->
<link href="../polymer/polymer.html" rel="import">

<link href="../core-dropdown/core-dropdown.html" rel="import">
<link href="../core-icon/core-icon.html" rel="import">
<link href="../core-icons/core-icons.html" rel="import">
<link href="../core-menu/core-menu.html" rel="import">

<link href="../paper-shadow/paper-shadow.html" rel="import">

<polymer-element name="paper-dropdown-menu">
<template>

  <link href="paper-dropdown-menu.css" rel="stylesheet">

  <div id="control" layout horizontal selected?="{{selectedItem}}" on-tap="{{toggle}}">
    <div flex>
      {{selectedItem ? (selectedItem.label || selected) : label}}
    </div>
    <core-icon id="arrow" icon="{{opened ? 'arrow-drop-up' : 'arrow-drop-down'}}"></core-icon>
  </div>

  <core-dropdown id="dropdown" relatedTarget="{{$.control}}" opened="{{opened}}" halign="{{halign}}" valign="{{valign}}">

    <paper-shadow z="1" target="{{$.dropdown}}" hasPosition></paper-shadow>

    <div class="menu-container">
      <core-menu id="menu" selected="{{selected}}" selectedItem="{{selectedItem}}" selectedClass="{{selectedClass}}" valueattr="{{valueattr}}" selectedProperty="{{selectedProperty}}" selectedAttribute="{{selectedAttribute}}" on-core-activate="{{activateAction}}">
        <content></content>
      </core-menu>
    </div>

  </core-dropdown>

</template>
<script>

  Polymer({

    publish: {

      /**
       * True if the menu is open.
       *
       * @attribute opened
       * @type boolean
       * @default false
       */
      opened: false,

      /**
       * A label for the control. The label is displayed if no item is selected.
       *
       * @attribute label
       * @type string
       * @default 'Select an item'
       */
      label: 'Select an item',

      /**
       * The currently selected element. By default this is the index of the item element.
       * If you want a specific attribute value of the element to be used instead of the
       * index, set `valueattr` to that attribute name.
       *
       * @attribute selected
       * @type Object
       * @default null
       */
      selected: null,

      /**
       * Specifies the attribute to be used for "selected" attribute.
       *
       * @attribute valueattr
       * @type string
       * @default 'name'
       */
      valueattr: 'name',

      /**
       * Specifies the CSS class to be used to add to the selected element.
       * 
       * @attribute selectedClass
       * @type string
       * @default 'core-selected'
       */
      selectedClass: 'core-selected',

      /**
       * Specifies the property to be used to set on the selected element
       * to indicate its active state.
       *
       * @attribute selectedProperty
       * @type string
       * @default ''
       */
      selectedProperty: '',

      /**
       * Specifies the attribute to set on the selected element to indicate
       * its active state.
       *
       * @attribute selectedAttribute
       * @type string
       * @default 'active'
       */
      selectedAttribute: 'selected',

      /**
       * The currently selected element.
       *
       * @attribute selectedItem
       * @type Object
       * @default null
       */
      selectedItem: null,

      /**
       * Horizontally align the overlay with the control.
       * @attribute halign
       * @type "left"|"right"
       * @default "left"
       */
      halign: 'left',

      /**
       * Vertically align the dropdown menu with the control.
       * @attribute valign
       * @type "top"|"bottom"
       * @default "bottom"
       */
      valign: 'top'

    },

    toggle: function() {
      this.opened = !this.opened;
    },

    activateAction: function() {
      this.opened = false;
    }

  });

</script>
</polymer-element>