application.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Some general UI pack related JS
  2. // Extend JS String with repeat method
  3. String.prototype.repeat = function (num) {
  4. return new Array(Math.round(num) + 1).join(this);
  5. };
  6. (function ($) {
  7. // Add segments to a slider
  8. $.fn.addSliderSegments = function () {
  9. return this.each(function () {
  10. var $this = $(this),
  11. option = $this.slider('option'),
  12. amount = (option.max - option.min)/option.step,
  13. orientation = option.orientation;
  14. if ( 'vertical' === orientation ) {
  15. var output = '', i;
  16. console.log(amount);
  17. for (i = 1; i <= amount - 1; i++) {
  18. output += '<div class="ui-slider-segment" style="top:' + 100 / amount * i + '%;"></div>';
  19. }
  20. $this.prepend(output);
  21. } else {
  22. var segmentGap = 100 / (amount) + '%';
  23. var segment = '<div class="ui-slider-segment" style="margin-left: ' + segmentGap + ';"></div>';
  24. $this.prepend(segment.repeat(amount - 1));
  25. }
  26. });
  27. };
  28. $(function () {
  29. // Todo list
  30. $('.todo').on('click', 'li', function () {
  31. $(this).toggleClass('todo-done');
  32. });
  33. // Custom Selects
  34. if ($('[data-toggle="select"]').length) {
  35. $('[data-toggle="select"]').select2();
  36. }
  37. // Checkboxes and Radio buttons
  38. $('[data-toggle="checkbox"]').radiocheck();
  39. $('[data-toggle="radio"]').radiocheck();
  40. // Tooltips
  41. $('[data-toggle=tooltip]').tooltip('show');
  42. // jQuery UI Sliders
  43. var $slider = $('#slider');
  44. if ($slider.length > 0) {
  45. $slider.slider({
  46. max: 15,
  47. step: 6,
  48. value: 3,
  49. orientation: 'horizontal',
  50. range: 'min'
  51. }).addSliderSegments();
  52. }
  53. var $verticalSlider = $('#vertical-slider');
  54. if ($verticalSlider.length) {
  55. $verticalSlider.slider({
  56. min: 1,
  57. max: 5,
  58. value: 3,
  59. orientation: 'vertical',
  60. range: 'min'
  61. }).addSliderSegments($verticalSlider.slider('option').max, 'vertical');
  62. }
  63. // Focus state for append/prepend inputs
  64. $('.input-group').on('focus', '.form-control', function () {
  65. $(this).closest('.input-group, .form-group').addClass('focus');
  66. }).on('blur', '.form-control', function () {
  67. $(this).closest('.input-group, .form-group').removeClass('focus');
  68. });
  69. // Make pagination demo work
  70. $('.pagination').on('click', 'a', function () {
  71. $(this).parent().siblings('li').removeClass('active').end().addClass('active');
  72. });
  73. $('.btn-group').on('click', 'a', function () {
  74. $(this).siblings().removeClass('active').end().addClass('active');
  75. });
  76. // Disable link clicks to prevent page scrolling
  77. $(document).on('click', 'a[href="#fakelink"]', function (e) {
  78. e.preventDefault();
  79. });
  80. // Switches
  81. if ($('[data-toggle="switch"]').length) {
  82. $('[data-toggle="switch"]').bootstrapSwitch();
  83. }
  84. // Typeahead
  85. if ($('#typeahead-demo-01').length) {
  86. var states = new Bloodhound({
  87. datumTokenizer: function (d) { return Bloodhound.tokenizers.whitespace(d.word); },
  88. queryTokenizer: Bloodhound.tokenizers.whitespace,
  89. limit: 4,
  90. local: [
  91. { word: 'Alabama' },
  92. { word: 'Alaska' },
  93. { word: 'Arizona' },
  94. { word: 'Arkansas' },
  95. { word: 'California' },
  96. { word: 'Colorado' }
  97. ]
  98. });
  99. states.initialize();
  100. $('#typeahead-demo-01').typeahead(null, {
  101. name: 'states',
  102. displayKey: 'word',
  103. source: states.ttAdapter()
  104. });
  105. }
  106. // make code pretty
  107. window.prettyPrint && prettyPrint();
  108. });
  109. })(jQuery);