open-orchestra/open-orchestra-cms-bundle

View on GitHub
BackofficeBundle/Resources/public/ecmascript/OpenOrchestra/Service/Form/Behavior/Manager.js

Summary

Maintainability
A
0 mins
Test Coverage
import AbstractBehavior from 'OpenOrchestra/Service/Form/Behavior/AbstractBehavior'

/**
 * @class Manager
 */
class Manager
{
    /**
     * Constructor
     */
    constructor() {
        this._behaviors = [];
        Backbone.Events.on('form:activate', this._activate, this);
        Backbone.Events.on('form:deactivate', this._deactivate, this);
    }

    /**
     * @param {Object} behavior
     */
    add(behavior) {
        if (!(behavior instanceof AbstractBehavior)) {
            throw TypeError("Manager accept only instance of AbstractBehavior");
        }
        this._behaviors.push(behavior);
    }

    /**
     * activate behavior
     *
     * @param {Object} view - instance of AbstractFormView
     */
    _activate(view) {
        for (let behavior of this._behaviors) {
            behavior.activateBehavior(view);
        }
    }

    /**
     * deactivate behavior
     *
     * @param {Object} view - instance of AbstractFormView
     */
    _deactivate(view) {
        for (let behavior of this._behaviors) {
            behavior.deactivateBehavior(view);
        }
    }
}

// unique instance of Manager
export default (new Manager);