| 1 | dojo.provide("dojox.rails.tests.plugd.trigger"); |
| 2 | (function(d){ |
| 3 | |
| 4 | var isfn = d.isFunction, |
| 5 | leaveRe = /mouse(enter|leave)/, |
| 6 | _fix = function(_, p){ |
| 7 | return "mouse" + (p == "enter" ? "over" : "out"); |
| 8 | }, |
| 9 | mix = d._mixin, |
| 10 | |
| 11 | // the guts of the node triggering logic: |
| 12 | // the function accepts node (not string|node), "on"-less event name, |
| 13 | // and an object of args to mix into the event. |
| 14 | realTrigger = d.doc.createEvent ? |
| 15 | function(n, e, a){ |
| 16 | // the sane branch |
| 17 | var ev = d.doc.createEvent("HTMLEvents"); |
| 18 | e = e.replace(leaveRe, _fix); |
| 19 | ev.initEvent(e, true, true); |
| 20 | a && mix(ev, a); |
| 21 | n.dispatchEvent(ev); |
| 22 | } : |
| 23 | function(n, e, a){ |
| 24 | // the janktastic branch |
| 25 | var ev = "on" + e, stop = false, lc = e.toLowerCase(), node = n; |
| 26 | try{ |
| 27 | // FIXME: is this worth it? for mixed-case native event support:? Opera ends up in the |
| 28 | // createEvent path above, and also fails on _some_ native-named events. |
| 29 | // if(lc !== e && d.indexOf(d.NodeList.events, lc) >= 0){ |
| 30 | // // if the event is one of those listed in our NodeList list |
| 31 | // // in lowercase form but is mixed case, throw to avoid |
| 32 | // // fireEvent. /me sighs. http://gist.github.com/315318 |
| 33 | // throw("janktastic"); |
| 34 | // } |
| 35 | n.fireEvent(ev); |
| 36 | }catch(er){ |
| 37 | console.warn("in catch", er); |
| 38 | // a lame duck to work with. we're probably a 'custom event' |
| 39 | var evdata = mix({ |
| 40 | type: e, target: n, faux: true, |
| 41 | // HACK: [needs] added support for customStopper to _base/event.js |
| 42 | // some tests will fail until del._stopPropagation has support. |
| 43 | _stopper: function(){ stop = this.cancelBubble; } |
| 44 | }, a); |
| 45 | |
| 46 | isfn(n[ev]) && n[ev](evdata); |
| 47 | |
| 48 | // handle bubbling of custom events, unless the event was stopped. |
| 49 | while(!stop && n !== d.doc && n.parentNode){ |
| 50 | n = n.parentNode; |
| 51 | isfn(n[ev]) && n[ev](evdata); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | ; |
| 56 | |
| 57 | d._trigger = function(/* DomNode|String */node, /* String */event, extraArgs){ |
| 58 | // summary: |
| 59 | // Helper for `dojo.trigger`, which handles the DOM cases. We should never |
| 60 | // be here without a domNode reference and a string eventname. |
| 61 | var n = d.byId(node), ev = event && event.slice(0, 2) == "on" ? event.slice(2) : event; |
| 62 | realTrigger(n, ev, extraArgs); |
| 63 | |
| 64 | }; |
| 65 | |
| 66 | d.trigger = function(obj, event, extraArgs){ |
| 67 | // summary: |
| 68 | // Trigger some event. It can be either a Dom Event, Custom Event, |
| 69 | // or direct function call. |
| 70 | // |
| 71 | // description: |
| 72 | // Trigger some event. It can be either a Dom Event, Custom Event, |
| 73 | // or direct function call. NOTE: This function does not trigger |
| 74 | // default behavior, only triggers bound event listeneres. eg: |
| 75 | // one cannot trigger("anchorNode", "onclick") and expect the browser |
| 76 | // to follow the href="" attribute naturally. |
| 77 | // |
| 78 | // obj: String|DomNode|Object|Function |
| 79 | // An ID, or DomNode reference, from which to trigger the event. |
| 80 | // If an Object, fire the `event` in the scope of this object, |
| 81 | // similar to calling dojo.hitch(obj, event)(). The return value |
| 82 | // in this case is returned from `dojo.trigger` |
| 83 | // |
| 84 | // event: String|Function |
| 85 | // The name of the event to trigger. Can be any DOM level 2 event |
| 86 | // and can be in either form: "onclick" or "click" for instance. |
| 87 | // In the object-firing case, this method can be a function or |
| 88 | // a string version of a member function, just like `dojo.hitch`. |
| 89 | // |
| 90 | // extraArgs: Object? |
| 91 | // An object to mix into the `event` object passed to any bound |
| 92 | // listeners. Be careful not to override important members, like |
| 93 | // `type`, or `preventDefault`. It will likely error. |
| 94 | // |
| 95 | // Additionally, extraArgs is moot in the object-triggering case, |
| 96 | // as all arguments beyond the `event` are curried onto the triggered |
| 97 | // function. |
| 98 | // |
| 99 | // example: |
| 100 | // | dojo.connect(node, "onclick", function(e){ /* stuff */ }); |
| 101 | // | // later: |
| 102 | // | dojo.trigger(node, "onclick"); |
| 103 | // |
| 104 | // example: |
| 105 | // | // or from within dojo.query: (requires dojo.NodeList) |
| 106 | // | dojo.query("a").onclick(function(){}).trigger("onclick"); |
| 107 | // |
| 108 | // example: |
| 109 | // | // fire obj.method() in scope of obj |
| 110 | // | dojo.trigger(obj, "method"); |
| 111 | // |
| 112 | // example: |
| 113 | // | // fire an anonymous function: |
| 114 | // | dojo.trigger(d.global, function(){ /* stuff */ }); |
| 115 | // |
| 116 | // example: |
| 117 | // | // fire and anonymous function in the scope of obj |
| 118 | // | dojo.trigger(obj, function(){ this == obj; }); |
| 119 | // |
| 120 | // example: |
| 121 | // | // with a connected function like: |
| 122 | // | dojo.connect(dojo.doc, "onclick", function(e){ |
| 123 | // | if(e && e.manuallydone){ |
| 124 | // | console.log("this was a triggered onclick, not natural"); |
| 125 | // | } |
| 126 | // | }); |
| 127 | // | // fire onclick, passing in a custom bit of info |
| 128 | // | dojo.trigger("someId", "onclick", { manuallydone:true }); |
| 129 | // |
| 130 | // returns: Anything |
| 131 | // Will not return anything in the Dom event case, but will return whatever |
| 132 | // return value is received from the triggered event. |
| 133 | return (isfn(obj) || isfn(event) || isfn(obj[event])) ? |
| 134 | d.hitch.apply(d, arguments)() : d._trigger.apply(d, arguments); |
| 135 | }; |
| 136 | |
| 137 | // adapt for dojo.query: |
| 138 | /*===== |
| 139 | dojo.extend(dojo.NodeList, { |
| 140 | trigger: function(event, data){ |
| 141 | // summary: |
| 142 | // Trigger some Event originating from each of the nodes in this |
| 143 | // `dojo.NodeList`. |
| 144 | // |
| 145 | // event: String |
| 146 | // Any strig identifier for the event.type to be triggered. |
| 147 | // |
| 148 | // data: Object |
| 149 | // Just like `extraArgs` for `dojo.trigger`, additional data |
| 150 | // to mix into the event object. |
| 151 | // |
| 152 | // example: |
| 153 | // | dojo.query("a").trigger("onclick"); |
| 154 | |
| 155 | return this; // dojo.NodeList |
| 156 | } |
| 157 | }); |
| 158 | =====*/ |
| 159 | d.NodeList.prototype.trigger = d.NodeList._adaptAsForEach(d._trigger); |
| 160 | |
| 161 | // if the node.js module is available, extend trigger into that. |
| 162 | if(d._Node && !d._Node.prototype.trigger){ |
| 163 | d.extend(d._Node, { |
| 164 | trigger: function(ev, data){ |
| 165 | // summary: |
| 166 | // Fire some some event originating from this node. |
| 167 | // Only available if both the `dojo.trigger` and `dojo.node` plugin |
| 168 | // are enabled. Allows chaining as all `dojo._Node` methods do. |
| 169 | // |
| 170 | // ev: String |
| 171 | // Some string event name to fire. eg: "onclick", "submit" |
| 172 | // |
| 173 | // data: Object |
| 174 | // Just like `extraArgs` for `dojo.trigger`, additional data |
| 175 | // to mix into the event object. |
| 176 | // |
| 177 | // example: |
| 178 | // | // fire onlick orginiating from a node with id="someAnchorId" |
| 179 | // | dojo.node("someAnchorId").trigger("click"); |
| 180 | |
| 181 | d._trigger(this, ev, data); |
| 182 | return this; // dojo._Node |
| 183 | } |
| 184 | }); |
| 185 | } |
| 186 | |
| 187 | })(dojo); |