@@ -382,6 +382,18 @@ $.widget("moogle.contextmenu", {
382
382
enableEntry : function ( cmd , flag ) {
383
383
this . _getMenuEntry ( cmd ) . toggleClass ( "ui-state-disabled" , ( flag === false ) ) ;
384
384
} ,
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
+ } ,
385
397
/** Return Menu element (UL). */
386
398
getMenu : function ( ) {
387
399
return this . $menu ;
@@ -414,29 +426,97 @@ $.widget("moogle.contextmenu", {
414
426
replaceMenu : function ( data ) {
415
427
this . _createUiMenu ( data ) ;
416
428
} ,
417
- /** Redefine menu entry (title or all of it) . */
429
+ /** Redefine a whole menu entry . */
418
430
setEntry : function ( cmd , entry ) {
419
431
var $ul ,
420
432
$entryLi = this . _getMenuEntry ( cmd ) ;
421
433
422
434
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 ) ;
435
446
}
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 } ) ;
436
454
} ,
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
+ // },
437
463
/** Show or hide the menu command. */
438
464
showEntry : function ( cmd , flag ) {
439
465
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");
440
520
}
441
521
} ) ;
442
522
@@ -461,27 +541,19 @@ $.extend($.moogle.contextmenu, {
461
541
href : "#"
462
542
} ) . appendTo ( $parentLi ) ;
463
543
464
- if ( entry . uiIcon ) {
465
- $wrapper . append ( $ ( "<span class='ui-icon' />" ) . addClass ( entry . uiIcon ) ) ;
466
- }
467
-
468
544
} 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
470
546
$parentLi . html ( "" + entry . title ) ;
471
- if ( entry . uiIcon ) {
472
- $parentLi
473
- . append ( $ ( "<span class='ui-icon' />" )
474
- . addClass ( entry . uiIcon ) ) ;
475
- }
547
+ $wrapper = $parentLi ;
476
548
477
549
} else {
478
550
// jQuery UI Menu 1.12 introduced `<div>` wrappers
479
551
$wrapper = $ ( "<div/>" , {
480
552
html : "" + entry . title
481
553
} ) . 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 ) ) ;
485
557
}
486
558
// Store option callbacks in entry's data
487
559
$ . each ( [ "action" , "disabled" , "title" , "tooltip" ] , function ( i , attr ) {
0 commit comments