-
Notifications
You must be signed in to change notification settings - Fork 7
/
plugins.R
107 lines (91 loc) · 2.14 KB
/
plugins.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#' @title jsTree plugin builder api
#' @description api to iteratively create jsTree plugins
#' @param name character, name of plugin
#' @param attr list, names of attributes relevant to the plugin
#' @return list
#' @details plugins and their characteristics can be found at \url{https://www.jstree.com/api/}
#' @examples
#' p <- plugin(name = 'search',
#' attr = list(case_sensitive = FALSE,
#' show_only_matches = FALSE))
#'
#' p
#'
#' p1 <- plugin(name = 'dnd',
#' attr = list(copy = FALSE))
#'
#' p1
#'
#'
#' p2 <- p + p1
#'
#' p2
#'
#' p2 + plugin(name = 'dnd',
#' attr = list(copy = TRUE))
#'
#' p2 - p + p1
#' @rdname plugin
#' @export
plugin <- function(name,attr){
ret <- setNames(list(attr),name)
attr(ret,'class') <- c('list','plugin')
return(ret)
}
#' @title Add/remove child plugins from plugin object
#' @description + will add/replace a plugin child to a plugin object,
#' - will remove a child from a plugin object
#' @param e1 plugin object
#' @param e2 child to add/remove
#' @return plugin object
#' @details DETAILS
#' @examples
#' @examples
#' p <- plugin(name = 'search',
#' attr = list(case_sensitive = FALSE,
#' show_only_matches = FALSE))
#'
#' p
#'
#' p1 <- plugin(name = 'dnd',
#' attr = list(copy = FALSE))
#'
#' p1
#'
#' p2 <- p + p1
#'
#' p2
#'
#' p2 - p + p1
#' @rdname plugin-ops
#' @export
`+.plugin` <- function(e1,e2){
idx <- grep(names(e2),names(e1))
if(length(idx)>0){
e1 <- e1[-idx]
}
e1 <- append(e1,e2)
attr(e1,'class') <- c('list','plugin')
return(e1)
}
#' @export
#' @rdname plugin-ops
`-.plugin` <- function(e1,e2){
idx <- grep(names(e2),names(e1))
if(length(idx)>0)
e1 <- e1[-idx]
attr(e1,'class') <- c('list','plugin')
return(e1)
}
#' @title Print method for plugin objects
#' @description Prints a plugin object in json form
#' @param x plugin object
#' @param \dots unused
#' @seealso
#' \code{\link[jsonlite]{toJSON, fromJSON}}
#' @rdname print-plugin
#' @export
#' @importFrom jsonlite toJSON
print.plugin <- function(x,...){
print(jsonlite::toJSON(x,pretty = TRUE,auto_unbox = TRUE))
}