Skip to content

Commit 2bd35ba

Browse files
committed
Refactored to avoid duplicate code
1 parent f524161 commit 2bd35ba

4 files changed

+73
-93
lines changed

jquery.ui-contextmenu.js

+69-89
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@
2222
"use strict";
2323

2424
var supportSelectstart = "onselectstart" in document.createElement("div"),
25-
match, uiVersion;
25+
match = $.ui.menu.version.match(/^(\d)\.(\d+)/),
26+
uiVersion = {
27+
major: parseInt(match[1], 10),
28+
minor: parseInt(match[2], 10)
29+
},
30+
isLTE110 = ( uiVersion.major < 2 && uiVersion.minor < 11 );
2631

2732
$.widget("moogle.contextmenu", {
2833
version: "@VERSION",
@@ -351,6 +356,54 @@ $.widget("moogle.contextmenu", {
351356
* Global functions
352357
*/
353358
$.extend($.moogle.contextmenu, {
359+
/** Convert a menu description into a into a <li> content. */
360+
createEntryMarkup: function(entry, $parentLi) {
361+
var $a = null;
362+
363+
if ( !/[^\-\u2014\u2013\s]/.test( entry.title ) ) {
364+
// hyphen, em dash, en dash: separator as defined by UI Menu 1.10
365+
$parentLi.text(entry.title);
366+
} else {
367+
if ( isLTE110 ) {
368+
// jQuery UI Menu 1.10 or before required an `<a>` tag
369+
$parentLi.attr("data-command", entry.cmd);
370+
$a = $("<a/>", {
371+
html: "" + entry.title,
372+
href: "#"
373+
}).appendTo($parentLi);
374+
375+
if ( entry.uiIcon ) {
376+
$a.append($("<span class='ui-icon' />").addClass(entry.uiIcon));
377+
}
378+
379+
} else {
380+
// jQuery UI Menu 1.11+ preferes to avoid `<a>` tags
381+
$parentLi
382+
.attr("data-command", entry.cmd)
383+
.html("" + entry.title);
384+
if ( $.isFunction(entry.action) ) {
385+
$parentLi.data("actionHandler", entry.action);
386+
}
387+
if ( entry.uiIcon ) {
388+
$parentLi
389+
.append($("<span class='ui-icon' />")
390+
.addClass(entry.uiIcon));
391+
}
392+
}
393+
if ( $.isFunction(entry.action) ) {
394+
$parentLi.data("actionHandler", entry.action);
395+
}
396+
if ( entry.disabled ) {
397+
$parentLi.addClass("ui-state-disabled");
398+
}
399+
if ( entry.addClass ) {
400+
$parentLi.addClass(entry.addClass);
401+
}
402+
if ( $.isPlainObject(entry.data) ) {
403+
$parentLi.data(entry.data);
404+
}
405+
}
406+
},
354407
/** Convert a nested array of command objects into a <ul> structure. */
355408
createMenuMarkup: function(options, $parentUl) {
356409
var i, menu, $ul, $li;
@@ -370,103 +423,30 @@ $.extend($.moogle.contextmenu, {
370423
}
371424
return $parentUl;
372425
},
426+
/** Returns true if the menu item has child menu items */
427+
isMenu: function(item) {
428+
if ( isLTE110 ) {
429+
return item.has(">a[aria-haspopup='true']").length > 0;
430+
} else {
431+
return item.is("[aria-haspopup='true']");
432+
}
433+
},
373434
/** Replaces the value of elem's first text node child */
374435
replaceFirstTextNodeChild: function(elem, text) {
375436
elem
376437
.contents()
377438
.filter(function() { return this.nodeType === 3; })
378439
.first()
379440
.replaceWith(text);
380-
}
381-
});
382-
383-
match = $.ui.menu.version.match(/^(\d)\.(\d+)/);
384-
385-
uiVersion = {
386-
major: parseInt(match[1], 10),
387-
minor: parseInt(match[2], 10)
388-
};
389-
390-
if ( uiVersion.major < 2 && uiVersion.minor < 11 ) {
391-
$.extend($.moogle.contextmenu, {
392-
/** Convert a menu description into a into a <li> content. */
393-
createEntryMarkup: function(entry, $parentLi) {
394-
var $a = null;
395-
396-
// if (entry.title.match(/^---/)) {
397-
if ( !/[^\-\u2014\u2013\s]/.test( entry.title ) ) {
398-
// hyphen, em dash, en dash: separator as defined by UI Menu 1.10
399-
$parentLi.text(entry.title);
400-
} else {
401-
$parentLi.attr("data-command", entry.cmd);
402-
$a = $("<a/>", {
403-
html: "" + entry.title, // allow to pass HTML markup
404-
href: "#"
405-
}).appendTo($parentLi);
406-
if ( $.isFunction(entry.action) ) {
407-
$parentLi.data("actionHandler", entry.action);
408-
}
409-
if ( entry.uiIcon ) {
410-
$a.append($("<span class='ui-icon' />").addClass(entry.uiIcon));
411-
}
412-
if ( entry.disabled ) {
413-
$parentLi.addClass("ui-state-disabled");
414-
}
415-
if ( entry.addClass ) {
416-
$parentLi.addClass(entry.addClass);
417-
}
418-
if ( $.isPlainObject(entry.data) ) {
419-
$parentLi.data(entry.data);
420-
}
421-
}
422-
},
423-
/** Returns true if the menu item has child menu items */
424-
isMenu: function(item) {
425-
return item.has(">a[aria-haspopup='true']").length > 0;
426-
},
427-
/** Updates the menu item's title */
428-
updateTitle: function(item, title) {
441+
},
442+
/** Updates the menu item's title */
443+
updateTitle: function(item, title) {
444+
if ( isLTE110 ) {
429445
$.moogle.contextmenu.replaceFirstTextNodeChild($("a", item), title);
430-
}
431-
});
432-
} else {
433-
$.extend($.moogle.contextmenu, {
434-
/** Convert a menu description into a into a <li> content. */
435-
createEntryMarkup: function(entry, $parentLi) {
436-
if ( !/[^\-\u2014\u2013\s]/.test( entry.title ) ) {
437-
$parentLi.text(entry.title);
438-
} else {
439-
$parentLi
440-
.attr("data-command", entry.cmd)
441-
.html("" + entry.title);
442-
if ( $.isFunction(entry.action) ) {
443-
$parentLi.data("actionHandler", entry.action);
444-
}
445-
if ( entry.uiIcon ) {
446-
$parentLi
447-
.append($("<span class='ui-icon' />")
448-
.addClass(entry.uiIcon));
449-
}
450-
if ( entry.disabled ) {
451-
$parentLi.addClass("ui-state-disabled");
452-
}
453-
if ( entry.addClass ) {
454-
$parentLi.addClass(entry.addClass);
455-
}
456-
if ( $.isPlainObject(entry.data) ) {
457-
$parentLi.data(entry.data);
458-
}
459-
}
460-
},
461-
/** Returns true if the menu item has child menu items */
462-
isMenu: function(item) {
463-
return item.is("[aria-haspopup='true']");
464-
},
465-
/** Updates the menu item's title */
466-
updateTitle: function(item, title) {
446+
} else {
467447
$.moogle.contextmenu.replaceFirstTextNodeChild(item, title);
468448
}
469-
});
470-
}
449+
}
450+
});
471451

472452
}));

jquery.ui-contextmenu.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)