plugin_development.rst 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 converse.js 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 grok 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. // Your plugin code goes in here
  23. });
  24. Security and access to the inner workings
  25. -----------------------------------------
  26. The globally available ``converse`` object, which exposes the API methods, such
  27. as ``initialize`` and ``plugins.add``, is a wrapper that encloses and protects
  28. a sensitive inner object, named ``_converse`` (not the underscore prefix).
  29. This inner ``_converse`` object contains all the Backbone models and views,
  30. as well as various other attributes and functions.
  31. Within a plugin, you will have access to this internal
  32. `"closured" <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures>`_
  33. ``_converse`` object, which is normally not exposed in the global variable scope.
  34. We inner ``_converse`` object is made private in order to safely hide and
  35. encapsulate sensitive information and methods which should not be exposed
  36. to any 3rd-party scripts that might be running in the same page.
  37. An example plugin
  38. -----------------
  39. .. code-block:: javascript
  40. (function (root, factory) {
  41. if (typeof define === 'function' && define.amd) {
  42. // AMD. Register as a module called "myplugin"
  43. define("myplugin", ["converse"], factory);
  44. } else {
  45. // Browser globals. If you're not using a module loader such as require.js,
  46. // then this line below executes. Make sure that your plugin's <script> tag
  47. // appears after the one from converse.js.
  48. factory(converse);
  49. }
  50. }(this, function (converse) {
  51. // Commonly used utilities and variables can be found under the "env"
  52. // namespace of the "converse" global.
  53. var Strophe = converse.env.Strophe,
  54. $iq = converse.env.$iq,
  55. $msg = converse.env.$msg,
  56. $pres = converse.env.$pres,
  57. $build = converse.env.$build,
  58. b64_sha1 = converse.env.b64_sha1;
  59. $ = converse.env.jQuery,
  60. _ = converse.env._,
  61. moment = converse.env.moment;
  62. // The following line registers your plugin.
  63. converse.plugins.add('myplugin', {
  64. initialize: function () {
  65. // Converse.js's plugin mechanism will call the initialize
  66. // method on any plugin (if it exists) as soon as the plugin has
  67. // been loaded.
  68. // Inside this method, you have access to the closured
  69. // _converse object, from which you can get any configuration
  70. // options that the user might have passed in via
  71. // converse.initialize. These values are stored in the
  72. // "user_settings" attribute.
  73. // Let's assume the user might pass in a custom setting, like so:
  74. //
  75. // converse.initialize({
  76. // "initialize_message": "My plugin has been initialized"
  77. // });
  78. //
  79. // Then we can alert that message, like so:
  80. alert(this._converse.user_settings.initialize_message);
  81. },
  82. overrides: {
  83. // If you want to override some function or a Backbone model or
  84. // view defined elsewhere in converse.js, then you do that under
  85. // this "overrides" namespace.
  86. // For example, the inner protected *_converse* object has a
  87. // method "onConnected". You can override that method as follows:
  88. onConnected: function () {
  89. // Overrides the onConnected method in converse.js
  90. // Top-level functions in "overrides" are bound to the
  91. // inner "_converse" object.
  92. var _converse = this;
  93. // Your custom code comes here.
  94. // ...
  95. // You can access the original function being overridden
  96. // via the __super__ attribute.
  97. // Make sure to pass on the arguments supplied to this
  98. // function and also to apply the proper "this" object.
  99. _converse.__super__.onConnected.apply(this, arguments);
  100. },
  101. XMPPStatus: {
  102. // Override converse.js's XMPPStatus Backbone model so that we can override the
  103. // function that sends out the presence stanza.
  104. sendPresence: function (type, status_message, jid) {
  105. // The "_converse" object is available via the __super__
  106. // attribute.
  107. var _converse = this.__super__.converse;
  108. // Custom code can come here
  109. // ...
  110. // You can call the original overridden method, by
  111. // accessing it via the __super__ attribute.
  112. // When calling it, you need to apply the proper
  113. // context as reference by the "this" variable.
  114. this.__super__.sendPresence.apply(this, arguments);
  115. }
  116. }
  117. }
  118. });
  119. }));