]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.document/print.js
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.document / print.js
diff --git a/bundles/org.simantics.document/print.js b/bundles/org.simantics.document/print.js
new file mode 100644 (file)
index 0000000..d562112
--- /dev/null
@@ -0,0 +1,74 @@
+\r
+function progress(work) {\r
+       console.log(work);\r
+}\r
+\r
+/**\r
+ * Wait until the test condition is true or a timeout occurs. Useful for waiting\r
+ * on a server response or for a ui change (fadeIn, etc.) to occur.\r
+ *\r
+ * @param testFx javascript condition that evaluates to a boolean,\r
+ * it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or\r
+ * as a callback function.\r
+ * @param onReady what to do when testFx condition is fulfilled,\r
+ * it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or\r
+ * as a callback function.\r
+ * @param timeOutMillis the max amount of time to wait. If not specified, 5 min is used.\r
+ */\r
+function waitFor(testFx, onReady, timeOutMillis) {\r
+    var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 300000, //< Default Max Timout is 5 min\r
+        start = new Date().getTime(),\r
+        condition = false;\r
+       var interval = setInterval(function() {\r
+               if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {\r
+                // If not time-out yet and condition not yet fulfilled\r
+               condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code\r
+            } else {\r
+                if(!condition) {\r
+                       // timeout\r
+                       phantom.exit(1);\r
+                } else {\r
+                    // Condition fulfilled (timeout and/or condition is 'true')\r
+                    typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled\r
+                    clearInterval(interval); //< Stop this interval\r
+                }\r
+            }\r
+        }, 250); //< repeat check every 250ms\r
+};\r
+\r
+phantom.onError = function(msg, trace) {\r
+  var msgStack = ['PHANTOM ERROR: ' + msg];\r
+  if (trace && trace.length) {\r
+    msgStack.push('TRACE:');\r
+    trace.forEach(function(t) {\r
+      msgStack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function +')' : ''));\r
+    });\r
+  }\r
+  console.error(msgStack.join('\n'));\r
+  phantom.exit(1);\r
+};\r
+\r
+var page = require('webpage').create();\r
+\r
+require('system');\r
+\r
+progress('#initialized');\r
+\r
+page.paperSize = {\r
+  width: "190mm",\r
+  height: "276mm",\r
+  orientation: "portrait",\r
+  margin: %%margin\r
+};\r
+\r
+page.zoomFactor = 1.57\r
+\r
+page.open('%%url', function (status) {\r
+\r
+       page.render('%%file', {quality: '100'});\r
+\r
+       progress('#rendered');\r
+       \r
+       phantom.exit();\r
+       \r
+});
\ No newline at end of file