All files / framework/core/js NewViewSupport.js

100% Statements 14/14
75% Branches 3/4
100% Functions 3/3
100% Lines 14/14

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                                  3x   3x                 3x                                     3x 9x 9x             3x                                                 3x 4x 4x 4x 4x 4x                         3x                                                                  
/*
Copyright 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
*/
 
/*
    The contents of this file were adapted from ViewComponentSupport.js and ComponentGraph.js in fluid-authoring
    See: https://github.com/fluid-project/fluid-authoring/blob/FLUID-4884/src/js/ViewComponentSupport.js
         https://github.com/fluid-project/fluid-authoring/blob/FLUID-4884/src/js/ComponentGraph.js
*/
 
var fluid_3_0_0 = fluid_3_0_0 || {};
 
(function ($, fluid) {
    "use strict";
 
    /**
     * A variant of fluid.viewComponent that bypasses the wacky "initView" and variant signature
     * workflow, sourcing instead its "container" from an option of that name, so that this argument
     * can participate in standard ginger resolution. This enables useful results such as a component
     * which can render its own container into the DOM on startup, whilst the container remains immutable.
     */
    fluid.defaults("fluid.newViewComponent", {
        gradeNames: ["fluid.modelComponent"],
        members: {
            // 3rd argument is throwaway to force evaluation of container
            dom: "@expand:fluid.initDomBinder({that}, {that}.options.selectors, {that}.container)",
            container: "@expand:fluid.container({that}.options.container)"
        }
    });
 
    /**
     * Used to add an element to a parent container. Internally it can use either of jQuery's prepend or append methods.
     *
     * @param {jQuery|DOMElement|Selector} parentContainer - any jQueryable selector representing the parent element to
     *                                                       inject the `elm` into.
     * @param {DOMElement|jQuery} elm - a DOM element or jQuery element to be added to the parent.
     * @param {String} method - (optional) a string representing the method to use to add the `elm` to the
     *                          `parentContainer`. The method can be "append" (default), "prepend", or "html" (will
     *                          replace the contents).
     */
    fluid.newViewComponent.addToParent = function (parentContainer, elm, method) {
        method = method || "append";
        $(parentContainer)[method](elm);
    };
 
    /**
     * Similar to fluid.newViewComponent; however, it will render its own markup including its container, into a
     * specified parent container.
     */
    fluid.defaults("fluid.containerRenderingView", {
        gradeNames: ["fluid.newViewComponent"],
        container: "@expand:{that}.renderContainer()",
        // The DOM element which this component should inject its markup into on startup
        parentContainer: "fluid.notImplemented", // must be overridden
        injectionType: "append",
        invokers: {
            renderMarkup: "fluid.identity({that}.options.markup.container)",
            renderContainer: "fluid.containerRenderingView.renderContainer({that}, {that}.renderMarkup, {that}.addToParent)",
            addToParent: {
                funcName: "fluid.newViewComponent.addToParent",
                args: ["{that}.options.parentContainer", "{arguments}.0", "{that}.options.injectionType"]
            }
        }
    });
 
    /**
     * Renders the components markup and inserts it into the parent container based on the addToParent method
     *
     * @param {Component} that - the component
     * @param {Function} renderMarkup - a function returning the components container markup to be inserted into the
     *                                  parentContainer element
     * @param {Function} addToParent - a function that inserts the container into the DOM
     * @return {DOMElement} - the container
     */
    fluid.containerRenderingView.renderContainer = function (that, renderMarkup, addToParent) {
        fluid.log("Rendering container for " + that.id);
        var containerMarkup = renderMarkup();
        var container = $(containerMarkup);
        addToParent(container);
        return container;
    };
 
    /**
     * Similar to fluid.newViewComponent; however, it will fetch a template and render it into the container.
     *
     * The template path must be supplied either via a top level `template` option or directly to the
     * `resources.template` option. The path may optionally include "terms" to use as tokens which will be resolved
     * from values specified in the `terms` option.
     *
     * The template is fetched on creation and rendered into the container after it has been fetched. After rendering
     * the `afterRender` event is fired.
     */
    fluid.defaults("fluid.templateRenderingView", {
        gradeNames: ["fluid.newViewComponent", "fluid.resourceLoader"],
        resources: {
            template: "fluid.notImplemented"
        },
        injectionType: "append",
        events: {
            afterRender: null
        },
        listeners: {
            "onResourcesLoaded.render": "{that}.render",
            "onResourcesLoaded.afterRender": {
                listener: "{that}.events.afterRender",
                args: ["{that}"],
                priority: "after:render"
            }
        },
        invokers: {
            render: {
                funcName: "fluid.newViewComponent.addToParent",
                args: ["{that}.container", "{that}.resources.template.resourceText", "{that}.options.injectionType"]
            }
        },
        distributeOptions: {
            "mapTemplateSource": {
                source: "{that}.options.template",
                removeSource: true,
                target: "{that}.options.resources.template"
            }
        }
    });
 
})(jQuery, fluid_3_0_0);