Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class ClassWatcher {
  2.  
  3.     constructor(targetNode, classToWatch, classAddedCallback, classRemovedCallback) {
  4.         this.targetNode = targetNode
  5.         this.classToWatch = classToWatch
  6.         this.classAddedCallback = classAddedCallback
  7.         this.classRemovedCallback = classRemovedCallback
  8.         this.observer = null
  9.         this.lastClassState = targetNode.classList.contains(this.classToWatch)
  10.  
  11.         this.init()
  12.     }
  13.  
  14.     init() {
  15.         this.observer = new MutationObserver(this.mutationCallback.bind(this))
  16.         this.observe()
  17.     }
  18.  
  19.     observe() {
  20.         this.observer.observe(this.targetNode, { attributes: true })
  21.     }
  22.  
  23.     disconnect() {
  24.         this.observer.disconnect()
  25.     }
  26.  
  27.     mutationCallback(mutationsList) {
  28.         for (let mutation of mutationsList) {
  29.             if (mutation.type === 'attributes' && mutation.attributeName === 'class') {
  30.                 let currentClassState = mutation.target.classList.contains(this.classToWatch)
  31.                 if (this.lastClassState !== currentClassState) {
  32.                     this.lastClassState = currentClassState
  33.                     if (currentClassState) {
  34.                         this.classAddedCallback()
  35.                     } else {
  36.                         this.classRemovedCallback()
  37.                     }
  38.                 }
  39.             }
  40.         }
  41.     }
  42. }
  43.  
  44. function getEventHandlerName(target)
  45. {
  46.     return Object.keys(target).find(
  47.         prop => prop.startsWith('__reactEventHandlers')
  48.     )
  49. }
  50.  
  51. const targetNode = $('.reaction-time-test')
  52. const callbackNull = () => {}
  53. const callbackReation = () => {
  54.     const handlerName = getEventHandlerName(targetNode)
  55.    
  56.     if (targetNode[handlerName] && targetNode[handlerName].onMouseDown) {
  57.         targetNode[handlerName].onMouseDown()
  58.         console.log('Click!')
  59.     } else {
  60.         console.log('Click handler not found.')
  61.     }
  62. }
  63.  
  64. const classWatcher = new ClassWatcher($('.reaction-time-test'), 'view-go', callbackReation, callbackNull)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement