diff --git a/package-lock.json b/package-lock.json index 3f058f3..9fd1fe5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "safety-outlier-explorer", - "version": "2.5.4", + "version": "2.5.5", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/safetyOutlierExplorer.js b/safetyOutlierExplorer.js index 45120c8..77b49bb 100644 --- a/safetyOutlierExplorer.js +++ b/safetyOutlierExplorer.js @@ -859,6 +859,12 @@ updateNormalRangeControl.call(this); } + function initCustomEvents() { + var chart = this; + chart.participantsSelected = []; + chart.events.participantsSelected = new CustomEvent('participantsSelected'); + } + function onInit() { // 1. Count number of unique participant IDs in data prior to data cleaning. countParticipants.call(this); @@ -874,6 +880,9 @@ // 5. Check controls. checkControls.call(this); + + // 6. Initialize custom events + initCustomEvents.call(this); } function identifyControls() { @@ -1587,6 +1596,11 @@ }); if (this.multiples.chart) this.multiples.chart.destroy(); delete this.selected_id; + + //Trigger participantsSelected event + this.participantsSelected = []; + this.events.participantsSelected.data = this.participantsSelected; + this.wrap.node().dispatchEvent(this.events.participantsSelected); } function addOverlayEventListener() { @@ -2009,6 +2023,11 @@ context.selected_id = context.multiples.id; highlightSelected.call(context); smallMultiples.call(context); + + //Trigger participantsSelected event + context.participantsSelected = [context.selected_id]; + context.events.participantsSelected.data = context.participantsSelected; + context.wrap.node().dispatchEvent(context.events.participantsSelected); }); } @@ -2060,6 +2079,11 @@ highlightSelected.call(_this); reorderMarks.call(_this); smallMultiples.call(_this); + + //Trigger participantsSelected event + _this.participantsSelected = [_this.selected_id]; + _this.events.participantsSelected.data = _this.participantsSelected; + _this.wrap.node().dispatchEvent(_this.events.participantsSelected); }); } @@ -2085,6 +2109,11 @@ highlightSelected.call(_this); reorderMarks.call(_this); smallMultiples.call(_this); + + //Trigger participantsSelected event + _this.participantsSelected = [_this.selected_id]; + _this.events.participantsSelected.data = _this.participantsSelected; + _this.wrap.node().dispatchEvent(_this.events.participantsSelected); }); } diff --git a/src/callbacks/onInit.js b/src/callbacks/onInit.js index eef7572..3ad41b3 100644 --- a/src/callbacks/onInit.js +++ b/src/callbacks/onInit.js @@ -3,6 +3,7 @@ import cleanData from './onInit/cleanData'; import addVariables from './onInit/addVariables'; import defineSets from './onInit/defineSets'; import checkControls from './onInit/checkControls'; +import initCustomEvents from './onInit/initCustomEvents'; export default function onInit() { // 1. Count number of unique participant IDs in data prior to data cleaning. @@ -19,4 +20,7 @@ export default function onInit() { // 5. Check controls. checkControls.call(this); + + // 6. Initialize custom events + initCustomEvents.call(this); } diff --git a/src/callbacks/onInit/initCustomEvents.js b/src/callbacks/onInit/initCustomEvents.js new file mode 100644 index 0000000..fb9ee21 --- /dev/null +++ b/src/callbacks/onInit/initCustomEvents.js @@ -0,0 +1,5 @@ +export default function initCustomEvents() { + var chart = this; + chart.participantsSelected = []; + chart.events.participantsSelected = new CustomEvent('participantsSelected'); +} diff --git a/src/callbacks/onResize/addEventListeners/addLineEventListeners.js b/src/callbacks/onResize/addEventListeners/addLineEventListeners.js index 9dbcea3..075f50e 100644 --- a/src/callbacks/onResize/addEventListeners/addLineEventListeners.js +++ b/src/callbacks/onResize/addEventListeners/addLineEventListeners.js @@ -23,5 +23,10 @@ export default function addLineEventListeners() { highlightSelected.call(this); reorderMarks.call(this); smallMultiples.call(this); + + //Trigger participantsSelected event + this.participantsSelected = [this.selected_id]; + this.events.participantsSelected.data = this.participantsSelected; + this.wrap.node().dispatchEvent(this.events.participantsSelected); }); } diff --git a/src/callbacks/onResize/addEventListeners/addPointEventListeners.js b/src/callbacks/onResize/addEventListeners/addPointEventListeners.js index eb39f28..338569d 100644 --- a/src/callbacks/onResize/addEventListeners/addPointEventListeners.js +++ b/src/callbacks/onResize/addEventListeners/addPointEventListeners.js @@ -23,5 +23,10 @@ export default function addPointEventListeners() { highlightSelected.call(this); reorderMarks.call(this); smallMultiples.call(this); + + //Trigger participantsSelected event + this.participantsSelected = [this.selected_id]; + this.events.participantsSelected.data = this.participantsSelected; + this.wrap.node().dispatchEvent(this.events.participantsSelected); }); } diff --git a/src/callbacks/onResize/addEventListeners/functions/clearSelected.js b/src/callbacks/onResize/addEventListeners/functions/clearSelected.js index 8756615..4d1867f 100644 --- a/src/callbacks/onResize/addEventListeners/functions/clearSelected.js +++ b/src/callbacks/onResize/addEventListeners/functions/clearSelected.js @@ -8,4 +8,9 @@ export default function clearSelected() { }); if (this.multiples.chart) this.multiples.chart.destroy(); delete this.selected_id; + + //Trigger participantsSelected event + this.participantsSelected = []; + this.events.participantsSelected.data = this.participantsSelected; + this.wrap.node().dispatchEvent(this.events.participantsSelected); } diff --git a/src/callbacks/onResize/addEventListeners/functions/smallMultiples/callbacks/onLayout/updateParticipantDropdown.js b/src/callbacks/onResize/addEventListeners/functions/smallMultiples/callbacks/onLayout/updateParticipantDropdown.js deleted file mode 100644 index e69de29..0000000 diff --git a/src/callbacks/onResize/addEventListeners/functions/smallMultiples/updateParticipantDropdown.js b/src/callbacks/onResize/addEventListeners/functions/smallMultiples/updateParticipantDropdown.js index 6aae72b..b3a5c90 100644 --- a/src/callbacks/onResize/addEventListeners/functions/smallMultiples/updateParticipantDropdown.js +++ b/src/callbacks/onResize/addEventListeners/functions/smallMultiples/updateParticipantDropdown.js @@ -27,5 +27,10 @@ export default function updateParticipantDropdown() { context.selected_id = context.multiples.id; highlightSelected.call(context); smallMultiples.call(context); + + //Trigger participantsSelected event + context.participantsSelected = [context.selected_id]; + context.events.participantsSelected.data = context.participantsSelected; + context.wrap.node().dispatchEvent(context.events.participantsSelected); }); } diff --git a/src/util/polyfills.js b/src/util/polyfills.js index 32225f3..b8097c7 100644 --- a/src/util/polyfills.js +++ b/src/util/polyfills.js @@ -125,3 +125,17 @@ Math.log10 = function(x) { return Math.log(x) * Math.LOG10E; }; + + + (function() { + if (typeof window.CustomEvent === 'function') return false; + + function CustomEvent(event, params) { + params = params || { bubbles: false, cancelable: false, detail: null }; + var evt = document.createEvent('CustomEvent'); + evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail); + return evt; + } + + window.CustomEvent = CustomEvent; + })(); diff --git a/test-page/index.js b/test-page/index.js index 6a24c42..e8d4269 100644 --- a/test-page/index.js +++ b/test-page/index.js @@ -44,5 +44,11 @@ d3.csv( settings ); instance.init(data); + + //quick test of participantSelected event + instance.wrap.on("participantsSelected",function(){ + console.log("Participant Selected Event:") + console.log(d3.event.data) + }) } );