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

77 lines
2.7KB

  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. if (nchar === 0) {
  20. this._timing = null
  21. return null
  22. }
  23. if (!this._timing && nchar > 0) {
  24. this._timing = Date.now()
  25. return null
  26. }
  27. return {nchar, nword, timing: this._timing};
  28. },
  29. setValue: function(el, value) {
  30. // This method is used for restoring the bookmarked state of your input
  31. // and allows you to set the input's state without triggering reactivity.
  32. // Basically, reverses .getValue()
  33. // e.g.; el.value = value
  34. console.error('typingSpeed.setValue() is not yet defined');
  35. },
  36. receiveMessage: function(el, data) {
  37. // Given the input's container and data, update the input
  38. // and its elements to reflect the given data.
  39. // The messages are sent from R/Shiny via
  40. // R> session$sendInputMessage(inputId, data)
  41. console.error('typingSpeed.receiveMessage() is not yet defined');
  42. // If you want the update to trigger reactivity, trigger a subscribed event
  43. // $(el).trigger("keyup")
  44. },
  45. subscribe: function(el, callback) {
  46. // Listen to events on your input element. The following block listens to
  47. // the change event, but you might want to listen to another event.
  48. // Repeat the block for each event type you want to subscribe to.
  49. $(el).on("keyup.typingSpeed", function(e) {
  50. // Use callback() or callback(true).
  51. // If using callback(true) the rate policy applies,
  52. // for example if you need to throttle or debounce
  53. // the values being sent back to the server.
  54. callback();
  55. });
  56. },
  57. getRatePolicy: function() {
  58. return {
  59. policy: 'debounce', // 'debounce', 'throttle' or 'direct' (default)
  60. delay: 100 // milliseconds for debounce or throttle
  61. };
  62. },
  63. unsubscribe: function(el) {
  64. $(el).off(".typingSpeed");
  65. }
  66. });
  67. Shiny.inputBindings.register(typingSpeed, 'js4shiny.typingSpeed');