PageRenderTime 6ms CodeModel.GetById 1ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/Objective-J/EventDispatcher.js

http://github.com/cacaodev/cappuccino
JavaScript | 84 lines | 48 code | 15 blank | 21 comment | 10 complexity | e461ffb4ef20f0f601053f715b42a0d1 MD5 | raw file
 1/*
 2 * EventDispatcher.js
 3 * Objective-J
 4 *
 5 * Created by Francisco Tolmasky.
 6 * Copyright 2008-2010, 280 North, Inc.
 7 *
 8 * This library is free software; you can redistribute it and/or
 9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23function EventDispatcher(/*Object*/ anOwner)
24{
25    this._eventListenersForEventNames = { };
26    this._owner = anOwner;
27}
28
29EventDispatcher.prototype.addEventListener = function(/*String*/ anEventName, /*Function*/ anEventListener)
30{
31    var eventListenersForEventNames = this._eventListenersForEventNames;
32
33    if (!hasOwnProperty.call(eventListenersForEventNames, anEventName))
34    {
35        var eventListenersForEventName = [];
36        eventListenersForEventNames[anEventName] = eventListenersForEventName;
37    }
38    else
39        var eventListenersForEventName = eventListenersForEventNames[anEventName];
40
41    var index = eventListenersForEventName.length;
42
43    while (index--)
44        if (eventListenersForEventName[index] === anEventListener)
45            return;
46
47    eventListenersForEventName.push(anEventListener);
48}
49
50EventDispatcher.prototype.removeEventListener = function(/*String*/ anEventName, /*Function*/ anEventListener)
51{
52    var eventListenersForEventNames = this._eventListenersForEventNames;
53
54    if (!hasOwnProperty.call(eventListenersForEventNames, anEventName))
55        return;
56
57    var eventListenersForEventName = eventListenersForEventNames[anEventName],
58        index = eventListenersForEventName.length;
59
60    while (index--)
61        if (eventListenersForEventName[index] === anEventListener)
62            return eventListenersForEventName.splice(index, 1);
63}
64
65EventDispatcher.prototype.dispatchEvent = function(/*Event*/ anEvent)
66{
67    var type = anEvent.type,
68        eventListenersForEventNames = this._eventListenersForEventNames;
69
70    if (hasOwnProperty.call(eventListenersForEventNames, type))
71    {
72        var eventListenersForEventName = this._eventListenersForEventNames[type],
73            index = 0,
74            count = eventListenersForEventName.length;
75
76        for (; index < count; ++index)
77            eventListenersForEventName[index](anEvent);
78    }
79
80    var manual = (this._owner || this)["on" + type];
81
82    if (manual)
83        manual(anEvent);
84}