-
Notifications
You must be signed in to change notification settings - Fork 1
/
kendoUI-convert.js
182 lines (153 loc) · 4.7 KB
/
kendoUI-convert.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
*
* Author : Nagesh Salunke
* License: https://github.com/salunkenagesh14/KendoUI-ChartExport-PhantomJS/blob/master/LICENSE
* (c) 20013-2014
* version: 1.0
*
* JavaScript for using kendo ui export on server side
* Supported formats
* 1. start as a web server
* 2. Export using command line arguments
*/
(function() {
"use strict";
var config={},
mapCLArguments,
render,
startServer,
args,
system = require( "system" ),
fs = require( "fs" ),
serverMode = true;
/**
* @desc mapping command line arguments
*/
mapCLArguments = function() {
var map = {},
i,
key;
for ( i = 0; i < system.args.length; i += 1 ) {
if ( system.args[i].charAt( 0 ) === "-" ) {
key = system.args[i].substr( 1, i.length );
map[key] = system.args[i + 1];
}
}
return map;
};
/**
* @desc render chart server side
* Default export format is .png
* @TODO add parameterized export format
*/
render = function( params, exitCallback ) {
var page = require( "webpage" ).create(),
renderSVG,
exit,
input = params.infile,
n = new Date().getUTCMilliseconds(),
output = config.tmpDir+"/KendoExportedchart_"+n+Math.floor((Math.random() * 1000) + 1)+".png",
createChart;
page.onConsoleMessage = function( msg ) {
console.log( msg );
};
page.open( "export.html", function( status ) {
// load chart in page
page.evaluate( createChart ,input);
//create png of the page
page.evaluate( function() {
var body = document.body;
body.style.backgroundColor = '#fff';
} );
page.render( output );
exit("Successfully Created "+output+" at server");
});
/**
* @desc appends chart in SVG format to document using KendoUI
*/
createChart = function( input ) {
$( "<div id=\"chart\"> </div>" ).appendTo( document.body );
try{
$( "#chart" ).kendoChart(input);
}catch( e ){
console.log("ERROR : Exception while creating chart from KendoUI");
}
};
/**
* @desc page close and return callback
*/
exit = function( resultMessage ) {
if ( serverMode ) {
page.close();
}
exitCallback( resultMessage );
};
};
/**
* @desc starts an export server given host and port
*/
startServer = function( host, port ) {
var server = require( "webserver" ).create();
server.listen( host + ":" + port, function( request, response ) {
var jsonStr = request.post,
params,
msg;
try {
params = JSON.parse( jsonStr );
if ( params.status ) {
// for server health validation
response.statusCode = 200;
response.write( "OK" );
response.close();
}
else {
render( params, function( result ) {
response.statusCode = 200;
response.write( result );
response.close();
} );
}
}
catch ( e ) {
msg = "Failed rendering: \n" + e;
response.statusCode = 500;
response.setHeader( "Content-Type", "text/plain" );
response.setHeader( "Content-Length", msg.length );
response.write( msg );
response.close();
}
});
// switch to serverMode
serverMode = true;
console.log( "OK, PhantomJS is ready for exporting kendoUI charts." );
};
//execution starts here
args = mapCLArguments();
// set tmpDir, for output temporary files.
if ( args.tmpdir === undefined ) {
config.tmpDir = fs.workingDirectory + "/tmp";
}
else {
config.tmpDir = args.tmpdir;
}
// exists tmpDir and is it writable?
if ( !fs.exists( config.tmpDir ) ) {
try {
fs.makeDirectory( config.tmpDir );
}
catch ( e ) {
console.log( "ERROR: Cannot create temp directory for " + config.tmpDir );
}
}
//start server if started in server mode
if ( args.host !== undefined && args.port !== undefined ) {
startServer( args.host, args.port );
}
else {
// command line export
render( args, function( msg ) {
console.log( msg );
phantom.exit();
});
}
}());