var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { autoinject, bindable, Container, createOverrideContext, customAttribute, ViewSlot } from "aurelia-framework";
import { Config } from "../config";
import { UIInternal } from "../utils/ui-internal";
var seed = 0;
export var UIContextmenu = (function () {
    function UIContextmenu(element, viewSlot) {
        var _this = this;
        this.element = element;
        this.viewSlot = viewSlot;
        this.theme = "";
        this.id = "tooltip-".concat(seed++);
        this.clickSubscription = UIInternal.subscribe(UIInternal.EVT_VIEWPORT_CLICK, function () {
            $(_this.contextMenu).remove();
        });
    }
    UIContextmenu.prototype.attached = function () {
        var _this = this;
        this.element.addEventListener("contextmenu", function (mouseEvent) { return _this.handleKeypressed(mouseEvent); });
    };
    UIContextmenu.prototype.createContextMenu = function () {
        var _this = this;
        var view = UIInternal.compileTemplate("<template><ui-menu class=\"ui-contextmenu ui-shadow\" ui-bg=\"white\" ref=\"contextMenu\" menu-items.bind=\"menuItems\"></ui-menu></template>", this, createOverrideContext(this));
        var container = Container.instance.get(Config).containerConfig.FloatingContainer;
        var slot = new ViewSlot(container, true);
        slot.add(view);
        view.attached();
        this.contextMenu.addEventListener("click", function (ev) { return _this.handleClick(ev); });
    };
    UIContextmenu.prototype.detached = function () {
        var _this = this;
        this.element.removeEventListener("contextmenu", function (mouseEvent) { return _this.handleKeypressed(mouseEvent); });
        this.element.removeEventListener("click", function (ev) { return _this.handleClick(ev); });
        this.clickSubscription.dispose();
    };
    UIContextmenu.prototype.hideContextMenu = function () {
        $(this.contextMenu).remove();
    };
    UIContextmenu.prototype.handleClick = function (ev) {
        this.hideContextMenu();
    };
    UIContextmenu.prototype.handleKeypressed = function (ev) {
        this.createContextMenu();
        this.contextMenu.style.position = "fixed";
        var maxWidth = document.body.clientWidth - this.contextMenu.clientWidth;
        var maxHeight = document.body.clientHeight - this.contextMenu.clientHeight;
        this.contextMenu.style.left = (ev.clientX > maxWidth ? maxWidth : ev.clientX) + 'px';
        this.contextMenu.style.top = (ev.clientY > maxHeight ? maxHeight : ev.clientY) + 'px';
        $(this.contextMenu).show();
        ev.preventDefault();
    };
    __decorate([
        bindable,
        __metadata("design:type", Object)
    ], UIContextmenu.prototype, "menuItems", void 0);
    __decorate([
        bindable(),
        __metadata("design:type", Object)
    ], UIContextmenu.prototype, "theme", void 0);
    UIContextmenu = __decorate([
        autoinject(),
        customAttribute("ui-contextmenu"),
        __metadata("design:paramtypes", [Element, ViewSlot])
    ], UIContextmenu);
    return UIContextmenu;
}());

//# sourceMappingURL=ui-contextmenu.js.map
