From 5b983027e09f43a16ab6af2fafeeba474654ea9a Mon Sep 17 00:00:00 2001
From: skhilton
Date: Tue, 28 Jan 2020 16:39:06 -0800
Subject: [PATCH 1/8] export state as a json
---
docs/_javascript/fileSaver.js | 184 +++++++++++++++++++++++++++++
docs/_javascript/line_plot_zoom.js | 16 +++
docs/_javascript/main.js | 7 ++
docs/index.html | 1 +
4 files changed, 208 insertions(+)
create mode 100644 docs/_javascript/fileSaver.js
diff --git a/docs/_javascript/fileSaver.js b/docs/_javascript/fileSaver.js
new file mode 100644
index 00000000..da8d6ef9
--- /dev/null
+++ b/docs/_javascript/fileSaver.js
@@ -0,0 +1,184 @@
+(function (global, factory) {
+ if (typeof define === "function" && define.amd) {
+ define([], factory);
+ } else if (typeof exports !== "undefined") {
+ factory();
+ } else {
+ var mod = {
+ exports: {}
+ };
+ factory();
+ global.FileSaver = mod.exports;
+ }
+})(this, function () {
+ "use strict";
+
+ /*
+ * FileSaver.js
+ * A saveAs() FileSaver implementation.
+ *
+ * By Eli Grey, http://eligrey.com
+ *
+ * License : https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md (MIT)
+ * source : http://purl.eligrey.com/github/FileSaver.js
+ */
+ // The one and only way of getting global scope in all environments
+ // https://stackoverflow.com/q/3277182/1008999
+ var _global = typeof window === 'object' && window.window === window ? window : typeof self === 'object' && self.self === self ? self : typeof global === 'object' && global.global === global ? global : void 0;
+
+ function bom(blob, opts) {
+ if (typeof opts === 'undefined') opts = {
+ autoBom: false
+ };else if (typeof opts !== 'object') {
+ console.warn('Deprecated: Expected third argument to be a object');
+ opts = {
+ autoBom: !opts
+ };
+ } // prepend BOM for UTF-8 XML and text/* types (including HTML)
+ // note: your browser will automatically convert UTF-16 U+FEFF to EF BB BF
+
+ if (opts.autoBom && /^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) {
+ return new Blob([String.fromCharCode(0xFEFF), blob], {
+ type: blob.type
+ });
+ }
+
+ return blob;
+ }
+
+ function download(url, name, opts) {
+ var xhr = new XMLHttpRequest();
+ xhr.open('GET', url);
+ xhr.responseType = 'blob';
+
+ xhr.onload = function () {
+ saveAs(xhr.response, name, opts);
+ };
+
+ xhr.onerror = function () {
+ console.error('could not download file');
+ };
+
+ xhr.send();
+ }
+
+ function corsEnabled(url) {
+ var xhr = new XMLHttpRequest(); // use sync to avoid popup blocker
+
+ xhr.open('HEAD', url, false);
+
+ try {
+ xhr.send();
+ } catch (e) {}
+
+ return xhr.status >= 200 && xhr.status <= 299;
+ } // `a.click()` doesn't work for all browsers (#465)
+
+
+ function click(node) {
+ try {
+ node.dispatchEvent(new MouseEvent('click'));
+ } catch (e) {
+ var evt = document.createEvent('MouseEvents');
+ evt.initMouseEvent('click', true, true, window, 0, 0, 0, 80, 20, false, false, false, false, 0, null);
+ node.dispatchEvent(evt);
+ }
+ }
+
+ var saveAs = _global.saveAs || ( // probably in some web worker
+ typeof window !== 'object' || window !== _global ? function saveAs() {}
+ /* noop */
+ // Use download attribute first if possible (#193 Lumia mobile)
+ : 'download' in HTMLAnchorElement.prototype ? function saveAs(blob, name, opts) {
+ var URL = _global.URL || _global.webkitURL;
+ var a = document.createElement('a');
+ name = name || blob.name || 'download';
+ a.download = name;
+ a.rel = 'noopener'; // tabnabbing
+ // TODO: detect chrome extensions & packaged apps
+ // a.target = '_blank'
+
+ if (typeof blob === 'string') {
+ // Support regular links
+ a.href = blob;
+
+ if (a.origin !== location.origin) {
+ corsEnabled(a.href) ? download(blob, name, opts) : click(a, a.target = '_blank');
+ } else {
+ click(a);
+ }
+ } else {
+ // Support blobs
+ a.href = URL.createObjectURL(blob);
+ setTimeout(function () {
+ URL.revokeObjectURL(a.href);
+ }, 4E4); // 40s
+
+ setTimeout(function () {
+ click(a);
+ }, 0);
+ }
+ } // Use msSaveOrOpenBlob as a second approach
+ : 'msSaveOrOpenBlob' in navigator ? function saveAs(blob, name, opts) {
+ name = name || blob.name || 'download';
+
+ if (typeof blob === 'string') {
+ if (corsEnabled(blob)) {
+ download(blob, name, opts);
+ } else {
+ var a = document.createElement('a');
+ a.href = blob;
+ a.target = '_blank';
+ setTimeout(function () {
+ click(a);
+ });
+ }
+ } else {
+ navigator.msSaveOrOpenBlob(bom(blob, opts), name);
+ }
+ } // Fallback to using FileReader and a popup
+ : function saveAs(blob, name, opts, popup) {
+ // Open a popup immediately do go around popup blocker
+ // Mostly only available on user interaction and the fileReader is async so...
+ popup = popup || open('', '_blank');
+
+ if (popup) {
+ popup.document.title = popup.document.body.innerText = 'downloading...';
+ }
+
+ if (typeof blob === 'string') return download(blob, name, opts);
+ var force = blob.type === 'application/octet-stream';
+
+ var isSafari = /constructor/i.test(_global.HTMLElement) || _global.safari;
+
+ var isChromeIOS = /CriOS\/[\d]+/.test(navigator.userAgent);
+
+ if ((isChromeIOS || force && isSafari) && typeof FileReader !== 'undefined') {
+ // Safari doesn't allow downloading of blob URLs
+ var reader = new FileReader();
+
+ reader.onloadend = function () {
+ var url = reader.result;
+ url = isChromeIOS ? url : url.replace(/^data:[^;]*;/, 'data:attachment/file;');
+ if (popup) popup.location.href = url;else location = url;
+ popup = null; // reverse-tabnabbing #460
+ };
+
+ reader.readAsDataURL(blob);
+ } else {
+ var URL = _global.URL || _global.webkitURL;
+ var url = URL.createObjectURL(blob);
+ if (popup) popup.location = url;else location.href = url;
+ popup = null; // reverse-tabnabbing #460
+
+ setTimeout(function () {
+ URL.revokeObjectURL(url);
+ }, 4E4); // 40s
+ }
+ });
+ _global.saveAs = saveAs.saveAs = saveAs;
+
+ if (typeof module !== 'undefined') {
+ module.exports = saveAs;
+ }
+});
diff --git a/docs/_javascript/line_plot_zoom.js b/docs/_javascript/line_plot_zoom.js
index 2ccd1796..5da7175d 100644
--- a/docs/_javascript/line_plot_zoom.js
+++ b/docs/_javascript/line_plot_zoom.js
@@ -460,7 +460,23 @@ function genomeLineChart() {
focus.classed("brush_select", true).classed("brush_deselect", false)
}
});
+ exportbuttonchange = function(){
+ var state = {
+ "site": d3.selectAll('.selected').data().map(d => +d.site),
+ "condition": d3.select("#condition").property('value'),
+ "site-metric": d3.select("#site").property('value'),
+ "mut-metric": d3.select("#mutation_metric").property('value'),
+ "protein-representation": polymerSelect.value
+ }
+ var fname = prompt("File name: ")
+ if(fname === null){
+ fname = "dms-view.json"
+ }
+ state = new Blob([JSON.stringify(state)], {type: "text/plain;charset=utf-8"});
+ saveAs(state, fname);
+ };
+ // brush select/deselect choices
d3.selectAll("input[name='mode']").on("change", function(){
lastBrushTypeClick = this.value;
if ( this.value === 'select' ) {
diff --git a/docs/_javascript/main.js b/docs/_javascript/main.js
index ebbe0d15..99967abf 100644
--- a/docs/_javascript/main.js
+++ b/docs/_javascript/main.js
@@ -80,6 +80,13 @@ window.addEventListener('DOMContentLoaded', (event) => {
.classed("button", true)
.on('click', clearbuttonchange);
+ var exportButton = d3.select("#line_plot")
+ .insert("button", "svg")
+ .text("export state")
+ .attr("id", "exportButton")
+ .classed("button", true)
+ .on('click', exportbuttonchange);
+
var conditiondropdown = d3.select("#line_plot")
.insert("select", "svg")
.attr("id", 'condition')
diff --git a/docs/index.html b/docs/index.html
index e69a0bc4..c805af66 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -12,6 +12,7 @@
+
From 3ee0f845da0d3f4519395b9e4959216671e6444e Mon Sep 17 00:00:00 2001
From: skhilton
Date: Tue, 4 Feb 2020 16:41:17 -0800
Subject: [PATCH 2/8] Move state code to own script and read in / update state
---
docs/_javascript/line_plot_zoom.js | 17 +---------
docs/_javascript/state.js | 53 ++++++++++++++++++++++++++++++
docs/index.html | 7 ++++
3 files changed, 61 insertions(+), 16 deletions(-)
create mode 100644 docs/_javascript/state.js
diff --git a/docs/_javascript/line_plot_zoom.js b/docs/_javascript/line_plot_zoom.js
index 5da7175d..4218b905 100644
--- a/docs/_javascript/line_plot_zoom.js
+++ b/docs/_javascript/line_plot_zoom.js
@@ -256,7 +256,7 @@ function genomeLineChart() {
updateLogoPlot();
}
- var selectSite = function(circlePoint){
+ selectSite = function(circlePoint){
var circleData = circlePoint.data()[0];
// update the FOCUS plot
circlePoint.style("fill", color_key[circleData.site])
@@ -460,21 +460,6 @@ function genomeLineChart() {
focus.classed("brush_select", true).classed("brush_deselect", false)
}
});
- exportbuttonchange = function(){
- var state = {
- "site": d3.selectAll('.selected').data().map(d => +d.site),
- "condition": d3.select("#condition").property('value'),
- "site-metric": d3.select("#site").property('value'),
- "mut-metric": d3.select("#mutation_metric").property('value'),
- "protein-representation": polymerSelect.value
- }
- var fname = prompt("File name: ")
- if(fname === null){
- fname = "dms-view.json"
- }
- state = new Blob([JSON.stringify(state)], {type: "text/plain;charset=utf-8"});
- saveAs(state, fname);
- };
// brush select/deselect choices
d3.selectAll("input[name='mode']").on("change", function(){
diff --git a/docs/_javascript/state.js b/docs/_javascript/state.js
new file mode 100644
index 00000000..cd0b2e49
--- /dev/null
+++ b/docs/_javascript/state.js
@@ -0,0 +1,53 @@
+exportbuttonchange = function(){
+ var state = {
+ "site": d3.selectAll('.selected').data().map(d => +d.site),
+ "condition": d3.select("#condition").property('value'),
+ "site-metric": d3.select("#site").property('value'),
+ "mut-metric": d3.select("#mutation_metric").property('value'),
+ "protein-representation": polymerSelect.value
+ }
+ var fname = prompt("File name: ")
+ if(fname === null){
+ fname = "dms-view.json"
+ }
+ state = new Blob([JSON.stringify(state)], {type: "text/plain;charset=utf-8"});
+ saveAs(state, fname);
+};
+
+function markdownButtonChange () {
+ // Try to load the user's provided URL to a Markdown document.
+ const markdownUrl = d3.select("#state-url").property('value');
+ if (markdownUrl.length > 0) {
+ d3.json(markdownUrl).then(updateState);
+ }
+}
+
+var markdownButton = d3.select("#state-url-submit")
+ .on("click", markdownButtonChange);
+
+function updateState(state){
+ // select sites
+ state["site"].forEach(function(site){
+ selectSite(d3.select("#site_" + site))
+ })
+
+ // update condition
+ updateDropDownMenu("#condition", state["condition"])
+
+ // update site metric
+ updateDropDownMenu("#site", state["site-metric"])
+
+ // update mut metric
+ updateDropDownMenu("#mutation_metric", state["mut-metric"])
+
+// update protein representation
+polymerSelect.value = state["protein-representation"]
+changeProteinRepresentation(state["protein-representation"])
+}
+
+function updateDropDownMenu(dropdownid, target){
+ d3.select(dropdownid)
+ .selectAll("option")
+ .property('selected', function(d){return d === target;})
+ d3.select(dropdownid).dispatch("change")
+}
diff --git a/docs/index.html b/docs/index.html
index c805af66..d5a3258d 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -29,6 +29,12 @@
Instructions:
Select points of interest by clicking or brushing (click, hold and swipe).
Deselect points by clicking again or brushing after changing the dropdown from select to deselect.
Change between different sera, different site-level metrics and different mutation-level metrics using the dropwdown menus.
+