[rkward-cvs] [rkward] packages/rkwarddev: make using global JS variables more robust by dividing definition and initialisation (through a helper function)

m.eik michalke meik.michalke at uni-duesseldorf.de
Fri Jan 2 21:55:41 UTC 2015


Git commit 38857891a9b20d5fe736c2cda292783f9e26e128 by m.eik michalke.
Committed on 02/01/2015 at 21:53.
Pushed by meikm into branch 'master'.

make using global JS variables more robust by dividing definition and initialisation (through a helper function)

M  +8    -3    packages/rkwarddev/ChangeLog
M  +4    -4    packages/rkwarddev/DESCRIPTION
M  +7    -4    packages/rkwarddev/R/rk-internal.R
M  +23   -5    packages/rkwarddev/R/rk.JS.doc.R
M  +4    -2    packages/rkwarddev/R/rk.paste.JS.R
M  +2    -2    packages/rkwarddev/R/rkwarddev-package.R
M  +1    -1    packages/rkwarddev/man/i18n.Rd
M  +1    -1    packages/rkwarddev/man/rk.JS.doc.Rd
M  +2    -2    packages/rkwarddev/man/rk.XML.vars.Rd
M  +5    -1    packages/rkwarddev/man/rk.paste.JS.Rd
M  +2    -2    packages/rkwarddev/man/rkwarddev-package.Rd

http://commits.kde.org/rkward/38857891a9b20d5fe736c2cda292783f9e26e128

diff --git a/packages/rkwarddev/ChangeLog b/packages/rkwarddev/ChangeLog
index c2424c1..a761708 100644
--- a/packages/rkwarddev/ChangeLog
+++ b/packages/rkwarddev/ChangeLog
@@ -1,6 +1,6 @@
 ChangeLog for package rkwarddev
 
-changes in version 0.06-6 (2014-12-17)
+changes in version 0.06-6 (2015-01-02)
 fixed:
   - rk.XML.optionset() does now allow to re-use objects defined in the same
     function call (i.e., refer to optioncolumns in the logic section)
@@ -20,8 +20,8 @@ fixed:
   - join() referenced a wrong object name if "var" was of class rk.JS.arr
   - rk.XML.about() had a malformed error message
   - rk.plugin.component() called XMLAttrs() with too many arguments
-  - rk.XML.values() and rk.XML.vars() do now handle all combinations of "horiz"
-    and "add.nodes" properly
+  - rk.XML.values() and rk.XML.vars() do now handle all combinations of
+    "horiz" and "add.nodes" properly
 added:
   - all "help" values can now be a list of character strings or XiMpLe nodes,
     to have more control over the markup
@@ -43,6 +43,8 @@ added:
   - added documentation to the used S4 classes
   - enhanced rk.XML.values() to also accept "options" and "duplicates", and
     rk.XML.vars() to also accept "duplicates"
+  - new argument "var" in rk.paste.JS() to define whether the JS keyword
+    "var" should be used or omitted for rk.JS.var objects
 changed:
   - function i18n() was changed to be used in JavaScript generation only;
     this breaks code using the function introduced with 0.06-4, but that was to
@@ -57,6 +59,9 @@ changed:
     they're more obvious to find and check
   - rk.XML.input() now checks wheter "size" is exactly one of "small",
     "medium" or "large"
+  - rk.JS.doc() now uses a more robust method to set global variables, namely
+    by defining an additional function setGlobalVars() and calling that
+    inside preprocess()
 removed:
   - the functions rk.set.language() rk.get.language() were no longer useful
     and therefore removed
diff --git a/packages/rkwarddev/DESCRIPTION b/packages/rkwarddev/DESCRIPTION
index 52687bd..a86a3ae 100644
--- a/packages/rkwarddev/DESCRIPTION
+++ b/packages/rkwarddev/DESCRIPTION
@@ -8,16 +8,16 @@ Depends:
 Suggests:
     testthat
 Enhances: rkward
