|
22 | 22 | // $menu = $(menu);
|
23 | 23 | // return $menu.data("ui-menu") || $menu.data("menu");
|
24 | 24 | // }
|
25 |
| - var NS = ".contextmenu", |
26 |
| - supportSelectstart = "onselectstart" in document.createElement("div"); |
| 25 | + var supportSelectstart = "onselectstart" in document.createElement("div"); |
27 | 26 |
|
28 | 27 | /** Return command without leading '#' (default to ""). */
|
29 | 28 | function normCommand(cmd){
|
|
53 | 52 | },
|
54 | 53 | /** Construtcor */
|
55 | 54 | _create: function () {
|
56 |
| - var opts = this.options, |
57 |
| - eventNames = "contextmenu" + NS, |
58 |
| - targetId = this.element.uniqueId().attr("id"); |
| 55 | + var eventNames, targetId, |
| 56 | + opts = this.options; |
59 | 57 |
|
| 58 | + console.log(this.element) |
| 59 | + |
60 | 60 | this.$headStyle = null;
|
61 | 61 | this.orgMenu = null;
|
62 | 62 | this.currentTarget = null;
|
63 |
| - |
64 |
| - if(opts.taphold){ |
65 |
| - eventNames += " taphold" + NS; |
66 |
| - } |
| 63 | + this.ns = "." + this.widgetName; |
| 64 | + |
67 | 65 | if(opts.preventSelect){
|
68 | 66 | // Create a global style for all potential menu targets
|
69 |
| - this.$headStyle = $("<style>") |
| 67 | + // If the contextmenu was bound to `document`, we apply the |
| 68 | + // selector relative to the <body> tag instead |
| 69 | + targetId = ($(this.element).is(document) ? $("body") : this.element).uniqueId().attr("id"); |
| 70 | + this.$headStyle = $("<style class='ui-contextmenu-style'>") |
70 | 71 | .prop("type", "text/css")
|
71 | 72 | .html("#" + targetId + " " + opts.delegate + " { " +
|
72 | 73 | "-webkit-user-select: none; " +
|
|
78 | 79 | .appendTo("head");
|
79 | 80 | // TODO: the selectstart is not supported by FF?
|
80 | 81 | if(supportSelectstart){
|
81 |
| - this.element.delegate(opts.delegate, "selectstart" + NS, function(event){ |
| 82 | + this.element.delegate(opts.delegate, "selectstart" + this.ns, function(event){ |
82 | 83 | event.preventDefault();
|
83 | 84 | });
|
84 | 85 | }
|
|
111 | 112 | }
|
112 | 113 | }, this)
|
113 | 114 | });
|
| 115 | + |
| 116 | + eventNames = "contextmenu" + this.ns; |
| 117 | + if(opts.taphold){ |
| 118 | + eventNames += " taphold" + this.ns; |
| 119 | + } |
114 | 120 | this.element.delegate(opts.delegate, eventNames, $.proxy(this._openMenu, this));
|
115 |
| - // emulate a 'taphold' event |
| 121 | + |
116 | 122 | this._trigger("init");
|
117 | 123 | },
|
118 | 124 | /** Destructor, called on $().contextmenu("destroy"). */
|
|
148 | 154 | return false;
|
149 | 155 | }
|
150 | 156 | // Register global event handlers that close the dropdown-menu
|
151 |
| - $(document).bind("keydown" + NS, function(event){ |
| 157 | + $(document).bind("keydown" + this.ns, function(event){ |
152 | 158 | if( event.which === $.ui.keyCode.ESCAPE ){
|
153 | 159 | self._closeMenu();
|
154 | 160 | }
|
155 |
| - }).bind("mousedown" + NS + " touchstart" + NS, function(event){ |
| 161 | + }).bind("mousedown" + this.ns + " touchstart" + this.ns, function(event){ |
156 | 162 | // Close menu when clicked outside menu
|
157 | 163 | if( !$(event.target).closest(".ui-menu-item").length ){
|
158 | 164 | self._closeMenu();
|
|
187 | 193 | });
|
188 | 194 |
|
189 | 195 | $(document)
|
190 |
| - .unbind("mousedown" + NS) |
191 |
| - .unbind("touchstart" + NS) |
192 |
| - .unbind("keydown" + NS); |
| 196 | + .unbind("mousedown" + this.ns) |
| 197 | + .unbind("touchstart" + this.ns) |
| 198 | + .unbind("keydown" + this.ns); |
193 | 199 | },
|
194 | 200 | /** Handle $().contextmenu("option", key, value) calls. */
|
195 | 201 | _setOption: function(key, value){
|
|
0 commit comments