all files / montage/core/event/ action-event-listener.js

41.67% Statements 5/12
0% Branches 0/8
33.33% Functions 1/3
41.67% Lines 5/12
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                                                                                                                                                                       
/**
 * @module montage/core/event/action-event-listener
 * @requires montage/core/core
 */
var Montage = require("../core").Montage;
 
/**
 * @class ActionEventListener
 * @extends Montage
 */
var ActionEventListener = exports.ActionEventListener = Montage.specialize( /** @lends ActionEventListener.prototype # */ {
 
    /**
     * The logical object handling received events
     * @type {Object}
     * @default null
     */
    handler: {
        value: null
    },
 
    /**
     * The name of the method to invoke on the handler object, or a function to
     * call with the handler as its context.
     *
     * If there is no handler set, the function is invoked with this
     * actionEventListener as the context.
     *
     * If neither handler nor action is set, the event is ignored.
     *
     * @property {String|Function} value
     * @default null
     *
     */
    action: {
        value: null
    },
 
    /**
     * Returns a new ActionEventListener instance with the specified handler
     * and action.
     *
     * @function
     * @param {Object} handler - The event handler
     * @param {String|Function} action - The event handler action
     * @returns {ActionEventListener} - The initialized ActionEventListener
     * */
    initWithHandler_action_: {
        value: function (handler, action) {
            this.handler = handler;
            this.action = action;
            return this;
        }
    },
 
    /**
     * @function
     * @param {Event} event
     */
    handleEvent: {
        value: function (event) {
            if (typeof this.action === "function") {
                var context = this.handler ? this.handler : this;
                this.action.call(context, event);
            } else if (this.handler && this.action) {
                this.handler[this.action](event);
            }
        }
    },
 
    /**
     * @function
     * @param {Serializer} serializer
     */
    serializeProperties: {
        value: function (serializer) {
            serializer.set("handler", this.handler, "reference");
            // TODO accepting an actual function is less than ideal from the
            // serialization standpoint
            serializer.set("action", this.action);
        }
    }
 
}, {
    blueprintModuleId: require("../core")._blueprintModuleIdDescriptor,
    blueprint: require("../core")._blueprintDescriptor
});