-Description: Provides functions to create plugin skeletons and XML
-    structures for RKWard.
+Description: Provides functions to create plugin skeletons and XML structures
+    for RKWard.
 License: GPL (>= 3)
 Encoding: UTF-8
 LazyLoad: yes
-URL: http://rkward.kde.org
+URL: http://rkward.sourceforge.net
 Authors at R: c(person(given="m.eik", family="michalke",
     email="meik.michalke at hhu.de", role=c("aut", "cre")))
 Version: 0.06-6
-Date: 2014-12-17
+Date: 2015-01-02
 Collate:
     '00_class_01_rk.JS.arr.R'
     '00_class_02_rk.JS.var.R'
diff --git a/packages/rkwarddev/R/rk-internal.R b/packages/rkwarddev/R/rk-internal.R
index b5b2687..637e73f 100644
--- a/packages/rkwarddev/R/rk-internal.R
+++ b/packages/rkwarddev/R/rk-internal.R
@@ -948,8 +948,9 @@ paste.JS.options <- function(object, level=2, indent.by="\t", array=NULL, funct=
 # append.modifier: if a modifier is given, should that become part of the variable name? this is mostly
 #   important for "checkbox", which has "state" as default modifier, but using the checkbox object will not
 #   notice this. works only for the first modifier given.
+# var: if FALSE, the variable is assumed to be already defined (globally?) and "var " will be omitted
 paste.JS.var <- function(object, level=2, indent.by="\t", JS.prefix=NULL, modifiers=NULL, default=NULL, append.modifier=NULL,
-  join=NULL, getter=NULL, names.only=FALSE, check.modifiers=FALSE){
+  join=NULL, getter=NULL, names.only=FALSE, check.modifiers=FALSE, var=TRUE){
   # paste several objects
   results <- unlist(sapply(slot(object, "vars"), function(this.obj){
       paste.JS.var(this.obj,
@@ -961,7 +962,9 @@ paste.JS.var <- function(object, level=2, indent.by="\t", JS.prefix=NULL, modifi
           append.modifier=append.modifier,
           join=join,
           getter=getter,
-          names.only=names.only)}))
+          names.only=names.only,
+          check.modifiers=check.modifiers,
+          var=var)}))
   if(!isTRUE(names.only) & !is.null(results)){
     results <- paste(results, collapse="\n")
   }
