Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reopenWidget method #4657

Merged
merged 4 commits into from Feb 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions app/assets/javascripts/discourse/lib/plugin-api.js.es6
Expand Up @@ -5,7 +5,7 @@ import { addButton } from 'discourse/widgets/post-menu';
import { includeAttributes } from 'discourse/lib/transform-post';
import { addToolbarCallback } from 'discourse/components/d-editor';
import { addWidgetCleanCallback } from 'discourse/components/mount-widget';
import { createWidget, decorateWidget, changeSetting } from 'discourse/widgets/widget';
import { createWidget, reopenWidget, decorateWidget, changeSetting } from 'discourse/widgets/widget';
import { onPageChange } from 'discourse/lib/page-tracker';
import { preventCloak } from 'discourse/widgets/post-stream';
import { h } from 'virtual-dom';
Expand Down Expand Up @@ -307,6 +307,16 @@ class PluginApi {
return createWidget(name, args);
}

/**
* Exposes the widget update ability to plugins. Updates the widget
* registry for the given widget name to include the properties on args
* See `reopenWidget` in `discourse/widgets/widget` from more ifo.
**/

reopenWidget(name, args) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll also need to bump the API version, otherwise people don't have a version to tie this new function to.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, don't quite follow that yet. Is this the current version here?

function getPluginApi(version) {
  version = parseFloat(version);
  if (version <= 0.6) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! If you just update that to 0.7 then anyone who uses 0.7 will be guaranteed to have your new function.

return reopenWidget(name, args);
}

/**
* Adds a property that can be summed for calculating the flag counter
**/
Expand Down Expand Up @@ -357,7 +367,7 @@ class PluginApi {
let _pluginv01;
function getPluginApi(version) {
version = parseFloat(version);
if (version <= 0.6) {
if (version <= 0.7) {
if (!_pluginv01) {
_pluginv01 = new PluginApi(version, Discourse.__container__);
}
Expand Down
11 changes: 11 additions & 0 deletions app/assets/javascripts/discourse/widgets/widget.js.es6
Expand Up @@ -125,6 +125,17 @@ export function createWidget(name, opts) {
return result;
}

export function reopenWidget(name, opts) {
let existing = _registry[name];
if (!existing) {
console.error(`Could not find widget ${name} in registry`);
return
}

Object.keys(opts).forEach(k => existing.prototype[k] = opts[k]);
return existing;
}

export default class Widget {
constructor(attrs, register, opts) {
opts = opts || {};
Expand Down