All files / framework/preferences/js PrimaryBuilder.js

100% Statements 24/24
91.67% Branches 11/12
100% Functions 5/5
100% Lines 24/24
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                      19x   19x     19x                   19x 432x 294x             432x 432x 432x             19x                                                                         19x 57x   57x         57x     57x 180x 180x 160x     57x 57x                 19x 537x 486x             19x                
/*
Copyright 2013 OCAD University
 
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.prefs.schemas");
 
    /**
     * A custom merge policy that merges primary schema blocks and
     * places them in the right location (consistent with the JSON schema
     * format).
     * @param {JSON} target - A base for merging the options.
     * @param {JSON} source - Options being merged.
     * @return {JSON} - The updated target.
     */
    fluid.prefs.schemas.merge = function (target, source) {
        if (!target) {
            target = {
                type: "object",
                properties: {}
            };
        }
        // We can handle both schema blocks in options directly and also inside
        // the |properties| field.
        source = source.properties || source;
        $.extend(true, target.properties, source);
        return target;
    };
 
    /*******************************************************************************
     * Primary builder grade
     *******************************************************************************/
 
    fluid.defaults("fluid.prefs.primaryBuilder", {
        gradeNames: ["fluid.component", "{that}.buildPrimary"],
        // An index of all schema grades registered with the framework.
        schemaIndex: {
            expander: {
                func: "fluid.indexDefaults",
                args: ["schemaIndex", {
                    gradeNames: "fluid.prefs.schemas",
                    indexFunc: "fluid.prefs.primaryBuilder.defaultSchemaIndexer"
                }]
            }
        },
        primarySchema: {},
        // A list of all necessarry top level preference names.
        typeFilter: [],
        invokers: {
            // An invoker used to generate a set of grades that comprise a
            // final version of the primary schema to be used by the PrefsEditor
            // builder.
            buildPrimary: {
                funcName: "fluid.prefs.primaryBuilder.buildPrimary",
                args: [
                    "{that}.options.schemaIndex",
                    "{that}.options.typeFilter",
                    "{that}.options.primarySchema"
                ]
            }
        }
    });
 
    /**
     * An invoker method that builds a list of grades that comprise a final version of the primary schema.
     * @param {JSON} schemaIndex - A global index of all schema grades registered with the framework.
     * @param {Array} typeFilter   - A list of all necessarry top level preference names.
     * @param {JSON} primarySchema - Primary schema provided as an option to the primary builder.
     * @return {Array} - A list of schema grades.
     */
    fluid.prefs.primaryBuilder.buildPrimary = function (schemaIndex, typeFilter, primarySchema) {
        var suppliedPrimaryGradeName = "fluid.prefs.schemas.suppliedPrimary" + fluid.allocateGuid();
        // Create a grade that has a primary schema passed as an option inclosed.
        fluid.defaults(suppliedPrimaryGradeName, {
            gradeNames: ["fluid.prefs.schemas"],
            schema: fluid.filterKeys(primarySchema.properties || primarySchema,
                typeFilter, false)
        });
        var primary = [];
        // Lookup all available schema grades from the index that match the
        // top level preference name.
        fluid.each(typeFilter, function merge(type) {
            var schemaGrades = schemaIndex[type];
            if (schemaGrades) {
                primary.push.apply(primary, schemaGrades);
            }
        });
        primary.push(suppliedPrimaryGradeName);
        return primary;
    };
 
    /**
     * An index function that indexes all shcema grades based on their
     * preference name.
     * @param {JSON} defaults -  Registered defaults for a schema grade.
     * @return {String}          A preference name.
     */
    fluid.prefs.primaryBuilder.defaultSchemaIndexer = function (defaults) {
        if (defaults.schema) {
            return fluid.keys(defaults.schema.properties);
        }
    };
 
    /*******************************************************************************
     * Base primary schema grade
     *******************************************************************************/
    fluid.defaults("fluid.prefs.schemas", {
        gradeNames: ["fluid.component"],
        mergePolicy: {
            schema: fluid.prefs.schemas.merge
        }
    });
 
})(jQuery, fluid_3_0_0);