]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document/print.js
Search-field in Help->Contents Fix.
[simantics/platform.git] / bundles / org.simantics.document / print.js
1 \r
2 function progress(work) {\r
3         console.log(work);\r
4 }\r
5 \r
6 /**\r
7  * Wait until the test condition is true or a timeout occurs. Useful for waiting\r
8  * on a server response or for a ui change (fadeIn, etc.) to occur.\r
9  *\r
10  * @param testFx javascript condition that evaluates to a boolean,\r
11  * it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or\r
12  * as a callback function.\r
13  * @param onReady what to do when testFx condition is fulfilled,\r
14  * it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or\r
15  * as a callback function.\r
16  * @param timeOutMillis the max amount of time to wait. If not specified, 5 min is used.\r
17  */\r
18 function waitFor(testFx, onReady, timeOutMillis) {\r
19     var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 300000, //< Default Max Timout is 5 min\r
20         start = new Date().getTime(),\r
21         condition = false;\r
22         var interval = setInterval(function() {\r
23                 if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {\r
24                 // If not time-out yet and condition not yet fulfilled\r
25                 condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code\r
26             } else {\r
27                 if(!condition) {\r
28                         // timeout\r
29                         phantom.exit(1);\r
30                 } else {\r
31                     // Condition fulfilled (timeout and/or condition is 'true')\r
32                     typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled\r
33                     clearInterval(interval); //< Stop this interval\r
34                 }\r
35             }\r
36         }, 250); //< repeat check every 250ms\r
37 };\r
38 \r
39 phantom.onError = function(msg, trace) {\r
40   var msgStack = ['PHANTOM ERROR: ' + msg];\r
41   if (trace && trace.length) {\r
42     msgStack.push('TRACE:');\r
43     trace.forEach(function(t) {\r
44       msgStack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function +')' : ''));\r
45     });\r
46   }\r
47   console.error(msgStack.join('\n'));\r
48   phantom.exit(1);\r
49 };\r
50 \r
51 var page = require('webpage').create();\r
52 \r
53 require('system');\r
54 \r
55 progress('#initialized');\r
56 \r
57 page.paperSize = {\r
58   width: "190mm",\r
59   height: "276mm",\r
60   orientation: "portrait",\r
61   margin: %%margin\r
62 };\r
63 \r
64 page.zoomFactor = 1.57\r
65 \r
66 page.open('%%url', function (status) {\r
67 \r
68         page.render('%%file', {quality: '100'});\r
69 \r
70         progress('#rendered');\r
71         \r
72         phantom.exit();\r
73         \r
74 });