All files / framework/preferences/js Store.js

98% Statements 49/50
86.36% Branches 19/22
100% Functions 7/7
98% Lines 49/50

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 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                          10x   10x     10x                 10x 13x 7x       10x 3x 3x                       10x                                               10x                                           10x         10x 10x 10x 10x 6x     4x 4x 4x       4x 4x 4x 3x   4x                   10x 7x 7x   7x 2x     7x 5x     7x                 10x 3x 3x   3x 3x               10x                         10x                             10x                             10x 21x 21x 21x 21x 21x     10x   10x                            
/*
Copyright 2009 University of Toronto
Copyright 2011-2017 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";
 
    fluid.defaults("fluid.prefs.store", {
        gradeNames: ["fluid.dataSource", "fluid.contextAware"],
        contextAwareness: {
            strategy: {
                defaultGradeNames: "fluid.prefs.cookieStore"
            }
        }
    });
 
    fluid.prefs.store.decodeURIComponent = function (payload) {
        if (typeof payload === "string") {
            return decodeURIComponent(payload);
        }
    };
 
    fluid.prefs.store.encodeURIComponent = function (payload) {
        Eif (typeof payload === "string") {
            return encodeURIComponent(payload);
        }
    };
 
    /****************
     * Cookie Store *
     ****************/
 
    /**
     * SettingsStore Subcomponent that uses a cookie for persistence.
     * @param options {Object}
     */
    fluid.defaults("fluid.prefs.cookieStore", {
        gradeNames: ["fluid.dataSource"],
        cookie: {
            name: "fluid-ui-settings",
            path: "/",
            expires: ""
        },
        listeners: {
            "onRead.impl": {
                listener: "fluid.prefs.cookieStore.getCookie",
                args: ["{arguments}.1"]
            },
            "onRead.decodeURI": {
                listener: "fluid.prefs.store.decodeURIComponent",
                priority: "before:encoding"
            }
        },
        invokers: {
            get: {
                args: ["{that}", "{arguments}.0", "{that}.options.cookie"] // directModel, options/callback
            }
        }
    });
 
    fluid.defaults("fluid.prefs.cookieStore.writable", {
        gradeNames: ["fluid.dataSource.writable"],
        listeners: {
            "onWrite.encodeURI": {
                func: "fluid.prefs.store.encodeURIComponent",
                priority: "before:impl"
            },
            "onWrite.impl": {
                listener: "fluid.prefs.cookieStore.writeCookie"
            },
            "onWriteResponse.decodeURI": {
                listener: "fluid.prefs.store.decodeURIComponent",
                priority: "before:encoding"
            }
        },
        invokers: {
            set: {
                args: ["{that}", "{arguments}.0", "{arguments}.1", "{that}.options.cookie"] // directModel, model, options/callback
            }
        }
    });
 
    fluid.makeGradeLinkage("fluid.prefs.cookieStore.linkage", ["fluid.dataSource.writable", "fluid.prefs.cookieStore"], "fluid.prefs.cookieStore.writable");
 
    /*
     * Retrieve and return the value of the cookie
     */
    fluid.prefs.cookieStore.getCookie = function (options) {
        var cookieName = fluid.get(options, ["directModel", "cookieName"]) || options.name;
        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);
        if (endIndex < startIndex) {
            endIndex = cookie.length;
        }
        return cookie.substring(startIndex, endIndex);
    };
 
    /**
     * Assembles the cookie string
     * @param {String} cookieName - name of the cookie
     * @param {String} data - the serialized data to be stored in the cookie
     * @param {Object} options - settings
     * @return {String} - A string representing the assembled cookie.
     */
    fluid.prefs.cookieStore.assembleCookie = function (cookieName, data, options) {
        options = options || {};
        var cookieStr = cookieName + "=" + data;
 
        if (options.expires) {
            cookieStr += "; expires=" + options.expires;
        }
 
        if (options.path) {
            cookieStr += "; path=" + options.path;
        }
 
        return cookieStr;
    };
 
    /**
     * Saves the settings into a cookie
     * @param {Object} payload - the serialized data to write to the cookie
     * @param {Object} options - settings
     * @return {Object} - The original payload.
     */
    fluid.prefs.cookieStore.writeCookie = function (payload, options) {
        var cookieName = fluid.get(options, ["directModel", "cookieName"]) || options.name;
        var cookieStr = fluid.prefs.cookieStore.assembleCookie(cookieName, payload, options);
 
        document.cookie = cookieStr;
        return payload;
    };
 
 
    /**************
     * Temp Store *
     **************/
 
    fluid.defaults("fluid.dataSource.encoding.model", {
        gradeNames: "fluid.component",
        invokers: {
            parse: "fluid.identity",
            render: "fluid.identity"
        },
        contentType: "application/json"
    });
 
    /**
     * SettingsStore mock that doesn't do persistence.
     * @param options {Object}
     */
    fluid.defaults("fluid.prefs.tempStore", {
        gradeNames: ["fluid.dataSource", "fluid.modelComponent"],
        components: {
            encoding: {
                type: "fluid.dataSource.encoding.model"
            }
        },
        listeners: {
            "onRead.impl": {
                listener: "fluid.identity",
                args: ["{that}.model"]
            }
        }
    });
 
    fluid.defaults("fluid.prefs.tempStore.writable", {
        gradeNames: ["fluid.dataSource.writable", "fluid.modelComponent"],
        components: {
            encoding: {
                type: "fluid.dataSource.encoding.model"
            }
        },
        listeners: {
            "onWrite.impl": {
                listener: "fluid.prefs.tempStore.write",
                args: ["{that}", "{arguments}.0", "{arguments}.1"]
            }
        }
    });
 
    fluid.prefs.tempStore.write = function (that, settings) {
        var transaction = that.applier.initiate();
        transaction.fireChangeRequest({path: "", type: "DELETE"});
        transaction.change("", settings);
        transaction.commit();
        return that.model;
    };
 
    fluid.makeGradeLinkage("fluid.prefs.tempStore.linkage", ["fluid.dataSource.writable", "fluid.prefs.tempStore"], "fluid.prefs.tempStore.writable");
 
    fluid.defaults("fluid.prefs.globalSettingsStore", {
        gradeNames: ["fluid.component"],
        components: {
            settingsStore: {
                type: "fluid.prefs.store",
                options: {
                    gradeNames: ["fluid.resolveRootSingle", "fluid.dataSource.writable"],
                    singleRootType: "fluid.prefs.store"
                }
            }
        }
    });
 
})(jQuery, fluid_3_0_0);