You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 satır
2.5KB

  1. // Ref: https://shiny.rstudio.com/articles/building-inputs.html
  2. // Ref: https://github.com/rstudio/shiny/blob/master/srcjs/input_binding.js
  3. const bindingName = new Shiny.InputBinding();
  4. $.extend(bindingName, {
  5. find: function(scope) {
  6. // Specify the selector that identifies your input. `scope` is a general
  7. // parent of your input elements. This function should return the nodes of
  8. // ALL of the inputs that are inside `scope`. These elements should all
  9. // have IDs that are used as the inputId on the server side.
  10. return scope.querySelectorAll("inputBindingSelector");
  11. },
  12. getValue: function(el) {
  13. // For a particular input, this function is given the element containing
  14. // your input. In this function, find or construct the value that will be
  15. // returned to Shiny. The ID of `el` is used for the inputId.
  16. // e.g: return el.value
  17. return 'FIXME';
  18. },
  19. setValue: function(el, value) {
  20. // This method is used for restoring the bookmarked state of your input
  21. // and allows you to set the input's state without triggering reactivity.
  22. // Basically, reverses .getValue()
  23. // e.g.; el.value = value
  24. console.error('bindingName.setValue() is not yet defined');
  25. },
  26. receiveMessage: function(el, data) {
  27. // Given the input's container and data, update the input
  28. // and its elements to reflect the given data.
  29. // The messages are sent from R/Shiny via
  30. // R> session$sendInputMessage(inputId, data)
  31. console.error('bindingName.receiveMessage() is not yet defined');
  32. // If you want the update to trigger reactivity, trigger a subscribed event
  33. $(el).trigger("change")
  34. },
  35. subscribe: function(el, callback) {
  36. // Listen to events on your input element. The following block listens to
  37. // the change event, but you might want to listen to another event.
  38. // Repeat the block for each event type you want to subscribe to.
  39. $(el).on("change.bindingName", function(e) {
  40. // Use callback() or callback(true).
  41. // If using callback(true) the rate policy applies,
  42. // for example if you need to throttle or debounce
  43. // the values being sent back to the server.
  44. callback();
  45. });
  46. },
  47. getRatePolicy: function() {
  48. return {
  49. policy: 'debounce', // 'debounce', 'throttle' or 'direct' (default)
  50. delay: 100 // milliseconds for debounce or throttle
  51. };
  52. },
  53. unsubscribe: function(el) {
  54. $(el).off(".bindingName");
  55. }
  56. });
  57. Shiny.inputBindings.register(bindingName, 'pkgName.bindingName');