[education/rkward] /: Provide a default implementation for a preview() function in plugins' JS template.

Thomas Friedrichsmeier null at kde.org
Wed May 4 17:20:22 BST 2022


Git commit d039a77b53c4724df3259eb801aff26995704b4a by Thomas Friedrichsmeier.
Committed on 02/05/2022 at 20:44.
Pushed by tfry into branch 'master'.

Provide a default implementation for a preview() function in plugins' JS template.

M  +1    -0    ChangeLog
M  +8    -12   doc/rkwardplugins/index.docbook
M  +6    -4    rkward/plugins/descriptive/descriptive_statistics.js
M  +3    -2    rkward/plugins/descriptive/descriptive_statistics.xml
M  +20   -18   rkward/plugins/uni1.2/code.js
M  +1    -0    rkward/plugins/uni1.2/description.xml
M  +9    -4    rkward/scriptbackends/common.js

https://invent.kde.org/education/rkward/commit/d039a77b53c4724df3259eb801aff26995704b4a

diff --git a/ChangeLog b/ChangeLog
index a6460546..27c7f72b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,7 @@
 TODOs:
 	- More tolerant handshake on Windows? Simply a matter of allowing more time?
 
+- A default preview() function is available to plugins, reducing the code needed to add preview functionaality
 - Better placement of messages in non-plot preview windows
 - Fixed some problems with cancelling running commands
 - Package installation uses inline widget to provide progress feedback, instead of separate dialogs
diff --git a/doc/rkwardplugins/index.docbook b/doc/rkwardplugins/index.docbook
index 8a38299f..2095736c 100644
--- a/doc/rkwardplugins/index.docbook
+++ b/doc/rkwardplugins/index.docbook
@@ -44,7 +44,7 @@ as Authors, publish date, the abstract, and Keywords -->
 
 
 <copyright>
-<year>2006-2020</year>
+<year>2006-2022</year>
 <holder>Thomas Friedrichsmeier</holder>
 </copyright>
 <!-- Translators: put here the copyright notice of the translation -->
@@ -52,8 +52,8 @@ as Authors, publish date, the abstract, and Keywords -->
      and in the FDL itself on how to use it. -->
 <legalnotice>&FDLNotice;</legalnotice>
 
-<date>2020-09-26</date>
-<releaseinfo>0.7.2</releaseinfo>
+<date>2022-04-24</date>
+<releaseinfo>0.7.4</releaseinfo>
 
 <abstract>
 <para>
@@ -1354,7 +1354,10 @@ This chapter contains information on some topics that are useful only to certain
 			And that is it for the &GUI; definition.
 		</para>
 		<para>
-			Adjusting the JS template is a little more work. You will have to create a new function called <function>preview()</function> in addition to the <function>preprocess()</function>, <function>calculate()</function>, &etc; functions. This function should generate the code needed to produce the plot, and only that. Esp. no printing of headers, <function>rk.graphics.on()</function>, or similar calls. See the <link linkend="plot_plugin_example">example</link>, below for the typical pattern that you will use.
+			Adjusting the JS template is only a little more work, here you will have to make sure, only the plot itself is generated, and shown in an onscreen-device, rather than directed to the output. I.e. no printing of headers, <function>rk.graphics.on()</function>, or similar calls. To assist you in this, &rkward; will call the <function>preprocess()</function>, <function>calculate()</function>, and <function>printout()</function> functions with an additional parameter that is set to <parameter>true</parameter>, when generating code for a preview. (The parameter is omitted when generating the final code. In javascript this will evaluate to <parameter>false</parameter> when used inside an <function>if</function>-statement.)  See the <link linkend="plot_plugin_example">example</link>, below for the typical pattern that you will use.
+		</para>
+		<para>
+			Alternatively, should you need more control than this, you can instead add a new function called <function>preview()</function> to your JS template, and generate the code required for a preview, there (probably, at least in part, again by calling <function>calculate()</function>, etc.).
 		</para>
 	</sect2>
 	<sect2 id="plot_options">
@@ -1399,16 +1402,9 @@ This chapter contains information on some topics that are useful only to certain
     // the "somepackage" is needed to create the plot
     echo ("require (somepackage)\n");
   }
