All files / universal/gpii/node_modules/settingsHandlers/src WebSocketsSettingsHandler.js

98.11% Statements 52/53
83.33% Branches 10/12
100% Functions 12/12
98.11% Lines 52/53

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                              1x 1x 1x   1x                                                                                       1x 10x 10x 10x 10x 10x 10x 10x           1x 10x 14x 14x 8x             1x         1x 20x     1x 1x 1x 7x 7x     1x     1x 18x 18x 142x     142x 71x 71x 71x         142x   142x   142x           18x 9x   18x   18x     1x 18x 12x 12x 12x             1x 1x 1x     1x 18x 18x    
/*!
  GPII Web Sockets Settings Handler
 
  Copyright 2014, 2015 Emergya
  Copyright 2015 Raising the Floor - International
 
  Licensed under the New BSD license. You may not use this file except in
  compliance with this License.
 
  You may obtain a copy of the License at
  https://github.com/gpii/universal/LICENSE.txt
*/
 
"use strict";
 
var fluid = require("infusion"),
    $ = fluid.registerNamespace("jQuery"),
    gpii = fluid.registerNamespace("gpii");
 
fluid.defaults("gpii.settingsHandlers.webSockets.component", {
    gradeNames: ["fluid.modelComponent"],
    changeApplierOptions: {
        resolverGetConfig: fluid.model.escapedGetConfig,
        resolverSetConfig: fluid.model.escapedSetConfig
    },
    members: {
        clients: {}
    },
    model: {
        settings: {}
    },
    modelListeners: {
        "settings.*": {
            funcName: "gpii.settingsHandlers.webSockets.settingsChanged",
            args: "{change}"
        }
    },
    invokers: {
        addClient: {
            funcName: "gpii.settingsHandlers.webSockets.addClient",
            args: ["{that}", "{arguments}.0", "{arguments}.1"]
        },
        removeClient: {
            funcName: "gpii.settingsHandlers.webSockets.removeClient",
            args: ["{that}", "{arguments}.0"]
        },
        getSettingsForId: {
            funcName: "gpii.settingsHandlers.webSockets.getSettingsForId",
            args: ["{that}", "{arguments}.0"]
        },
        getImpl: {
            funcName: "gpii.settingsHandlers.webSockets.getImpl",
            args: ["{arguments}.0", "{that}"]
        },
        setImpl: {
            funcName: "gpii.settingsHandlers.webSockets.setImpl",
            args: ["{arguments}.0", "{that}"]
        }
    }
});
 
///////////////// Clients /////////////////////
 
gpii.settingsHandlers.webSockets.addClient = function (that, solutionId, client) {
    var initialSettings = fluid.get(that.model.settings, [solutionId]);
    var currentValue = fluid.get(that.clients, [solutionId, client.id]);
    Eif (!currentValue) {
        fluid.set(that.clients, [solutionId, client.id], client);
        client.sendTypedMessage("connectionSucceeded", initialSettings);
        client.events.onDestroy.addListener(function () {
            that.removeClient(client);
        });
    }
 
};
 
gpii.settingsHandlers.webSockets.removeClient = function (that, client) {
    for (var solutionId in that.clients) {
        delete that.clients[solutionId][client.id];
        if ($.isEmptyObject(that.clients[solutionId])) {
            delete that.clients[solutionId];
        }
    }
};
 
///////////////// Settings /////////////////////
 
gpii.settingsHandlers.webSockets.settingsChanged = function (/*change*/) {
    // Function retained for debugging purposes
    // console.log("A change in settings has been registered: " + JSON.stringify(change));
};
 
gpii.settingsHandlers.webSockets.getSettingsForId = function (that, solutionId) {
    return fluid.get(that.model.settings, [solutionId]);
};
 
gpii.settingsHandlers.webSockets.getImpl = function (payload, that) {
    var path = payload.options.path;
    var results = fluid.transform(payload.settings, function (value, key) {
        var currentValue = fluid.get(that.model.settings, [path, key]);
        return currentValue;
    });
 
    return results;
};
 
gpii.settingsHandlers.webSockets.setImpl = function (payload, that) {
    var path = payload.options.path;
    var results = fluid.transform(payload.settings, function (value, key) {
        var oldValue = fluid.get(that.model.settings, [path, key]);
        var type;
 
        if (oldValue === undefined) {
            type = "ADD";
        } else Eif (value === undefined) {
            type = "DELETE";
        } else {
            type = null;
        }
 
        that.applier.change(["settings", path, key], value, type);
 
        var newValue = fluid.get(that.model.settings, [path, key]);
 
        return {
            oldValue: oldValue,
            newValue: newValue
        };
    });
 
    if ($.isEmptyObject(that.model.settings[path])) {
        delete that.model.settings[path];
    }
    gpii.settingsHandlers.webSockets.notifySettings(path, that);
 
    return results;
};
 
gpii.settingsHandlers.webSockets.notifySettings = function (id, that) {
    if (id in that.clients) {
        var newSettings = gpii.settingsHandlers.webSockets.getSettingsForId(that, id);
        for (var client in that.clients[id]) {
            that.clients[id][client].sendTypedMessage("onSettingsChanged", newSettings);
        }
    }
};
 
// Top-level driver methods for SettingsHandler
 
gpii.settingsHandlers.webSockets.get = function (payload) {
    var instance = gpii.settingsHandlers.webSockets.instance; // this is placed there by FlowManager's mountInstance
    return gpii.settingsHandlers.invokeSettingsHandler(instance.getImpl, payload);
};
 
gpii.settingsHandlers.webSockets.set = function (payload) {
    var instance = gpii.settingsHandlers.webSockets.instance; // this is placed there by FlowManager's mountInstance
    return gpii.settingsHandlers.invokeSettingsHandler(instance.setImpl, payload);
};