loading.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * loading.js - loading element for blessed
  3. * Copyright (c) 2013-2015, Christopher Jeffrey and contributors (MIT License).
  4. * https://github.com/chjj/blessed
  5. */
  6. /**
  7. * Modules
  8. */
  9. var Node = require('./node');
  10. var Box = require('./box');
  11. var Text = require('./text');
  12. /**
  13. * Loading
  14. */
  15. function Loading(options) {
  16. if (!(this instanceof Node)) {
  17. return new Loading(options);
  18. }
  19. options = options || {};
  20. Box.call(this, options);
  21. this._.icon = new Text({
  22. parent: this,
  23. align: 'center',
  24. top: 2,
  25. left: 1,
  26. right: 1,
  27. height: 1,
  28. content: '|'
  29. });
  30. }
  31. Loading.prototype.__proto__ = Box.prototype;
  32. Loading.prototype.type = 'loading';
  33. Loading.prototype.load = function(text) {
  34. var self = this;
  35. // XXX Keep above:
  36. // var parent = this.parent;
  37. // this.detach();
  38. // parent.append(this);
  39. this.show();
  40. this.setContent(text);
  41. if (this._.timer) {
  42. this.stop();
  43. }
  44. this.screen.lockKeys = true;
  45. this._.timer = setInterval(function() {
  46. if (self._.icon.content === '|') {
  47. self._.icon.setContent('/');
  48. } else if (self._.icon.content === '/') {
  49. self._.icon.setContent('-');
  50. } else if (self._.icon.content === '-') {
  51. self._.icon.setContent('\\');
  52. } else if (self._.icon.content === '\\') {
  53. self._.icon.setContent('|');
  54. }
  55. self.screen.render();
  56. }, 200);
  57. };
  58. Loading.prototype.stop = function() {
  59. this.screen.lockKeys = false;
  60. this.hide();
  61. if (this._.timer) {
  62. clearInterval(this._.timer);
  63. delete this._.timer;
  64. }
  65. this.screen.render();
  66. };
  67. /**
  68. * Expose
  69. */
  70. module.exports = Loading;