rikschennink / conditioner

💆🏻 Frizz free, context-aware, JavaScript modules

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

.getModule() and .getModules() doesn't include a search on the target node

mark-ettema opened this issue · comments

Situation:
When using .getModule() and a selector and/or a context is supplied I expect that modules on that node are also part of the result. What if oftern want to accomplish is to retrieve modules from a known node.

As a work-around I wrote wrappers around these methods. What they basicly do is suppling the parent of the desired context and then will filter the result to exclude siblings.

/**
 * Conditioner.getModules() doesn't include a search on the target node.
 * This method extends the default behavior with this functionality.
 * @param   {String}                path
 * @param   {String|HTMLElement}    [param2]
 * @param   {HTMLElement}           [param3]
 * @returns {Array.<ModuleController>|null}
 */
getModules: function (path, param2, param3) {
    var selector = typeof param2 === 'string' ? param2 : '*';
    var context = (param2 instanceof HTMLElement ? param2 : param3) || document.body;
    var moduleControllers = conditoner.getModules(path, selector, context.parentNode);
    if (!Array.isArray(moduleControllers)) {
        return null;
    }
    moduleControllers = moduleControllers
        .filter(function (moduleController) {
            return moduleController._element === context || context.contains(moduleController._element);
        });
    return moduleControllers.length ? moduleControllers : null;
},

/**
 * Conditioner.getModule() doesn't include a search on the target node.
 * This method extends the default behavior with this functionality.
 * When there is more than one result, the first will be returned.
 * @param   {String}                path
 * @param   {String|HTMLElement}    [param2]
 * @param   {HTMLElement}           [param3]
 * @returns {ModuleController|null}
 */
getModule: function (path, param2, param3) {
    var moduleControllers = this.getModules(path, param2, param3);
    if (!Array.isArray(moduleControllers)) {
        return null;
    }
    return moduleControllers
        .reverse()
        .reduce(function (prev, current) {
            return current;
        }, null);
}
commented

I've ran into this problem in my own projects as well. But, I've setup the getModule(s) function to match the behavior of the querySelector which also searches within a given context and does not take the supplied context into account.

The develop branch contains a version of Conditioner where I tried to remedy this problem without automatically searching from the parentnode. See the updated getModule function description below.

/***
 * Returns the first [ModuleController](#modulecontroller) matching the supplied query.
 *
 * - `getModule(element)` get module on the given element
 * - `getModule(element, path)` get module with path on the given element
 * - `getModule(path)` get first module with given path
 * - `getModule(path, filter)` get first module with path in document scope
 * - `getModule(path, context)` get module with path, search within context
 * - `getModule(path, filter, context)` get module with path, search within matched elements in context
 *
 * @method getModule
 * @memberof Conditioner
 * @param {...*=} arguments
 * @returns {(ModuleController|null)} module - The found module.
 * @public
 */

That solution is will do! When can I expect a release with this feature?

commented

I'm working on Conditioner the coming weeks so expect a small update soon.