"use strict";var utils=this.utils||{};utils.ActionMenu=function(){function getPath(){var path=document.querySelector('[src*="'+FILE_NAME+'.js"]').src;return path.substring(0,path.lastIndexOf("/")+1)}function addStyleSheet(){var link=document.createElement("link");link.type="text/css",link.rel="stylesheet",link.href=getPath()+"action_menu_behavior.css",document.head.appendChild(link)}function initialize(){addStyleSheet();var elements=document.querySelectorAll('[role="dialog"][data-type="action"]');Array.prototype.forEach.call(elements,function(element){utils.ActionMenu.bind(element)})}function destroy(){Object.keys(actionMenus).forEach(function(id){actionMenus[id].destroy()}),actionMenus=Object.create(null),counter=0}function build(descriptor){var form=document.createElement("form");form.setAttribute("role","dialog"),form.dataset.type="action",form.classList.add("hidden"),descriptor.id&&(form.id=descriptor.id);var section=document.createElement("section"),h1=document.createElement("h1");h1.textContent=descriptor.title||"",section.appendChild(h1),form.appendChild(section);var actions=descriptor.actions;if(actions){var menu=document.createElement("menu");menu.classList.add("actions"),actions.forEach(function(action){var button=document.createElement("button");button.id=action.id,button.textContent=action.title,menu.appendChild(button);var classes=action.classList;classes&&classes.trim().split(/\s+/g).forEach(function(clazz){button.classList.add(clazz)})}),form.appendChild(menu)}return form}var FILE_NAME="action_menu",actionMenus=Object.create(null),counter=0,Action=function(container){this.container=container,container.dataset.id=++counter,this.callbacks=Object.create(null),container.addEventListener("click",this),container.addEventListener("animationend",this)};return Action.prototype={get id(){return this.container.id},show:function(){this.container.classList.remove("hidden"),this.container.classList.add("onviewport")},hide:function(){this.container.classList.remove("onviewport")},addEventListener:function(type,callback){"click"===type&&"function"==typeof callback&&(this.callbacks[callback]=callback)},removeEventListener:function(type,callback){"click"===type&&"function"==typeof callback&&delete this.callbacks[callback]},handleEvent:function(evt){switch(evt.type){case"click":if(evt.stopPropagation(),evt.preventDefault(),"BUTTON"!==evt.target.tagName)return;window.setTimeout(this.hide.bind(this)),"function"==typeof this.onclick&&this.onclick(evt);var callbacks=this.callbacks;Object.keys(callbacks).forEach(function(callback){setTimeout(function(){callbacks[callback](evt)})});break;case"animationend":var eventName="actionmenu-showed";"hide"===evt.animationName&&(this.container.classList.add("hidden"),eventName="actionmenu-hidden"),window.dispatchEvent(new CustomEvent(eventName))}},destroy:function(){this.container&&(this.container.removeEventListener("click",this),this.container.removeEventListener("animationend",this),this.container.parentNode.removeChild(this.container),this.container=this.callbacks=this.onclick=null)}},"complete"===document.readyState?initialize():document.addEventListener("DOMContentLoaded",function loaded(){document.removeEventListener("DOMContentLoaded",loaded),initialize()}),{get:function(id){var elem=document.getElementById(id);if(elem)return actionMenus[elem.dataset.id]},bind:function(elem){elem="object"==typeof elem?elem:document.querySelector(elem);var out=actionMenus[elem.dataset.id];return out?out:(out=new Action(elem),actionMenus[elem.dataset.id]=out,out)},create:function(descriptor){return build(descriptor||{})},destroy:destroy}}();