All files / framework/core/js MessageResolver.js

100% Statements 31/31
77.78% Branches 14/18
100% Functions 8/8
100% Lines 31/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                        42x   42x     42x                                                           42x 5511x 5511x 72x 5x     5439x                             42x 2459x 3x   2456x 2456x 2456x           42x 5511x 5558x 5558x 5558x 5439x                               42x 8x 8x 14x                                     42x 56x 7x 7x           49x 49x        
/*
Copyright 2011-2016 OCAD University
Copyright 2011 Lucendo Development Ltd.
 
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.defaults("fluid.messageResolver", {
        gradeNames: ["fluid.component"],
        mergePolicy: {
            messageBase: "nomerge",
            parents: "nomerge"
        },
        resolveFunc: fluid.stringTemplate,
        parseFunc: fluid.identity,
        messageBase: {},
        members: {
            messageBase: "@expand:{that}.options.parseFunc({that}.options.messageBase)"
        },
        invokers: {
            lookup: "fluid.messageResolver.lookup({that}, {arguments}.0)", // messagecodes
            resolve: "fluid.messageResolver.resolve({that}, {arguments}.0, {arguments}.1)" // messagecodes, args
        },
        parents: []
    });
 
    /**
     *
     * Look up the first matching message template, starting with the current grade and working up through its parents.
     * Returns both the template for the message and the function used to resolve the localised value.  By default
     * the resolve function is `fluid.stringTemplate`, and the template returned uses its syntax.
     *
     * @param {Object} that - The component itself.
     * @param {Array} messagecodes - One or more message codes to look up templates for.
     * @return {Object} - An object that contains`template` and `resolveFunc` members (see above).
     *
     */
    fluid.messageResolver.lookup = function (that, messagecodes) {
        var resolved = fluid.messageResolver.resolveOne(that.messageBase, messagecodes);
        if (resolved === undefined) {
            return fluid.find(that.options.parents, function (parent) {
                return parent ? parent.lookup(messagecodes) : undefined;
            });
        } else {
            return {template: resolved, resolveFunc: that.options.resolveFunc};
        }
    };
 
    /**
     *
     * Look up the first message that corresponds to a message code found in `messageCodes`.  Then, resolve its
     * localised value.  By default, supports variable substitutions using `fluid.stringTemplate`.
     *
     * @param {Object} that - The component itself.
     * @param {Array} messagecodes - A list of message codes to look for.
     * @param {Object} args - A map of variables that may potentially be used as part of the final output.
     * @return {String} - The final message, localised, with any variables found in `args`.
     *
     */
    fluid.messageResolver.resolve = function (that, messagecodes, args) {
        if (!messagecodes) {
            return "[No messagecodes provided]";
        }
        messagecodes = fluid.makeArray(messagecodes);
        var looked = that.lookup(messagecodes);
        return looked ? looked.resolveFunc(looked.template, args) :
            "[Message string for key " + messagecodes[0] + " not found]";
    };
 
 
    // unsupported, NON-API function
    fluid.messageResolver.resolveOne = function (messageBase, messagecodes) {
        for (var i = 0; i < messagecodes.length; ++i) {
            var code = messagecodes[i];
            var message = messageBase[code];
            if (message !== undefined) {
                return message;
            }
        }
    };
 
    /**
     *
     * Converts a data structure consisting of a mapping of keys to message strings, into a "messageLocator" function
     * which maps an array of message codes, to be tried in sequence until a key is found, and an array of substitution
     * arguments, into a substituted message string.
     *
     * @param {Object} messageBase - A body of messages to wrap in a resolver function.
     * @param {Function} resolveFunc (Optional) - A "resolver" function to use instead of the default `fluid.stringTemplate`.
     * @return {Function} - A "messageLocator" function (see above).
     *
     */
    fluid.messageLocator = function (messageBase, resolveFunc) {
        var resolver = fluid.messageResolver({messageBase: messageBase, resolveFunc: resolveFunc});
        return function (messagecodes, args) {
            return resolver.resolve(messagecodes, args);
        };
    };
 
    /**
     *
     * Resolve a "message source", which is either itself a resolver, or an object representing a bundle of messages
     * and the associated resolution function.
     *
     * When passing a "data" object, it is expected to have a `type` element that is set to `data`, and to have a
     * `messages` array and a `resolveFunc` function that can be used to resolve messages.
     *
     * A "resolver" is expected to be an object with a `type` element that is set to `resolver` that exposes a `resolve`
     * function.
     *
     * @param {Object} messageSource - See above.
     * @return {Function|String} - A resolve function or a `String` representing the final resolved output.
     *
     */
    fluid.resolveMessageSource = function (messageSource) {
        if (messageSource.type === "data") {
            Eif (messageSource.url === undefined) {
                return fluid.messageLocator(messageSource.messages, messageSource.resolveFunc);
            }
            else {
                // TODO: fetch via AJAX, and convert format if necessary
            }
        }
        else Eif (messageSource.type === "resolver") {
            return messageSource.resolver.resolve;
        }
    };
})(jQuery, fluid_3_0_0);