1
1
'use strict' ;
2
+
2
3
// ---external global dependencies
3
- /* global d3:false, MathJax:false, FB:false, PLOTLYENV:false,
4
+ /* global d3:false, MathJax:false
4
5
Promise:false */
5
6
6
7
var util = module . exports = { } ,
7
8
Plotly = require ( './plotly' ) ;
8
9
9
- // Script Loader
10
- /////////////////////////////
11
-
12
- util . scriptLoader = function ( d , w ) {
13
- var config = { loadDelay : 0 } ;
14
- function exports ( _scriptNames ) {
15
- var loadDelay = config . loadDelay ;
16
- var scriptNames = [ ] . concat ( _scriptNames ) ;
17
-
18
- // Generic script injection
19
- var newScript , baseScript = d . getElementsByTagName ( 'script' ) [ 0 ] ;
20
- function loadScript ( id , src , delay , callback ) {
21
- if ( d . getElementById ( id ) ) { return ; }
22
- setTimeout ( function ( ) {
23
- newScript = d . createElement ( 'script' ) ;
24
- newScript . type = 'text/javascript' ;
25
- newScript . id = id ;
26
- newScript . async = true ;
27
- newScript . src = src ;
28
- baseScript . parentNode . insertBefore ( newScript , baseScript ) ;
29
- if ( callback ) callback . call ( window ) ;
30
- } , delay ) ;
31
- }
32
-
33
- var protocol = / ^ h t t p : / . test ( d . location ) ? 'http' : 'https' ;
34
- var protocol2 = ( protocol === 'https' ) ? 'https://ssl' : 'http://www' ;
35
-
36
- if ( scriptNames . indexOf ( 'facebook' ) !== - 1 ) {
37
- var body = document . querySelector ( 'body' ) ;
38
- var bodyFirstChild = body . firstChild ;
39
- var newDiv = document . createElement ( 'div' ) ;
40
- newDiv . id = 'fb-root' ;
41
- body . insertBefore ( newDiv , bodyFirstChild ) ;
42
- w . fbAsyncInit = function ( ) {
43
- FB . init ( {
44
- appId : PLOTLYENV . FACEBOOK_PAGE_APP_ID ,
45
- /*channelUrl : 'WWW.YOUR_DOMAIN.COM/channel.html'*/
46
- status : true ,
47
- cookie : true ,
48
- xfbml : true
49
- } ) ;
50
- } ;
51
- var debug = false ;
52
- loadScript ( 'facebook-jssdk' , '//connect.facebook.net/en_US/all' +
53
- ( debug ? '/debug' : '' ) + '.js' , loadDelay ) ;
54
- }
55
-
56
- if ( scriptNames . indexOf ( 'googleAnalytics' ) !== - 1 ) {
57
- w . _gaq = w . _gaq || [ ] ;
58
- w . _gaq . push ( [ '_setAccount' , PLOTLYENV . GOOGLEANALYTICS_ACCOUNT ] ) ;
59
- w . _gaq . push ( [ '_setSiteSpeedSampleRate' , 10 ] ) ;
60
- w . _gaq . push ( [ '_trackPageview' ] ) ;
61
- loadScript ( 'google-analytics' , protocol2 + '.google-analytics.com/ga.js' , loadDelay ) ;
62
- }
63
-
64
- if ( scriptNames . indexOf ( 'googlePlus' ) !== - 1 ) {
65
- loadScript ( 'google-plus' , 'https://apis.google.com/js/plusone.js' , loadDelay ) ;
66
- }
67
-
68
- if ( scriptNames . indexOf ( 'twitter' ) !== - 1 ) {
69
- loadScript ( 'twitter-wjs' , protocol + '://platform.twitter.com/widgets.js' , loadDelay ) ;
70
- }
71
-
72
- }
73
-
74
- exports . config = function ( _config ) {
75
- config = _config ;
76
- return this ;
77
- } ;
78
-
79
- return exports ;
80
- } ;
81
-
82
-
83
- // Image exporter
84
- /////////////////////////////
85
-
86
- util . imageExporter = function ( ) {
87
-
88
- var dispatch = d3 . dispatch ( 'success' , 'error' ) ;
89
- var imageFormat = 'png' ,
90
- targetSize = { width : 300 , height : 150 } ,
91
- sourceSize = { width : 300 , height : 150 } ,
92
- outputType = 'url' ,
93
- debugLevel = 0 ,
94
- canvasContainer ,
95
- title = 'Converted Image' ,
96
- canvasElId = 'canvasEl' ,
97
- canvasContainerId = 'canvasContainer' ;
98
-
99
- function exports ( _svg ) {
100
- var xmlString , w , h ;
101
- if ( typeof _svg === 'string' ) {
102
- xmlString = _svg ;
103
- w = sourceSize . width ;
104
- h = sourceSize . height ;
105
- }
106
- else if ( ! ! _svg . append || ! ! _svg . className ) {
107
- var svgNode = ( ! ! _svg . append ) ? _svg . node ( ) : _svg ;
108
- var serializer = new XMLSerializer ( ) ;
109
- xmlString = serializer . serializeToString ( svgNode ) ;
110
- w = svgNode . offsetWidth ;
111
- h = svgNode . offsetHeight ;
112
- }
113
- else return sendError ( 'wrong input svg (d3 selection or DOM node)' ) ;
114
-
115
- if ( canvasContainer ) canvasContainer . html ( '' ) ;
116
- canvasContainer = d3 . select ( 'body' ) . append ( 'div' )
117
- . attr ( { id : canvasContainerId } )
118
- . style ( { position : 'absolute' , top : 0 , left : 0 , 'z-index' : 1000 } ) ;
119
- if ( debugLevel < 2 ) canvasContainer . style ( { visibility : 'hidden' } ) ;
120
- var canvasEl = canvasContainer . append ( 'canvas' ) . attr ( { id : canvasElId , width : w , height : h } ) ;
121
- var canvasNode = canvasEl . node ( ) ;
122
-
123
- var ctx = canvasNode . getContext ( '2d' ) ;
124
- var DOMURL = self . URL || self . webkitURL || self ;
125
- var img = new Image ( ) ;
126
- var svg = new Blob ( [ xmlString ] , { type : 'image/svg+xml;charset=utf-8' } ) ;
127
- var url = DOMURL . createObjectURL ( svg ) ;
128
- img . onload = function ( ) {
129
- ctx . drawImage ( img , 0 , 0 ) ;
130
- DOMURL . revokeObjectURL ( url ) ;
131
- encodeAll ( ) ;
132
- } ;
133
- img . onerror = function ( ) {
134
- DOMURL . revokeObjectURL ( url ) ;
135
- return sendError ( 'img didnt load' ) ;
136
- } ;
137
- img . src = url ;
138
-
139
- function encodeAll ( ) {
140
- setTimeout ( function ( ) {
141
- var imgData ;
142
- if ( imageFormat === 'jpeg' ) {
143
- imgData = canvasNode . toDataURL ( 'image/jpeg' ) ;
144
- }
145
- else if ( imageFormat === 'png' ) {
146
- imgData = canvasNode . toDataURL ( 'image/png' ) ;
147
- }
148
- else if ( imageFormat === 'webp' ) {
149
- imgData = canvasNode . toDataURL ( 'image/webp' ) ;
150
- }
151
- else if ( imageFormat === 'svg' ) imgData = _svg ;
152
- else {
153
- return sendError ( { err : 'Image format is not jpeg, png, or svg' , code : 400 } ) ;
154
- }
155
-
156
- if ( debugLevel === 0 ) {
157
- canvasContainer . remove ( ) ;
158
- canvasEl . remove ( ) ;
159
- }
160
- if ( imgData ) {
161
- dispatch . success ( imgData ) ;
162
- }
163
- else sendError ( { err : 'Image is empty' , code : 530 } ) ;
164
- } , 0 ) ;
165
- }
166
- }
167
-
168
- function sendError ( _msg ) {
169
- dispatch . error ( _msg ) ;
170
- if ( debugLevel === 1 ) console . log ( 'Error: ' + _msg ) ;
171
- else if ( debugLevel === 2 ) throw ( _msg ) ;
172
- }
173
- d3 . rebind ( exports , dispatch , 'on' ) ;
174
- exports . imageFormat = function ( _imageFormat ) {
175
- imageFormat = _imageFormat ; //png, svg, jpg, pdf
176
- return this ;
177
- } ;
178
- exports . outputType = function ( _outputType ) {
179
- outputType = _outputType ; //img, link, url, forceDownload, downloadLink
180
- return this ;
181
- } ;
182
- exports . debugLevel = function ( _level ) {
183
- debugLevel = _level ;
184
- return this ;
185
- } ;
186
- exports . title = function ( _title ) {
187
- title = _title ;
188
- return this ;
189
- } ;
190
- exports . sourceSize = function ( _sourceSize ) {
191
- sourceSize = _sourceSize ;
192
- return this ;
193
- } ;
194
- exports . targetSize = function ( _targetSize ) {
195
- targetSize = _targetSize ;
196
- return this ;
197
- } ;
198
- exports . canvasContainerId = function ( _canvasContainerId ) {
199
- canvasContainerId = _canvasContainerId ;
200
- return this ;
201
- } ;
202
- exports . canvasElId = function ( _canvasElId ) {
203
- canvasElId = _canvasElId ;
204
- return this ;
205
- } ;
206
- return exports ;
207
- } ;
208
-
209
10
// Append SVG
210
11
/////////////////////////////
211
12
@@ -226,17 +27,6 @@ d3.selection.prototype.appendSVG = function(_svgString) {
226
27
return d3 . select ( this . node ( ) . lastChild ) ;
227
28
} ;
228
29
229
-
230
- // Simple templating
231
- /////////////////////////////
232
-
233
- util . compileTemplate = function ( _template , _values ) {
234
- return [ ] . concat ( _values ) . map ( function ( d ) {
235
- return _template . replace ( / { ( [ ^ } ] * ) } / g, function ( s , key ) { return d [ key ] || '' ; } ) ;
236
- } ) . join ( '\n' ) ;
237
- } ;
238
-
239
-
240
30
// Complex templating
241
31
// @see https://github.com/jashkenas/underscore/blob/master/underscore.js#L1234
242
32
/////////////////////////////
@@ -321,14 +111,6 @@ util.tmpl = function(text, data) {
321
111
// Text utilities
322
112
/////////////////////////////
323
113
324
- util . getSVGBBoxFromString = function ( _string , _style ) {
325
- var tmp = d3 . select ( 'body' ) . append ( 'div' ) . style ( { visibility : 'none' } ) ;
326
- var textSelection = tmp . append ( 'svg' ) . append ( 'text' ) . text ( _string ) . style ( _style ) ;
327
- var bBox = textSelection . node ( ) . getBBox ( ) ;
328
- tmp . remove ( ) ;
329
- return bBox ;
330
- } ;
331
-
332
114
util . html_entity_decode = function ( s ) {
333
115
var hiddenDiv = d3 . select ( 'body' ) . append ( 'div' ) . style ( { display : 'none' } ) . html ( '' ) ;
334
116
var replaced = s . replace ( / ( & [ ^ ; ] * ; ) / gi, function ( d ) {
@@ -354,59 +136,14 @@ util.jsHook = function(_el){
354
136
} ;
355
137
356
138
357
- // Word wrap
139
+ // text converter
358
140
/////////////////////////////
359
141
360
- util . wrap = function ( _wrapW ) {
361
- return function ( ) {
362
- var that = this ;
363
-
364
- function tspanify ( ) {
365
- var lineH = this . node ( ) . getBBox ( ) . height ;
366
- this . text ( '' )
367
- . selectAll ( 'tspan' )
368
- . data ( lineArray )
369
- . enter ( ) . append ( 'tspan' )
370
- . attr ( {
371
- x : 0 ,
372
- y : function ( d , i ) { return ( i + 1 ) * lineH ; }
373
- } )
374
- . text ( function ( d ) { return d . join ( ' ' ) ; } ) ;
375
- }
376
-
377
- function checkW ( _text ) {
378
- var textTmp = that
379
- . style ( { visibility : 'hidden' } )
380
- . text ( _text ) ;
381
- var textW = textTmp . node ( ) . getBBox ( ) . width ;
382
- that . style ( { visibility : 'visible' } ) . text ( text ) ;
383
- return textW ;
384
- }
385
-
386
- var text = this . text ( ) ;
387
- var parentNode = this . node ( ) . parentNode ;
388
- var textSplitted = text . split ( ' ' ) ;
389
- var lineArray = [ [ ] ] ;
390
- var count = 0 ;
391
- textSplitted . forEach ( function ( d ) {
392
- if ( checkW ( lineArray [ count ] . concat ( d ) . join ( ' ' ) , parentNode ) >= _wrapW ) {
393
- count ++ ;
394
- lineArray [ count ] = [ ] ;
395
- }
396
- lineArray [ count ] . push ( d ) ;
397
- } ) ;
398
-
399
- this . call ( tspanify ) ;
400
- } ;
401
- } ;
402
-
403
142
util . getSize = function ( _selection , _dimension ) {
404
143
return _selection . node ( ) . getBoundingClientRect ( ) [ _dimension ] ;
405
144
} ;
406
145
407
146
408
- // text converter
409
- /////////////////////////////
410
147
411
148
util . convertToTspans = function ( _context , _callback ) {
412
149
var str = _context . text ( ) ;
@@ -675,48 +412,6 @@ util.convertToSvg = function(_str){
675
412
return result . join ( '' ) ;
676
413
} ;
677
414
678
- util . alignSVGWith = function ( _base , _options ) {
679
- return function ( ) {
680
- var baseBBox = _base . node ( ) . getBBox ( ) ;
681
- var alignH = '50%' ;
682
- var alignTextH = alignH ;
683
- var anchor = 'middle' ;
684
- var vMargin = 0 ;
685
- var hMargin = _options . horizontalMargin || 0 ;
686
- if ( _options . orientation === 'under' ) vMargin = baseBBox . y + baseBBox . height ;
687
- else if ( _options . orientation === 'over' ) vMargin = baseBBox . y ;
688
- else if ( _options . orientation === 'inside' ) {
689
- vMargin = baseBBox . y ;
690
- }
691
- if ( _options . verticalMargin ) vMargin += _options . verticalMargin ;
692
- if ( _options . horizontalAlign === 'center' ) {
693
- alignH = '50%' ;
694
- anchor = 'middle' ;
695
- hMargin = hMargin / 4 ;
696
- }
697
- else if ( _options . horizontalAlign === 'right' ) {
698
- alignH = '0%' ;
699
- anchor = 'start' ;
700
- }
701
- else if ( _options . horizontalAlign === 'left' ) {
702
- alignH = '100%' ;
703
- anchor = 'end' ;
704
- hMargin = - hMargin ;
705
- }
706
- else if ( typeof _options . horizontalAlign === 'number' ) {
707
- alignH = _options . horizontalAlign ;
708
- anchor = 'middle' ;
709
- }
710
- if ( _options . orientation === 'inside' ) {
711
- alignTextH = 0 ;
712
- }
713
-
714
- this . attr ( { x : alignTextH , dx : hMargin , y : vMargin } ) . style ( { 'text-anchor' : anchor } )
715
- . selectAll ( 'tspan.line' ) . attr ( { x : alignH , dx : hMargin , y : vMargin } ) ;
716
-
717
- return this ;
718
- } ;
719
- } ;
720
415
721
416
util . alignHTMLWith = function ( _base , container , options ) {
722
417
var alignH = options . horizontalAlign ,
0 commit comments