Skip to content

Commit

Permalink
Remove dojo/aspect as a dep from dojo/on and create Evented and topic…
Browse files Browse the repository at this point in the history
… modules, refs #12451 !strict

git-svn-id: http://svn.dojotoolkit.org/src/dojo/trunk@26489 560b804f-0ae3-0310-86f3-f6aa0a117693
  • Loading branch information
kriszyp committed Sep 8, 2011
1 parent e6ab9a9 commit c3e0cd9
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 83 deletions.
32 changes: 32 additions & 0 deletions Evented.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
define(["./aspect", "./on"], function(aspect, on){
// summary:
// The export of this module is a class that can be used as a mixin or base class,
// to add on() and emit() methods to a class
// for listening for events and emiting events:
// |define(["dojo/Evented"], function(Evented){
// | var EventedWidget = dojo.declare([Evented, dijit._Widget], {...});
// | widget = new EventedWidget();
// | widget.on("open", function(event){
// | ... do something with event
// | });
// |
// | widget.emit("open", {name:"some event", ...});

"use strict";
var after = aspect.after;
function Evented(){
}
Evented.prototype = {
on: function(type, listener){
return on.parse(this, type, listener, function(target, type){
return after(target, 'on' + type, listener, true);
});
},
emit: function(type, event){
var args = [this];
args.push.apply(args, arguments);
return on.emit.apply(on, args);
}
};
return Evented;
});
19 changes: 10 additions & 9 deletions _base/connect.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
define(["./kernel", "../on", "../aspect", "./event", "../mouse", "./sniff", "./lang", "../keys"], function(kernel, on, aspect, eventModule, mouse, has, lang){
define(["./kernel", "../on", "../topic", "../aspect", "./event", "../mouse", "./sniff", "./lang", "../keys"], function(kernel, on, hub, aspect, eventModule, mouse, has, lang){
// module:
// dojo/_base/connect
// summary:
Expand All @@ -19,13 +19,15 @@ has.add("events-keypress-typed", function(){ // keypresses should only occur a p
});

function connect_(obj, event, context, method, dontFix){
if(typeof event == "string" && event.substring(0, 2) == "on"){
event = event.substring(2);
}else if(!obj || !(obj.addEventListener || obj.attachEvent)){
method = lang.hitch(context, method);
if(!obj || !(obj.addEventListener || obj.attachEvent)){
// it is a not a DOM node and we are using the dojo.connect style of treating a
// method like an event, must go right to aspect
return aspect.after(obj || kernel.global, event, lang.hitch(context, method), true);
return aspect.after(obj || kernel.global, event, method, true);
}
if(typeof event == "string" && event.substring(0, 2) == "on"){
event = event.substring(2);
}
if(!obj){
obj = kernel.global;
}
Expand All @@ -43,7 +45,7 @@ function connect_(obj, event, context, method, dontFix){
break;
}
}
return on(obj, event, lang.hitch(context, method), dontFix);
return on(obj, event, method, dontFix);
}

var _punctMap = {
Expand Down Expand Up @@ -184,12 +186,11 @@ var connect = {
},

subscribe:function(topic, context, method){
return on(topic, lang.hitch(context, method));
return hub.on(topic, lang.hitch(context, method));
},

publish:function(topic, args){
topic = "on" + topic;
on[topic] && on[topic].apply(this, args || []);
return hub.emit.apply(hub, [topic].concat(args));
},

connectPublisher:function(topic, obj, event){
Expand Down
4 changes: 2 additions & 2 deletions _base/fx.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
define(["./kernel", "./lang", "./Color", "./connect", "./sniff", "../dom", "../dom-style"], function(dojo, lang, Color, connect, has, dom, style){
define(["./kernel", "./lang", "../Evented", "./Color", "./connect", "./sniff", "../dom", "../dom-style"], function(dojo, lang, Evented, Color, connect, has, dom, style){
// module:
// dojo/_base/fx
// summary:
Expand Down Expand Up @@ -48,7 +48,7 @@ define(["./kernel", "./lang", "./Color", "./connect", "./sniff", "../dom", "../d
}

};

dojo.Animation.prototype = new Evented();
// Alias to drop come 2.0:
dojo._Animation = dojo.Animation;

Expand Down
6 changes: 3 additions & 3 deletions data/ItemFileReadStore.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
define(["../_base/kernel", "../_base/lang", "../_base/declare", "../_base/array", "../_base/xhr",
"../_base/window", "./util/filter", "./util/simpleFetch", "../date/stamp"
], function(kernel, lang, declare, array, xhr, window, filterUtil, simpleFetch, dateStamp) {
"../Evented", "../_base/window", "./util/filter", "./util/simpleFetch", "../date/stamp"
], function(kernel, lang, declare, array, xhr, Evented, window, filterUtil, simpleFetch, dateStamp) {
// module:
// dojo/data/ItemFileReadStore
// summary:
// TODOC


var ItemFileReadStore = declare("dojo.data.ItemFileReadStore", null,{
var ItemFileReadStore = declare("dojo.data.ItemFileReadStore", [Evented],{
// summary:
// The ItemFileReadStore implements the dojo.data.api.Read API and reads
// data from JSON files that have contents in this format --
Expand Down
4 changes: 2 additions & 2 deletions dnd/Container.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
define(["../main", "./common", "../parser"], function(dojo) {
define(["../main", "../Evented", "./common", "../parser"], function(dojo, Evented) {
// module:
// dojo/dnd/Container
// summary:
Expand Down Expand Up @@ -54,7 +54,7 @@ dojo.dnd.Item = function(){
}
=====*/

dojo.declare("dojo.dnd.Container", null, {
dojo.declare("dojo.dnd.Container", Evented, {
// summary:
// a Container object, which knows when mouse hovers over it,
// and over which element it hovers
Expand Down
4 changes: 2 additions & 2 deletions dnd/Manager.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
define(["../main", "./common", "./autoscroll", "./Avatar"], function(dojo) {
define(["../main", "../Evented", "./common", "./autoscroll", "./Avatar"], function(dojo, Evented) {
// module:
// dojo/dnd/Manager
// summary:
// TODOC


var Manager = dojo.declare("dojo.dnd.Manager", null, {
var Manager = dojo.declare("dojo.dnd.Manager", [Evented], {
// summary:
// the manager of DnD operations (usually a singleton)
constructor: function(){
Expand Down
4 changes: 2 additions & 2 deletions dnd/Moveable.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
define(["../main", "../touch", "./Mover"], function(dojo, touch) {
define(["../main", "../Evented", "../touch", "./Mover"], function(dojo, Evented, touch) {
// module:
// dojo/dnd/Moveable
// summary:
Expand Down Expand Up @@ -26,7 +26,7 @@ dojo.declare("dojo.dnd.__MoveableArgs", [], {
});
=====*/

dojo.declare("dojo.dnd.Moveable", null, {
dojo.declare("dojo.dnd.Moveable", [Evented], {
// object attributes (for markup)
handle: "",
delay: 0,
Expand Down
4 changes: 2 additions & 2 deletions dnd/Mover.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
define(["../main", "../touch", "./common", "./autoscroll"], function(dojo, touch) {
define(["../main", "../Evented", "../touch", "./common", "./autoscroll"], function(dojo, Evented, touch) {
// module:
// dojo/dnd/Mover
// summary:
// TODOC


dojo.declare("dojo.dnd.Mover", null, {
dojo.declare("dojo.dnd.Mover", [Evented], {
constructor: function(node, e, host){
// summary:
// an object which makes a node follow the mouse, or touch-drag on touch devices.
Expand Down
4 changes: 3 additions & 1 deletion fx.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
define([
"./_base/lang",
"./Evented",
"./_base/kernel",
"./_base/array",
"./_base/connect",
Expand All @@ -8,7 +9,7 @@ define([
"./dom-style",
"./dom-geometry",
"require" // for context sensitive loading of Toggler
], function(lang, dojo, arrayUtil, connect, baseFx, dom, domStyle, geom, require) {
], function(lang, Evented, dojo, arrayUtil, connect, baseFx, dom, domStyle, geom, require) {

// module:
// dojo/fx
Expand Down Expand Up @@ -54,6 +55,7 @@ if(dojo && dojo.ready && !dojo.isAsync){
if(a.delay){ this.duration += a.delay; }
}, this);
};
_chain.prototype = new Evented();
lang.extend(_chain, {
_onAnimate: function(){
this._fire("onAnimate", arguments);
Expand Down
58 changes: 13 additions & 45 deletions on.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
define(["./aspect", "./_base/kernel", "./has"], function(aspect, dojo, has){
define(["./has!dom-addeventlistener?:./aspect", "./_base/kernel", "./has"], function(aspect, dojo, has){
// summary:
// The export of this module is a function that provides core event listening functionality. With this function
// you can provide a target, event type, and listener to be notified of
Expand All @@ -17,8 +17,8 @@ define(["./aspect", "./_base/kernel", "./has"], function(aspect, dojo, has){
// | define(["dojo/on"], function(listen){
// | on(button, "click", clickHandler);
// | ...
// Plain JavaScript objects can also have their own events.
// | var obj = {};
// Evented JavaScript objects can also have their own events.
// | var obj = new Evented;
// | on(obj, "foo", fooHandler);
// And then we could publish a "foo" event:
// | on.emit(obj, "foo", {key: "value"});
Expand All @@ -29,43 +29,20 @@ define(["./aspect", "./_base/kernel", "./has"], function(aspect, dojo, has){
// which would trigger fooHandler. Note that for a simple object this is equivalent to calling:
// | obj.onfoo({key:"value"});
// If you use on.emit on a DOM node, it will use native event dispatching when possible.
// You can also use listen function itself as a pub/sub hub:
// | on("some/topic", function(event){
// | ... do something with event
// | });
// | on.publish("some/topic", {name:"some event", ...});
// Evented:
// The "Evented" property of the export of this module can be used as a mixin or base class, to add on() and emit() methods to a class
// for listening for events and emiting events:
// | var Evented = on.Evented;
// | var EventedWidget = dojo.declare([Evented, dijit._Widget], {...});
// | widget = new EventedWidget();
// | widget.on("open", function(event){
// | ... do something with event
// | });
// |
// | widget.emit("open", {name:"some event", ...});

"use strict";
var after = aspect.after;
if(has("dom")){ // check to make sure we are in a browser, this module should work anywhere
var major = window.ScriptEngineMajorVersion;
has.add("jscript", major && (major() + ScriptEngineMinorVersion() / 10));
has.add("event-orientationchange", has("touch") && !has("android")); // TODO: how do we detect this?
}
var on = function(target, type, listener, dontFix){
if(!listener){
// two args, do pub/sub or window listening
// if there is a window event for the given target, we will target the window
return on(has("dom") && ("on" + target) in window ? window : on,
target, type);
}
if(target.on){
// delegate to the target's on() method, so it can handle it's own listening if it wants
return target.on(type, listener);
}
// delegate to main listener code
return addListener(target, type, listener, dontFix, this);
return on.parse(target, type, listener, addListener, dontFix, this);
};
on.pausable = function(target, type, listener, dontFix){
// summary:
Expand Down Expand Up @@ -100,12 +77,7 @@ define(["./aspect", "./_base/kernel", "./has"], function(aspect, dojo, has){
});
return signal;
};
var prototype = (on.Evented = function(){}).prototype;
prototype.on = function(type, listener, dontFix){
return addListener(this, type, listener, dontFix, this);
};
var touchEvents = /^touch/;
function addListener(target, type, listener, dontFix, matchesTarget){
on.parse = function(target, type, listener, addListener, dontFix, matchesTarget){
if(type.call){
// event handler function
// on(node, dojo.touch.press, touchListener);
Expand All @@ -128,7 +100,10 @@ define(["./aspect", "./_base/kernel", "./has"], function(aspect, dojo, has){
};
return handles;
}

return addListener(target, type, listener, dontFix, matchesTarget)
};
var touchEvents = /^touch/;
function addListener(target, type, listener, dontFix, matchesTarget){
// event delegation:
var selector = type.match(/(.*):(.*)/);
// if we have a selector:event, the last one is interpreted as an event, and we use event delegation
Expand Down Expand Up @@ -169,8 +144,7 @@ define(["./aspect", "./_base/kernel", "./has"], function(aspect, dojo, has){
if(fixAttach && target.attachEvent){
return fixAttach(target, type, listener);
}
// use aop
return after(target, type, listener, true);
throw new Error("Target must be an event emitter");
}

on.selector = function(selector, eventType){
Expand Down Expand Up @@ -209,7 +183,7 @@ define(["./aspect", "./_base/kernel", "./has"], function(aspect, dojo, has){
this.bubbles = false;
}
var slice = [].slice,
syntheticDispatch = prototype.emit = on.emit = function(target, type, event){
syntheticDispatch = on.emit = function(target, type, event){
// summary:
// Fires an event on the target object.
// target:
Expand Down Expand Up @@ -258,12 +232,6 @@ define(["./aspect", "./_base/kernel", "./has"], function(aspect, dojo, has){
// | direction: "left-to-right"
// | });
var args = slice.call(arguments, 2);
if(typeof target == "string"){
// two argument case
args.unshift(event = type);
type = target;
target = this;
}
var method = "on" + type;
if("parentNode" in target){
// node (or node-like), create event controller methods
Expand Down Expand Up @@ -315,7 +283,7 @@ define(["./aspect", "./_base/kernel", "./has"], function(aspect, dojo, has){
}
return target.dispatchEvent(nativeEvent) && nativeEvent;
}
return syntheticDispatch.call(on, target, type, event); // emit for a non-node
return syntheticDispatch.apply(on, arguments); // emit for a non-node
};
}else{
// no addEventListener, basically old IE event normalization
Expand Down Expand Up @@ -402,7 +370,7 @@ define(["./aspect", "./_base/kernel", "./has"], function(aspect, dojo, has){
emiter.listeners.push(handle = (_dojoIEListeners_.push(listener) - 1));
return new IESignal(handle);
}
return after(target, type, listener, true);
return aspect.after(target, type, listener, true);
};

var _setKeyChar = function(evt){
Expand Down
Loading

0 comments on commit c3e0cd9

Please sign in to comment.