Advertisement
Guest User

Untitled

a guest
May 31st, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. (function () {
  2. 'use strict';
  3.  
  4. function Timeline($elem, options) {
  5. var that = this;
  6.  
  7. this.$elem = $elem;
  8.  
  9. this.$widget = null;
  10. this.$points = null;
  11. this.points = {};
  12.  
  13. this.valueRange = [0, 0];
  14.  
  15. options = $.extend({
  16. min: 10,
  17. max: 19,
  18. min_step: 3,
  19. default: 12,
  20. default_count: 3
  21. }, options);
  22.  
  23. this.renderHtml = function () {
  24. this.$widget = $('<ul class="js-timeline"/>');
  25. for (var i = options.min; i <= options.max; i++) {
  26. this.points[i] = $('<li><a data-hour="' + i + '" href="#">' + i + '</a></li>').appendTo(this.$widget);
  27. }
  28. this.$widget.insertAfter(this.$elem);
  29. this.$points = this.$widget.find('li');
  30. };
  31.  
  32. this.getPoint = function (hour) {
  33. var index = hour;
  34. return index in this.points ? this.points[index] : null;
  35. };
  36.  
  37. this.setValue = function (start, count) {
  38. if (count < options.min_step) {
  39. count = options.min_step;
  40. }
  41.  
  42. this.$points.removeClass('active-center').removeClass('active-left').removeClass('active-right');
  43.  
  44. for (var i = 0; i < count; i++) {
  45. var cssClass = 'active-center';
  46. if (i === 0) {
  47. cssClass = 'active-left';
  48. }
  49. else if (i === count - 1) {
  50. cssClass = 'active-right';
  51. }
  52.  
  53. var $point = this.getPoint(start + i);
  54. if ($point) {
  55. $point.addClass(cssClass);
  56. }
  57. }
  58.  
  59. this.valueRange = [start, start + count];
  60. };
  61.  
  62. this.onSelect = function (hour) {
  63. if (hour > this.valueRange[1]) {
  64. this.setValue(this.valueRange[0], hour - this.valueRange[0] + 1);
  65. }
  66. else if (hour < this.valueRange[0]) {
  67. this.setValue(hour, this.valueRange[1] - hour);
  68. }
  69. else if (hour == this.valueRange[0] || hour == this.valueRange[1]) {
  70. return;
  71. }
  72.  
  73. var diff = Math.round(this.valueRange[1] - this.valueRange[0] / 2);
  74. var delta = hour - this.valueRange[0];
  75. if (delta < diff) {
  76. this.setValue(this.valueRange[0], delta + 1);
  77. }
  78. else {
  79. this.setValue(this.valueRange[0] + delta, hour - this.valueRange[1]);
  80. }
  81. };
  82.  
  83. this.renderHtml();
  84.  
  85. this.setValue(options.default, options.default_count);
  86.  
  87. this.$points.find('a').on('click', function () {
  88. that.onSelect($(this).data('hour'));
  89. return false;
  90. });
  91. }
  92.  
  93. $.fn.timeline = function (options) {
  94. $(this).each(function () {
  95. return new Timeline($(this), options);
  96. });
  97. }
  98. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement