Fx.ProgressBar.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**
  2. * Fx.ProgressBar
  3. *
  4. * @version 1.1
  5. *
  6. * @license MIT License
  7. *
  8. * @author Harald Kirschner <mail [at] digitarald [dot] de>
  9. * @copyright Authors
  10. */
  11. Fx.ProgressBar = new Class({
  12. Extends: Fx,
  13. options: {
  14. text: null,
  15. url: null,
  16. transition: Fx.Transitions.Circ.easeOut,
  17. fit: true,
  18. link: 'cancel'
  19. },
  20. initialize: function(element, options) {
  21. this.element = $(element);
  22. this.parent(options);
  23. var url = this.options.url;
  24. if (url) {
  25. this.element.setStyles({
  26. 'background-image': 'url(' + url + ')',
  27. 'background-repeat': 'no-repeat'
  28. });
  29. }
  30. if (this.options.fit) {
  31. url = url || this.element.getStyle('background-image').replace(/^url\(["']?|["']?\)$/g, '');
  32. if (url) {
  33. var fill = new Image();
  34. fill.onload = function() {
  35. this.fill = fill.width;
  36. fill = fill.onload = null;
  37. this.set(this.now || 0);
  38. }.bind(this);
  39. fill.src = url;
  40. if (!this.fill && fill.width) fill.onload();
  41. }
  42. } else {
  43. this.set(0);
  44. }
  45. },
  46. start: function(to, total) {
  47. return this.parent(this.now, (arguments.length == 1) ? to.limit(0, 100) : to / total * 100);
  48. },
  49. set: function(to) {
  50. this.now = to;
  51. var css = (this.fill)
  52. ? (((this.fill / -2) + (to / 100) * (this.element.width || 1) || 0).round() + 'px')
  53. : ((100 - to) + '%');
  54. this.element.setStyle('backgroundPosition', css + ' 0px').title = Math.round(to) + '%';
  55. var text = $(this.options.text);
  56. if (text) text.set('text', Math.round(to) + '%');
  57. return this;
  58. }
  59. });