From 9b8871d7ddc2adfa017a7d7a49241cd64f83613b Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Fri, 12 Feb 2016 15:46:36 -0500 Subject: [PATCH] PluginAPI for more stable backwards compatibility going forward --- .../initializers/post-decorations.js.es6 | 10 +++--- .../discourse/lib/plugin-api.js.es6 | 31 ++++++++++++++++--- .../initializers/apply-details.js.es6 | 7 +++-- .../javascripts/initializers/lazyYT.js.es6 | 12 +++---- 4 files changed, 41 insertions(+), 19 deletions(-) diff --git a/app/assets/javascripts/discourse/initializers/post-decorations.js.es6 b/app/assets/javascripts/discourse/initializers/post-decorations.js.es6 index 26c9ec600c4..38a6186c0f2 100644 --- a/app/assets/javascripts/discourse/initializers/post-decorations.js.es6 +++ b/app/assets/javascripts/discourse/initializers/post-decorations.js.es6 @@ -1,11 +1,13 @@ -import { decorateCooked } from 'discourse/lib/plugin-api'; import highlightSyntax from 'discourse/lib/highlight-syntax'; import lightbox from 'discourse/lib/lightbox'; +import { getPluginApi } from 'discourse/lib/plugin-api'; export default { name: "post-decorations", - initialize: function(container) { - decorateCooked(container, highlightSyntax); - decorateCooked(container, lightbox); + initialize() { + const api = getPluginApi('0.1'); + + api.decorateCooked(highlightSyntax); + api.decorateCooked(lightbox); } }; diff --git a/app/assets/javascripts/discourse/lib/plugin-api.js.es6 b/app/assets/javascripts/discourse/lib/plugin-api.js.es6 index 874a10527c0..c65b6a241fd 100644 --- a/app/assets/javascripts/discourse/lib/plugin-api.js.es6 +++ b/app/assets/javascripts/discourse/lib/plugin-api.js.es6 @@ -8,8 +8,31 @@ function decorate(klass, evt, cb) { klass.reopen(mixin); } -export function decorateCooked(container, cb) { - addDecorator(cb); - decorate(ComposerEditor, 'previewRefreshed', cb); - decorate(container.lookupFactory('view:user-stream'), 'didInsertElement', cb); +export function decorateCooked() { + console.warn('`decorateCooked` has been removed. Use `getPluginApi(version).decorateCooked` instead'); } + +class PluginApi { + constructor(version, container) { + this.version = version; + this.container = container; + } + + decorateCooked(cb) { + addDecorator(cb); + decorate(ComposerEditor, 'previewRefreshed', cb); + decorate(this.container.lookupFactory('view:user-stream'), 'didInsertElement', cb); + } +} + +let _pluginv01; + +export function getPluginApi(version) { + if (version === "0.1") { + if (!_pluginv01) { + _pluginv01 = new PluginApi(version, Discourse.__container__); + } + return _pluginv01; + } +} + diff --git a/plugins/discourse-details/assets/javascripts/initializers/apply-details.js.es6 b/plugins/discourse-details/assets/javascripts/initializers/apply-details.js.es6 index 9813a1d6eac..b7fdf420242 100644 --- a/plugins/discourse-details/assets/javascripts/initializers/apply-details.js.es6 +++ b/plugins/discourse-details/assets/javascripts/initializers/apply-details.js.es6 @@ -1,10 +1,11 @@ -import { decorateCooked } from "discourse/lib/plugin-api"; +import { getPluginApi } from 'discourse/lib/plugin-api'; export default { name: "apply-details", - initialize(container) { - decorateCooked(container, $elem => $("details", $elem).details()); + initialize() { + const api = getPluginApi('0.1'); + api.decorateCooked($elem => $("details", $elem).details()); } }; diff --git a/plugins/lazyYT/assets/javascripts/initializers/lazyYT.js.es6 b/plugins/lazyYT/assets/javascripts/initializers/lazyYT.js.es6 index 0743a30c371..1f1fbe7fca1 100644 --- a/plugins/lazyYT/assets/javascripts/initializers/lazyYT.js.es6 +++ b/plugins/lazyYT/assets/javascripts/initializers/lazyYT.js.es6 @@ -1,13 +1,9 @@ -/** - Apply lazyYT when the app boots -**/ -import { decorateCooked } from 'discourse/lib/plugin-api'; +import { getPluginApi } from 'discourse/lib/plugin-api'; export default { name: "apply-lazyYT", - initialize: function(container) { - decorateCooked(container, function($elem) { - $('.lazyYT', $elem).lazyYT(); - }); + initialize() { + const api = getPluginApi('0.1'); + api.decorateCooked($elem => $('.lazyYT', $elem).lazyYT()); } };