plugin_development.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. .. raw:: html
  2. <div id="banner"><a href="https://github.com/jcbrand/converse.js/blob/master/docs/source/theming.rst">Edit me on GitHub</a></div>
  3. .. _`writing-a-plugin`:
  4. Writing a plugin
  5. ================
  6. .. contents:: Table of Contents
  7. :depth: 2
  8. :local:
  9. Introduction
  10. ------------
  11. Developers are able to extend and override the objects, functions and the
  12. Backbone models and views that make up converse.js by means of writing plugins.
  13. Converse.js uses `pluggable.js <https://github.com/jcbrand/pluggable.js/>`_ as
  14. its plugin architecture.
  15. To understand how this plugin architecture works, please read the
  16. `pluggable.js documentation <https://jcbrand.github.io/pluggable.js/>`_
  17. and to understand its inner workins, please refer to the `annotated source code
  18. <https://jcbrand.github.io/pluggable.js/docs/pluggable.html>`_.
  19. You register a converse.js plugin as follows:
  20. .. code-block:: javascript
  21. converse.plugins.add('myplugin', {
  22. initialize: function () {
  23. // This method gets called once converse.initialize has been called
  24. // and the plugin itself has been loaded.
  25. // Inside this method, you have access to the closured
  26. // _converse object as an attribute on "this".
  27. // E.g. this._converse
  28. },
  29. });
  30. Security and access to the inner workings
  31. -----------------------------------------
  32. The globally available ``converse`` object, which exposes the API methods, such
  33. as ``initialize`` and ``plugins.add``, is a wrapper that encloses and protects
  34. a sensitive inner object, named ``_converse`` (not the underscore prefix).
  35. This inner ``_converse`` object contains all the Backbone models and views,
  36. as well as various other attributes and functions.
  37. Within a plugin, you will have access to this internal
  38. `"closured" <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures>`_
  39. ``_converse`` object, which is normally not exposed in the global variable scope.
  40. The inner ``_converse`` object is made private in order to safely hide and
  41. encapsulate sensitive information and methods which should not be exposed
  42. to any 3rd-party scripts that might be running in the same page.
  43. Loading a plugin
  44. -----------------
  45. Converse.js uses the UMD (Universal Modules Definition) as its module syntax.
  46. This makes modules loadable via `require.js`, `webpack` or other module
  47. loaders, but also includable as old-school `<script>` tags in your HTML.
  48. Here's an example of the plugin shown above wrapped inside a UMD module:
  49. .. code-block:: javascript
  50. (function (root, factory) {
  51. if (typeof define === 'function' && define.amd) {
  52. // AMD. Register as a module called "myplugin"
  53. define("myplugin", ["converse"], factory);
  54. } else {
  55. // Browser globals. If you're not using a module loader such as require.js,
  56. // then this line below executes. Make sure that your plugin's <script> tag
  57. // appears after the one from converse.js.
  58. factory(converse);
  59. }
  60. }(this, function (converse) {
  61. converse.plugins.add('myplugin', {
  62. initialize: function () {
  63. // This method gets called once converse.initialize has been called
  64. // and the plugin itself has been loaded.
  65. // Inside this method, you have access to the closured
  66. // _converse object as an attribute on "this".
  67. // E.g. this._converse
  68. },
  69. });
  70. });
  71. Accessing 3rd party libraries
  72. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  73. Immediately inside the module shown above you can access 3rd party libraries (such
  74. moment, underscore and jQuery) via the ``converse.env`` map.
  75. The code for it would look something like this:
  76. .. code-block:: javascript
  77. // Commonly used utilities and variables can be found under the "env"
  78. // namespace of the "converse" global.
  79. var Strophe = converse.env.Strophe,
  80. $iq = converse.env.$iq,
  81. $msg = converse.env.$msg,
  82. $pres = converse.env.$pres,
  83. $build = converse.env.$build,
  84. b64_sha1 = converse.env.b64_sha1;
  85. $ = converse.env.jQuery,
  86. _ = converse.env._,
  87. moment = converse.env.moment;
  88. These dependencies are closured so that they don't pollute the global
  89. namespace, that's why you need to access them in such a way inside the module.
  90. Overrides
  91. ---------
  92. Plugins can override core code or code from other plugins. Refer to the full
  93. example at the bottom for code details.
  94. Use the ``overrides`` functionality with caution. It basically resorts to
  95. monkey patching which pollutes the call stack and can make your code fragile
  96. and prone to bugs when Converse.js gets updated. Too much use of ``overrides``
  97. is therefore a "code smell" which should ideally be avoided.
  98. A better approach is to listen to the events emitted by Converse.js, and to add
  99. your code in event handlers. This is however not always possible, in which case
  100. the overrides are a powerful tool.
  101. Optional plugin dependencies
  102. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  103. When using ``overrides``, the code that you want to override (which is either
  104. in ``converse-core`` or in other plugins), needs to be loaded already by the
  105. type the ``overrides`` object is being parsed.
  106. So it's important to include overridden plugins in the AMD ``define`` statement
  107. at the top of the plugin module.
  108. However, sometimes you want to override parts of another plugin if it exists, but you
  109. don't want anything to break if it doesn't exist (for example when using a
  110. custom build which excludes that plugin). An example is the
  111. `converse-dragresize <https://github.com/jcbrand/converse.js/blob/master/src/converse-dragresize.js>`_
  112. plugin, which will add drag-resize handles to the headlines box (which shows
  113. messages of type ``headline``) but doesn't care if that particular plugin isn't
  114. actually loaded.
  115. In this case, you can't specify the plugin as a dependency in the ``define``
  116. statement at the top of the plugin, since it might not always be available,
  117. which would cause ``require.js`` to throw an error.
  118. To resolve this problem we thave the ``optional_dependencies`` Array attribute.
  119. With this you can specify those dependencies which need to be loaded before
  120. your plugin, if they exist. If they don't exist, they won't be ignored.
  121. If the setting :ref:`strict_plugin_dependencies` is set to true,
  122. an error will be raised if the plugin is not found, thereby making them
  123. non-optional.
  124. A full example plugin
  125. ---------------------
  126. .. code-block:: javascript
  127. (function (root, factory) {
  128. if (typeof define === 'function' && define.amd) {
  129. // AMD. Register as a module called "myplugin"
  130. define("myplugin", ["converse"], factory);
  131. } else {
  132. // Browser globals. If you're not using a module loader such as require.js,
  133. // then this line below executes. Make sure that your plugin's <script> tag
  134. // appears after the one from converse.js.
  135. factory(converse);
  136. }
  137. }(this, function (converse) {
  138. // Commonly used utilities and variables can be found under the "env"
  139. // namespace of the "converse" global.
  140. var Strophe = converse.env.Strophe,
  141. $iq = converse.env.$iq,
  142. $msg = converse.env.$msg,
  143. $pres = converse.env.$pres,
  144. $build = converse.env.$build,
  145. b64_sha1 = converse.env.b64_sha1;
  146. $ = converse.env.jQuery,
  147. _ = converse.env._,
  148. moment = converse.env.moment;
  149. // The following line registers your plugin.
  150. converse.plugins.add('myplugin', {
  151. initialize: function () {
  152. // Converse.js's plugin mechanism will call the initialize
  153. // method on any plugin (if it exists) as soon as the plugin has
  154. // been loaded.
  155. // Inside this method, you have access to the closured
  156. // _converse object, from which you can get any configuration
  157. // options that the user might have passed in via
  158. // converse.initialize. These values are stored in the
  159. // "user_settings" attribute.
  160. // Let's assume the user might pass in a custom setting, like so:
  161. //
  162. // converse.initialize({
  163. // "initialize_message": "My plugin has been initialized"
  164. // });
  165. //
  166. // Then we can alert that message, like so:
  167. alert(this._converse.user_settings.initialize_message);
  168. },
  169. // Optional dependencies are other plugins which might be
  170. // overridden or relied upon, and therefore need to be loaded before
  171. // this plugin. They are called "optional" because they might not be
  172. // available, in which case any overrides applicable to them will be
  173. // ignored.
  174. // It's possible however to make optional dependencies non-optional.
  175. // If the setting "strict_plugin_dependencies" is set to true,
  176. // an error will be raised if the plugin is not found.
  177. //
  178. // NB: These plugins need to have already been loaded via require.js.
  179. optional_dependencies: [],
  180. overrides: {
  181. // If you want to override some function or a Backbone model or
  182. // view defined elsewhere in converse.js, then you do that under
  183. // this "overrides" namespace.
  184. // For example, the inner protected *_converse* object has a
  185. // method "onConnected". You can override that method as follows:
  186. onConnected: function () {
  187. // Overrides the onConnected method in converse.js
  188. // Top-level functions in "overrides" are bound to the
  189. // inner "_converse" object.
  190. var _converse = this;
  191. // Your custom code comes here.
  192. // ...
  193. // You can access the original function being overridden
  194. // via the __super__ attribute.
  195. // Make sure to pass on the arguments supplied to this
  196. // function and also to apply the proper "this" object.
  197. _converse.__super__.onConnected.apply(this, arguments);
  198. },
  199. XMPPStatus: {
  200. // Override converse.js's XMPPStatus Backbone model so that we can override the
  201. // function that sends out the presence stanza.
  202. sendPresence: function (type, status_message, jid) {
  203. // The "_converse" object is available via the __super__
  204. // attribute.
  205. var _converse = this.__super__._converse;
  206. // Custom code can come here
  207. // ...
  208. // You can call the original overridden method, by
  209. // accessing it via the __super__ attribute.
  210. // When calling it, you need to apply the proper
  211. // context as reference by the "this" variable.
  212. this.__super__.sendPresence.apply(this, arguments);
  213. }
  214. }
  215. }
  216. });
  217. }));