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.

105 lines
2.8 KiB

  1. // Generated by CoffeeScript 1.6.2
  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.config = extendr.deepClone(this.config);
  22. this.instanceConfig = extendr.deepClone(this.instanceConfig);
  23. this.initialConfig = this.config;
  24. this.setConfig(config);
  25. if (this.isEnabled() === false) {
  26. return this;
  27. }
  28. this.bindEvents();
  29. this;
  30. }
  31. BasePlugin.prototype.setInstanceConfig = function(instanceConfig) {
  32. if (instanceConfig) {
  33. extendr.safeDeepExtendPlainObjects(this.instanceConfig, instanceConfig);
  34. if (this.config) {
  35. extendr.safeDeepExtendPlainObjects(this.config, instanceConfig);
  36. }
  37. }
  38. return this;
  39. };
  40. BasePlugin.prototype.setConfig = function(instanceConfig) {
  41. var configPackages, configsToMerge, docpad, userConfig;
  42. if (instanceConfig == null) {
  43. instanceConfig = null;
  44. }
  45. docpad = this.docpad;
  46. userConfig = this.docpad.config.plugins[this.name];
  47. this.config = this.docpad.config.plugins[this.name] = {};
  48. if (instanceConfig) {
  49. this.setInstanceConfig(instanceConfig);
  50. }
  51. configPackages = [this.initialConfig, userConfig, this.instanceConfig];
  52. configsToMerge = [this.config];
  53. docpad.mergeConfigurations(configPackages, configsToMerge);
  54. return this;
  55. };
  56. BasePlugin.prototype.getConfig = function() {
  57. return this.config;
  58. };
  59. BasePlugin.prototype.bindEvents = function() {
  60. var docpad, events, pluginInstance;
  61. pluginInstance = this;
  62. docpad = this.docpad;
  63. events = docpad.getEvents();
  64. eachr(events, function(eventName) {
  65. var eventHandler;
  66. if (typeChecker.isFunction(pluginInstance[eventName])) {
  67. eventHandler = pluginInstance[eventName];
  68. return docpad.on(eventName, function(opts, next) {
  69. if (pluginInstance.isEnabled() === false) {
  70. return next();
  71. }
  72. return ambi(eventHandler.bind(pluginInstance), opts, next);
  73. });
  74. }
  75. });
  76. return this;
  77. };
  78. BasePlugin.prototype.isEnabled = function() {
  79. return this.config.enabled !== false;
  80. };
  81. return BasePlugin;
  82. })();
  83. module.exports = BasePlugin;