morris.coffee 1006 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. Morris = window.Morris = {}
  2. $ = jQuery
  3. # Very simple event-emitter class.
  4. #
  5. # @private
  6. class Morris.EventEmitter
  7. on: (name, handler) ->
  8. unless @handlers?
  9. @handlers = {}
  10. unless @handlers[name]?
  11. @handlers[name] = []
  12. @handlers[name].push(handler)
  13. @
  14. fire: (name, args...) ->
  15. if @handlers? and @handlers[name]?
  16. for handler in @handlers[name]
  17. handler(args...)
  18. # Make long numbers prettier by inserting commas.
  19. #
  20. # @example
  21. # Morris.commas(1234567) -> '1,234,567'
  22. Morris.commas = (num) ->
  23. if num?
  24. ret = if num < 0 then "-" else ""
  25. absnum = Math.abs(num)
  26. intnum = Math.floor(absnum).toFixed(0)
  27. ret += intnum.replace(/(?=(?:\d{3})+$)(?!^)/g, ',')
  28. strabsnum = absnum.toString()
  29. if strabsnum.length > intnum.length
  30. ret += strabsnum.slice(intnum.length)
  31. ret
  32. else
  33. '-'
  34. # Zero-pad numbers to two characters wide.
  35. #
  36. # @example
  37. # Morris.pad2(1) -> '01'
  38. Morris.pad2 = (number) -> (if number < 10 then '0' else '') + number