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.

136 lines
3.8 KiB

  1. // Generated by CoffeeScript 1.6.3
  2. var BasePlugin, ambi, eachr, extendr, typeChecker,
  3. __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
  4. extendr = require('extendr');
  5. typeChecker = require('typechecker');
  6. ambi = require('ambi');
  7. eachr = require('eachr');
  8. BasePlugin = (function() {
  9. BasePlugin.prototype.docpad = null;
  10. BasePlugin.prototype.name = null;
  11. BasePlugin.prototype.config = {};
  12. BasePlugin.prototype.instanceConfig = {};
  13. BasePlugin.prototype.priority = 500;
  14. function BasePlugin(opts) {
  15. this.getConfig = __bind(this.getConfig, this);
  16. this.setConfig = __bind(this.setConfig, this);
  17. var config, docpad, me;
  18. me = this;
  19. docpad = opts.docpad, config = opts.config;
  20. this.docpad = docpad;
  21. this.bindListeners();
  22. this.config = extendr.deepClone(this.config);
  23. this.instanceConfig = extendr.deepClone(this.instanceConfig);
  24. this.initialConfig = this.config;
  25. this.setConfig(config);
  26. if (this.isEnabled() === false) {
  27. return this;
  28. }
  29. this.addListeners();
  30. this;
  31. }
  32. BasePlugin.prototype.setInstanceConfig = function(instanceConfig) {
  33. if (instanceConfig) {
  34. extendr.safeDeepExtendPlainObjects(this.instanceConfig, instanceConfig);
  35. if (this.config) {
  36. extendr.safeDeepExtendPlainObjects(this.config, instanceConfig);
  37. }
  38. }
  39. return this;
  40. };
  41. BasePlugin.prototype.setConfig = function(instanceConfig) {
  42. var configPackages, configsToMerge, docpad, userConfig;
  43. if (instanceConfig == null) {
  44. instanceConfig = null;
  45. }
  46. docpad = this.docpad;
  47. userConfig = this.docpad.config.plugins[this.name];
  48. this.config = this.docpad.config.plugins[this.name] = {};
  49. if (instanceConfig) {
  50. this.setInstanceConfig(instanceConfig);
  51. }
  52. configPackages = [this.initialConfig, userConfig, this.instanceConfig];
  53. configsToMerge = [this.config];
  54. docpad.mergeConfigurations(configPackages, configsToMerge);
  55. if (!this.isEnabled()) {
  56. this.removeListeners();
  57. }
  58. return this;
  59. };
  60. BasePlugin.prototype.getConfig = function() {
  61. return this.config;
  62. };
  63. BasePlugin.prototype.bindEvents = function() {
  64. return this.addListeners();
  65. };
  66. BasePlugin.prototype.bindListeners = function() {
  67. var docpad, events, pluginInstance;
  68. pluginInstance = this;
  69. docpad = this.docpad;
  70. events = docpad.getEvents();
  71. eachr(events, function(eventName) {
  72. var eventHandler;
  73. eventHandler = pluginInstance[eventName];
  74. if (typeChecker.isFunction(eventHandler)) {
  75. return pluginInstance[eventName] = eventHandler.bind(pluginInstance);
  76. }
  77. });
  78. return this;
  79. };
  80. BasePlugin.prototype.addListeners = function() {
  81. var docpad, events, pluginInstance;
  82. pluginInstance = this;
  83. docpad = this.docpad;
  84. events = docpad.getEvents();
  85. eachr(events, function(eventName) {
  86. var eventHandler, eventHandlerPriority;
  87. eventHandler = pluginInstance[eventName];
  88. if (typeChecker.isFunction(eventHandler)) {
  89. eventHandlerPriority = pluginInstance[eventName + 'Priority'] || pluginInstance.priority || null;
  90. eventHandler.priority = eventHandlerPriority;
  91. return docpad.off(eventName, eventHandler).on(eventName, eventHandler);
  92. }
  93. });
  94. return this;
  95. };
  96. BasePlugin.prototype.removeListeners = function() {
  97. var docpad, events, pluginInstance;
  98. pluginInstance = this;
  99. docpad = this.docpad;
  100. events = docpad.getEvents();
  101. eachr(events, function(eventName) {
  102. var eventHandler;
  103. eventHandler = pluginInstance[eventName];
  104. if (typeChecker.isFunction(eventHandler)) {
  105. return docpad.off(eventName, eventHandler);
  106. }
  107. });
  108. return this;
  109. };
  110. BasePlugin.prototype.isEnabled = function() {
  111. return this.config.enabled !== false;
  112. };
  113. return BasePlugin;
  114. })();
  115. module.exports = BasePlugin;