components_buttons.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* ------------------------------------------------------------------------------
  2. *
  3. * # Buttons and button dropdowns
  4. *
  5. * Demo JS code for components_buttons.html page
  6. *
  7. * ---------------------------------------------------------------------------- */
  8. // Setup module
  9. // ------------------------------
  10. var Buttons = function() {
  11. //
  12. // Setup module components
  13. //
  14. // Progress buttons
  15. var _componentLadda = function() {
  16. if (typeof Ladda == 'undefined') {
  17. console.warn('Warning - ladda.min.js is not loaded.');
  18. return;
  19. }
  20. // Button with spinner
  21. Ladda.bind('.btn-ladda-spinner', {
  22. dataSpinnerSize: 16,
  23. timeout: 2000
  24. });
  25. // Button with progress
  26. Ladda.bind('.btn-ladda-progress', {
  27. callback: function(instance) {
  28. var progress = 0;
  29. var interval = setInterval(function() {
  30. progress = Math.min(progress + Math.random() * 0.1, 1);
  31. instance.setProgress(progress);
  32. if( progress === 1 ) {
  33. instance.stop();
  34. clearInterval(interval);
  35. }
  36. }, 200);
  37. }
  38. });
  39. };
  40. // Loading button
  41. var _componentLoadingButton = function() {
  42. $('.btn-loading').on('click', function () {
  43. var btn = $(this),
  44. initialText = btn.data('initial-text'),
  45. loadingText = btn.data('loading-text');
  46. btn.html(loadingText).addClass('disabled');
  47. setTimeout(function () {
  48. btn.html(initialText).removeClass('disabled');
  49. }, 3000)
  50. });
  51. };
  52. //
  53. // Return objects assigned to module
  54. //
  55. return {
  56. init: function() {
  57. _componentLadda();
  58. _componentLoadingButton();
  59. }
  60. }
  61. }();
  62. // Initialize module
  63. // ------------------------------
  64. document.addEventListener('DOMContentLoaded', function() {
  65. Buttons.init();
  66. });