jquery.spin.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * Copyright (c) 2011-2013 Felix Gnass
  3. * Licensed under the MIT license
  4. */
  5. /*
  6. Basic Usage:
  7. ============
  8. $('#el').spin(); // Creates a default Spinner using the text color of #el.
  9. $('#el').spin({ ... }); // Creates a Spinner using the provided options.
  10. $('#el').spin(false); // Stops and removes the spinner.
  11. Using Presets:
  12. ==============
  13. $('#el').spin('small'); // Creates a 'small' Spinner using the text color of #el.
  14. $('#el').spin('large', '#fff'); // Creates a 'large' white Spinner.
  15. Adding a custom preset:
  16. =======================
  17. $.fn.spin.presets.flower = {
  18. lines: 9
  19. length: 10
  20. width: 20
  21. radius: 0
  22. }
  23. $('#el').spin('flower', 'red');
  24. */
  25. (function(factory) {
  26. if (typeof exports == 'object') {
  27. // CommonJS
  28. factory(require('jquery'), require('spin'))
  29. }
  30. else if (typeof define == 'function' && define.amd) {
  31. // AMD, register as anonymous module
  32. define(['jquery', 'spin'], factory)
  33. }
  34. else {
  35. // Browser globals
  36. if (!window.Spinner) throw new Error('Spin.js not present')
  37. factory(window.jQuery, window.Spinner)
  38. }
  39. }(function($, Spinner) {
  40. $.fn.spin = function(opts, color) {
  41. return this.each(function() {
  42. var $this = $(this),
  43. data = $this.data();
  44. if (data.spinner) {
  45. data.spinner.stop();
  46. delete data.spinner;
  47. }
  48. if (opts !== false) {
  49. opts = $.extend(
  50. { color: color || $this.css('color') },
  51. $.fn.spin.presets[opts] || opts
  52. )
  53. // Begin WordPress Additions
  54. // To use opts.right, you need to have specified a length, width, and radius.
  55. if ( typeof opts.right !== 'undefined' && typeof opts.length !== 'undefined'
  56. && typeof opts.width !== 'undefined' && typeof opts.radius !== 'undefined' ) {
  57. var pad = $this.css( 'padding-left' );
  58. pad = ( typeof pad === 'undefined' ) ? 0 : parseInt( pad, 10 );
  59. opts.left = $this.outerWidth() - ( 2 * ( opts.length + opts.width + opts.radius ) ) - pad - opts.right;
  60. delete opts.right;
  61. }
  62. // End WordPress Additions
  63. data.spinner = new Spinner(opts).spin(this)
  64. }
  65. })
  66. }
  67. $.fn.spin.presets = {
  68. tiny: { lines: 8, length: 2, width: 2, radius: 3 },
  69. small: { lines: 8, length: 4, width: 3, radius: 5 },
  70. large: { lines: 10, length: 8, width: 4, radius: 8 }
  71. }
  72. }));
  73. // Jetpack Presets Overrides:
  74. (function($){
  75. $.fn.spin.presets.wp = { trail: 60, speed: 1.3 };
  76. $.fn.spin.presets.small = $.extend( { lines: 8, length: 2, width: 2, radius: 3 }, $.fn.spin.presets.wp );
  77. $.fn.spin.presets.medium = $.extend( { lines: 8, length: 4, width: 3, radius: 5 }, $.fn.spin.presets.wp );
  78. $.fn.spin.presets.large = $.extend( { lines: 10, length: 6, width: 4, radius: 7 }, $.fn.spin.presets.wp );
  79. $.fn.spin.presets['small-left'] = $.extend( { left: 5 }, $.fn.spin.presets.small );
  80. $.fn.spin.presets['small-right'] = $.extend( { right: 5 }, $.fn.spin.presets.small );
  81. $.fn.spin.presets['medium-left'] = $.extend( { left: 5 }, $.fn.spin.presets.medium );
  82. $.fn.spin.presets['medium-right'] = $.extend( { right: 5 }, $.fn.spin.presets.medium );
  83. $.fn.spin.presets['large-left'] = $.extend( { left: 5 }, $.fn.spin.presets.large );
  84. $.fn.spin.presets['large-right'] = $.extend( { right: 5 }, $.fn.spin.presets.large );
  85. })(jQuery);