Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { TweenLite, Back } from 'gsap'
  2.  
  3. function parseISOLocal(s) {
  4.     if (!s) {
  5.         return new Date()
  6.     }
  7.     var b = s.split(/\D/)
  8.     return new Date(b[0], b[1] - 1, b[2], b[3], b[4], b[5]);
  9. }
  10.  
  11. // JSON.parse()
  12.  
  13. /*eslint-disable */
  14. try {
  15.     var stats = JSON.parse(window.stats).Clocks
  16.     var root = window.urlApi
  17. } catch (e) {
  18.     var stats = []
  19.     var root = '/'
  20.     console.log('catch');
  21. }
  22. /*eslint-enable */
  23.  
  24. const counter = (comp, node) => {
  25.     comp.init(() => {
  26.         this.__shareButton = node.querySelector('#share-button')
  27.         this.__nextButton = node.querySelector('#counter-next')
  28.         this.__prevButton = node.querySelector('#counter-prev')
  29.         this.__shareBtnText = node.querySelector('#share-button-text')
  30.  
  31.         if (stats.length > 1) {
  32.             comp.elm.className += ' -slide-on'
  33.         }
  34.  
  35.         this.__shareButton.addEventListener('click', () => {
  36.             this.shareHandler()
  37.         })
  38.         this.__nextButton.addEventListener('click', () => {
  39.             let n = (this.__selectedIndex + 1) % stats.length
  40.             this.select(n)
  41.         })
  42.         this.__prevButton.addEventListener('click', () => {
  43.             let n = (this.__selectedIndex - 1) % stats.length
  44.             if (n < 0) {
  45.                 n += stats.length
  46.             }
  47.             this.select(n)
  48.         })
  49.  
  50.         this.__t0 = parseISOLocal(window.t0)
  51.         this.__t1 = new Date()
  52.  
  53.         this.__title = new StringAnimation(node.querySelector('.counter-title'), 'EVERY ', ', A WOMAN IS A VICTIM')
  54.         this.__info = new StringAnimation(node.querySelector('#counter-info-subtitle'), '--------------------------------------------------------------------------------------------------------------------------------> OF ', ' <--------------------------------------------------------------------------------------------------------------------------------')
  55.         this.__content = new StringReveal(node.querySelector('#counter-text')) // new StringAnimation(node.querySelector('#counter-text'), 'MULHERES', '')
  56.  
  57.         this.counter = new Counter(node)
  58.         this.counter.display = 0
  59.         this.select(0)
  60.  
  61.         this.__lastW = window.innerWidth
  62.  
  63.         window.onresize = () => {
  64.             let ww = window.innerWidth
  65.  
  66.             if ((ww > 1200 && this.__lastW <= 1200) || (ww <= 1200 && this.__lastW > 1200)) {
  67.                 this.updateMainText()
  68.             }
  69.             this.__lastW = ww
  70.         }
  71.     })
  72.  
  73.     this.select = (index) => {
  74.         if (stats.length <= 0) {
  75.             return false
  76.         }
  77.         this.__multiplier = stats[index].y_total / 365 / 24 / 60 / 60
  78.         this.__selectedIndex = index
  79.         this.update(true)
  80.  
  81.         let t = (1 / this.__multiplier).toFixed(1)
  82.  
  83.     /* eslint-disable */
  84.          ~~t == t ? t = ~~t : t = (t)
  85.  
  86.         let s
  87.         let dec
  88.         if (t >= 3600) {
  89.             t /= 3600
  90.             s = parseFloat(t.toPrecision(2))
  91.             ~~s == s ? s = parseInt(s).toString() : s.toString()
  92.             s += (t > 2) ? '</span> HOURS' : '</span> HOUR'
  93.         }
  94.         else if (t >= 60 && t < 3600) {
  95.             t /= 60
  96.             s = parseFloat(t.toPrecision(2))
  97.             ~~s == s ? s = parseInt(s).toString() : s.toString()
  98.             s += (t >= 2) ? '</span> MINUTES' : '</span> MINUTE'
  99.         }
  100.         else {
  101.             s = String(t)
  102.             s += (t >= 2) ? '</span> SECONDS' : '</span> SECOND'
  103.         }
  104.         /* eslint-enable */
  105.  
  106.         this.updateMainText()
  107.  
  108.         this.__title.to(s)
  109.         this.__info.to(stats[index].label)
  110.     }
  111.  
  112.     this.updateMainText = () => {
  113.         let ctext = (stats[this.__selectedIndex].content)
  114.         if (window.innerWidth <= 1200) {
  115.             ctext = ctext.replace(/<br[^>]*>/gi, ' ')
  116.         }
  117.         // this.__content.innerHTML = 'MULHERES' + ctext
  118.         this.__content.to(ctext)
  119.     }
  120.  
  121.     this.createTimer = () => {
  122.         this.updateLoop = setInterval(this.update.bind(this), 400)
  123.     }
  124.  
  125.     this.getCurrentDate = () => {
  126.         let now = new Date()
  127.         let dif = now.getTime() - this.__t1.getTime()
  128.         return (new Date(this.__t0.getTime() + dif))
  129.     }
  130.  
  131.     this.update = (slow) => {
  132.         let tm = this.getCurrentDate()
  133.         let s = tm.getSeconds()
  134.         let m = tm.getMinutes()
  135.         let h = tm.getHours()
  136.  
  137.         let ts = s + (60 * m) + (60 * 60 * h)
  138.         let n = Math.round(ts * this.__multiplier)
  139.         let time = (slow) ? 1.5 : 0.2
  140.         let callback = (slow) ? this.createTimer.bind(this) : null
  141.  
  142.         if (slow) {
  143.             clearInterval(this.updateLoop)
  144.         }
  145.  
  146.         if (n !== this.counter.display) {
  147.             TweenLite.to(this.counter, time, {
  148.                 display: n,
  149.                 ease: Back.easeOut.config(slow ? 1.5 : 3),
  150.                 onComplete: callback
  151.             })
  152.         }
  153.     }
  154.  
  155.     this.shareHandler = () => {
  156.         let setErrorState = function() {
  157.             comp.publish('img-share', 'http://clocksofviolence.com/assets/img/en/relogios-da-violencia-share-site.jpg')
  158.             comp.publish('open-share', document.location.href)
  159.             this.__shareBtnText.innerHTML = 'SHARE THIS CLOCK'
  160.         }.bind(this)
  161.  
  162.         this.__shareBtnText.innerHTML = 'WAIT'
  163.         let idx = this.__selectedIndex
  164.         if (idx >= 10) {
  165.             idx++
  166.         }
  167.  
  168.         // let url = 'http://mariadapenha.aceite.fbiz.com.br/relogio-da-violencia/api'
  169.         // let url = window.hyojun.config.root + 'api'
  170.         let url = root
  171.         let data = 'shareId=' + stats[idx].shareId
  172.         let request = new XMLHttpRequest()
  173.         request.open('POST', url, true)
  174.         request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  175.         request.onload = () => {
  176.             if (request.status >= 200 && request.status < 400) {
  177.                 let response = JSON.parse(request.responseText)
  178.                 if (response.success) {
  179.                     comp.publish('img-share', response.imageSrc)
  180.                     comp.publish('open-share', response.shareUrl)
  181.                     this.__shareBtnText.innerHTML = 'SHARE THIS CLOCK'
  182.                 } else {
  183.                     console.error('api failed', response.erro || 'unkown')
  184.                     setErrorState()
  185.                 }
  186.             } else {
  187.                 console.error('request status not ok')
  188.                 setErrorState()
  189.             }
  190.         }
  191.         request.onerror = () => {
  192.             console.error('request failed')
  193.             setErrorState()
  194.         }
  195.         request.send(data)
  196.     }
  197.  
  198. }
  199.  
  200. class Counter {
  201.     constructor(node) {
  202.         this.__size = 5
  203.         this.__digits = []
  204.         this.__dot = node.querySelector('#counter-dot')
  205.  
  206.         let i, el, dg
  207.         for (i = 0; i < this.__size; i++) {
  208.             el = node.querySelector('#counter-digit-' + (i + 1))
  209.             dg = new Digit(el)
  210.             this.__digits.push(dg)
  211.         }
  212.  
  213.         this.display = 0
  214.     }
  215.  
  216.     get display() {
  217.         return (this.__display)
  218.     }
  219.     set display(value) {
  220.         if (value < 0) {
  221.             value = 0
  222.         }
  223.         this.__display = value
  224.         let i, d, d10, v, r
  225.         for (i = 0; i < this.__size; i++) {
  226.             d = Math.pow(10, i)
  227.             if (Math.ceil(value) <= d) {
  228.                 this.__digits[i].number = 0
  229. //              this.__digits[i].visible = false
  230.                 continue
  231.             }
  232.             d10 = (d - 1) / (d)
  233.             v = (value / d) % 10
  234.             r = (v % 1) - d10
  235.             r = Math.pow(Math.max(r * d, 0), 2)
  236.             this.__digits[i].number = Math.floor(v) + r
  237. //          this.__digits[i].visible = true
  238.         }
  239.  
  240. //      this.__dot.style.visibility = (value >= 1000) ? 'visible' : 'hidden'
  241.     }
  242. }
  243.  
  244. class Digit {
  245.     constructor($el) {
  246.         this.__el = $el
  247.         this.__numbers = []
  248.         this.__v = true
  249.  
  250.         this.__container = document.createElement('div')
  251.         this.__container.style.position = 'absolute'
  252.         this.__el.appendChild(this.__container)
  253.  
  254.         let i, nb
  255.         for (i = 0; i <= 10; i++) {
  256.             nb = new DNumber(i)
  257.             this.__container.appendChild(nb.element)
  258.             this.__numbers.push(nb)
  259.         }
  260.     }
  261.  
  262.     get number() {
  263.         return (this.__number)
  264.     }
  265.     set number(value) {
  266.         this.__number = value
  267.         this.__container.style.top = -((value % 10) * 100) + '%'
  268.     }
  269.  
  270.     get visible() {
  271.         return (this.__v)
  272.     }
  273.     set visible(value) {
  274.         if (value === this.__v) {
  275.             return
  276.         }
  277.         this.__v = value
  278.         if (!value) {
  279.             this.__el.className += ' hidden'
  280.         } else {
  281.             this.__el.className = this.__el.className.replace(' hidden', '')
  282.         }
  283.     }
  284. }
  285.  
  286. class DNumber {
  287.     constructor(n) {
  288.         this.element = document.createElement('div')
  289.         this.element.className = 'number number-' + n
  290.         this.rotation = 0
  291.     }
  292. }
  293.  
  294. class StringAnimation {
  295.     constructor(target, left, right) {
  296.         this.__target = target
  297.         this.__left = left
  298.         this.__right = right
  299.         this.__step = 0
  300.         this.__message = ''
  301.     }
  302.  
  303.     to(string) {
  304.         if (this.__message.length > 0) {
  305.             TweenLite.to(this, (this.__message.length + 1) * 0.02, {
  306.                 step: 0,
  307.                 onComplete: () => {
  308.                     this.__message = ''
  309.                     this.to(string)
  310.                 }
  311.             })
  312.         } else {
  313.             this.__message = string
  314.             TweenLite.to(this, (string.length + 1) * 0.05, {
  315.                 step: (string.length)
  316.             })
  317.         }
  318.     }
  319.  
  320.     get step() {
  321.         return (this.__step)
  322.     }
  323.     set step(value) {
  324.         this.__step = value
  325.  
  326.         let i, s
  327.         s = ''
  328.         for (i = 0; i < value; i++) {
  329.             if (value >= i + 1) {
  330.                 s += this.__message.charAt(i)
  331.             } else {
  332.                 s += String.fromCharCode(Math.floor(Math.random() * 74) + 48)
  333.             }
  334.         }
  335.  
  336.         this.__target.innerHTML = this.__left + s + this.__right
  337.     }
  338. }
  339.  
  340. class StringReveal {
  341.     constructor(target) {
  342.         this.__target = target
  343.         this.__step = 0
  344.     }
  345.  
  346.     to(string) {
  347.         this.__message = string
  348.         this.__step = 0
  349.         this.__target.innerHTML = string
  350.         TweenLite.killTweensOf(this)
  351.         TweenLite.to(this, 2, {
  352.             step: 1
  353.         })
  354.     }
  355.  
  356.     get step() {
  357.         return (this.__step)
  358.     }
  359.     set step(value) {
  360.         this.__step = value
  361.         this.__target.style.opacity = value
  362.     }
  363.  
  364.     get target() {
  365.         return (this.__target)
  366.     }
  367. }
  368.  
  369. jails('counter', counter)
  370. jails.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement