|
19 | 19 | $.widget("ui.contextmenu", { |
20 | 20 | version: "0.4.0", |
21 | 21 | options: { |
22 | | - delegate: "[data-menu]", // selector |
| 22 | + delegate: "[data-menu]", // selector |
23 | 23 | hide: { effect: "fadeOut", duration: "fast"}, |
24 | 24 | show: { effect: "slideDown", duration: "slow"}, |
| 25 | + position: null, // specify positional preferences (added for issue #18 and #13). |
25 | 26 | ignoreParentSelect: true, // Don't trigger 'select' for sub-menu parents |
26 | 27 | menu: null, // selector or jQuery or a function returning such |
27 | 28 | preventSelect: false, // disable text selection of target |
28 | | - taphold: false, // open menu on taphold events (requires external plugins) |
| 29 | + taphold: false, // open menu on taphold events (requires external plugins) |
29 | 30 | // Events: |
30 | | - beforeOpen: $.noop, // menu about to open; return `false` to prevent opening |
31 | | - blur: $.noop, // menu option lost focus |
32 | | - close: $.noop, // menu was closed |
33 | | - create: $.noop, // menu was initialized |
34 | | - focus: $.noop, // menu option got focus |
35 | | - init: $.noop, // ui-contextmenu was initialized |
36 | | - open: $.noop, // menu was opened |
37 | | - select: $.noop // menu option was selected; return `false` to prevent closing |
| 31 | + beforeOpen: $.noop, // menu about to open; return `false` to prevent opening |
| 32 | + blur: $.noop, // menu option lost focus |
| 33 | + close: $.noop, // menu was closed |
| 34 | + create: $.noop, // menu was initialized |
| 35 | + focus: $.noop, // menu option got focus |
| 36 | + init: $.noop, // ui-contextmenu was initialized |
| 37 | + open: $.noop, // menu was opened |
| 38 | + select: $.noop // menu option was selected; return `false` to prevent closing |
38 | 39 | }, |
39 | 40 | /** Construtcor */ |
40 | 41 | _create: function () { |
|
122 | 123 | }, |
123 | 124 | /** Open popup (called on 'contextmenu' event). */ |
124 | 125 | _openMenu: function(event){ |
125 | | - var self = this, |
| 126 | + var opts = this.options, |
| 127 | + posOption = opts.position, |
| 128 | + self = this, |
126 | 129 | $menu = this._getMenu(), |
127 | 130 | openEvent = event, |
128 | 131 | // if called by 'open' method, 'relatedTarget' is the requested target object |
|
150 | 153 | self._closeMenu(); |
151 | 154 | } |
152 | 155 | }); |
| 156 | + |
| 157 | + // required for custom positioning (issue #18 and #13). |
| 158 | + if ($.isFunction(posOption)) { |
| 159 | + posOption = posOption(event, ui); |
| 160 | + } |
| 161 | + posOption = $.extend({ |
| 162 | + my: "left top", |
| 163 | + at: "left bottom", |
| 164 | + of: parentTarget, |
| 165 | + collision: "fit" |
| 166 | + }, posOption); |
| 167 | + |
153 | 168 | // Finally display the popup |
154 | 169 | $menu |
155 | 170 | .show() // required to fix positioning error (issue #3) |
156 | 171 | .css({ |
157 | 172 | position: "absolute", |
158 | 173 | left: 0, |
159 | 174 | top: 0 |
160 | | - }).position({ |
161 | | - my: "left top", |
162 | | - at: "left bottom", |
163 | | - of: parentTarget, |
164 | | - collision: "fit" |
165 | | - }).hide(); |
| 175 | + }).position(posOption).hide(); |
166 | 176 |
|
167 | 177 | this._show($menu, this.options.show, function(){ |
168 | 178 | self._trigger.call(self, "open", event); |
|
227 | 237 | $menu.menu("refresh"); |
228 | 238 | }else{ |
229 | 239 | $.error("not implemented"); |
230 | | -// this.orgMenu = opts.menu; |
231 | | -// opts.menu = $.ui.contextmenu.createMenuMarkup(data); |
| 240 | +// this.orgMenu = opts.menu; |
| 241 | +// opts.menu = $.ui.contextmenu.createMenuMarkup(data); |
232 | 242 | } |
233 | 243 | }else{ |
234 | | -// if(this.orgMenu){ |
235 | | -// // re-use existing temporary <ul> |
236 | | -// }else{ |
237 | | -// } |
238 | | -// $menu.menu("option", "menu", opts.menu); |
| 244 | +// if(this.orgMenu){ |
| 245 | +// // re-use existing temporary <ul> |
| 246 | +// }else{ |
| 247 | +// } |
| 248 | +// $menu.menu("option", "menu", opts.menu); |
239 | 249 | $.error("not implemented"); |
240 | 250 | } |
241 | 251 | }, |
|
0 commit comments