|
41 | 41 | open: $.noop, // menu was opened |
42 | 42 | select: $.noop // menu option was selected; return `false` to prevent closing |
43 | 43 | }, |
44 | | - /** |
45 | | - * |
46 | | - */ |
| 44 | + /** Contrutcor */ |
47 | 45 | _create: function () { |
48 | 46 | var opts = this.options, |
49 | 47 | eventNames = "contextmenu" + NS, |
|
75 | 73 | }); |
76 | 74 | } |
77 | 75 | } |
| 76 | + // If a menu definition array was passed, create a hidden <ul> |
| 77 | + // and generate the structure now |
78 | 78 | if($.isArray(opts.menu)){ |
79 | 79 | this.orgMenu = opts.menu; |
80 | 80 | opts.menu = $.ui.contextmenu.createMenuMarkup(opts.menu); |
81 | 81 | } |
82 | | - // Create - but hide - context-menu |
| 82 | + // Create - but hide - the jQuery UI Menu |
83 | 83 | this._getMenu() |
84 | 84 | .hide() |
85 | 85 | .addClass("ui-contextmenu") |
|
105 | 105 | // emulate a 'taphold' event |
106 | 106 | this._trigger("init"); |
107 | 107 | }, |
108 | | - /** |
109 | | - * |
110 | | - */ |
| 108 | + /** Destructor, called on $().contextmenu("destroy"). */ |
111 | 109 | _destroy: function(key, value){ |
112 | 110 | if(this.$headStyle){ |
113 | 111 | this.$headStyle.remove(); |
114 | 112 | this.$headStyle = null; |
115 | 113 | } |
| 114 | + // Remove temporary <ul> if any |
116 | 115 | if(this.orgMenu){ |
117 | 116 | this.options.menu.remove(); |
118 | 117 | this.options.menu = this.orgMenu; |
119 | 118 | this.orgMenu = null; |
120 | 119 | } |
121 | 120 | }, |
122 | | - /** |
123 | | - * Handle $().contextmenu("option", ...) calls. |
124 | | - */ |
125 | | - _setOption: function(key, value){ |
126 | | - $.Widget.prototype._setOption.apply(this, arguments); |
127 | | - }, |
128 | | - /** Return ui-menu root element as jQuery object. */ |
129 | | - _getMenu: function(){ |
130 | | - // this.options.menu may be a string, jQuery or a function returning that. |
131 | | - var $menu = this.options.menu; |
132 | | - if( $.isFunction($menu) ){ |
133 | | - $menu = $menu(); |
134 | | - } |
135 | | - return (typeof $menu === "string") ? $($menu) : $menu; |
136 | | - }, |
137 | | - /** Return ui-menu widget instance (works on pre and post jQueryUI 1.9). */ |
138 | | -// _getMenuWidget: function(){ |
139 | | -// var $menu = this._getMenu(); |
140 | | -// return $menu.data("ui-menu") || $menu.data("menu"); |
141 | | -// }, |
142 | | - /** Open dropdown. */ |
| 121 | + /** Open popup (called on 'contextmenu' event). */ |
143 | 122 | _openMenu: function(event){ |
144 | 123 | var self = this, |
145 | 124 | $menu = this._getMenu(), |
|
169 | 148 | self._closeMenu(); |
170 | 149 | } |
171 | 150 | }); |
| 151 | + // Finally display the popup |
172 | 152 | $menu |
173 | 153 | .show() // required to fix positioning error (issue #3) |
174 | 154 | .css({ |
|
185 | 165 | self._trigger.call(self, "open", event); |
186 | 166 | }); |
187 | 167 | }, |
188 | | - /** Close dropdown. */ |
| 168 | + /** Close popup. */ |
189 | 169 | _closeMenu: function(){ |
190 | 170 | var self = this, |
191 | 171 | $menu = this._getMenu(); |
|
198 | 178 | .unbind("touchstart" + NS) |
199 | 179 | .unbind("keydown" + NS); |
200 | 180 | }, |
201 | | - /** |
202 | | - * Open context menu on a specific target (must match options.delegate) |
203 | | - */ |
| 181 | + /** Handle $().contextmenu("option", key, value) calls. */ |
| 182 | + _setOption: function(key, value){ |
| 183 | +// var opts = this.options, |
| 184 | +// $menu = this._getMenu(); |
| 185 | + |
| 186 | + switch(key){ |
| 187 | + case "menu": |
| 188 | + this.replaceMenu(value); |
| 189 | + break; |
| 190 | + } |
| 191 | + $.Widget.prototype._setOption.apply(this, arguments); |
| 192 | + }, |
| 193 | + /** Return ui-menu root element as jQuery object. */ |
| 194 | + _getMenu: function(){ |
| 195 | + // this.options.menu may be a string, jQuery or a function returning that. |
| 196 | + var $menu = this.options.menu; |
| 197 | +// if( $.isFunction($menu) ){ |
| 198 | +// $menu = $menu(); |
| 199 | +// } |
| 200 | + return (typeof $menu === "string") ? $($menu) : $menu; |
| 201 | + }, |
| 202 | + /** Open context menu on a specific target (must match options.delegate) */ |
204 | 203 | open: function(target){ |
205 | 204 | // Fake a 'contextmenu' event |
206 | 205 | var e = jQuery.Event("contextmenu", {target: target.get(0)}); |
|
212 | 211 | }, |
213 | 212 | /** Enable or disable the menu command. */ |
214 | 213 | enableEntry: function(cmd, flag){ |
| 214 | + // TODO: should be $menu.find(...)! |
215 | 215 | var $entry = this.element.find("a[href=#" + normCommand(cmd) + "]"); |
216 | 216 | $entry.toggleClass("ui-state-disabled", (flag === false)); |
217 | 217 | }, |
218 | 218 | /** Redefine the whole menu. */ |
219 | | - replaceMenu: function(menu){ |
220 | | - // TODO: $menu.refresh() if menu was modified |
221 | | - $.error("not implemented"); |
| 219 | + replaceMenu: function(data){ |
| 220 | +// return this.element.contextmenu("option", "menu", menu); |
| 221 | + var opts = this.options, |
| 222 | + $menu = this._getMenu(); |
| 223 | + |
| 224 | + if($.isArray(data)){ |
| 225 | + if(this.orgMenu){ |
| 226 | + // re-use existing temporary <ul> |
| 227 | + $menu.empty(); |
| 228 | + $.ui.contextmenu.createMenuMarkup(data, opts.menu); |
| 229 | + $menu.menu("refresh"); |
| 230 | + }else{ |
| 231 | + $.error("not implemented"); |
| 232 | +// this.orgMenu = opts.menu; |
| 233 | +// opts.menu = $.ui.contextmenu.createMenuMarkup(data); |
| 234 | + } |
| 235 | + }else{ |
| 236 | +// if(this.orgMenu){ |
| 237 | +// // re-use existing temporary <ul> |
| 238 | +// }else{ |
| 239 | +// } |
| 240 | +// $menu.menu("option", "menu", opts.menu); |
| 241 | + $.error("not implemented"); |
| 242 | + } |
222 | 243 | }, |
223 | 244 | /** Redefine menu entry (title or all of it). */ |
224 | 245 | setEntry: function(cmd, titleOrData){ |
|
245 | 266 | } |
246 | 267 | }); |
247 | 268 |
|
248 | | - |
| 269 | +/* |
| 270 | + * Global functions |
| 271 | + */ |
249 | 272 | $.extend($.ui.contextmenu, { |
250 | 273 | /** Convert a menu description into a into a <li> content. */ |
251 | 274 | createEntryMarkup: function(entry, $parentLi){ |
|
0 commit comments