-
-  function preview () {
-    // we call all stages of the general code. Only the printout () function needs to be called slightly different for the plot preview
-    preprocess ();
-    // calculate (); // in this example, the plugin has no calculate () function.
-    printout (true); // in this case, 'true' means: Create the plot, but not any headers or other output.
-  }
   
   function printout (is_preview) {
-    // If "is_preview" is set to false, it generates the full code, including headers.
+    // If "is_preview" is set to false/undefined, it generates the full code, including headers.
     // If "is_preview" is set to true, only the essentials will be generated.
 
     if (!is_preview) {
diff --git a/rkward/plugins/descriptive/descriptive_statistics.js b/rkward/plugins/descriptive/descriptive_statistics.js
index 5769bbef..fbb2e063 100644
--- a/rkward/plugins/descriptive/descriptive_statistics.js
+++ b/rkward/plugins/descriptive/descriptive_statistics.js
@@ -68,10 +68,12 @@ function calculate () {
 	echo ('}\n');
 }
 
-function printout () {
-	new Header (i18n ("Descriptive statistics")).addFromUI ("trim").print ();
-	if (getValue ("mad")) {
-		new Header (i18n ("Median Absolute Deviation"), 3).addFromUI ("constMad").addFromUI ("mad_type").print ();
+function printout (is_preview) {
+	if (!is_preview) {
+		new Header (i18n ("Descriptive statistics")).addFromUI ("trim").print ();
+		if (getValue ("mad")) {
+			new Header (i18n ("Median Absolute Deviation"), 3).addFromUI ("constMad").addFromUI ("mad_type").print ();
+		}
 	}
 	echo ('rk.results (results)\n');
 	if (getValue ("save_to_file")) echo ('write.csv(file="' + getValue ("file") + '", results)\n');
diff --git a/rkward/plugins/descriptive/descriptive_statistics.xml b/rkward/plugins/descriptive/descriptive_statistics.xml
index 3c9026ec..eb028401 100644
--- a/rkward/plugins/descriptive/descriptive_statistics.xml
+++ b/rkward/plugins/descriptive/descriptive_statistics.xml
@@ -15,8 +15,9 @@
 						<varslot multi="true" source="vars" id="groups" num_dimensions="1" label="group by:" required="false" />
 					</column>
 				</row>
-					<checkbox value_unchecked="0" checked="flase" value="1" id="save_to_file" label="Save results to file" />
-					<browser type="savefile" size="small" id="file" filter="*.csv" initial="data" label="Name of the file" />
+				<checkbox value_unchecked="0" checked="flase" value="1" id="save_to_file" label="Save results to file" />
+				<browser type="savefile" size="small" id="file" filter="*.csv" initial="data" label="Name of the file" />
+				<preview id="preview" mode="output"/>
 			</tab>
 			<tab label="Mean Standard Deviation" id="tab_mean_sd">
 				<column>
diff --git a/rkward/plugins/uni1.2/code.js b/rkward/plugins/uni1.2/code.js
index fb72c26b..7c28c18d 100644
--- a/rkward/plugins/uni1.2/code.js
+++ b/rkward/plugins/uni1.2/code.js
@@ -93,27 +93,29 @@ function calculate () {
 	}
 }
 
-function printout () {
-	header = new Header (i18n ("Univariate statistics")).addFromUI ("narm");
-	if (getBoolean("trim.state")) {
-		header.add (i18n ("Proportion of trimmed values for trimmed mean"), getString ("pourcent"));
-	}
-	if (getBoolean("mad.state")) {
-		header.add (i18n ("Constant for the MAD estimation"), getString ("constMad"));
-	}
-	if (getBoolean("huber.state")) {
-		header.add (i18n ("Winsorized values for Huber estimator"), getString ("winsor"));
-		header.add (i18n ("Tolerance in Huber estimator"), getString ("tol"));
-		if (getBoolean ("customMu.state")) {
-			header.add (i18n ("Mu for Huber estimator"), getString ("mu"));
+function printout (is_preview) {
+	if (!is_preview) {
+		header = new Header (i18n ("Univariate statistics")).addFromUI ("narm");
+		if (getBoolean("trim.state")) {
+			header.add (i18n ("Proportion of trimmed values for trimmed mean"), getString ("pourcent"));
 		}
-		if (getBoolean ("customS.state")) {
-			header.add (i18n ("S for Huber estimator"), getString ("s"));
+		if (getBoolean("mad.state")) {
+			header.add (i18n ("Constant for the MAD estimation"), getString ("constMad"));
 		}
-		header.add (i18n ("Initial value"), getString ("initmu"));
+		if (getBoolean("huber.state")) {
+			header.add (i18n ("Winsorized values for Huber estimator"), getString ("winsor"));
+			header.add (i18n ("Tolerance in Huber estimator"), getString ("tol"));
+			if (getBoolean ("customMu.state")) {
+				header.add (i18n ("Mu for Huber estimator"), getString ("mu"));
+			}
+			if (getBoolean ("customS.state")) {
+				header.add (i18n ("S for Huber estimator"), getString ("s"));
+			}
+			header.add (i18n ("Initial value"), getString ("initmu"));
+		}
+		header.print ();
+		echo ('\n');
 	}
-	header.print ();
-	echo ('\n');
 	echo ('rk.results (results)\n');
 	if (getValue ("save_to_file")) echo ('write.csv(file="' + getValue ("file") + '", results)\n');
 }
diff --git a/rkward/plugins/uni1.2/description.xml b/rkward/plugins/uni1.2/description.xml
index 20f3ce03..5f547621 100644
--- a/rkward/plugins/uni1.2/description.xml
+++ b/rkward/plugins/uni1.2/description.xml
@@ -26,6 +26,7 @@
 						<varselector id="vars" />
 						<varslot multi="true" classes="numeric integer" id="z" source="vars" label="Variable" required="true" />
 					</row>
+					<preview id="preview" mode="output"/>
 				</column>
 					<checkbox value_unchecked="0" checked="false" value="1" id="save_to_file" label="Save results to file" />
 					<browser type="savefile" size="small" id="file" filter="*.csv" initial="data" label="Name of the file" />
diff --git a/rkward/scriptbackends/common.js b/rkward/scriptbackends/common.js
index 3654380a..32b7a2bf 100644
--- a/rkward/scriptbackends/common.js
+++ b/rkward/scriptbackends/common.js
@@ -179,10 +179,15 @@ do_printout = function () {
 	return (flushOutput ());
 }
 
-do_preview = function () {
-	if (typeof (preview) == "undefined") return ("");
-	preview ();
-	return (flushOutput ());
+do_preview = function() {
+	if (typeof(preview) == "undefined") {
+		if (typeof(preprocess) != "undefined") preprocess(true);
+		if (typeof(calculate) != "undefined") calculate(true);
+		if (typeof(printout) != "undefined") printout(true);
+	} else {
+		preview();
+	}
+	return (flushOutput());
 }
 
 // for compatibility with the converted PHP code



More information about the rkward-tracker mailing list