var $ = require('jquery');

module.exports = function() {
    var open;

    $(document)
        .on('click', function(e) {
            var target = $(e.target), current;

            if (open) {
                current = open;

                open
                    .attr('aria-expanded', false)
                    .closest('.dropdown')
                    .removeClass('dropdown--open')
                ;

                open = null;
            }

            if (!!target.not(current).length && target.is('[data-dropdown-toggle]')) {
                open = target
                    .attr('aria-expanded', true)
                    .closest('.dropdown')
                    .addClass('dropdown--open')
                    .end()
                ;

                e.preventDefault();
            }
        })
    ;
};
