var system=require("system"),fs=require("fs");var LOG_ERROR=1,LOG_WARN=2,LOG_NOTICE=3,LOG_INFO=4,LOG_DEBUG=5;var EXIT_ERROR=-1,EXIT_NORMAL=0,EXIT_REQUEST=1,EXIT_TIMEOUT=2;var statusMsg,chartType,pageMsg,renderMsg,responseMsg;var CHECK_DELAY=1000*5;var TIMEOUT=0;var INTERVAL=1000*5;var MAX_RENDER_TIME=1000*60*5;var DEFAULT_WIDTH=800;var DEFAULT_HEIGHT=600;var isOSX=/mac/i.test(system.os["name"]);var LOG_LEVEL=LOG_WARN;var LOG_FILE="";var timeLog,renderTimeLog,page,cssStr;var jsName;phantom.onError=phantomErr; var clog=CLOG(LOG_LEVEL,LOG_FILE);if(system.args.length>=2&&/^(?:[0-9]{1,3}\.){3}[0-9]{1,3}|localhost/.test(system.args[1])){var args=system.args;var address=args[1].split(":");var ip=address[0],port=address[1];args=args.slice(2);if(args.length%2){exit()}for(var i=0,len=args.length;i3){clog.setFile(v)}break;case"t":if(!isNaN(v)){TIMEOUT=+v}break;case"j":jsName=v;break;default:break }}page=initPage();startServer(ip,port);setTimeout(supervisor,CHECK_DELAY)}else{console.log("example: ./PhantomJS van-convert.js 127.0.0.1:2333");exit()}function initPage(){var a=require("webpage").create();a.onError=pageErr;a.content=''+""+'
'; a.evaluate(function(){FR={};FR.i18nText=function(c){return c}});a.injectJs("format.js");a.injectJs("vancharts-all.js");if(jsName){var b=jsName.split(",");b.forEach(function(c){a.injectJs(c)})}a.evaluate(shim);cssStr&&a.evaluate(addStyleTag,cssStr);return a}function render(c,b){renderTimeLog=+Date.now();page.onError=function(g,f){renderTimeLog=0;clog.notice("chart type: "+c.option.chartType);b("page error");pageErr(g,f)};page.onConsoleMessage=function(h,g,f){renderTimeLog=0;renderMsg=h;if(h==="done"){clog.log(h,g,f); window.setTimeout(function(){var j=page.renderBase64("PNG");b(j)},c.time)}else{clog.info(h,g,f)}};var e=c.zoom||1,d=c.width||DEFAULT_WIDTH,a=c.height||DEFAULT_HEIGHT;d*=e;a*=e;page.zoomFactor=(c.scale||1)*e;page.viewportSize={width:d,height:a};page.clipRect={top:0,left:0,width:d,height:a};if(c.js){page.evaluate(function(){if(Date.brokenSetMonth){Date.prototype.setMonth=Date.brokenSetMonth}});page.evaluateJavaScript("function(){"+c.js+"}")}page.evaluate(function(p,h){var g=p.width||DEFAULT_WIDTH;var o=p.height||DEFAULT_HEIGHT; var j=p.option,n=window;n.chartBg=document.getElementById("bg");n.chartContainer=document.getElementById("container");if(j.geo&&j.borderWidth){g-=2*j.borderWidth;o-=2*j.borderWidth}try{n.vanCharts&&n.vanCharts.clear()}catch(l){}n.chartContainer.innerHTML="";n.chartContainer.style.backgroundImage="";n.chartContainer.style.borderStyle="none";n.chartContainer.style.width=g+"px";n.chartContainer.style.height=o+"px";n.chartBg.style.width=g+"px";n.chartBg.style.height=o+"px";n.chartBg.style.display=""; if(h&&j["style"]==="gradual"){n.chartBg.style.display="block"}VanCharts.initPaddingScale(j.scale);j["plotOptions"]=j["plotOptions"]||{};j["plotOptions"]["animation"]=false;var m=j.wrapperName;if(m){var f=new window[m]();n.vanCharts=f._exportInit(n.chartContainer,j);console.log("done")}else{n.vanCharts=VanCharts.init(n.chartContainer);n.vanCharts.setOptions(j)}},c,isOSX)}function addCss(b,a){page.onConsoleMessage=function(e,d,c){if(e==="done"){a('{"status": "success"}')}else{clog.error("add css error"); a('{"status": "css injected error."}')}};cssStr=b.css;page.evaluate(addStyleTag,cssStr)}function addStyleTag(a){(function(){var b=document.createElement("style");b.innerText=a;document.body.appendChild(b);console.log("done")})()}function supervisor(){if(!(INTERVAL>1)){return}if(!(TIMEOUT>1)){return}timeLog=+Date.now();renderTimeLog=0;setInterval(function(){if(Date.now()-renderTimeLog=LOG_DEBUG){e.apply(this,arguments)}},info:function(){if(h>=LOG_INFO){e.apply(this,arguments)}},notice:function(){if(h>=LOG_NOTICE){e.apply(this,arguments) }},warn:function(){if(h>=LOG_WARN){e.apply(this,arguments)}g&&g.write(c(arguments))},error:function(){if(h>=LOG_ERROR){e.apply(this,arguments)}g&&g.write(c(arguments))},setFile:f,setLevel:b,popAll:function(){var j=a.join("");a=[];return j},flush:function(){g&&g.flush()},close:function(){g&&g.close()}}}function pageErr(c,b){pageMsg=c;var a=["\r\nPAGE ERROR: "+c];if(b&&b.length){a.push("TRACE:");b.forEach(function(d){a.push(" -> "+d.file+": "+d.line+(d["function"]?' (in function "'+d["function"]+'")':"")) })}clog.notice(a.join("\r\n"));page.close();page=initPage()}function phantomErr(c,b){var a=["\r\nPHANTOM ERROR: "+c];if(b&&b.length){a.push("TRACE:");b.forEach(function(d){a.push(" -> "+(d.file||d.sourceURL)+": "+d.line+(d["function"]?" (in function "+d["function"]+")":""))})}clog.error(a.join("\r\n"));exit(EXIT_ERROR)}function exit(a){switch(a){case EXIT_ERROR:clog.error("Exit with error.");break;case EXIT_TIMEOUT:clog.warn("Exit due to timeout.");break;case EXIT_REQUEST:clog.warn("Exit by request"); break;default:case EXIT_NORMAL:clog.warn("Exit.");break}clog.warn("exit pid:"+system.pid);clog.warn("chartType:"+chartType);clog.warn("pageMsg:"+pageMsg);clog.warn("renderMsg:"+renderMsg);clog.warn("statusMsg:"+statusMsg);clog.warn("responseMsg:"+responseMsg);clog.close();phantom.exit()}function convertDate(a){return a.replace(/\d{13}/g,dateFun)}function dateFun(b){var a=new Date(+b);return a.getFullYear()+"-"+addZero(a.getMonth()+1)+"-"+addZero(a.getDate())+" "+addZero(a.getHours())+":"+addZero(a.getMinutes())+":"+addZero(a.getSeconds()) }function addZero(a){return("0"+a).slice(-2)}function shim(){if(!Function.prototype.bind){Function.prototype.bind=function(a){if(typeof this!=="function"){throw new TypeError("Function.prototype.bind -what is trying to be bound is not callable")}var e=Array.prototype.slice.call(arguments,1),d=this,b=function(){},c=function(){return d.apply(this instanceof b&&a?this:a,e.concat(Array.prototype.slice.call(arguments)))};b.prototype=this.prototype;c.prototype=new b();return c}}};