All files / framework/enhancement/js ContextAwareness.js

94.37% Statements 67/71
82.5% Branches 33/40
100% Functions 17/17
94.37% Lines 67/71
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234                          72x   72x     72x   72x           72x 252x 367x                         72x 252x 367x   367x 37x   367x 44x 323x 32x 291x 4x 287x 287x                                         72x 252x 252x                   72x 56x 56x 56x 56x 58x 58x 58x 58x                           72x                                               72x   404x 404x 404x 404x 404x       72x 342x     342x 342x 358x     358x 358x 275x           72x 298x 298x 298x 342x 342x   298x                                 72x 54x 54x             54x         72x 122x     72x               72x   72x 72x         72x 72x     72x                    
/*
Copyright 2008-2009 University of Toronto
Copyright 2010-2016 OCAD University
Copyright 2015-2016 Raising the Floor - International
 
Licensed under the Educational Community License (ECL), Version 2.0 or the New
BSD license. You may not use this file except in compliance with one these
Licenses.
 
You may obtain a copy of the ECL 2.0 License and BSD License at
https://github.com/fluid-project/infusion/raw/master/Infusion-LICENSE.txt
*/
 
var fluid_3_0_0 = fluid_3_0_0 || {};
 
(function ($, fluid) {
    "use strict";
 
    fluid.registerNamespace("fluid.contextAware");
 
    fluid.defaults("fluid.contextAware.marker", {
        gradeNames: ["fluid.component"]
    });
 
 
    // unsupported, NON-API function
    fluid.contextAware.makeCheckMarkers = function (checks, path, instantiator) {
        fluid.each(checks, function (value, markerTypeName) {
            fluid.constructSingle(path, {
                type: markerTypeName,
                gradeNames: "fluid.contextAware.marker",
                value: value
            }, instantiator);
        });
 
    };
    /** Peforms the computation for `fluid.contextAware.makeChecks` and returns a structure suitable for being sent to `fluid.contextAware.makeCheckMarkers` -
     *
     * @return A hash of marker type names to grade names - this can be sent to fluid.contextAware.makeCheckMarkers
     */
    // unsupported, NON-API function
    fluid.contextAware.performChecks = function (checkHash) {
        return fluid.transform(checkHash, function (checkRecord) {
            Iif (typeof(checkRecord) === "function") {
                checkRecord = {func: checkRecord};
            } else if (typeof(checkRecord) === "string") {
                checkRecord = {funcName: checkRecord};
            }
            if (fluid.isPrimitive(checkRecord)) {
                return checkRecord;
            } else if ("value" in checkRecord) {
                return checkRecord.value;
            } else if ("func" in checkRecord) {
                return checkRecord.func();
            } else Eif ("funcName" in checkRecord) {
                return fluid.invokeGlobalFunction(checkRecord.funcName);
            } else {
                fluid.fail("Error in contextAwareness check record ", checkRecord, " - must contain an entry with name value, func, or funcName");
            }
        });
    };
 
    /**
     * Takes an object whose keys are check context names and whose values are check records, designating a collection of context markers which might be registered at a location
     * in the component tree.
     *
     * @param {Object} checkHash - The keys in this structure are the context names to be supplied if the check passes, and the values are check records.
     * A check record contains:
     *    ONE OF:
     *    value {Any} [optional] A literal value name to be attached to the context
     *    func {Function} [optional] A zero-arg function to be called to compute the value
     *    funcName {String} [optional] The name of a zero-arg global function which will compute the value
     * If the check record consists of a Number or Boolean, it is assumed to be the value given to "value".
     * @param {String|Array} [path] - [optional] The path in the component tree at which the check markers are to be registered. If omitted, "" is assumed
     * @param {Instantiator} [instantiator] - [optional] The instantiator holding the component tree which will receive the markers. If omitted, use `fluid.globalInstantiator`.
     */
    fluid.contextAware.makeChecks = function (checkHash, path, instantiator) {
        var checkOptions = fluid.contextAware.performChecks(checkHash);
        fluid.contextAware.makeCheckMarkers(checkOptions, path, instantiator);
    };
 
    /**
     * Forgets a check made at a particular level of the component tree.
     *
     * @param {String[]} markerNames - The marker typeNames whose check values are to be forgotten.
     * @param {String|String[]} [path] - [optional] The path in the component tree at which the check markers are to be removed. If omitted, "" is assumed
     * @param {Instantiator} [instantiator] - [optional] The instantiator holding the component tree the markers are to be removed from. If omitted, use `fluid.globalInstantiator`.
     */
    fluid.contextAware.forgetChecks = function (markerNames, path, instantiator) {
        instantiator = instantiator || fluid.globalInstantiator;
        path = path || [];
        var markerArray = fluid.makeArray(markerNames);
        fluid.each(markerArray, function (markerName) {
            var memberName = fluid.typeNameToMemberName(markerName);
            var segs = fluid.model.parseToSegments(path, instantiator.parseEL, true);
            segs.push(memberName);
            fluid.destroy(segs, instantiator);
        });
    };
 
    /** A grade to be given to a component which requires context-aware adaptation.
     * This grade consumes configuration held in the block named "contextAwareness", which is an object whose keys are check namespaces and whose values hold
     * sequences of "checks" to be made in the component tree above the component. The value searched by
     * each check is encoded as the element named `contextValue` - this either represents an IoC reference to a component
     * or a particular value held at the component. If this reference has no path component, the path ".options.value" will be assumed.
     * These checks seek contexts which
     * have been previously registered using fluid.contextAware.makeChecks. The first context which matches
     * with a value of `true` terminates the search, and returns by applying the grade names held in `gradeNames` to the current component.
     * If no check matches, the grades held in `defaultGradeNames` will be applied.
     */
    fluid.defaults("fluid.contextAware", {
        gradeNames: ["{that}.check"],
        mergePolicy: {
            contextAwareness: "noexpand"
        },
        contextAwareness: {
            // Hash of names (check namespaces) to records: {
            //     checks: {}, // Hash of check namespace to: {
            //         contextValue: IoCExpression testing value in environment,
            //         gradeNames: gradeNames which will be output,
            //         priority: String/Number for priority of check [optional]
            //         equals: Value to be compared to contextValue [optional - default is `true`]
            //     defaultGradeNames: // String or String[] holding default gradeNames which will be output if no check matches [optional]
            //     priority: // Number or String encoding priority relative to other records (same format as with event listeners) [optional]
            // }
        },
        invokers: {
            check: {
                funcName: "fluid.contextAware.check",
                args: ["{that}", "{that}.options.contextAwareness"]
            }
        }
    });
 
    fluid.contextAware.getCheckValue = function (that, reference) {
        // cf. core of distributeOptions!
        var targetRef = fluid.parseContextReference(reference);
        var targetComponent = fluid.resolveContext(targetRef.context, that);
        var path = targetRef.path || ["options", "value"];
        var value = fluid.getForComponent(targetComponent, path);
        return value;
    };
 
    // unsupported, NON-API function
    fluid.contextAware.checkOne = function (that, contextAwareRecord) {
        Iif (contextAwareRecord.checks && contextAwareRecord.checks.contextValue) {
            fluid.fail("Nesting error in contextAwareness record ", contextAwareRecord, " - the \"checks\" entry must contain a hash and not a contextValue/gradeNames record at top level");
        }
        var checkList = fluid.parsePriorityRecords(contextAwareRecord.checks, "contextAwareness checkRecord");
        return fluid.find(checkList, function (check) {
            Iif (!check.contextValue) {
                fluid.fail("Cannot perform check for contextAwareness record ", check, " without a valid field named \"contextValue\"");
            }
            var value = fluid.contextAware.getCheckValue(that, check.contextValue);
            if (check.equals === undefined ? value : value === check.equals) {
                return check.gradeNames;
            }
        }, contextAwareRecord.defaultGradeNames);
    };
 
    // unsupported, NON-API function
    fluid.contextAware.check = function (that, contextAwarenessOptions) {
        var gradeNames = [];
        var contextAwareList = fluid.parsePriorityRecords(contextAwarenessOptions, "contextAwareness adaptationRecord");
        fluid.each(contextAwareList, function (record) {
            var matched = fluid.contextAware.checkOne(that, record);
            gradeNames = gradeNames.concat(fluid.makeArray(matched));
        });
        return gradeNames;
    };
 
    /** Given a set of options, broadcast an adaptation to all instances of a particular component in a particular context. ("new demands blocks").
     * This has the effect of fabricating a grade with a particular name with an options distribution to `{/ typeName}` for the required component,
     * and then constructing a single well-known instance of it.
     * Options layout:
     *  distributionName {String} A grade name - the name to be given to the fabricated grade
     *  targetName {String} A grade name - the name of the grade to receive the adaptation
     *  adaptationName {String} the name of the contextAwareness record to receive the record - this will be a simple string
     *  checkName {String} the name of the check within the contextAwareness record to receive the record - this will be a simple string
     *  record {Object} the record to be broadcast into contextAwareness - should contain entries
     *      contextValue {IoC expression} the context value to be checked to activate the adaptation
     *      gradeNames {String/String[]} the grade names to be supplied to the adapting target (matching advisedName)
     *
     * @param {Object} options - The options to use when making an adaptation.  See above for supported sub-options.
     */
    fluid.contextAware.makeAdaptation = function (options) {
        fluid.expect("fluid.contextAware.makeAdaptation", options, ["distributionName", "targetName", "adaptationName", "checkName", "record"]);
        fluid.defaults(options.distributionName, {
            gradeNames: ["fluid.component"],
            distributeOptions: {
                target: "{/ " + options.targetName + "}.options.contextAwareness." + options.adaptationName + ".checks." + options.checkName,
                record: options.record
            }
        });
        fluid.constructSingle([], options.distributionName);
    };
 
    // Context awareness for the browser environment
 
    fluid.contextAware.isBrowser = function () {
        return typeof(window) !== "undefined" && window.document;
    };
 
    fluid.contextAware.makeChecks({
        "fluid.browser": {
            funcName: "fluid.contextAware.isBrowser"
        }
    });
 
    // Context awareness for the reported browser platform name (operating system)
 
    fluid.registerNamespace("fluid.contextAware.browser");
 
    fluid.contextAware.browser.getPlatformName = function () {
        return typeof(navigator) !== "undefined" && navigator.platform ? navigator.platform : undefined;
    };
 
    // Context awareness for the reported user agent name
 
    fluid.contextAware.browser.getUserAgent = function () {
        return typeof(navigator) !== "undefined" && navigator.userAgent ? navigator.userAgent : undefined;
    };
 
    fluid.contextAware.makeChecks({
        "fluid.browser.platformName": {
            funcName: "fluid.contextAware.browser.getPlatformName"
        },
        "fluid.browser.userAgent": {
            funcName: "fluid.contextAware.browser.getUserAgent"
        }
    });
 
})(jQuery, fluid_3_0_0);