- Breaking change: Rename "svgutils.js" to "utilities.js" (make in conformity with JSDoc module naming convention) - Breaking change: Rename "svgedit.js" to "namespaces.js" (to make clear purpose and avoid confusing with editor) - Breaking change: Rename "jquery-svg.js" to "jQuery.attr.js" - Breaking change: Rename "jquery.contextMenu.js" to "jQuery.contextMenu.js" - Breaking change: Rename "jquery.jpicker.js" to "jQuery.jPicker.js" - Breaking change: Rename "JQuerySpinBtn.css" to "jQuery.SpinButton.css" - Breaking change: Rename "JQuerySpinBtn.js" to "jQuery.SpinButton.js" (to have file name more closely reflect name) - Breaking change: Rename "jquery.svgicons.js" to "jQuery.svgIcons.js" - Breaking change: Rename "jquery.jgraduate.js" to "jQuery.jGraduate.js" - Breaking change: Rename "pathseg.js" to "svgpathseg.js" (as it is a poyfill of SVGPathSeg) - Breaking change: Rename `addSvgElementFromJson()` to `addSVGElementFromJson` for consistency - Breaking change: Rename `changeSvgContent()` to `changeSVGContent()` for consistency - Breaking change: Have `exportPDF` resolve with `output` and `outputType` rather than `dataurlstring` (as type may vary) - Breaking change: Rename `extensions/mathjax/MathJax.js` to `extensions/mathjax/MathJax.min.js` - Breaking change: Avoid recent change to have editor ready callbacks return Promises (we're not using and advantageous to keep sequential) - Breaking change: Avoid recent addition of locale-side function in ext-imagelib for l10n - Breaking change: Change name of ext-arrows.js from `Arrows` to `arrows` for sake of file path (not localized anyways). - Breaking change: Change `addlangData` extension event to `addLangData` for consistency with method name - Breaking change: Have `readLang` return lang and data but do not call `setLang` - Fix: Have general locales load first so extensions may use - Fix: Provide `importLocale` to extensions `init` so it may delay adding of the extension until locale data loaded - Fix: Ensure call to `rasterExport` without `imgType` properly sets MIME type to PNG - Fix: Wrong name for moinsave - Update: Update WebAppFind per new API changes - Enhancement: Make `setStrings` public on editor for late setting (used by `ext-shapes.js`) - Enhancement: Add `extensions_added` event - Enhancement: Add `message` event (Relay messages including those which have been been received prior to extension load) - Enhancement: Allow SVGEdit to work out of the box--avoid need for copying sample config file. Should also help with Github-based file servers - Enhancement: Allow avoiding "name" in extension export (just extract out of file name) - Enhancement: Add stack blur to canvg by default (and refactoring it) - Enhancement: Return `Promise` for `embedImage` (as with some other loading methods) - Enhancement: Supply `importLocale` to `langReady` to facilitate extension locale loading - Enhancement: Recover if an extension fails to load (just log and otherwise ignore) - Enhancement: More i18n of extensions (also fixed issue with some console warnings about missing locale strings); i18nize Hello World too - Enhancement: Allowing importing of locales within `addLangData` - npm: Update devDeps - Docs: Migrate copies of all old wiki pages to docs/from-old-wiki folder; intended for a possible move to Markdown, so raw HTML (with formatting) was not preserved, though named links had their absolute URL links preserved - Docs: Begin deleting `SvgCanvas.md` as ensuring jsdoc has replacements - Docs: Add Edtior doc file for help to general users - Docs: Clarify/simplify install instructions - npm/Docs (JSDoc): Add script to check for overly generic types - Docs (JSDoc): For config/prefs and extension creating, link to tutorials (moved tutorials to own directory to avoid recursion problems by jsdoc) - Docs (JSDoc): Add modules (upper case for usual main entrance files or regular names) - Docs (JSDoc): Fill out missing areas; indicate return of `undefined`; consistency with `@returns` - Docs (JSDoc): Add our own layout template to support overflow - Docs (JSDoc): Use cleverLinks and disallow unknown tags - Docs (JSDoc): Insist on "pedantic" flag; put output directory in config - Docs (JSDoc): Use more precise Integer/Float over number, the specific type of array/function/object - Docs (JSDoc): Use `@throws`, `@enum`, `@event`/`@fires`/`@listens` - Docs: Generally update/improve docs (fixes #92) - Docs: Update links to `latest` path (Avoid needing to update such references upon each release) - Docs: 80 chars max - Refactoring: Drop code for extension as function (already requiring export to be an object) - Refactoring: Object destructuring, `Object.entries`, Object shorthand, array extras, more camelCase variable names - Refactoring: Add a `Command` base class - Refactoring: Simplify svgicons `callback` ready detection - Refactoring: Put `let` or `const` closer to scope - Refactoring: Remove unneeded `delimiter` from regex escaping utility - Refactoring: Clearer variable names - Refactoring: Use (non-deprecated) Event constructors - Testing: Use new Sinon
260 lines
8.7 KiB
JavaScript
Executable File
260 lines
8.7 KiB
JavaScript
Executable File
/**
|
|
* jQuery Context Menu Plugin
|
|
* Cory S.N. LaViska
|
|
* A Beautiful Site ({@link https://abeautifulsite.net/})
|
|
* Modified by Alexis Deveria
|
|
*
|
|
* More info: {@link https://abeautifulsite.net/2008/09/jquery-context-menu-plugin/}
|
|
*
|
|
* @module jQueryContextMenu
|
|
* @todo Update to latest version and adapt (and needs jQuery update as well): {@link https://github.com/swisnl/jQuery-contextMenu}
|
|
* @version 1.01
|
|
*
|
|
* @license
|
|
* Terms of Use
|
|
*
|
|
* This plugin is dual-licensed under the GNU General Public License
|
|
* and the MIT License and is copyright A Beautiful Site, LLC.
|
|
*
|
|
*/
|
|
import {isMac} from '../browser.js';
|
|
|
|
/**
|
|
* @callback module:jQueryContextMenu.jQueryContextMenuCallback
|
|
* @param {string} href The `href` value after the first character (for bypassing an initial `#`)
|
|
* @param {external:jQuery} srcElement The wrapped jQuery srcElement
|
|
* @param {{x: Float, y: Float, docX: Float, docY: Float}} coords
|
|
*/
|
|
|
|
/**
|
|
* @typedef {PlainObject} module:jQueryContextMenu.jQueryContextMenuConfig
|
|
* @property {string} menu
|
|
* @property {Float} inSpeed
|
|
* @property {Float} outSpeed
|
|
* @property {boolean} allowLeft
|
|
*/
|
|
|
|
/**
|
|
* Adds {@link external:jQuery.fn.contextMenu}, {@link external:jQuery.fn.disableContextMenuItems}, {@link external:jQuery.fn.enableContextMenuItems}, {@link external:jQuery.fn.disableContextMenu}, {@link external:jQuery.fn.enableContextMenu}, {@link external:jQuery.fn.destroyContextMenu}
|
|
* @function module:jQueryContextMenu.jQueryContextMenu
|
|
* @param {external:jQuery} $ The jQuery object to wrap (with `contextMenu`, `disableContextMenuItems`, `enableContextMenuItems`, `disableContextMenu`, `enableContextMenu`, `destroyContextMenu`)
|
|
* @returns {external:jQuery}
|
|
*/
|
|
function jQueryContextMenu ($) {
|
|
const win = $(window);
|
|
const doc = $(document);
|
|
|
|
$.extend($.fn, {
|
|
/**
|
|
* @memberof external:jQuery.fn
|
|
* @param {module:jQueryContextMenu.jQueryContextMenuConfig} o
|
|
* @param {module:jQueryContextMenu.jQueryContextMenuCallback} callback
|
|
* @returns {external:jQuery}
|
|
*/
|
|
contextMenu (o, callback) {
|
|
// Defaults
|
|
if (o.menu === undefined) return false;
|
|
if (o.inSpeed === undefined) o.inSpeed = 150;
|
|
if (o.outSpeed === undefined) o.outSpeed = 75;
|
|
// 0 needs to be -1 for expected results (no fade)
|
|
if (o.inSpeed === 0) o.inSpeed = -1;
|
|
if (o.outSpeed === 0) o.outSpeed = -1;
|
|
// Loop each context menu
|
|
$(this).each(function () {
|
|
const el = $(this);
|
|
const offset = $(el).offset();
|
|
|
|
const menu = $('#' + o.menu);
|
|
|
|
// Add contextMenu class
|
|
menu.addClass('contextMenu');
|
|
// Simulate a true right click
|
|
$(this).bind('mousedown', function (e) {
|
|
const evt = e;
|
|
$(this).mouseup(function (e) {
|
|
const srcElement = $(this);
|
|
srcElement.unbind('mouseup');
|
|
if (evt.button === 2 || o.allowLeft ||
|
|
(evt.ctrlKey && isMac())) {
|
|
e.stopPropagation();
|
|
// Hide context menus that may be showing
|
|
$('.contextMenu').hide();
|
|
// Get this context menu
|
|
|
|
if (el.hasClass('disabled')) return false;
|
|
|
|
// Detect mouse position
|
|
let x = e.pageX, y = e.pageY;
|
|
|
|
const xOff = win.width() - menu.width(),
|
|
yOff = win.height() - menu.height();
|
|
|
|
if (x > xOff - 15) x = xOff - 15;
|
|
if (y > yOff - 30) y = yOff - 30; // 30 is needed to prevent scrollbars in FF
|
|
|
|
// Show the menu
|
|
doc.unbind('click');
|
|
menu.css({ top: y, left: x }).fadeIn(o.inSpeed);
|
|
// Hover events
|
|
menu.find('A').mouseover(function () {
|
|
menu.find('LI.hover').removeClass('hover');
|
|
$(this).parent().addClass('hover');
|
|
}).mouseout(function () {
|
|
menu.find('LI.hover').removeClass('hover');
|
|
});
|
|
|
|
// Keyboard
|
|
doc.keypress(function (e) {
|
|
switch (e.keyCode) {
|
|
case 38: // up
|
|
if (!menu.find('LI.hover').length) {
|
|
menu.find('LI:last').addClass('hover');
|
|
} else {
|
|
menu.find('LI.hover').removeClass('hover').prevAll('LI:not(.disabled)').eq(0).addClass('hover');
|
|
if (!menu.find('LI.hover').length) menu.find('LI:last').addClass('hover');
|
|
}
|
|
break;
|
|
case 40: // down
|
|
if (!menu.find('LI.hover').length) {
|
|
menu.find('LI:first').addClass('hover');
|
|
} else {
|
|
menu.find('LI.hover').removeClass('hover').nextAll('LI:not(.disabled)').eq(0).addClass('hover');
|
|
if (!menu.find('LI.hover').length) menu.find('LI:first').addClass('hover');
|
|
}
|
|
break;
|
|
case 13: // enter
|
|
menu.find('LI.hover A').trigger('click');
|
|
break;
|
|
case 27: // esc
|
|
doc.trigger('click');
|
|
break;
|
|
}
|
|
});
|
|
|
|
// When items are selected
|
|
menu.find('A').unbind('mouseup');
|
|
menu.find('LI:not(.disabled) A').mouseup(function () {
|
|
doc.unbind('click').unbind('keypress');
|
|
$('.contextMenu').hide();
|
|
// Callback
|
|
if (callback) {
|
|
callback($(this).attr('href').substr(1), $(srcElement), {x: x - offset.left, y: y - offset.top, docX: x, docY: y});
|
|
}
|
|
return false;
|
|
});
|
|
|
|
// Hide bindings
|
|
setTimeout(function () { // Delay for Mozilla
|
|
doc.click(function () {
|
|
doc.unbind('click').unbind('keypress');
|
|
menu.fadeOut(o.outSpeed);
|
|
return false;
|
|
});
|
|
}, 0);
|
|
}
|
|
});
|
|
});
|
|
|
|
// Disable text selection
|
|
if ($.browser.mozilla) {
|
|
$('#' + o.menu).each(function () { $(this).css({MozUserSelect: 'none'}); });
|
|
} else if ($.browser.msie) {
|
|
$('#' + o.menu).each(function () { $(this).bind('selectstart.disableTextSelect', function () { return false; }); });
|
|
} else {
|
|
$('#' + o.menu).each(function () { $(this).bind('mousedown.disableTextSelect', function () { return false; }); });
|
|
}
|
|
// Disable browser context menu (requires both selectors to work in IE/Safari + FF/Chrome)
|
|
$(el).add($('UL.contextMenu')).bind('contextmenu', function () { return false; });
|
|
});
|
|
return $(this);
|
|
},
|
|
|
|
/**
|
|
* Disable context menu items on the fly
|
|
* @memberof external:jQuery.fn
|
|
* @param {undefined|string} o Comma-separated
|
|
* @returns {external:jQuery}
|
|
*/
|
|
disableContextMenuItems (o) {
|
|
if (o === undefined) {
|
|
// Disable all
|
|
$(this).find('LI').addClass('disabled');
|
|
return $(this);
|
|
}
|
|
$(this).each(function () {
|
|
if (o !== undefined) {
|
|
const d = o.split(',');
|
|
for (let i = 0; i < d.length; i++) {
|
|
$(this).find('A[href="' + d[i] + '"]').parent().addClass('disabled');
|
|
}
|
|
}
|
|
});
|
|
return $(this);
|
|
},
|
|
|
|
/**
|
|
* Enable context menu items on the fly
|
|
* @memberof external:jQuery.fn
|
|
* @param {undefined|string} o Comma-separated
|
|
* @returns {external:jQuery}
|
|
*/
|
|
enableContextMenuItems (o) {
|
|
if (o === undefined) {
|
|
// Enable all
|
|
$(this).find('LI.disabled').removeClass('disabled');
|
|
return $(this);
|
|
}
|
|
$(this).each(function () {
|
|
if (o !== undefined) {
|
|
const d = o.split(',');
|
|
for (let i = 0; i < d.length; i++) {
|
|
$(this).find('A[href="' + d[i] + '"]').parent().removeClass('disabled');
|
|
}
|
|
}
|
|
});
|
|
return $(this);
|
|
},
|
|
|
|
/**
|
|
* Disable context menu(s)
|
|
* @memberof external:jQuery.fn
|
|
* @returns {external:jQuery}
|
|
*/
|
|
disableContextMenu () {
|
|
$(this).each(function () {
|
|
$(this).addClass('disabled');
|
|
});
|
|
return $(this);
|
|
},
|
|
|
|
/**
|
|
* Enable context menu(s)
|
|
* @memberof external:jQuery.fn
|
|
* @returns {external:jQuery}
|
|
*/
|
|
enableContextMenu () {
|
|
$(this).each(function () {
|
|
$(this).removeClass('disabled');
|
|
});
|
|
return $(this);
|
|
},
|
|
|
|
/**
|
|
* Destroy context menu(s)
|
|
* @memberof external:jQuery.fn
|
|
* @returns {external:jQuery}
|
|
*/
|
|
destroyContextMenu () {
|
|
// Destroy specified context menus
|
|
$(this).each(function () {
|
|
// Disable action
|
|
$(this).unbind('mousedown').unbind('mouseup');
|
|
});
|
|
return $(this);
|
|
}
|
|
});
|
|
return $;
|
|
}
|
|
|
|
export default jQueryContextMenu;
|