all files / framework/preferences/js/ Store.js

92.11% Statements 35/38
75% Branches 9/12
100% Functions 5/5
92.11% Lines 35/38
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                          12×   12×               12×               12×                                 12×                                           12×                                   12× 14×   14×     14× 10×     14×               12×                       12×                           12× 44× 44×     12×                            
/*
Copyright 2009 University of Toronto
Copyright 2011-2013 OCAD University
Copyright 2015 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";
 
    /**
     * A Generic data source grade that defines an API for getting and setting
     * data.
     */
     // TODO: unify with Kettle's and ultimately Infusion's dataSource
    fluid.defaults("fluid.prefs.dataSource", {
        gradeNames: ["fluid.component"],
        invokers: {
            get: "fluid.notImplemented",
            set: "fluid.notImplemented"
        }
    });
 
    fluid.defaults("fluid.prefs.store", {
        gradeNames: ["fluid.prefs.dataSource", "fluid.contextAware"],
        contextAwareness: {
            strategy: {
                defaultGradeNames: "fluid.prefs.cookieStore"
            }
        }
    });
 
    /****************
     * Cookie Store *
     ****************/
 
    /**
     * SettingsStore Subcomponent that uses a cookie for persistence.
     * @param {Object} options
     */
    fluid.defaults("fluid.prefs.cookieStore", {
        gradeNames: ["fluid.prefs.dataSource"],
        cookie: {
            name: "fluid-ui-settings",
            path: "/",
            expires: ""
        },
        invokers: {
            get: {
                funcName: "fluid.prefs.cookieStore.get",
                args: "{that}.options.cookie.name"
            },
            set: {
                funcName: "fluid.prefs.cookieStore.set",
                args: ["{arguments}.0", "{that}.options.cookie"]
            }
        }
    });
 
    /**
     * Retrieve and return the value of the cookie
     */
    fluid.prefs.cookieStore.get = function (cookieName) {
        var cookie = document.cookie;
        if (cookie.length <= 0) {
            return;
        }
 
        var cookiePrefix = cookieName + "=";
        var startIndex = cookie.indexOf(cookiePrefix);
        Iif (startIndex < 0) {
            return;
        }
 
        startIndex = startIndex + cookiePrefix.length;
        var endIndex = cookie.indexOf(";", startIndex);
        Eif (endIndex < startIndex) {
            endIndex = cookie.length;
        }
        var cookieSection = cookie.substring(startIndex, endIndex);
        var togo;
        try {
            togo = JSON.parse(decodeURIComponent(cookieSection));
        } catch (e) {
            fluid.log("Error parsing cookie " + cookieSection + " as JSON - clearing");
            document.cookie = "";
        }
        return togo;
    };
 
    /**
     * Assembles the cookie string
     * @param {Object} cookie settings
     */
    fluid.prefs.cookieStore.assembleCookie = function (cookieOptions) {
        var cookieStr = cookieOptions.name + "=" + cookieOptions.data;
 
        if (cookieOptions.expires) {
            cookieStr += "; expires=" + cookieOptions.expires;
        }
 
        if (cookieOptions.path) {
            cookieStr += "; path=" + cookieOptions.path;
        }
 
        return cookieStr;
    };
 
    /**
     * Saves the settings into a cookie
     * @param {Object} settings
     * @param {Object} cookieOptions
     */
    fluid.prefs.cookieStore.set = function (settings, cookieOptions) {
        cookieOptions.data = encodeURIComponent(JSON.stringify(settings));
        document.cookie = fluid.prefs.cookieStore.assembleCookie(cookieOptions);
    };
 
 
    /**************
     * Temp Store *
     **************/
 
    /**
     * SettingsStore mock that doesn't do persistence.
     * @param {Object} options
     */
    fluid.defaults("fluid.prefs.tempStore", {
        gradeNames: ["fluid.prefs.dataSource", "fluid.modelComponent"],
        invokers: {
            get: {
                funcName: "fluid.identity",
                args: "{that}.model"
            },
            set: {
                funcName: "fluid.prefs.tempStore.set",
                args: ["{arguments}.0", "{that}.applier"]
            }
        }
    });
 
    fluid.prefs.tempStore.set = function (settings, applier) {
        applier.fireChangeRequest({path: "", type: "DELETE"});
        applier.change("", settings);
    };
 
    fluid.defaults("fluid.prefs.globalSettingsStore", {
        gradeNames: ["fluid.component"],
        components: {
            settingsStore: {
                type: "fluid.prefs.store",
                options: {
                    gradeNames: ["fluid.resolveRootSingle"],
                    singleRootType: "fluid.prefs.store"
                }
            }
        }
    });
 
})(jQuery, fluid_3_0_0);