File size: 2,965 Bytes
cf0f72e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
(function(){
    'use strict';

    /**
     * @class Global.event.EventDispatcher
     * manipulate publish / subscribe interface
     * @extends Global.core.BaseClass
     *
     *     var instance = new Global.event.EventDispatcher(),
     *         callback = Global.core.Functions.bind(console.log);
     *
     *     instance.addEventListener('hoge', callback);
     *     instance.dispatchEvent('hoge'); // log argumetns
     *
     */
    Global.define('Global.event.EventDispatcher',{

        /**
         * @method init
         * @constructor
         */
        init: function(config) {
            this.listeners = {};
            this._super(config);
        },

        /**
        * @method addEventListener
        * Add listener
        * @param {String} type Event type
        * @param {Function} listener Callback function
        *
        */
        addEventListener: function(type, listener){
            (this.listeners[type] || (this.listeners[type] = [])).push(listener);
        },

        /**
        * @method hasEventListener
        * Return true this class listens passed type of event.
        * @param {String} type Event type
        * @return {Boolean}
        */
        hasEventListener: function(type){
            return !!this.listeners[type];
        },

        /**
        * @method  removeEventListener
        * Remove listener
        * @param {String} type Event type
        * @param {Function} listener Callback function
        */
        removeEventListener: function(type, listener){
            var fncs = this.listeners[type];
            if(fncs){
                for (var i = fncs.length-1; i >= 0; i--){
                    if (fncs[i] === listener){
                        fncs.splice(i, 1);
                    }
                }
            }
        },

        /**
        * @method  onesEventListener
        * Event you passed is only called once.
        * @param {String} type Event type
        * @param {Function} listener Callback function
        */
        onesEventListener: function(type, listener){
            var self = this,
                callback = function (event){
                    self.removeEventListener(type, callback);
                    listener.apply(self, [event]);
                    callback = null;
                };
            this.addEventListener(type, callback);
        },

        /**
        * @method dispatchEvent
        * Dispatch event message.
        * @param {String} type Event type
        * @param {Object} data Attach data
        */
        dispatchEvent: function(type, data){
            var fncs = this.listeners[type],
                event = new Global.event.Event({target:this, type: type, data: data});

            if(fncs){
                fncs = fncs.concat();
                for (var i = 0, n = fncs.length; i < n; i++){
                    fncs[i].apply(this, [event]);
                }
            }
        }
    });
})();