Automattic / media-explorer

With Media Explorer, you can now search for tweets and videos on Twitter and YouTube directly from the Add Media screen in WordPress.
127 stars 33 forks source link

Allow 3rd-party plugins to use custom views and controllers #74

Open r-a-y opened 7 years ago

r-a-y commented 7 years ago

MEXP's JS has hardcoded references to its own views and controllers. This makes things impossible for more than one plugin to override these internals.

For example, MEXP's built-in YouTube service manually overrides MEXP's own media.view.MEXP and media.view.Toolbar.MEXP, so no other plugin can touch these without conflicts occurring: https://github.com/Automattic/media-explorer/blob/master/js/mexp.js#L85 https://github.com/Automattic/media-explorer/blob/master/services/youtube/js.js#L11

https://github.com/Automattic/media-explorer/blob/master/js/mexp.js#L36 https://github.com/Automattic/media-explorer/blob/master/services/youtube/js.js#L219

This commit allows each service to set their own views and controllers and refactors MEXP to allow these views and controllers to be initialized.

Specifically, adds a JS utility function - stringToFunction() - so we can instantiate custom views (could do better here).

Plugins can override the default views and controllers by registering them on the "mexp_js_{$service_id}" hook. See how we're using this hook with our Google Drive MEXP plugin.

I didn't change MEXP's YouTube JS to do this yet. If this commit looks good in your eyes, then I'll add a commit for YouTube.