9
9
*/
10
10
; ( function ( $ , window , document , undefined ) {
11
11
"use strict" ;
12
- var supportSelectstart = "onselectstart" in document . createElement ( "div" ) ;
13
-
14
- /** Return command without leading '#' (default to ""). */
15
- function normCommand ( cmd ) {
16
- return ( cmd && cmd . match ( / ^ # / ) ) ? cmd . substring ( 1 ) : ( cmd || "" ) ;
17
- }
18
-
12
+ var supportSelectstart = "onselectstart" in document . createElement ( "div" ) ,
13
+ match , uiVersion ;
19
14
20
15
$ . widget ( "moogle.contextmenu" , {
21
16
version : "1.4.0-1" ,
141
136
select : $ . proxy ( function ( event , ui ) {
142
137
// User selected a menu entry
143
138
var retval ,
144
- isParent = ( ui . item . has ( ">a[aria-haspopup='true']" ) . length > 0 ) ,
145
- $a = ui . item . find ( ">a" ) ,
146
- actionHandler = $a . data ( "actionHandler" ) ;
147
- ui . cmd = normCommand ( $a . attr ( "href" ) ) ;
139
+ isParent = $ . moogle . contextmenu . isMenu ( ui . item ) ,
140
+ actionHandler = ui . item . data ( "actionHandler" ) ;
141
+ ui . cmd = ui . item . attr ( "data-command" ) ;
148
142
ui . target = $ ( this . currentTarget ) ;
149
143
// ignore clicks, if they only open a sub-menu
150
144
if ( ! isParent || ! this . options . ignoreParentSelect ) {
252
246
}
253
247
$ . Widget . prototype . _setOption . apply ( this , arguments ) ;
254
248
} ,
255
- /** Return ui-menu entry (<A> or <LI> tag). */
256
- _getMenuEntry : function ( cmd , wantLi ) {
257
- var $entry = this . $menu . find ( "li a[href=#" + normCommand ( cmd ) + "]" ) ;
258
- 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 + "]" ) ;
259
252
} ,
260
253
/** Close context menu. */
261
254
close : function ( ) {
265
258
} ,
266
259
/** Enable or disable the menu command. */
267
260
enableEntry : function ( cmd , flag ) {
268
- this . _getMenuEntry ( cmd , true ) . toggleClass ( "ui-state-disabled" , ( flag === false ) ) ;
261
+ this . _getMenuEntry ( cmd ) . toggleClass ( "ui-state-disabled" , ( flag === false ) ) ;
269
262
} ,
270
263
/** Return Menu element (UL). */
271
264
getMenu : function ( ) {
291
284
} ,
292
285
/** Redefine menu entry (title or all of it). */
293
286
setEntry : function ( cmd , titleOrData ) {
294
- var $parent ,
295
- $entry = this . _getMenuEntry ( cmd , false ) ;
287
+ var $entry = this . _getMenuEntry ( cmd ) ;
296
288
297
289
if ( typeof titleOrData === "string" ) {
298
- if ( $entry . children ( "span" ) . length ) {
299
- // Replace <a> text without removing <span> child
300
- $entry
301
- . contents ( )
302
- . filter ( function ( ) { return this . nodeType === 3 ; } )
303
- . first ( )
304
- . replaceWith ( titleOrData ) ;
305
- } else {
306
- // <a> tag only contains text (above code doesn't work here)
307
- $entry . text ( titleOrData ) ;
308
- }
290
+ $ . moogle . contextmenu . updateTitle ( $entry , titleOrData ) ;
309
291
} else {
310
- $parent = $ entry. closest ( "li" ) . empty ( ) ;
292
+ $entry . empty ( ) ;
311
293
titleOrData . cmd = titleOrData . cmd || cmd ;
312
- $ . moogle . contextmenu . createEntryMarkup ( titleOrData , $parent ) ;
294
+ $ . moogle . contextmenu . createEntryMarkup ( titleOrData , $entry ) ;
313
295
}
314
296
} ,
315
297
/** Show or hide the menu command. */
316
298
showEntry : function ( cmd , flag ) {
317
- this . _getMenuEntry ( cmd , true ) . toggle ( flag !== false ) ;
299
+ this . _getMenuEntry ( cmd ) . toggle ( flag !== false ) ;
318
300
}
319
301
} ) ;
320
302
321
303
/*
322
304
* Global functions
323
305
*/
324
306
$ . extend ( $ . moogle . contextmenu , {
325
- /** Convert a menu description into a into a <li> content. */
326
- createEntryMarkup : function ( entry , $parentLi ) {
327
- var $a = null ;
328
-
329
- // if(entry.title.match(/^---/)){
330
- if ( ! / [ ^ \- \u2014 \u2013 \s ] / . test ( entry . title ) ) {
331
- // hyphen, em dash, en dash: separator as defined by UI Menu 1.10
332
- $parentLi . text ( entry . title ) ;
333
- } else {
334
- $a = $ ( "<a/>" , {
335
- // text: "" + entry.title,
336
- html : "" + entry . title , // allow to pass HTML markup
337
- href : "#" + normCommand ( entry . cmd )
338
- } ) . appendTo ( $parentLi ) ;
339
- if ( $ . isFunction ( entry . action ) ) {
340
- $a . data ( "actionHandler" , entry . action ) ;
341
- }
342
- if ( entry . uiIcon ) {
343
- $a . append ( $ ( "<span class='ui-icon' />" ) . addClass ( entry . uiIcon ) ) ;
344
- }
345
- if ( entry . disabled ) {
346
- $parentLi . addClass ( "ui-state-disabled" ) ;
347
- }
348
- if ( $ . isPlainObject ( entry . data ) ) {
349
- $a . data ( entry . data ) ;
350
- }
351
- }
352
- return $a ;
353
- } ,
354
307
/** Convert a nested array of command objects into a <ul> structure. */
355
308
createMenuMarkup : function ( options , $parentUl ) {
356
309
var i , menu , $ul , $li ;
@@ -369,7 +322,98 @@ $.extend($.moogle.contextmenu, {
369
322
}
370
323
}
371
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 ) ;
372
333
}
373
334
} ) ;
374
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 < 2 && 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
+
375
419
} ( jQuery , window , document ) ) ;
0 commit comments