Skip to content

Commit 2b600e7

Browse files
authored
Refactor set entry (mar10#122)
* Add updateEntry() method * Add tests * Fix jscs warnings
1 parent 66f22cd commit 2b600e7

File tree

7 files changed

+239
-71
lines changed

7 files changed

+239
-71
lines changed

.editorconfig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ root = true
55

66
; Unix-style newlines with a newline ending every file
77
[*]
8-
; end_of_line = lf
8+
end_of_line = lf
99
insert_final_newline = true
1010
trim_trailing_whitespace = true
1111

@@ -16,8 +16,9 @@ indent_size = 4
1616

1717
; Tab indentation (no size specified)
1818
[*.js]
19-
; indent_style = tab
2019
indent_style = tab
20+
;indent_style = space
21+
;indent_size = 4
2122

2223
; Indentation override for all JS under lib directory
2324
;[lib/**.js]

CHANGELOG.md

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1-
# 1.17.1-1 / Unreleased
2-
3-
*
1+
# 1.18.0-0 / Unreleased
2+
3+
* [CHANGE] Refactored entry update methods
4+
- `setEntry(cmd, data)`:
5+
Clarify that this method resets all attributes *not* passed in
6+
`data` are reset to defaults.<br>
7+
Also, passing a plain title string as `data` is deprecated: use `setTitle()` instead.
8+
- Add method `updateEntry(cmd, data)`.<br>
9+
Example: `updateEntry("info", {title: "Show info...", enable: false})`<br>
10+
Unlike `setEntry()`, this method only updates menu attibutes that are
11+
passed in `data`, leaving other attributes intact.
12+
- Add methods `getEntry()`, `getEntryWrapper()`, `setIcon`, `setTitle`
13+
(in addition to the existing `enableEntry` and `showEntry`).
414

515
# 1.17.0 / 2017-04-17
616

@@ -13,7 +23,7 @@
1323

1424
# 1.16.0 / 2017-03-30
1525

16-
* [FEATURE] #114: Pass `extraData` argument to `select` and other events.
26+
* [FEATURE] #114: Pass `extraData` argument to `select` and other events.
1727
The same ui.extraData instance is passed to all events of a open-select-close
1828
sequence, so it can also be used to pass data between events.
1929

@@ -24,7 +34,7 @@
2434
# 1.14.0 / 2017-01-30
2535

2636
* #108: Update AMD dependency to be compatible with the jQuery 1.12 layout:
27-
"jquery-ui/ui/widgets/menu"
37+
"jquery-ui/ui/widgets/menu"
2838
NOTE: this is not backwards compatible with jQuery 1.11 and before:
2939
http://jqueryui.com/upgrade-guide/1.12/#official-package-on-npm
3040

@@ -103,11 +113,11 @@
103113

104114
* [FEATURE] New optional parameter open(..., extraData).
105115
* [FEATURE] New option `autoTrigger: true` can be set to `false` to prevent
106-
opening menus on browser's `contextmenu` event (if you want to use the `open()`
116+
opening menus on browser's `contextmenu` event (if you want to use the `open()`
107117
method instead).
108118
* [FEATURE] New option `preventContextMenuForPopup`to prevent opening the browser's
109119
system context menu on menu entries.
110-
* [CHANGE] `setEntry()` and `replaceMenu()` now allow to define titles with HTML
120+
* [CHANGE] `setEntry()` and `replaceMenu()` now allow to define titles with HTML
111121
markup.
112122

113123
# 1.2.4 / 2013-12-25

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# jquery.ui-contextmenu [![GitHub version](https://badge.fury.io/gh/mar10%2Fjquery-ui-contextmenu.svg)](https://github.com/mar10/jquery-ui-contextmenu/releases/latest) [![Build Status](https://travis-ci.org/mar10/jquery-ui-contextmenu.png?branch=master)](https://travis-ci.org/mar10/jquery-ui-contextmenu) [![Selenium Test Status](https://saucelabs.com/buildstatus/sauce-contextmenu)](https://saucelabs.com/u/sauce-contextmenu)
1+
# jquery.ui-contextmenu [![GitHub version](https://badge.fury.io/gh/mar10%2Fjquery-ui-contextmenu.svg)](https://github.com/mar10/jquery-ui-contextmenu/releases/latest) [![Build Status](https://travis-ci.org/mar10/jquery-ui-contextmenu.png?branch=master)](https://travis-ci.org/mar10/jquery-ui-contextmenu) [![Selenium Test Status](https://saucelabs.com/buildstatus/sauce-contextmenu)](https://saucelabs.com/u/sauce-contextmenu) [![npm](https://img.shields.io/npm/dm/ui-contextmenu.svg)](https://www.npmjs.com/package/ui-contextmenu)
22

33
> A jQuery plugin that provides a context menu (based on the standard [jQueryUI menu] widget).
44
5-
* Define menus from `<ul>` element or definition list (i.e.
5+
* Define menus from `<ul>` element or definition list (i.e.
66
`[{title: "Paste", cmd: "paste"}, ...]`).
77
* Themable using [jQuery ThemeRoller](http://jqueryui.com/themeroller/).
88
* Supports delegation (i.e. can be bound to elements that don't exist at the
@@ -26,11 +26,11 @@ See also the [Change Log](https://github.com/mar10/jquery-ui-contextmenu/blob/ma
2626
[Live demo page](http://wwwendt.de/tech/demo/jquery-contextmenu/demo/):<br>
2727
[ ![sample](demo/teaser.png?raw=true) ](http://wwwendt.de/tech/demo/jquery-contextmenu/demo/ "Live demo")
2828

29-
See also the unit tests and live examples
29+
See also the unit tests and live examples
3030

3131
**More:**
3232

33-
* Play with [jsFiddle](http://jsfiddle.net/mar10/6o3u8a88/) or
33+
* Play with [jsFiddle](http://jsfiddle.net/mar10/6o3u8a88/) or
3434
[Plunker](http://plnkr.co/edit/Bbcoqy?p=preview)
3535
* Run the [unit tests](http://rawgit.com/mar10/jquery-ui-contextmenu/master/test/index.html).
3636

@@ -42,8 +42,8 @@ First, include dependencies:
4242
* jQuery 1.7+ (1.10 or later recommended)
4343
* jQuery UI 1.9+ (at least core, widget, menu), 1.11+ recommended
4444
* One of the ThemeRoller CSS themes or a custom one
45-
* jquery.ui-contextmenu.js (also available as CDN on
46-
[jsdelivr](http://www.jsdelivr.com/#!jquery.ui-contextmenu),
45+
* jquery.ui-contextmenu.js (also available as CDN on
46+
[jsDelivr](https://cdn.jsdelivr.net/gh/mar10/jquery-ui-contextmenu/),
4747
[cdnjs](https://cdnjs.com/libraries/jquery.ui-contextmenu)), or
4848
[UNPKG](https://unpkg.com/ui-contextmenu@latest/jquery.ui-contextmenu.min.js)
4949

@@ -90,11 +90,11 @@ initialize the menu from embedded &lt;ul> markup</a>.
9090

9191
For more information:
9292

93-
* [Read the Tutorial](https://github.com/mar10/jquery-ui-contextmenu/wiki) and
93+
* [Read the Tutorial](https://github.com/mar10/jquery-ui-contextmenu/wiki) and
9494
[API Reference](https://github.com/mar10/jquery-ui-contextmenu/wiki/ApiRef)
9595
* Have a look at the [Live demo page](http://wwwendt.de/tech/demo/jquery-contextmenu/demo/)
9696
* Ask questions on [Stackoverflow](http://stackoverflow.com/questions/tagged/jquery-ui-contextmenu)
97-
* Play with [jsFiddle](http://jsfiddle.net/mar10/6o3u8a88/) or
97+
* Play with [jsFiddle](http://jsfiddle.net/mar10/6o3u8a88/) or
9898
[Plunker](http://plnkr.co/edit/Bbcoqy?p=preview)
9999

100100

demo/index-1-12.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,14 @@
141141
// .contextmenu("replaceMenu", [{title: "aaa"}, {title: "bbb"}])
142142
// .contextmenu("replaceMenu", "#options2")
143143
// .contextmenu("setEntry", "cut", {title: "Cuty", uiIcon: "ui-icon-heart", disabled: true})
144-
.contextmenu("setEntry", "copy", "Copy '" + $target.text() + "'")
145-
.contextmenu("setEntry", "paste", "Paste" + (CLIPBOARD ? " '" + CLIPBOARD + "'" : ""))
144+
.contextmenu("setTitle", "copy", "Copy '" + $target.text() + "'")
145+
.contextmenu("setTitle", "paste", "Paste" + (CLIPBOARD ? " '" + CLIPBOARD + "'" : ""))
146146
.contextmenu("enableEntry", "paste", (CLIPBOARD !== ""));
147147

148+
// $(document)
149+
// .contextmenu("setIcon", "paste", "ui-icon-heart")
150+
// .contextmenu("setTooltip", "paste", "ui-icon-heart");
151+
148152
// Optionally return false, to prevent opening the menu now
149153
}
150154
});

jquery.ui-contextmenu.js

Lines changed: 98 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,18 @@ $.widget("moogle.contextmenu", {
382382
enableEntry: function(cmd, flag) {
383383
this._getMenuEntry(cmd).toggleClass("ui-state-disabled", (flag === false));
384384
},
385+
/** Return ui-menu entry (LI tag) as jQuery object. */
386+
getEntry: function(cmd) {
387+
return this._getMenuEntry(cmd);
388+
},
389+
/** Return ui-menu entry wrapper as jQuery object.
390+
UI 1.10: this is the <a> tag inside the LI
391+
UI 1.11: this is the LI istself
392+
UI 1.12: this is the <div> tag inside the LI
393+
*/
394+
getEntryWrapper: function(cmd) {
395+
return this._getMenuEntry(cmd).find(">[role=menuitem]").addBack("[role=menuitem]");
396+
},
385397
/** Return Menu element (UL). */
386398
getMenu: function() {
387399
return this.$menu;
@@ -414,29 +426,97 @@ $.widget("moogle.contextmenu", {
414426
replaceMenu: function(data) {
415427
this._createUiMenu(data);
416428
},
417-
/** Redefine menu entry (title or all of it). */
429+
/** Redefine a whole menu entry. */
418430
setEntry: function(cmd, entry) {
419431
var $ul,
420432
$entryLi = this._getMenuEntry(cmd);
421433

422434
if (typeof entry === "string") {
423-
$.moogle.contextmenu.updateTitle($entryLi, entry);
424-
} else {
425-
$entryLi.empty();
426-
entry.cmd = entry.cmd || cmd;
427-
$.moogle.contextmenu.createEntryMarkup(entry, $entryLi);
428-
if ($.isArray(entry.children)) {
429-
$ul = $("<ul/>").appendTo($entryLi);
430-
$.moogle.contextmenu.createMenuMarkup(entry.children, $ul);
431-
}
432-
// #110: jQuery UI 1.12: refresh only works when this class is not set:
433-
$entryLi.removeClass("ui-menu-item");
434-
this.getMenu().menu("refresh");
435+
window.console && window.console.warn(
436+
"setEntry(cmd, t) with a plain string title is deprecated since v1.18." +
437+
"Use setTitle(cmd, '" + entry + "') instead.");
438+
return this.setTitle(cmd, entry);
439+
}
440+
$entryLi.empty();
441+
entry.cmd = entry.cmd || cmd;
442+
$.moogle.contextmenu.createEntryMarkup(entry, $entryLi);
443+
if ($.isArray(entry.children)) {
444+
$ul = $("<ul/>").appendTo($entryLi);
445+
$.moogle.contextmenu.createMenuMarkup(entry.children, $ul);
435446
}
447+
// #110: jQuery UI 1.12: refresh only works when this class is not set:
448+
$entryLi.removeClass("ui-menu-item");
449+
this.getMenu().menu("refresh");
450+
},
451+
/** Set icon (pass null to remove). */
452+
setIcon: function(cmd, icon) {
453+
return this.updateEntry(cmd, { uiIcon: icon });
436454
},
455+
/** Set title. */
456+
setTitle: function(cmd, title) {
457+
return this.updateEntry(cmd, { title: title });
458+
},
459+
// /** Set tooltip (pass null to remove). */
460+
// setTooltip: function(cmd, tooltip) {
461+
// this._getMenuEntry(cmd).attr("title", tooltip);
462+
// },
437463
/** Show or hide the menu command. */
438464
showEntry: function(cmd, flag) {
439465
this._getMenuEntry(cmd).toggle(flag !== false);
466+
},
467+
/** Redefine selective attributes of a menu entry. */
468+
updateEntry: function(cmd, entry) {
469+
var $icon, $wrapper,
470+
$entryLi = this._getMenuEntry(cmd);
471+
472+
if ( entry.title !== undefined ) {
473+
$.moogle.contextmenu.updateTitle($entryLi, "" + entry.title);
474+
}
475+
if ( entry.tooltip !== undefined ) {
476+
if ( entry.tooltip === null ) {
477+
$entryLi.removeAttr("title");
478+
} else {
479+
$entryLi.attr("title", entry.tooltip);
480+
}
481+
}
482+
if ( entry.uiIcon !== undefined ) {
483+
$wrapper = this.getEntryWrapper(cmd),
484+
$icon = $wrapper.find("span.ui-icon").not(".ui-menu-icon");
485+
$icon.remove();
486+
if ( entry.uiIcon ) {
487+
$wrapper.append($("<span class='ui-icon' />").addClass(entry.uiIcon));
488+
}
489+
}
490+
if ( entry.hide !== undefined ) {
491+
$entryLi.toggle(!entry.hide);
492+
} else if ( entry.show !== undefined ) {
493+
// Note: `show` is an undocumented variant. `hide: false` is preferred
494+
$entryLi.toggle(!!entry.show);
495+
}
496+
// if ( entry.isHeader !== undefined ) {
497+
// $entryLi.toggleClass("ui-widget-header", !!entry.isHeader);
498+
// }
499+
if ( entry.data !== undefined ) {
500+
$entryLi.data(entry.data);
501+
}
502+
503+
// Set/clear class names, but handle ui-state-disabled separately
504+
if ( entry.disabled === undefined ) {
505+
entry.disabled = $entryLi.hasClass("ui-state-disabled");
506+
}
507+
if ( entry.setClass ) {
508+
if ( $entryLi.hasClass("ui-menu-item") ) {
509+
entry.setClass += " ui-menu-item";
510+
}
511+
$entryLi.removeClass();
512+
$entryLi.addClass(entry.setClass);
513+
} else if ( entry.addClass ) {
514+
$entryLi.addClass(entry.addClass);
515+
}
516+
$entryLi.toggleClass("ui-state-disabled", !!entry.disabled);
517+
// // #110: jQuery UI 1.12: refresh only works when this class is not set:
518+
// $entryLi.removeClass("ui-menu-item");
519+
// this.getMenu().menu("refresh");
440520
}
441521
});
442522

@@ -461,27 +541,19 @@ $.extend($.moogle.contextmenu, {
461541
href: "#"
462542
}).appendTo($parentLi);
463543

464-
if ( entry.uiIcon ) {
465-
$wrapper.append($("<span class='ui-icon' />").addClass(entry.uiIcon));
466-
}
467-
468544
} else if ( isLTE111 ) {
469-
// jQuery UI Menu 1.11 preferes to avoid `<a>` tags
545+
// jQuery UI Menu 1.11 preferes to avoid `<a>` tags or <div> wrapper
470546
$parentLi.html("" + entry.title);
471-
if ( entry.uiIcon ) {
472-
$parentLi
473-
.append($("<span class='ui-icon' />")
474-
.addClass(entry.uiIcon));
475-
}
547+
$wrapper = $parentLi;
476548

477549
} else {
478550
// jQuery UI Menu 1.12 introduced `<div>` wrappers
479551
$wrapper = $("<div/>", {
480552
html: "" + entry.title
481553
}).appendTo($parentLi);
482-
if ( entry.uiIcon ) {
483-
$wrapper.append($("<span class='ui-icon' />").addClass(entry.uiIcon));
484-
}
554+
}
555+
if ( entry.uiIcon ) {
556+
$wrapper.append($("<span class='ui-icon' />").addClass(entry.uiIcon));
485557
}
486558
// Store option callbacks in entry's data
487559
$.each( [ "action", "disabled", "title", "tooltip" ], function(i, attr) {

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "ui-contextmenu",
33
"filename": "jquery.ui-contextmenu.min.js",
44
"main": "jquery.ui-contextmenu.js",
5-
"version": "1.17.1-0",
5+
"version": "1.18.0-0",
66
"title": "jQuery UI context menu plugin",
77
"description": "Turn a jQuery UI Menu widget into a contextmenu.",
88
"keywords": [
@@ -69,4 +69,4 @@
6969
]
7070
}
7171
]
72-
}
72+
}

0 commit comments

Comments
 (0)