Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Nested Menu as Promise #52

Merged
merged 3 commits into from
Apr 19, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 48 additions & 25 deletions contextMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ angular.module('ui.bootstrap.contextMenu', [])
var processItem = function ($scope, event, model, item, $ul, $li, $promises, $q, $, level) {
/// <summary>Process individual item</summary>
"use strict";
var nestedMenu = angular.isArray(item[1])
? item[1] : angular.isArray(item[2])
? item[2] : angular.isArray(item[3])
? item[3] : null;
// nestedMenu is either an Array or a Promise that will return that array.
var nestedMenu = angular.isArray(item[1]) || (item[1] && angular.isFunction(item[1].then))
? item[1] : angular.isArray(item[2]) || (item[2] && angular.isFunction(item[2].then))
? item[2] : angular.isArray(item[3]) || (item[3] && angular.isFunction(item[3].then))
? item[3] : null;

// if html property is not defined, fallback to text, otherwise use default text
// if first item in the item array is a function then invoke .call()
Expand Down Expand Up @@ -107,28 +108,37 @@ angular.module('ui.bootstrap.contextMenu', [])
/// </summary>
"use strict";
$q.all($promises).then(function () {
if (level === 0) {
var topCoordinate = event.pageY;
var menuHeight = angular.element($ul[0]).prop('offsetHeight');
var winHeight = event.view.innerHeight;
if (topCoordinate > menuHeight && winHeight - topCoordinate < menuHeight) {
topCoordinate = event.pageY - menuHeight;
}

var leftCoordinate = event.pageX;
var menuWidth = angular.element($ul[0]).prop('offsetWidth');
var winWidth = event.view.innerWidth;
if (leftCoordinate > menuWidth && winWidth - leftCoordinate < menuWidth) {
leftCoordinate = event.pageX - menuWidth;
var topCoordinate = event.pageY;
var menuHeight = angular.element($ul[0]).prop('offsetHeight');
var winHeight = event.view.innerHeight;
if (topCoordinate > menuHeight && winHeight - topCoordinate < menuHeight) {
topCoordinate = event.pageY - menuHeight;
} else if(winHeight <= menuHeight) {
// If it really can't fit, reset the height of the menu to one that will fit
angular.element($ul[0]).css({"height": winHeight - 5, "overflow-y": "scroll"});
// ...then set the topCoordinate height to 0 so the menu starts from the top
topCoordinate = 0;
} else if(winHeight - topCoordinate < menuHeight) {
var reduceThreshold = 5;
if(topCoordinate < reduceThreshold) {
reduceThreshold = topCoordinate;
}
topCoordinate = winHeight - menuHeight - reduceThreshold;
}

$ul.css({
display: 'block',
position: 'absolute',
left: leftCoordinate + 'px',
top: topCoordinate + 'px'
});
var leftCoordinate = event.pageX;
var menuWidth = angular.element($ul[0]).prop('offsetWidth');
var winWidth = event.view.innerWidth;
if (leftCoordinate > menuWidth && winWidth - leftCoordinate < menuWidth) {
leftCoordinate = event.pageX - menuWidth;
}

$ul.css({
display: 'block',
position: 'absolute',
left: leftCoordinate + 'px',
top: topCoordinate + 'px'
});
});

};
Expand All @@ -138,11 +148,24 @@ angular.module('ui.bootstrap.contextMenu', [])
if (enabled) {
var openNestedMenu = function ($event) {
removeContextMenus(level + 1);
/*
* The object here needs to be constructed and filled with data
* on an "as needed" basis. Copying the data from event directly
* or cloning the event results in unpredictable behavior.
*/
var ev = {
pageX: event.pageX + $ul[0].offsetWidth - 1,
pageY: $ul[0].offsetTop + $li[0].offsetTop - 3
pageY: $ul[0].offsetTop + $li[0].offsetTop - 3,
view: event.view || window
};
renderContextMenu($scope, ev, nestedMenu, model, level + 1);

/*
* At this point, nestedMenu can only either be an Array or a promise.
* Regardless, passing them to when makes the implementation singular.
*/
$q.when(nestedMenu).then(function(promisedNestedMenu) {
renderContextMenu($scope, ev, promisedNestedMenu, model, level + 1);
});
};

$li.on('click', function ($event) {
Expand Down