Wijmo 5 - Context Menu Directive
HTML
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<script src="http://cdn.wijmo.com/5.20143.39/controls/wijmo.min.js"></script>
<link rel="stylesheet" href="http://cdn.wijmo.com/5.20143.39/styles/wijmo.min.css">
<script src="http://cdn.wijmo.com/5.20143.39/controls/wijmo.input.min.js"></script>
<script src="http://cdn.wijmo.com/5.20143.39/interop/angular/wijmo.angular.min.js"></script>
<div ng-app="app" ng-controller="appCtrl">
<h3>Wijmo Context Menu Directive</h3>
<!-- the menu -->
<wj-menu id="ctxMenu" ng-show="false">
<wj-menu-item cmd="cmdOpen" cmd-param="1">Open...</wj-menu-item>
<wj-menu-item cmd="cmdSave" cmd-param="2">Save</wj-menu-item>
<wj-menu-item cmd="cmdSave" cmd-param="3">Save As...</wj-menu-item>
<wj-menu-item cmd="cmdNew" cmd-param="4">New...</wj-menu-item>
<wj-menu-separator></wj-menu-separator>
<wj-menu-item cmd="cmdExit" cmd-param="5">Exit</wj-menu-item>
</wj-menu>
<!-- attach context menu to elements -->
<p>
The elements below have context menus.
Right-click any of them to see the menus.</p>
<div ctx-menu="ctxMenu" class="box">
I have a context menu!
</div>
<p></p>
<div ctx-menu="ctxMenu" class="box" style="margin-left:40px">
me too!
</div>
<p></p>
<div ctx-menu="ctxMenu" class="box" style="float:right;margin-top:200px">
and I have a context menu.
</div>
</div>
CSS
.box {
display: inline-block;
background-color: yellow;
padding: 12px;
border: 1px solid black;
}
JavaScript
// define app, include Wijmo 5 directives
var app = angular.module('app', ['wj']);
// controller
app.controller('appCtrl', function ($scope) {
// controller defines menu commands:
$scope.cmdOpen = function (parm) {
alert('open' + parm);
}
$scope.cmdSave = function (parm) {
alert('save' + parm);
}
$scope.cmdNew = function (parm) {
alert('new' + parm);
}
$scope.cmdExit = function (parm) {
alert('exit' + parm);
}
});
// context menu directive
app.directive('ctxMenu', function () {
return {
restrict: 'A',
link: function (scope, element, attrs) {
// get context menu and drop-down list
var host = document.getElementById(attrs.ctxMenu),
menu = wijmo.Control.getControl(host),
dropDown = host.querySelector('[wj-part="dropdown"]');
// show the drop-down list in response to the contextmenu command
element[0].addEventListener('contextmenu', function (e) {
e.preventDefault();
menu.selectedIndex = -1;
showPopup(dropDown, e);
dropDown.focus();
});
dropDown.addEventListener('blur', function() {
setTimeout(function() {
dropDown.style.visibility = 'hidden';
}, 100);
}, true);
}
}
function showPopup(popup, ref) {
// make sure popup is a child of the body
var p = popup.parentElement;
if (p && p != document.body) {
p.removeChild(popup);
p = null;
}
if (!p) {
document.body.appendChild(popup)
}
// calculate popup position
wijmo.setCss(popup, {
visibility: 'hidden',
display: ''
});
var sz = popup.getBoundingClientRect(),
left = ref.clientX - sz.width / 2,
top = ref.clientY - sz.height /...