epiclock.retro.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*!
  2. * Retro renderer for epiclock
  3. *
  4. * Copyright (c) Eric Garside
  5. * Dual licensed under:
  6. * MIT: http://www.opensource.org/licenses/mit-license.php
  7. * GPLv3: http://www.opensource.org/licenses/gpl-3.0.html
  8. */
  9. "use strict";
  10. /*global window, jQuery */
  11. /*jslint white: true, browser: true, onevar: true, undef: true, eqeqeq: true, bitwise: true, regexp: true, strict: true, newcap: true, immed: true, maxerr: 50, indent: 4 */
  12. (function ($) {
  13. //------------------------------
  14. //
  15. // Constants
  16. //
  17. //------------------------------
  18. /**
  19. * Because epiclock returns values as 2 digits in one number, we need an "inner template" to contain
  20. * the actual image objects.
  21. */
  22. var innerTemplate = '<span class="epiclock-img"><span class="epiclock-animation"></span></span>';
  23. //------------------------------
  24. //
  25. // Animation
  26. //
  27. //------------------------------
  28. /**
  29. * Animate a given element. The animation for the retro clock has four stages:
  30. * :a1 - First stage of the animation
  31. * :a2 - Second stage of the animation
  32. * :a3 - Third stage of the animation
  33. * :s - Static image, end of animation.
  34. *
  35. * @param element The element being animated.
  36. */
  37. function animate()
  38. {
  39. var clock = this;
  40. setTimeout(function ()
  41. {
  42. $('.a1', clock.container).removeClass('a1').addClass('a2');
  43. setTimeout(function ()
  44. {
  45. $('.a2', clock.container).removeClass('a2').addClass('s');
  46. }, 150);
  47. }, 150);
  48. }
  49. //------------------------------
  50. //
  51. // Setup
  52. //
  53. //------------------------------
  54. $.epiclock.addRenderer('retro', function (element, value)
  55. {
  56. /**
  57. * Determine if this is a collection of digits, or the am/pm string, and parser
  58. * the value accordingly.
  59. */
  60. var digits = value.substring(1) === 'm' ? [value] : value.split('').reverse(),
  61. /**
  62. * The last value of this element.
  63. */
  64. last = element.data('epiclock-last'),
  65. /**
  66. * Comparison values for the last array as compared to this one.
  67. */
  68. compare = last ? last.split('').reverse() : [],
  69. /**
  70. * The image instances for this block. If these don't yet exist, they will be created in the digit for...each callback.
  71. */
  72. image = $.makeArray($('.epiclock-img', element)).reverse();
  73. $.each(digits, function (index, digit)
  74. {
  75. /**
  76. * We don't want to change the image part if it hasn't been updated.
  77. */
  78. if (digit === compare[index])
  79. {
  80. return;
  81. }
  82. /**
  83. * Animate the number after the clock has changed.
  84. */
  85. $('.epiclock-animation', $(image[index] || $(innerTemplate).prependTo(element)).removeClass('d' + compare[index]).addClass('d' + digit)).removeClass('s').addClass('a1');
  86. });
  87. },
  88. function ()
  89. {
  90. this.bind('rendered', animate);
  91. });
  92. }(jQuery));