R/gedit.R

##' @include gcomponent.R
NULL

## TODO: width is wrong

##' gedit
##'
##' @export
gedit <- function(text="", width=25, coerce.with=NULL, placeholder=NULL,
                  container, handler, action=NULL,
                  ..., ext.args=list()
                  ) {

  placeholder <- placeholder %||% list(...)$initial.msg %||% ""
  
  obj <- new_item()
  class(obj) <- c("GEdit", "GWidget", "GComponent", class(obj))

  ## vals
  set_vals(obj, value=text, items=list(coerce.with=coerce.with))


  ## js
  constructor <- "Ext.form.field.Text"
  args <- list(value=as.character(text),
               enableKeyEvents=TRUE,
               width=ifelse(is.character(width), width, sprintf("%spx", 8*width)),
               emptyText=placeholder
               )

  
  args <- merge_list(args, ext.args, add_dots(obj, ...))
  push_queue(write_ext_constructor(obj, constructor, args))
  
  ## add
  add(container, obj, ...)


  
  ## handlers
  transport <- function(h,...) {}
  addHandlerChanged(obj, transport)
  
  if(!missing(handler)) 
    addHandlerChanged(obj, handler, action)

  obj
}

## handlers
before_handler.GEdit <- function(obj, signal, params, ...) {
  message("before handler:", capture.output(params))
   set_vals(obj, value=params$value)
}

##' gedit changed event
##'
##' @inheritParams addHandler
##' @export
##' @rdname gWidgets-handlers
##' @method addHandlerChanged GEdit
##' @S3method addHandlerChanged GEdit
addHandlerChanged.GEdit <- function(obj, handler, action=NULL, ...) {
  addHandlerBlur(obj, handler, action, ...)
}
              
##' gedit blur event
##'
##' @inheritParams addHandler
##' @export
##' @rdname gWidgets-handlers
##' @method addHandlerBlur GEdit
##' @S3method addHandlerBlur GEdit
addHandlerBlur.GEdit <- function(obj, handler, action=NULL, ...) {
  addHandler(obj, "blur", handler, action, ...,
             params="var params = {value: w.getValue()};"
             )
}

##' gedit changed event
##'
##' @inheritParams addHandler
##' @export
##' @rdname gWidgets-handlers
##' @method addHandlerEnter GEdit
##' @S3method addHandlerEnter GEdit
addHandlerEnter.GEdit <- function(obj, handler, action=NULL, ...) {
  addHandler(obj, "enter", handler, action, ...,
             params="var params = {value: w.getValue()};"
             )
}

               
jverzani/gWidgetsWWW2.rapache documentation built on May 20, 2019, 5:19 a.m.