|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/* global $:false */ |
| 4 | +/* global pullf: false */ |
| 5 | + |
| 6 | +// TODO remove jQuery dependency |
| 7 | + |
| 8 | +var Plotly = require('../plotly'); |
| 9 | +var d3 = require('d3'); |
| 10 | +var isNumeric = require('../isnumeric'); |
| 11 | + |
| 12 | +function showSources(td) { |
| 13 | + if(td._context && td._context.staticPlot) return; |
| 14 | + // show the sources of data in the active tab |
| 15 | + var allsources = td.sourcelist; |
| 16 | + if(!allsources) { |
| 17 | + getSources(td); |
| 18 | + return; |
| 19 | + } |
| 20 | + var container = d3.select(td).select('.js-sourcelinks'), |
| 21 | + extsources = allsources.filter(function(v){ |
| 22 | + return isNumeric(v.ref_fid); |
| 23 | + }), |
| 24 | + firstsource = extsources[0] || allsources[0]; |
| 25 | + container.text(''); |
| 26 | + td.shouldshowsources = false; |
| 27 | + // no sources at all? quit |
| 28 | + if(!firstsource) return; |
| 29 | + |
| 30 | + // find number of unique internal and external sources |
| 31 | + var extobj = {}, plotlyobj = {}; |
| 32 | + extsources.forEach(function(v){ extobj[v.url] = 1; }); |
| 33 | + allsources.forEach(function(v){ |
| 34 | + if(!isNumeric(v.ref_fid)) plotlyobj[v.ref_fid] = 1; |
| 35 | + }); |
| 36 | + |
| 37 | + var fidparts = String(firstsource.ref_fid).split(':'), |
| 38 | + isplot = Plotly.Lib.isPlotDiv(td), |
| 39 | + workspace = !isplot || td._context.workspace, |
| 40 | + mainlink, |
| 41 | + extraslink; |
| 42 | + |
| 43 | + if(isplot) { // svg version for plots |
| 44 | + // only sources from the same user? also quit, if we're on a plot |
| 45 | + var thisuser = firstsource.fid.split(':')[0]; |
| 46 | + if(allsources.every(function(v){ |
| 47 | + return String(v.ref_fid).split(':')[0]===thisuser; |
| 48 | + })) { |
| 49 | + return; |
| 50 | + } |
| 51 | + td.shouldshowsources = true; |
| 52 | + |
| 53 | + /** |
| 54 | + * in case someone REALLY doesn't want to show sources |
| 55 | + * they can hide them... |
| 56 | + * but you can always see them by going to the grid |
| 57 | + */ |
| 58 | + if(td.layout.hidesources) return; |
| 59 | + container.append('tspan').text('Source: '); |
| 60 | + mainlink = container.append('a').attr({'xlink:xlink:href':'#'}); |
| 61 | + if(isNumeric(firstsource.ref_fid)) { |
| 62 | + mainlink.attr({ |
| 63 | + 'xlink:xlink:show':'new', |
| 64 | + 'xlink:xlink:href':firstsource.ref_url |
| 65 | + }); |
| 66 | + } |
| 67 | + else if(!workspace){ |
| 68 | + mainlink.attr({ |
| 69 | + 'xlink:xlink:show':'new', |
| 70 | + 'xlink:xlink:href':'/'+fidparts[1]+'/~'+fidparts[0] |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + if(allsources.length>1) { |
| 75 | + container.append('tspan').text(' - '); |
| 76 | + extraslink = container.append('a') |
| 77 | + .attr({'xlink:xlink:href':'#'}); |
| 78 | + } |
| 79 | + } |
| 80 | + else { // html version for grids (and scripts?) |
| 81 | + if(!container.node()) { |
| 82 | + container = d3.select(td).select('.grid-container') |
| 83 | + .append('div') |
| 84 | + .attr('class', 'grid-sourcelinks js-sourcelinks'); |
| 85 | + } |
| 86 | + container.append('span').text('Source: '); |
| 87 | + mainlink = container.append('a').attr({ |
| 88 | + 'href':'#', |
| 89 | + 'class': 'link--impt' |
| 90 | + }); |
| 91 | + if(isNumeric(firstsource.ref_fid)) { |
| 92 | + mainlink.attr({ |
| 93 | + 'target':'_blank', |
| 94 | + 'href':firstsource.ref_url |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + if(allsources.length>1) { |
| 99 | + container.append('span').text(' - '); |
| 100 | + extraslink = container.append('a') |
| 101 | + .attr({href: '#'}) |
| 102 | + .classed('link--impt', true); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + mainlink.text(firstsource.ref_filename); |
| 107 | + |
| 108 | + function pullSource(){ |
| 109 | + pullf({fid: firstsource.ref_fid}); |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | + function fullSourcing(){ |
| 114 | + var sourceModal = $('#sourceModal'), |
| 115 | + sourceViewer = sourceModal.find('#source-viewer').empty(); |
| 116 | + |
| 117 | + sourceViewer.data('jsontree', '') |
| 118 | + .jsontree(JSON.stringify(sourceObj), |
| 119 | + {terminators: false, collapsibleOuter: false}) |
| 120 | + .show(); |
| 121 | + if(workspace) { |
| 122 | + sourceModal.find('[data-fid]').click(function(){ |
| 123 | + sourceModal.modal('hide'); |
| 124 | + pullf({fid:$(this).attr('data-fid')}); |
| 125 | + return false; |
| 126 | + }); |
| 127 | + } |
| 128 | + else { |
| 129 | + sourceModal.find('[data-fid]').each(function(){ |
| 130 | + fidparts = $(this).attr('data-fid').split(':'); |
| 131 | + $(this).attr({href:'/~'+fidparts[0]+'/'+fidparts[1]}); |
| 132 | + }); |
| 133 | + if(window.self !== window.top) { |
| 134 | + // in an iframe: basically fill the frame |
| 135 | + sourceModal.css({ |
| 136 | + left: '10px', |
| 137 | + right: '10px', |
| 138 | + bottom: '10px', |
| 139 | + width: 'auto', |
| 140 | + height: 'auto', |
| 141 | + margin: 0 |
| 142 | + }); |
| 143 | + } |
| 144 | + } |
| 145 | + sourceModal.modal('show'); |
| 146 | + |
| 147 | + sourceModal.find('.close') |
| 148 | + .off('click') |
| 149 | + .on('click', function(){ |
| 150 | + sourceModal.modal('hide'); |
| 151 | + return false; |
| 152 | + }); |
| 153 | + return false; |
| 154 | + } |
| 155 | + |
| 156 | + if(!isplot || workspace) mainlink.on('click', pullSource); |
| 157 | + |
| 158 | + if(extraslink) extraslink.text('Full list').on('click', fullSourcing); |
| 159 | + |
| 160 | + function makeSourceObj(container, refByUid) { |
| 161 | + if(cnt < 0) { |
| 162 | + console.log('infinite loop?'); |
| 163 | + return container; |
| 164 | + } |
| 165 | + cnt--; |
| 166 | + |
| 167 | + allsources.forEach(function(src){ |
| 168 | + if(src.ref_by_uid === refByUid) { |
| 169 | + var linkval; |
| 170 | + if(isNumeric(src.ref_fid)) { |
| 171 | + linkval = '<a href="' + src.ref_url + '" target="_blank">' + |
| 172 | + src.ref_filename + '</a>'; |
| 173 | + } |
| 174 | + else { |
| 175 | + var refUser = src.ref_fid.split(':')[0], |
| 176 | + fn = (refUser !== window.user ? refUser + ': ' : '') + |
| 177 | + src.ref_filename; |
| 178 | + linkval = '<a href="#" data-fid="' + src.ref_fid + '">'+ |
| 179 | + fn + '</a>'; |
| 180 | + } |
| 181 | + container[linkval] = makeSourceObj({}, src.uid); |
| 182 | + } |
| 183 | + }); |
| 184 | + return container; |
| 185 | + } |
| 186 | + |
| 187 | + var cnt = allsources.length, |
| 188 | + sourceObj = makeSourceObj({}, null); |
| 189 | +} |
| 190 | + |
| 191 | +function getSources(td) { |
| 192 | + var extrarefs = (td.ref_fids || []).join(','); |
| 193 | + if(!td.fid && !extrarefs) return; |
| 194 | + if(!window.PLOTLYENV || !window.PLOTLYENV.DOMAIN_WEBAPP) return; |
| 195 | + |
| 196 | + $.get('/getsources', {fid: td.fid, extrarefs:extrarefs}, function(res) { |
| 197 | + td.sourcelist = JSON.parse(res); |
| 198 | + if(!Array.isArray(td.sourcelist)) { |
| 199 | + console.log('sourcelist error',td.sourcelist); |
| 200 | + td.sourcelist = []; |
| 201 | + } |
| 202 | + showSources(td); |
| 203 | + }); |
| 204 | +} |
| 205 | + |
| 206 | +module.exports = showSources; |
0 commit comments