@@ -1006,7 +1009,7 @@ paste.JS.var <- function(object, level=2, indent.by="\t", JS.prefix=NULL, modifi
       if(isTRUE(names.only)){
         results <- c(results, camelCode(c(JS.prefix, JS.var)))
       } else {
-        results <- paste0(main.indent, "var ", camelCode(c(JS.prefix, JS.var)), " = ", getter, "(\"", XML.var, "\")", join.code, ";")
+        results <- paste0(main.indent, ifelse(isTRUE(var), "var ", ""), camelCode(c(JS.prefix, JS.var)), " = ", getter, "(\"", XML.var, "\")", join.code, ";")
       }
     } else {}
     if(length(modifiers) > 0){
@@ -1024,7 +1027,7 @@ paste.JS.var <- function(object, level=2, indent.by="\t", JS.prefix=NULL, modifi
           if(isTRUE(names.only)){
             return(this.name)
           } else {
-            return(paste0(main.indent, "var ", this.name,
+            return(paste0(main.indent, ifelse(isTRUE(var), "var ", ""), this.name,
               " = ", getter, "(\"", XML.var, ".", this.modif, "\")", join.code, ";"))
           }
         })
diff --git a/packages/rkwarddev/R/rk.JS.doc.R b/packages/rkwarddev/R/rk.JS.doc.R
index 461c0d4..9bbf9d5 100644
--- a/packages/rkwarddev/R/rk.JS.doc.R
+++ b/packages/rkwarddev/R/rk.JS.doc.R
@@ -22,7 +22,7 @@
 #'
 #' @param require A character vector with names of R packages that the dialog depends on.
 #' @param variables Either a character string to be included to read in all needed variables from the dialog (see \code{\link{rk.JS.scan}}),
-#'    or an object of class \code{rk.JS.var} which will be coerced into character. These variables will be defined in
+#'    or a (list of) objects of class \code{rk.JS.var} which will be coerced into character. These variables will be defined in
 #'    the \code{calculate()} and/or \code{doPrintout()} functions.
 #' @param globals Like \code{variables}, but these variables will be defined globally. If \code{variables} is set as well,
 #'    the function tries to remove duplicate definitions.
@@ -61,19 +61,36 @@
 rk.JS.doc <- function(require=c(), variables=NULL, globals=NULL, results.header=NULL, header.add=list(),
   preprocess=NULL, calculate=NULL, printout=NULL, doPrintout=NULL, load.silencer=NULL, gen.info=TRUE, indent.by="\t",
   guess.getter=FALSE){
+  # variable to determine whether to add setGlobalVars() to preprocess() later
+  addSetGlobalVars <- FALSE
 
   # some data transformation
+  if(all(sapply(variables, function(x){inherits(x, "rk.JS.var")}))){
+    variables <- rk.paste.JS(
+      paste0(unlist(sapply(variables, function(x){rk.paste.JS(x, var=FALSE)})))
+    )
+  } else {}
   if(inherits(variables, "rk.JS.var")){
-    variables <- rk.paste.JS(variables)
   } else {}
-  if(inherits(globals, "rk.JS.var")){
-    globals <- rk.paste.JS(globals, level=1)
+  if(all(sapply(globals, function(x){inherits(x, "rk.JS.var")}))){
+    globalNames <- paste0("var ", unlist(sapply(globals, function(x){paste.JS.var(x, names.only=TRUE)})), ";", collapse="\n")
+    globalFunction <- paste0(
+      "function setGlobalVars(){\n",
+        paste0(
+          unlist(sapply(globals, function(x){rk.paste.JS(x, var=FALSE, level=2)})),
+          collapse="\n"
+        ),
+      "\n}",
+      collapse="\n"
+    )
+    globals <- paste0(globalNames, "\n\n", globalFunction)
+    addSetGlobalVars <- TRUE
   } else {}
 
   js.gen.info <- ifelse(isTRUE(gen.info), rk.paste.JS(generator.info, level=1), "")
 
   if(!is.null(globals)){
-    js.globals <- paste(
+    js.globals <- paste0(
       "// define variables globally\n",
       paste0(globals, collapse=""))
     if(!is.null(variables)){
@@ -107,6 +124,7 @@ rk.JS.doc <- function(require=c(), variables=NULL, globals=NULL, results.header=
       return(req.result)
     }))
   js.preprocess <- paste0("function preprocess(){\n",
+    ifelse(isTRUE(addSetGlobalVars), paste0(indent(2, by=indent.by), "setGlobalVars();\n"), ""),
     indent(2, by=indent.by), "// add requirements etc. here\n",
     paste(js.require, collapse=""),
     "\n",
diff --git a/packages/rkwarddev/R/rk.paste.JS.R b/packages/rkwarddev/R/rk.paste.JS.R
index 2e9304a..5ce5f6c 100644
--- a/packages/rkwarddev/R/rk.paste.JS.R
+++ b/packages/rkwarddev/R/rk.paste.JS.R
@@ -40,6 +40,8 @@
 #' @param getter For \code{rk.JS.var} objects only: A character string, naming the JavaScript function which should be used to get the
 #'    values in the actual plugin. Depending on the XML element, \code{"getString"}, \code{"getBool"} or \code{"getList"} can be
 #'    useful alternatives. For backwards compatibility, the default is set to \code{"getValue"}.
+#' @param var For \code{rk.JS.var} objects only: Logical, if \code{FALSE} the variable(s) are assumed to already be defined (globally?)
+#'    and the JS keyword "var" will be omitted.
 #' @param empty.e For \code{rk.JS.ite} objects only: Logical, if \code{TRUE} will force to add empty \code{else \{\}} brackets when
 #'    there is no \code{else} statement defined, which is considered to enhance code readability by some.
 #' @return A character string.
@@ -60,7 +62,7 @@
 #' @export
 
 rk.paste.JS <- function(..., level=2, indent.by="\t", funct=NULL, array=NULL,
-  var.prefix=NULL, modifiers=NULL, default=NULL, join=NULL, getter=NULL, empty.e=FALSE){
+  var.prefix=NULL, modifiers=NULL, default=NULL, join=NULL, getter=NULL, var=TRUE, empty.e=FALSE){
   stopifnot(level > 0)
   all.objects <- list(...)
 
@@ -81,7 +83,7 @@ rk.paste.JS <- function(..., level=2, indent.by="\t", funct=NULL, array=NULL,
       result <- paste.JS.optionsset(this.object, level=level, indent.by=indent.by)
     } else if(inherits(this.object, "rk.JS.var")){
       result <- paste.JS.var(this.object, level=level, indent.by=indent.by, JS.prefix=var.prefix,
-        modifiers=modifiers, default=default, join=join, getter=getter)
+        modifiers=modifiers, default=default, join=join, getter=getter, var=var)
     } else if(inherits(this.object, "rk.JS.echo")){
       result <- slot(this.object, "value")
     } else if(inherits(this.object, "rk.JS.i18n")){
diff --git a/packages/rkwarddev/R/rkwarddev-package.R b/packages/rkwarddev/R/rkwarddev-package.R
index 0ea7606..4e03bf0 100644
--- a/packages/rkwarddev/R/rkwarddev-package.R
+++ b/packages/rkwarddev/R/rkwarddev-package.R
@@ -4,13 +4,13 @@
 #' Package: \tab rkwarddev\cr
 #' Type: \tab Package\cr
 #' Version: \tab 0.06-6\cr
-#' Date: \tab 2014-12-17\cr
+#' Date: \tab 2015-01-02\cr
 #' Depends: \tab R (>= 2.9.0),methods,XiMpLe (>= 0.03-21),rkward (>= 0.5.7)\cr
 #' Enhances: \tab rkward\cr
 #' Encoding: \tab UTF-8\cr
 #' License: \tab GPL (>= 3)\cr
 #' LazyLoad: \tab yes\cr
-#' URL: \tab http://rkward.kde.org\cr
+#' URL: \tab http://rkward.sourceforge.net\cr
 #' }
 #'
 #' Provides functions to create plugin skeletons and XML structures for RKWard.
diff --git a/packages/rkwarddev/man/i18n.Rd b/packages/rkwarddev/man/i18n.Rd
index bf9cc14..ef30f5d 100644
--- a/packages/rkwarddev/man/i18n.Rd
+++ b/packages/rkwarddev/man/i18n.Rd
@@ -9,7 +9,7 @@ i18n(msgid, ..., context = NULL, plural = NULL, newline = "")
 \item{msgid}{Character string, the message to be translated (if applicable,
       its singular form).}
 
-\item{...}{Either character string wich will be pasted unquoted to be used in conjunctions with
+\item{...}{Either character string which will be pasted unquoted to be used in conjunctions with
 placeholders in msgid,
       or XiMpLe.node objects of which the JavaScript variable name will be
 used.}
diff --git a/packages/rkwarddev/man/rk.JS.doc.Rd b/packages/rkwarddev/man/rk.JS.doc.Rd
index dacdb39..4e1129b 100644
--- a/packages/rkwarddev/man/rk.JS.doc.Rd
+++ b/packages/rkwarddev/man/rk.JS.doc.Rd
@@ -13,7 +13,7 @@ rk.JS.doc(require = c(), variables = NULL, globals = NULL,
 \item{require}{A character vector with names of R packages that the dialog depends on.}
 
 \item{variables}{Either a character string to be included to read in all needed variables from the dialog (see \code{\link{rk.JS.scan}}),
-or an object of class \code{rk.JS.var} which will be coerced into character. These variables will be defined in
+or a (list of) objects of class \code{rk.JS.var} which will be coerced into character. These variables will be defined in
 the \code{calculate()} and/or \code{doPrintout()} functions.}
 
 \item{globals}{Like \code{variables},
diff --git a/packages/rkwarddev/man/rk.XML.vars.Rd b/packages/rkwarddev/man/rk.XML.vars.Rd
index 39b36f9..9720ba0 100644
--- a/packages/rkwarddev/man/rk.XML.vars.Rd
+++ b/packages/rkwarddev/man/rk.XML.vars.Rd
@@ -108,7 +108,7 @@ cat(pasteXML(test.vars))
 }
 \seealso{
 \code{\link[rkwarddev:rk.XML.varslot]{rk.XML.varslot}},
-   \code{\link[rkwarddev:rk.XML.varselector]{rk.XML.varselector}},
-   and the \href{help:rkwardplugins}{Introduction to Writing Plugins for RKWard}
+       \code{\link[rkwarddev:rk.XML.varselector]{rk.XML.varselector}},
+       and the \href{help:rkwardplugins}{Introduction to Writing Plugins for RKWard}
 }
 
diff --git a/packages/rkwarddev/man/rk.paste.JS.Rd b/packages/rkwarddev/man/rk.paste.JS.Rd
index 520e69f..600b58b 100644
--- a/packages/rkwarddev/man/rk.paste.JS.Rd
+++ b/packages/rkwarddev/man/rk.paste.JS.Rd
@@ -5,7 +5,7 @@
 \usage{
 rk.paste.JS(..., level = 2, indent.by = "\\t", funct = NULL,
   array = NULL, var.prefix = NULL, modifiers = NULL, default = NULL,
-  join = NULL, getter = NULL, empty.e = FALSE)
+  join = NULL, getter = NULL, var = TRUE, empty.e = FALSE)
 }
 \arguments{
 \item{...}{Objects of class \code{rk.JS.ite}, \code{rk.JS.arr}, \code{rk.JS.opt},
@@ -48,6 +48,10 @@ values in the actual plugin. Depending on the XML element, \code{"getString"},
 useful alternatives. For backwards compatibility,
       the default is set to \code{"getValue"}.}
 
+\item{var}{For \code{rk.JS.var} objects only: Logical,
+      if \code{FALSE} the variable(s) are assumed to already be defined (globally?)
+and the JS keyword "var" will be omitted.}
+
 \item{empty.e}{For \code{rk.JS.ite} objects only: Logical,
       if \code{TRUE} will force to add empty \code{else \{\}} brackets when
 there is no \code{else} statement defined,
diff --git a/packages/rkwarddev/man/rkwarddev-package.Rd b/packages/rkwarddev/man/rkwarddev-package.Rd
index 56d5bc0..9808c2a 100644
--- a/packages/rkwarddev/man/rkwarddev-package.Rd
+++ b/packages/rkwarddev/man/rkwarddev-package.Rd
@@ -11,13 +11,13 @@ A collection of tools for RKWard plugin development.
 Package: \tab rkwarddev\cr
 Type: \tab Package\cr
 Version: \tab 0.06-6\cr
-Date: \tab 2014-12-17\cr
+Date: \tab 2015-01-02\cr
 Depends: \tab R (>= 2.9.0),methods,XiMpLe (>= 0.03-21),rkward (>= 0.5.7)\cr
 Enhances: \tab rkward\cr
 Encoding: \tab UTF-8\cr
 License: \tab GPL (>= 3)\cr
 LazyLoad: \tab yes\cr
-URL: \tab http://rkward.kde.org\cr
+URL: \tab http://rkward.sourceforge.net\cr
 }
 
 Provides functions to create plugin skeletons and XML structures for RKWard.





More information about the rkward-tracker mailing list