9
9
*/
10
10
; ( function ( $ , window , document , undefined ) {
11
11
"use strict" ;
12
- var supportSelectstart = "onselectstart" in document . createElement ( "div" ) ;
12
+ var supportSelectstart = "onselectstart" in document . createElement ( "div" ) ,
13
+ match , uiVersion ;
13
14
14
15
$ . widget ( "moogle.contextmenu" , {
15
16
version : "1.4.0-1" ,
135
136
select : $ . proxy ( function ( event , ui ) {
136
137
// User selected a menu entry
137
138
var retval ,
138
- isParent = ( ui . item . has ( ">a[aria-haspopup='true']" ) . length > 0 ) ,
139
+ isParent = $ . moogle . contextmenu . isMenu ( ui . item ) ,
139
140
actionHandler = ui . item . data ( "actionHandler" ) ;
140
141
ui . cmd = ui . item . attr ( "data-command" ) ;
141
142
ui . target = $ ( this . currentTarget ) ;
245
246
}
246
247
$ . Widget . prototype . _setOption . apply ( this , arguments ) ;
247
248
} ,
248
- /** Return ui-menu entry (<A> or <LI> tag). */
249
- _getMenuEntry : function ( cmd , wantLi ) {
250
- var $entry = this . $menu . find ( "li[data-command=" + cmd + "] a" ) ;
251
- return wantLi ? $entry . closest ( "li" ) : $entry ;
249
+ /** Return ui-menu entry (<LI> tag). */
250
+ _getMenuEntry : function ( cmd ) {
251
+ return this . $menu . find ( "li[data-command=" + cmd + "]" ) ;
252
252
} ,
253
253
/** Close context menu. */
254
254
close : function ( ) {
258
258
} ,
259
259
/** Enable or disable the menu command. */
260
260
enableEntry : function ( cmd , flag ) {
261
- this . _getMenuEntry ( cmd , true ) . toggleClass ( "ui-state-disabled" , ( flag === false ) ) ;
261
+ this . _getMenuEntry ( cmd ) . toggleClass ( "ui-state-disabled" , ( flag === false ) ) ;
262
262
} ,
263
263
/** Return Menu element (UL). */
264
264
getMenu : function ( ) {
284
284
} ,
285
285
/** Redefine menu entry (title or all of it). */
286
286
setEntry : function ( cmd , titleOrData ) {
287
- var $parent ,
288
- $entry = this . _getMenuEntry ( cmd , false ) ;
287
+ var $entry = this . _getMenuEntry ( cmd ) ;
289
288
290
289
if ( typeof titleOrData === "string" ) {
291
- if ( $entry . children ( "span" ) . length ) {
292
- // Replace <a> text without removing <span> child
293
- $entry
294
- . contents ( )
295
- . filter ( function ( ) { return this . nodeType === 3 ; } )
296
- . first ( )
297
- . replaceWith ( titleOrData ) ;
298
- } else {
299
- // <a> tag only contains text (above code doesn't work here)
300
- $entry . text ( titleOrData ) ;
301
- }
290
+ $ . moogle . contextmenu . updateTitle ( $entry , titleOrData ) ;
302
291
} else {
303
- $parent = $ entry. closest ( "li" ) . empty ( ) ;
292
+ $entry . empty ( ) ;
304
293
titleOrData . cmd = titleOrData . cmd || cmd ;
305
- $ . moogle . contextmenu . createEntryMarkup ( titleOrData , $parent ) ;
294
+ $ . moogle . contextmenu . createEntryMarkup ( titleOrData , $entry ) ;
306
295
}
307
296
} ,
308
297
/** Show or hide the menu command. */
309
298
showEntry : function ( cmd , flag ) {
310
- this . _getMenuEntry ( cmd , true ) . toggle ( flag !== false ) ;
299
+ this . _getMenuEntry ( cmd ) . toggle ( flag !== false ) ;
311
300
}
312
301
} ) ;
313
302
314
303
/*
315
304
* Global functions
316
305
*/
317
306
$ . extend ( $ . moogle . contextmenu , {
318
- /** Convert a menu description into a into a <li> content. */
319
- createEntryMarkup : function ( entry , $parentLi ) {
320
- var $a = null ;
321
-
322
- // if(entry.title.match(/^---/)){
323
- if ( ! / [ ^ \- \u2014 \u2013 \s ] / . test ( entry . title ) ) {
324
- // hyphen, em dash, en dash: separator as defined by UI Menu 1.10
325
- $parentLi . text ( entry . title ) ;
326
- } else {
327
- $parentLi . attr ( "data-command" , entry . cmd ) ;
328
- $a = $ ( "<a/>" , {
329
- // text: "" + entry.title,
330
- html : "" + entry . title , // allow to pass HTML markup
331
- href : "#"
332
- } ) . appendTo ( $parentLi ) ;
333
- if ( $ . isFunction ( entry . action ) ) {
334
- $parentLi . data ( "actionHandler" , entry . action ) ;
335
- }
336
- if ( entry . uiIcon ) {
337
- $a . append ( $ ( "<span class='ui-icon' />" ) . addClass ( entry . uiIcon ) ) ;
338
- }
339
- if ( entry . disabled ) {
340
- $parentLi . addClass ( "ui-state-disabled" ) ;
341
- }
342
- if ( $ . isPlainObject ( entry . data ) ) {
343
- $a . data ( entry . data ) ;
344
- }
345
- }
346
- return $a ;
347
- } ,
348
307
/** Convert a nested array of command objects into a <ul> structure. */
349
308
createMenuMarkup : function ( options , $parentUl ) {
350
309
var i , menu , $ul , $li ;
@@ -363,7 +322,98 @@ $.extend($.moogle.contextmenu, {
363
322
}
364
323
}
365
324
return $parentUl ;
325
+ } ,
326
+ /** Replaces the value of elem's first text node child*/
327
+ replaceFirstTextNodeChild : function ( elem , text ) {
328
+ elem
329
+ . contents ( )
330
+ . filter ( function ( ) { return this . nodeType === 3 ; } )
331
+ . first ( )
332
+ . replaceWith ( text ) ;
366
333
}
367
334
} ) ;
368
335
336
+ match = $ . ui . menu . version . match ( / ^ ( \d ) \. ( \d + ) .* $ / ) ;
337
+
338
+ uiVersion = {
339
+ major : parseInt ( match [ 1 ] , 10 ) ,
340
+ minor : parseInt ( match [ 2 ] , 10 )
341
+ } ;
342
+
343
+ if ( uiVersion . major < 3 && uiVersion . minor < 11 ) {
344
+ $ . extend ( $ . moogle . contextmenu , {
345
+ /** Convert a menu description into a into a <li> content. */
346
+ createEntryMarkup : function ( entry , $parentLi ) {
347
+ var $a = null ;
348
+
349
+ // if(entry.title.match(/^---/)){
350
+ if ( ! / [ ^ \- \u2014 \u2013 \s ] / . test ( entry . title ) ) {
351
+ // hyphen, em dash, en dash: separator as defined by UI Menu 1.10
352
+ $parentLi . text ( entry . title ) ;
353
+ } else {
354
+ $parentLi . attr ( "data-command" , entry . cmd ) ;
355
+ $a = $ ( "<a/>" , {
356
+ html : "" + entry . title , // allow to pass HTML markup
357
+ href : "#"
358
+ } ) . appendTo ( $parentLi ) ;
359
+ if ( $ . isFunction ( entry . action ) ) {
360
+ $parentLi . data ( "actionHandler" , entry . action ) ;
361
+ }
362
+ if ( entry . uiIcon ) {
363
+ $a . append ( $ ( "<span class='ui-icon' />" ) . addClass ( entry . uiIcon ) ) ;
364
+ }
365
+ if ( entry . disabled ) {
366
+ $parentLi . addClass ( "ui-state-disabled" ) ;
367
+ }
368
+ if ( $ . isPlainObject ( entry . data ) ) {
369
+ $a . data ( entry . data ) ;
370
+ }
371
+ }
372
+ } ,
373
+ /** Returns true if the menu item has child menu items */
374
+ isMenu : function ( item ) {
375
+ return item . has ( ">a[aria-haspopup='true']" ) . length > 0 ;
376
+ } ,
377
+ /** Updates the menu item's title */
378
+ updateTitle : function ( item , title ) {
379
+ $ . moogle . contextmenu . replaceFirstTextNodeChild ( $ ( "a" , item ) , title ) ;
380
+ }
381
+ } ) ;
382
+ } else {
383
+ $ . extend ( $ . moogle . contextmenu , {
384
+ /** Convert a menu description into a into a <li> content. */
385
+ createEntryMarkup : function ( entry , $parentLi ) {
386
+ if ( ! / [ ^ \- \u2014 \u2013 \s ] / . test ( entry . title ) ) {
387
+ $parentLi . text ( entry . title ) ;
388
+ } else {
389
+ $parentLi
390
+ . attr ( "data-command" , entry . cmd )
391
+ . html ( "" + entry . title ) ;
392
+ if ( $ . isFunction ( entry . action ) ) {
393
+ $parentLi . data ( "actionHandler" , entry . action ) ;
394
+ }
395
+ if ( entry . uiIcon ) {
396
+ $parentLi
397
+ . append ( $ ( "<span class='ui-icon' />" )
398
+ . addClass ( entry . uiIcon ) ) ;
399
+ }
400
+ if ( entry . disabled ) {
401
+ $parentLi . addClass ( "ui-state-disabled" ) ;
402
+ }
403
+ if ( $ . isPlainObject ( entry . data ) ) {
404
+ $parentLi . data ( entry . data ) ;
405
+ }
406
+ }
407
+ } ,
408
+ /** Returns true if the menu item has child menu items */
409
+ isMenu : function ( item ) {
410
+ return item . is ( "[aria-haspopup='true']" ) ;
411
+ } ,
412
+ /** Updates the menu item's title */
413
+ updateTitle : function ( item , title ) {
414
+ $ . moogle . contextmenu . replaceFirstTextNodeChild ( item , title ) ;
415
+ }
416
+ } ) ;
417
+ }
418
+
369
419
} ( jQuery , window , document ) ) ;
0 commit comments