gpii-universal
Version:
Cross platform, core components of the GPII personalization infrastructure.
159 lines (132 loc) • 5.14 kB
JavaScript
/*!
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]);
if (!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 if (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);
};