Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

68 lines
2.6KB

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