From 78fbb84f9e5bc618ccea1090074e07724d98fc3e Mon Sep 17 00:00:00 2001 From: "Pawel Spychalski (DzikuVx)" Date: Thu, 21 Nov 2019 18:28:52 +0100 Subject: [PATCH] Basic framework to work with features --- gulpfile.js | 1 + js/defaults_dialog.js | 18 +++++------ js/feature_framework.js | 67 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 10 deletions(-) create mode 100644 js/feature_framework.js diff --git a/gulpfile.js b/gulpfile.js index 4df300a5..797c526a 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -114,6 +114,7 @@ sources.js = [ './tabs/advanced_tuning.js', './js/peripherals.js', './js/appUpdater.js', + './js/feature_framework.js', './js/defaults_dialog.js', './node_modules/openlayers/dist/ol.js' ]; diff --git a/js/defaults_dialog.js b/js/defaults_dialog.js index 25c353ce..d68ab183 100644 --- a/js/defaults_dialog.js +++ b/js/defaults_dialog.js @@ -1,3 +1,4 @@ +/*global mspHelper,$,GUI,MSP,BF_CONFIG,chrome*/ 'use strict'; var helper = helper || {}; @@ -192,20 +193,17 @@ helper.defaultsDialog = (function() { privateScope.setFeaturesBits = function (selectedDefaultPreset) { if (selectedDefaultPreset.features && selectedDefaultPreset.features.length > 0) { + helper.features.reset(); - for (let i in selectedDefaultPreset.features) { - if (selectedDefaultPreset.features.hasOwnProperty(i)) { - let feature = selectedDefaultPreset.features[i]; - - if (feature.state) { - BF_CONFIG.features = bit_set(BF_CONFIG.features, feature.bit); - } else { - BF_CONFIG.features = bit_clear(BF_CONFIG.features, feature.bit); - } + for (const feature of selectedDefaultPreset.features) { + if (feature.state) { + helper.features.set(feature.bit); + } else { + helper.features.unset(feature.bit); } } - mspHelper.saveBfConfig(function () { + helper.features.execute(function () { privateScope.setSettings(selectedDefaultPreset); }); } else { diff --git a/js/feature_framework.js b/js/feature_framework.js new file mode 100644 index 00000000..8b7caf29 --- /dev/null +++ b/js/feature_framework.js @@ -0,0 +1,67 @@ +/*global mspHelper,BF_CONFIG*/ +'use strict'; + +var helper = helper || {}; + +/* +Helper to work with FEATURES via MSP + +Usage: + +1. Reset everything +helper.features.reset(); + +2. Push feature bits you want to set +helper.features.set(5); + +3. Push feature bits you want to unset +helper.features.set(8); + +4. Execute and provide a callback that will be executed after MSP is done +helper.features.execute(function () { +//Do things crap over here +}); + +*/ +helper.features = (function() { + + let publicScope = {}, + privateScope = {}; + + let toSet = [], + toUnset = [], + exitPoint; + + publicScope.reset = function () { + toSet = []; + toUnset = []; + }; + + publicScope.set = function (bit) { + toSet.push(bit); + }; + + publicScope.unset = function (bit) { + toUnset.push(bit); + }; + + publicScope.execute = function(callback) { + exitPoint = callback; + mspHelper.loadBfConfig(privateScope.setBits); + }; + + privateScope.setBits = function () { + + for (const bit of toSet) { + BF_CONFIG.features = bit_set(BF_CONFIG.features, bit); + } + + for (const bit of toUnset) { + BF_CONFIG.features = bit_clear(BF_CONFIG.features, bit); + } + + mspHelper.saveBfConfig(exitPoint); + } + + return publicScope; +})(); \ No newline at end of file