plugin-manager.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * Plugin Manager
  3. */
  4. var PluginManager = function(less) {
  5. this.less = less;
  6. this.visitors = [];
  7. this.preProcessors = [];
  8. this.postProcessors = [];
  9. this.installedPlugins = [];
  10. this.fileManagers = [];
  11. };
  12. /**
  13. * Adds all the plugins in the array
  14. * @param {Array} plugins
  15. */
  16. PluginManager.prototype.addPlugins = function(plugins) {
  17. if (plugins) {
  18. for (var i = 0; i < plugins.length; i++) {
  19. this.addPlugin(plugins[i]);
  20. }
  21. }
  22. };
  23. /**
  24. *
  25. * @param plugin
  26. */
  27. PluginManager.prototype.addPlugin = function(plugin) {
  28. this.installedPlugins.push(plugin);
  29. plugin.install(this.less, this);
  30. };
  31. /**
  32. * Adds a visitor. The visitor object has options on itself to determine
  33. * when it should run.
  34. * @param visitor
  35. */
  36. PluginManager.prototype.addVisitor = function(visitor) {
  37. this.visitors.push(visitor);
  38. };
  39. /**
  40. * Adds a pre processor object
  41. * @param {object} preProcessor
  42. * @param {number} priority - guidelines 1 = before import, 1000 = import, 2000 = after import
  43. */
  44. PluginManager.prototype.addPreProcessor = function(preProcessor, priority) {
  45. var indexToInsertAt;
  46. for (indexToInsertAt = 0; indexToInsertAt < this.preProcessors.length; indexToInsertAt++) {
  47. if (this.preProcessors[indexToInsertAt].priority >= priority) {
  48. break;
  49. }
  50. }
  51. this.preProcessors.splice(indexToInsertAt, 0, {preProcessor: preProcessor, priority: priority});
  52. };
  53. /**
  54. * Adds a post processor object
  55. * @param {object} postProcessor
  56. * @param {number} priority - guidelines 1 = before compression, 1000 = compression, 2000 = after compression
  57. */
  58. PluginManager.prototype.addPostProcessor = function(postProcessor, priority) {
  59. var indexToInsertAt;
  60. for (indexToInsertAt = 0; indexToInsertAt < this.postProcessors.length; indexToInsertAt++) {
  61. if (this.postProcessors[indexToInsertAt].priority >= priority) {
  62. break;
  63. }
  64. }
  65. this.postProcessors.splice(indexToInsertAt, 0, {postProcessor: postProcessor, priority: priority});
  66. };
  67. /**
  68. *
  69. * @param manager
  70. */
  71. PluginManager.prototype.addFileManager = function(manager) {
  72. this.fileManagers.push(manager);
  73. };
  74. /**
  75. *
  76. * @returns {Array}
  77. * @private
  78. */
  79. PluginManager.prototype.getPreProcessors = function() {
  80. var preProcessors = [];
  81. for (var i = 0; i < this.preProcessors.length; i++) {
  82. preProcessors.push(this.preProcessors[i].preProcessor);
  83. }
  84. return preProcessors;
  85. };
  86. /**
  87. *
  88. * @returns {Array}
  89. * @private
  90. */
  91. PluginManager.prototype.getPostProcessors = function() {
  92. var postProcessors = [];
  93. for (var i = 0; i < this.postProcessors.length; i++) {
  94. postProcessors.push(this.postProcessors[i].postProcessor);
  95. }
  96. return postProcessors;
  97. };
  98. /**
  99. *
  100. * @returns {Array}
  101. * @private
  102. */
  103. PluginManager.prototype.getVisitors = function() {
  104. return this.visitors;
  105. };
  106. /**
  107. *
  108. * @returns {Array}
  109. * @private
  110. */
  111. PluginManager.prototype.getFileManagers = function() {
  112. return this.fileManagers;
  113. };
  114. module.exports = PluginManager;