|
1 | | -/*global app, countlyVue, countlySDK, CV, countlyCommon, CountlyHelpers*/ |
| 1 | +/*global app, countlyVue, countlySDK, CV, countlyCommon, CountlyHelpers, Vue*/ |
2 | 2 | (function() { |
3 | 3 | var enable_logs = false; |
4 | 4 | var SC_VER = 2; // check/update sdk/api/api.js for this |
|
103 | 103 |
|
104 | 104 | var SDKConfigurationView = countlyVue.views.create({ |
105 | 105 | template: CV.T('/sdk/templates/config.html'), |
| 106 | + mixins: [countlyVue.mixins.hasDrawers('sdkSettings')], |
106 | 107 | created: function() { |
107 | 108 | var self = this; |
108 | 109 | Promise.all([ |
|
409 | 410 | }); |
410 | 411 | }, |
411 | 412 | methods: { |
| 413 | + openSettingsDrawer: function() { |
| 414 | + this.openDrawer('sdkSettings', {}); |
| 415 | + var self = this; |
| 416 | + this.$nextTick(function() { |
| 417 | + var config = Object.assign({}, self.$store.getters["countlySDK/sdk/all"]); |
| 418 | + if (typeof config.bom_rqp !== "undefined") { |
| 419 | + config.bom_rqp = config.bom_rqp / 100; |
| 420 | + } |
| 421 | + for (var key in config) { // remove internal |
| 422 | + if (self.configs[key] && self.configs[key].type === "preset") { |
| 423 | + delete config[key]; |
| 424 | + } |
| 425 | + if (self.configs[key] && self.configs[key].enforced === false) { |
| 426 | + delete config[key]; |
| 427 | + } |
| 428 | + } |
| 429 | + var obj = {v: SC_VER, t: Date.now(), c: config}; |
| 430 | + var attempt = 0; |
| 431 | + var applyJson = function() { |
| 432 | + if (self.$refs.sdkSettingsDrawer && self.$refs.sdkSettingsDrawer.setRawJson) { |
| 433 | + self.$refs.sdkSettingsDrawer.setRawJson(JSON.stringify(obj, null, 2)); |
| 434 | + } |
| 435 | + else if (attempt < 5) { // retry a few times if component not yet ready |
| 436 | + attempt += 1; setTimeout(applyJson, 150); |
| 437 | + } |
| 438 | + }; |
| 439 | + applyJson(); |
| 440 | + }); |
| 441 | + }, |
| 442 | + onRawSettingsSaved: function(updated) { |
| 443 | + var cfg = updated.c || updated; |
| 444 | + if (typeof cfg.bom_rqp !== 'undefined') { |
| 445 | + cfg.bom_rqp = Math.round(cfg.bom_rqp * 100); // revert to integer percentage |
| 446 | + } |
| 447 | + // derive enforcement from key presence |
| 448 | + var enforcement = {}; |
| 449 | + for (var k in this.configs) { |
| 450 | + if (this.configs[k].type === 'preset') { |
| 451 | + continue; |
| 452 | + } |
| 453 | + enforcement[k] = Object.prototype.hasOwnProperty.call(cfg, k); |
| 454 | + } |
| 455 | + |
| 456 | + // save config and enforcement |
| 457 | + var self = this; |
| 458 | + var saveConfigReq = CV.$.ajax({ |
| 459 | + type: 'POST', |
| 460 | + url: countlyCommon.API_PARTS.data.w + '/sdk-config/upload', |
| 461 | + data: { app_id: countlyCommon.ACTIVE_APP_ID, config: JSON.stringify({c: cfg}) }, |
| 462 | + dataType: 'json' |
| 463 | + }); |
| 464 | + var saveEnforcementReq = CV.$.ajax({ |
| 465 | + type: 'POST', |
| 466 | + url: countlyCommon.API_PARTS.data.w + '/sdk-config/update-enforcement', |
| 467 | + data: { app_id: countlyCommon.ACTIVE_APP_ID, enforcement: JSON.stringify(enforcement) }, |
| 468 | + dataType: 'json' |
| 469 | + }); |
| 470 | + Promise.all([saveConfigReq, saveEnforcementReq]).then(function() { |
| 471 | + for (var key in enforcement) { // reflect enforcement changes in UI |
| 472 | + if (self.configs[key]) { |
| 473 | + self.configs[key].enforced = enforcement[key]; |
| 474 | + } |
| 475 | + } |
| 476 | + self.$store.dispatch('countlySDK/initialize').then(function() { |
| 477 | + self.$store.dispatch('countlySDK/initializeEnforcement').then(function() { |
| 478 | + self.diff = []; |
| 479 | + CountlyHelpers.notify({title: 'SDK Config', message: 'Configuration saved', type: 'success'}); |
| 480 | + }); |
| 481 | + }); |
| 482 | + }).catch(function(xhr) { |
| 483 | + CountlyHelpers.alert((xhr && xhr.responseJSON && xhr.responseJSON.result) || 'Error saving configuration', 'red'); |
| 484 | + }); |
| 485 | + }, |
412 | 486 | onChange: function(key, value) { |
413 | 487 | log("onChange", key, value); |
414 | 488 | this.configs[key].value = value; |
|
758 | 832 | } |
759 | 833 | } |
760 | 834 | }); |
| 835 | + Vue.component("cly-sdk-settings-drawer", countlyVue.views.create({ |
| 836 | + props: { controls: {type: Object} }, |
| 837 | + data: function() { |
| 838 | + return { rawJson: '', jsonError: '', showInfo: true, original: '', diffKeys: [] }; |
| 839 | + }, |
| 840 | + computed: { |
| 841 | + isSaveDisabled: function() { |
| 842 | + return !!this.jsonError || this.rawJson.trim().length === 0 || this.rawJson === this.original; |
| 843 | + } |
| 844 | + }, |
| 845 | + methods: { |
| 846 | + setRawJson: function(v) { |
| 847 | + this.rawJson = v; this.original = v; this.jsonError = ''; this.updateDiff(); |
| 848 | + }, |
| 849 | + formatJson: function() { |
| 850 | + try { |
| 851 | + this.rawJson = JSON.stringify(JSON.parse(this.rawJson), null, 2); this.jsonError = ''; |
| 852 | + } |
| 853 | + catch (e) { |
| 854 | + this.jsonError = 'Invalid JSON: ' + e.message; |
| 855 | + } |
| 856 | + this.updateDiff(); |
| 857 | + }, |
| 858 | + onInput: function() { |
| 859 | + try { |
| 860 | + JSON.parse(this.rawJson); this.jsonError = ''; |
| 861 | + } |
| 862 | + catch (e) { |
| 863 | + this.jsonError = e.message; |
| 864 | + } |
| 865 | + this.updateDiff(); |
| 866 | + }, |
| 867 | + updateDiff: function() { |
| 868 | + var diff = []; |
| 869 | + try { |
| 870 | + var current = JSON.parse(this.rawJson); |
| 871 | + var original = JSON.parse(this.original); |
| 872 | + var curC = current.c || current; var orgC = original.c || original; |
| 873 | + var keys = {}; Object.keys(curC).forEach(k=>{ |
| 874 | + keys[k] = true; |
| 875 | + }); Object.keys(orgC).forEach(k=>{ |
| 876 | + keys[k] = true; |
| 877 | + }); |
| 878 | + Object.keys(keys).forEach(k=>{ |
| 879 | + if (JSON.stringify(curC[k]) !== JSON.stringify(orgC[k])) { |
| 880 | + diff.push(k); |
| 881 | + } |
| 882 | + }); |
| 883 | + } |
| 884 | + catch (e) { |
| 885 | + // Ignore JSON parse errors while user is typing |
| 886 | + } |
| 887 | + this.diffKeys = diff; |
| 888 | + }, |
| 889 | + handleClose: function() { |
| 890 | + this.jsonError = ''; |
| 891 | + }, |
| 892 | + onSubmit: function() { |
| 893 | + if (this.jsonError) { |
| 894 | + return; |
| 895 | + } |
| 896 | + var parsed; |
| 897 | + try { |
| 898 | + parsed = JSON.parse(this.rawJson); |
| 899 | + } |
| 900 | + catch (e) { |
| 901 | + this.jsonError = e.message; return; |
| 902 | + } |
| 903 | + this.$emit('saved', parsed); |
| 904 | + this.original = this.rawJson; |
| 905 | + } |
| 906 | + }, |
| 907 | + template: CV.T('/sdk/templates/settings_drawer.html') |
| 908 | + })); |
761 | 909 | countlyVue.container.registerTab("/manage/sdk", { |
762 | 910 | priority: 2, |
763 | 911 | route: "#/manage/sdk/configurations", |
|
0 commit comments