code formatting changes.
This commit is contained in:
parent
8ea257e68b
commit
42114580ec
@ -4,16 +4,16 @@
|
|||||||
License: MIT */
|
License: MIT */
|
||||||
|
|
||||||
(function (undefined) {
|
(function (undefined) {
|
||||||
'use strict';
|
"use strict";
|
||||||
|
|
||||||
// client
|
// client
|
||||||
if (typeof ko !== undefined + '') {
|
if (typeof ko !== undefined + "") {
|
||||||
bindContextMenu(ko);
|
bindContextMenu(ko);
|
||||||
}
|
}
|
||||||
|
|
||||||
// node
|
// node
|
||||||
if (typeof module !== undefined + '' && module.exports && typeof require !== undefined + '') {
|
if (typeof module !== undefined + "" && module.exports && typeof require !== undefined + "") {
|
||||||
bindContextMenu(require('knockout'));
|
bindContextMenu(require("knockout"));
|
||||||
}
|
}
|
||||||
|
|
||||||
function bindContextMenu(ko) {
|
function bindContextMenu(ko) {
|
||||||
@ -23,7 +23,7 @@
|
|||||||
var registerEvent = utils.registerEventHandler;
|
var registerEvent = utils.registerEventHandler;
|
||||||
var isObservable = ko.isObservable;
|
var isObservable = ko.isObservable;
|
||||||
|
|
||||||
registerEvent(document, 'click', function (event) {
|
registerEvent(document, "click", function (event) {
|
||||||
var button = event.which || event.button;
|
var button = event.which || event.button;
|
||||||
if (!event.defaultPrevented && button < 2) {
|
if (!event.defaultPrevented && button < 2) {
|
||||||
hideCurrentMenu();
|
hideCurrentMenu();
|
||||||
@ -53,16 +53,16 @@
|
|||||||
init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
|
init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
|
||||||
var eventsToHandle = valueAccessor() || {};
|
var eventsToHandle = valueAccessor() || {};
|
||||||
var allBindings = allBindingsAccessor();
|
var allBindings = allBindingsAccessor();
|
||||||
var defaultClass = allBindings.contextMenuClass || 'context-menu';
|
var defaultClass = allBindings.contextMenuClass || "context-menu";
|
||||||
var activeElement;
|
var activeElement;
|
||||||
|
|
||||||
// bind on click? bind on context click?
|
// bind on click? bind on context click?
|
||||||
if (allBindings.bindMenuOnClick) {
|
if (allBindings.bindMenuOnClick) {
|
||||||
registerEvent(element, 'click', openMenu);
|
registerEvent(element, "click", openMenu);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (allBindings.bindMenuOnContextMenu === undefined || allBindings.bindMenuOnContextMenu) {
|
if (allBindings.bindMenuOnContextMenu === undefined || allBindings.bindMenuOnContextMenu) {
|
||||||
registerEvent(element, 'contextmenu', openMenu);
|
registerEvent(element, "contextmenu", openMenu);
|
||||||
}
|
}
|
||||||
|
|
||||||
elementMapping.push({
|
elementMapping.push({
|
||||||
@ -76,7 +76,7 @@
|
|||||||
if (activeElement) {
|
if (activeElement) {
|
||||||
activeElement.hide();
|
activeElement.hide();
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
function mouseX(evt) {
|
function mouseX(evt) {
|
||||||
@ -100,16 +100,14 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function openMenu(event) {
|
function openMenu(event) {
|
||||||
var menuElement;
|
|
||||||
|
|
||||||
activeElement = getMenu(event);
|
activeElement = getMenu(event);
|
||||||
menuElement = activeElement.element;
|
var menuElement = activeElement.element;
|
||||||
|
|
||||||
hideCurrentMenu();
|
hideCurrentMenu();
|
||||||
|
|
||||||
if (menuElement) {
|
if (menuElement) {
|
||||||
// make visibility hidden, then add to DOM so that we can get the height/width of the menu
|
// make visibility hidden, then add to DOM so that we can get the height/width of the menu
|
||||||
menuElement.style.visibility = 'hidden';
|
menuElement.style.visibility = "hidden";
|
||||||
(document.body || document).appendChild(menuElement);
|
(document.body || document).appendChild(menuElement);
|
||||||
|
|
||||||
// set location
|
// set location
|
||||||
@ -118,26 +116,26 @@
|
|||||||
var rightOfViewport = window.innerWidth + window.pageXOffset;
|
var rightOfViewport = window.innerWidth + window.pageXOffset;
|
||||||
|
|
||||||
if (mouseY(event) + menuElement.offsetHeight > bottomOfViewport) {
|
if (mouseY(event) + menuElement.offsetHeight > bottomOfViewport) {
|
||||||
menuElement.style.top = 1 * (bottomOfViewport - menuElement.offsetHeight - 10) + 'px';
|
menuElement.style.top = 1 * (bottomOfViewport - menuElement.offsetHeight - 10) + "px";
|
||||||
} else {
|
} else {
|
||||||
menuElement.style.top = mouseY(event) + 'px';
|
menuElement.style.top = mouseY(event) + "px";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mouseX(event) + menuElement.offsetWidth > rightOfViewport) {
|
if (mouseX(event) + menuElement.offsetWidth > rightOfViewport) {
|
||||||
menuElement.style.left = 1 * (rightOfViewport - menuElement.offsetWidth - 10) + 'px';
|
menuElement.style.left = 1 * (rightOfViewport - menuElement.offsetWidth - 10) + "px";
|
||||||
} else {
|
} else {
|
||||||
menuElement.style.left = mouseX(event) + 'px';
|
menuElement.style.left = mouseX(event) + "px";
|
||||||
}
|
}
|
||||||
|
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
event.stopPropagation();
|
event.stopPropagation();
|
||||||
} else {
|
} else {
|
||||||
menuElement.style.top = (element.offsetTop + element.offsetHeight) + 'px';
|
menuElement.style.top = (element.offsetTop + element.offsetHeight) + "px";
|
||||||
menuElement.style.left = (element.offsetLeft + element.offsetWidth) + 'px';
|
menuElement.style.left = (element.offsetLeft + element.offsetWidth) + "px";
|
||||||
}
|
}
|
||||||
|
|
||||||
// now set to visible
|
// now set to visible
|
||||||
menuElement.style.visibility = '';
|
menuElement.style.visibility = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
// replace current menu with the recently created
|
// replace current menu with the recently created
|
||||||
@ -163,17 +161,17 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (elements.length) {
|
if (elements.length) {
|
||||||
menu = document.createElement('div');
|
menu = document.createElement("div");
|
||||||
menu.className = defaultClass;
|
menu.className = defaultClass;
|
||||||
|
|
||||||
// you may need padding to menus that has checks
|
// you may need padding to menus that has checks
|
||||||
menu.innerHTML = '<ul class="' + (hasChecks ? 'has-checks' : '') + '">' +
|
menu.innerHTML = "<ul class=\"" + (hasChecks ? "has-checks" : "") + '">' +
|
||||||
elements.join('') +
|
elements.join("") +
|
||||||
'</ul>';
|
"</ul>";
|
||||||
|
|
||||||
// map items to actions
|
// map items to actions
|
||||||
elements.forEach(function (item, index) {
|
elements.forEach(function (item, index) {
|
||||||
registerEvent(menu.children[0].children[index], 'click', function (event) {
|
registerEvent(menu.children[0].children[index], "click", function (event) {
|
||||||
var result = actions[index](viewModel, event);
|
var result = actions[index](viewModel, event);
|
||||||
|
|
||||||
if (!result && event) {
|
if (!result && event) {
|
||||||
@ -199,7 +197,7 @@
|
|||||||
function pushItem(eventName) {
|
function pushItem(eventName) {
|
||||||
var item = getMenuProperties(eventName);
|
var item = getMenuProperties(eventName);
|
||||||
var classes = [];
|
var classes = [];
|
||||||
var id = '';
|
var id = "";
|
||||||
var liHtml;
|
var liHtml;
|
||||||
|
|
||||||
if (item.isVisible) {
|
if (item.isVisible) {
|
||||||
@ -211,25 +209,25 @@
|
|||||||
|
|
||||||
// set css classes
|
// set css classes
|
||||||
if (item.isChecked) {
|
if (item.isChecked) {
|
||||||
classes.push('checked');
|
classes.push("checked");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (item.isDisabled) {
|
if (item.isDisabled) {
|
||||||
classes.push('disabled');
|
classes.push("disabled");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (item.isSeparator) {
|
if (item.isSeparator) {
|
||||||
classes.push('separator');
|
classes.push("separator");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (item.url) {
|
if (item.url) {
|
||||||
classes.push('with-url');
|
classes.push("with-url");
|
||||||
}
|
}
|
||||||
|
|
||||||
liHtml = '<li ' + (id ? ('id="' + id + '" ') : '') +
|
liHtml = "<li " + (id ? ('id="' + id + '" ') : "") +
|
||||||
' class="' + classes.join(' ') + '">' +
|
' class="' + classes.join(" ") + '">' +
|
||||||
item.html +
|
item.html +
|
||||||
'</li>';
|
"</li>";
|
||||||
|
|
||||||
elements.push(liHtml);
|
elements.push(liHtml);
|
||||||
actions.push(item.action);
|
actions.push(item.action);
|
||||||
@ -240,8 +238,8 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getMenuProperties(eventName) {
|
function getMenuProperties(eventName) {
|
||||||
var text = '';
|
var text = "";
|
||||||
var html = '';
|
var html = "";
|
||||||
var currentEvent = ko.isObservable(eventsToHandle) ?
|
var currentEvent = ko.isObservable(eventsToHandle) ?
|
||||||
eventsToHandle()[eventName] :
|
eventsToHandle()[eventName] :
|
||||||
eventsToHandle[eventName];
|
eventsToHandle[eventName];
|
||||||
@ -268,18 +266,18 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (url) {
|
if (url) {
|
||||||
html = '<a href="' + url + '">' + text + '</a>';
|
html = '<a href="' + url + '">' + text + "</a>";
|
||||||
} else {
|
} else {
|
||||||
html = text;
|
html = text;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((isObservable(item) && typeof item() === 'boolean') ||
|
if ((isObservable(item) && typeof item() === "boolean") ||
|
||||||
(isObservable(item.action) && typeof item.action() === 'boolean')) {
|
(isObservable(item.action) && typeof item.action() === "boolean")) {
|
||||||
isBoolean = true;
|
isBoolean = true;
|
||||||
|
|
||||||
if ((item.action && item.action()) ||
|
if ((item.action && item.action()) ||
|
||||||
(typeof item === 'function' && item())) {
|
(typeof item === "function" && item())) {
|
||||||
isChecked = true;
|
isChecked = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -295,23 +293,23 @@
|
|||||||
isDisabled: isDisabled,
|
isDisabled: isDisabled,
|
||||||
isBoolean: isBoolean,
|
isBoolean: isBoolean,
|
||||||
isSeparator: isSeparator,
|
isSeparator: isSeparator,
|
||||||
action: action,
|
action: action
|
||||||
};
|
};
|
||||||
|
|
||||||
function action(viewModel, event) {
|
function action(viewModel, event) {
|
||||||
var error = eventName + ' option must have an action or an url.';
|
var error = eventName + " option must have an action or an url.";
|
||||||
|
|
||||||
if (isDisabled) {
|
if (isDisabled) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if option is a boolean
|
// check if option is a boolean
|
||||||
if (isObservable(item) && typeof item() === 'boolean') {
|
if (isObservable(item) && typeof item() === "boolean") {
|
||||||
item(!item());
|
item(!item());
|
||||||
}
|
}
|
||||||
|
|
||||||
// is an object? well, lets check it properties
|
// is an object? well, lets check it properties
|
||||||
else if (typeof item === 'object') {
|
else if (typeof item === "object") {
|
||||||
// check if has an action or if its a separator
|
// check if has an action or if its a separator
|
||||||
if (!item.action && !url && !isSeparator) {
|
if (!item.action && !url && !isSeparator) {
|
||||||
throw error;
|
throw error;
|
||||||
@ -319,7 +317,7 @@
|
|||||||
|
|
||||||
// evaluate action
|
// evaluate action
|
||||||
else if (item.action) {
|
else if (item.action) {
|
||||||
if (isObservable(item.action) && typeof item.action() === 'boolean') {
|
if (isObservable(item.action) && typeof item.action() === "boolean") {
|
||||||
item.action(!item.action());
|
item.action(!item.action());
|
||||||
} else {
|
} else {
|
||||||
item.action(viewModel, event);
|
item.action(viewModel, event);
|
||||||
@ -328,7 +326,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
// its not an observable, should be a function
|
// its not an observable, should be a function
|
||||||
else if (typeof item === 'function') {
|
else if (typeof item === "function") {
|
||||||
item(viewModel, event);
|
item(viewModel, event);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -340,7 +338,7 @@
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function hideCurrentMenu() {
|
function hideCurrentMenu() {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user