A Twitch.tv viewer reward and games system.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
1.9 KiB

  1. 'use strict';
  2. var Node = require('./node');
  3. /**
  4. * Initialize a new `Block` with an optional `node`.
  5. *
  6. * @param {Node} node
  7. * @api public
  8. */
  9. var Block = module.exports = function Block(node){
  10. this.nodes = [];
  11. if (node) this.push(node);
  12. };
  13. // Inherit from `Node`.
  14. Block.prototype = Object.create(Node.prototype);
  15. Block.prototype.constructor = Block;
  16. Block.prototype.type = 'Block';
  17. /**
  18. * Block flag.
  19. */
  20. Block.prototype.isBlock = true;
  21. /**
  22. * Replace the nodes in `other` with the nodes
  23. * in `this` block.
  24. *
  25. * @param {Block} other
  26. * @api private
  27. */
  28. Block.prototype.replace = function(other){
  29. other.nodes = this.nodes;
  30. };
  31. /**
  32. * Pust the given `node`.
  33. *
  34. * @param {Node} node
  35. * @return {Number}
  36. * @api public
  37. */
  38. Block.prototype.push = function(node){
  39. return this.nodes.push(node);
  40. };
  41. /**
  42. * Check if this block is empty.
  43. *
  44. * @return {Boolean}
  45. * @api public
  46. */
  47. Block.prototype.isEmpty = function(){
  48. return 0 == this.nodes.length;
  49. };
  50. /**
  51. * Unshift the given `node`.
  52. *
  53. * @param {Node} node
  54. * @return {Number}
  55. * @api public
  56. */
  57. Block.prototype.unshift = function(node){
  58. return this.nodes.unshift(node);
  59. };
  60. /**
  61. * Return the "last" block, or the first `yield` node.
  62. *
  63. * @return {Block}
  64. * @api private
  65. */
  66. Block.prototype.includeBlock = function(){
  67. var ret = this
  68. , node;
  69. for (var i = 0, len = this.nodes.length; i < len; ++i) {
  70. node = this.nodes[i];
  71. if (node.yield) return node;
  72. else if (node.textOnly) continue;
  73. else if (node.includeBlock) ret = node.includeBlock();
  74. else if (node.block && !node.block.isEmpty()) ret = node.block.includeBlock();
  75. if (ret.yield) return ret;
  76. }
  77. return ret;
  78. };
  79. /**
  80. * Return a clone of this block.
  81. *
  82. * @return {Block}
  83. * @api private
  84. */
  85. Block.prototype.clone = function(){
  86. var clone = new Block;
  87. for (var i = 0, len = this.nodes.length; i < len; ++i) {
  88. clone.push(this.nodes[i].clone());
  89. }
  90. return clone;
  91. };