new Manager()
A class which provides event related behaviors. This class basically manages all DOM3 bubbling event types such as "select", "focusin", "focusout", "click", "dblclick", "mousedown", "mouseup", "mousemove", "mouseover", "mouseout", "mousewheel", "beforeinput", "input", "keydown" and "keyup". This class also manages a capturing event "resize" exceptionally. When the managed events occur from DOM, event manager receives them and then sends to the added observers sequentially. The custom events are identically treated by the DOM managing mechanism.
- Since:
-
- 2.0.0
- See:
Throws:
Will throw an error if attempt to create another instance of caph.module.event.Manager.
Example
var Observer = $class({ $extends: caph.ui.base.Component, // handles click event. onclick: function(message) { console.log(message.mouse().left); // true }, // handles keydown event. onkeydown: function(message) { console.log(message.key().enter); // true } }); var observer = new Observer(); observer.on({ // attaches custom user message handler custom: function(message) { console.log('Hello! ' + message.detail.value); // Hello! test } }); var eventManager = caph.module.event.Manager.getInstance(); eventManager.addObserver(observer); // true eventManager.sendTo(observer, new caph.event.Message({ type: "click", detail: { // left button click button: 0, which: 1 } }); // true eventManager.sendTo(observer, { type: "keydown", detail: { keyCode: 13 // enter key code } }); // 'test' // 'Hello! test' eventManager.sendTo(observer, { type: "custom", detail: { value: "test" } });
Methods
-
<static> getInstance() → {caph.module.event.Manager}
-
Gets the singleton instance.
- Since:
-
- 2.0.0
Returns:
Example
var EventManager = caph.module.event.Manager.getInstance();
-
addObserver(observer) → {caph.moudle.event.Manager}
-
Adds an observer to handle the event types. If the observer has event handlers, it will be added to the corresponding subject which represents the event type. The event handler names always start with "on" prefix and consist of lowercase event type name. The event type name also can be a string of which first letter is capitalized. The other cases are not permitted.
Parameters:
Name Type Description observer
caph.module.mixins.event.Observer An observer which handles the event types.
- Since:
-
- 2.0.0
Throws:
Will throw an error if the observer parameter does not mixin caph.module.mixins.event.Observer or the observer has the duplicated event handler name or do not follow the event handler naming convention.
Returns:
- Type
- caph.moudle.event.Manager
Example
var Observer = $class({ $extends: caph.ui.base.Component, onclick: function(message) { console.log(message); } }); caph.module.event.Manager.getInstance().addObserver(new Observer());
-
addSubject(type) → {caph.moudle.event.Manager}
-
Adds a subject which represents the given event type. The event type name is always lowercase string.
Parameters:
Name Type Description type
String The name of the event type. The type value is treated as lowercase.
- Since:
-
- 2.0.0
Returns:
- Type
- caph.moudle.event.Manager
Example
var EventManager = caph.module.event.Manager.getInstance(); EventManager.addSubject('click'); EventManager.addSubject('test');
-
removeObserver(observer, types) → {caph.moudle.event.Manager}
-
Removes an observer from the corresponding subject which represents the given event types.
Parameters:
Name Type Argument Description observer
caph.module.mixins.event.Observer An observer to be removed from the event types.
types
Array.<String> <optional>
An array of event type which is handled by observer currently. The observer is no longer observing the specified event types. If types parameter is not passed, observer will be removed from all event types.
- Since:
-
- 2.0.0
Throws:
Will throw an error if the observer parameter is not an instance of caph.module.mixins.event.Observer or the types parameter is not an array of string.
Returns:
- Type
- caph.moudle.event.Manager
Example
var EventManager = caph.module.event.Manager.getInstance(); var Observer = $class({ $extends: caph.ui.base.Component, onclick: function(message) { console.log(message); }, onkeydown: function(message) { console.log(message); }, onkeyup: function(message) { console.log(message); } }); var observer = new Observer(); EventManager.addObserver(observer); EventManager.removeObserver(observer, ['click']); EventManager.removeObserver(observer); // remove all
-
send(message) → {caph.moudle.event.Manager}
-
Sends a message to all observers which handle the corresponding event type.
Parameters:
Name Type Description message
caph.event.Messsage | Object A message to be sent. If message is an object, refer to the caph.event.Message constructor.
- Since:
-
- 2.0.0
Throws:
Will throw an error if the message parameter is not an instance of caph.event.Message or an object.
Returns:
- Type
- caph.moudle.event.Manager
Example
var EventManager = caph.module.event.Manager.getInstance(); var Observer = $class({ $extends: caph.ui.base.Component, onclick: function(message) { console.log(message); } }); EventManager.addObserver(new Observer()); EventManager.addObserver(new Observer()); EventManager.send({ type: 'click', detail: { offsetX: 10, offsetY: 10 } }); EventManager.send(new caph.event.Message({ type: 'click', detail: { offsetX: 10, offsetY: 10 } });
-
sendTo(observer, message) → {caph.moudle.event.Manager}
-
Sends a message to the designated observer which handles the corresponding event type. If the message propagation is stopped, will not propagate any more.
Parameters:
Name Type Description observer
Array.<caph.module.mixins.event.Observer> | caph.module.mixins.event.Observer An array of the observers or an observer to receive a message.
message
caph.event.Messsage | Object A message to be sent. If message is an object, refer to the caph.event.Message constructor.
- Since:
-
- 2.0.0
Throws:
Will throw an error if the observer parameter is not an array of the objects which mixin the "caph.module.mixins.event.Observer" or observer parameter does not mixin the "caph.module.mixins.event.Observer" or the message parameter is not an instance of caph.event.Message or an object.
Returns:
- Type
- caph.moudle.event.Manager
Example
var EventManager = caph.module.event.Manager.getInstance(); var Observer = $class({ $extends: caph.ui.base.Component, onclick: function(message) { console.log(message); } }); var observer = new Observer(); EventManager.addObserver(observer); EventManager.sendTo(observer, { type: 'click', detail: { offsetX: 10, offsetY: 10 } }); EventManager.sendTo(observer, new caph.event.Message({ type: 'click', detail: { offsetX: 10, offsetY: 10 } });