plugin_development.rst 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. Writing a converse.js plugin
  2. ============================
  3. Developers are able to extend and override the objects, functions and the
  4. Backbone models and views that make up converse.js by means of writing plugins.
  5. Converse.js uses `pluggable.js <https://github.com/jcbrand/pluggable.js/>`_ as
  6. its plugin architecture.
  7. To understand how this plugin architecture works, please read the
  8. `pluggable.js documentation <https://jcbrand.github.io/pluggable.js/>`_
  9. and to grok its inner workins, please refer to the `annotated source code
  10. <https://jcbrand.github.io/pluggable.js/docs/pluggable.html>`_.
  11. You register a converse.js plugin as follows:
  12. .. code-block:: javascript
  13. converse.plugins.add('myplugin', {
  14. // Your plugin code goes in here
  15. });
  16. Security and access to the inner workings
  17. -----------------------------------------
  18. The globally available ``converse`` object, which exposes the API methods, such
  19. as ``initialize`` and ``plugins.add``, is a wrapper that encloses and protects
  20. a sensitive inner object.
  21. This inner object contains all the Backbone models and views, as well as
  22. various other attributes and functions.
  23. Within a plugin, you will have access to this internal
  24. `"closured" <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures>`_
  25. converse object, which is normally not exposed in the global variable scope. The
  26. hiding of this inner object is due to the fact that it contains sensitive information,
  27. such as the user's JID and password (if they logged in manually). You should
  28. therefore make sure NOT to expose this object globally.
  29. An example plugin
  30. -----------------
  31. .. code-block:: javascript
  32. (function (root, factory) {
  33. if (typeof define === 'function' && define.amd) {
  34. // AMD. Register as a module called "myplugin"
  35. define("myplugin", ["converse"], factory);
  36. } else {
  37. // Browser globals. If you're not using a module loader such as require.js,
  38. // then this line below executes. Make sure that your plugin's <script> tag
  39. // appears after the one from converse.js.
  40. factory(converse);
  41. }
  42. }(this, function (converse_api) {
  43. // Commonly used utilities and variables can be found under the "env"
  44. // namespace of converse_api
  45. // Strophe methods for building stanzas
  46. var Strophe = converse_api.env.Strophe,
  47. $iq = converse_api.env.$iq,
  48. $msg = converse_api.env.$msg,
  49. $pres = converse_api.env.$pres,
  50. $build = converse_api.env.$build,
  51. b64_sha1 = converse_api.env.b64_sha1;
  52. // Other frequently used utilities
  53. var $ = converse_api.env.jQuery,
  54. _ = converse_api.env._,
  55. moment = converse_api.env.moment;
  56. // The following line registers your plugin.
  57. converse_api.plugins.add('myplugin', {
  58. initialize: function () {
  59. // Converse.js's plugin mechanism will call the initialize
  60. // method on any plugin (if it exists) as soon as the plugin has
  61. // been loaded.
  62. // Inside this method, you have access to the protected "inner"
  63. // converse object, from which you can get any configuration
  64. // options that the user might have passed in via
  65. // converse.initialize. These values are stored in the
  66. // "user_settings" attribute.
  67. // Let's assume the user might in a custom setting, like so:
  68. // converse.initialize({
  69. // "initialize_message": "My plugin has been initialized"
  70. // });
  71. //
  72. // Then we can alert that message, like so:
  73. alert(this.converse.user_settings.initialize_message);
  74. },
  75. myFunction: function () {
  76. // This is a function which does not override anything in
  77. // converse.js itself, but in which you still have access to
  78. // the protected "inner" converse object.
  79. var converse = this.converse;
  80. // Custom code comes here
  81. // ...
  82. },
  83. overrides: {
  84. // If you want to override some function or a Backbone model or
  85. // view defined inside converse, then you do that under this
  86. // "overrides" namespace.
  87. // For example, the inner protected *converse* object has a
  88. // method "onConnected". You can override that method as follows:
  89. onConnected: function () {
  90. // Overrides the onConnected method in converse.js
  91. // Top-level functions in "overrides" are bound to the
  92. // inner "converse" object.
  93. var converse = this;
  94. // Your custom code comes here.
  95. // ...
  96. // You can access the original function being overridden
  97. // via the __super__ attribute.
  98. // Make sure to pass on the arguments supplied to this
  99. // function and also to apply the proper "this" object.
  100. this.__super__.onConnected.apply(this, arguments);
  101. },
  102. XMPPStatus: {
  103. // Override converse.js's XMPPStatus Backbone model so that we can override the
  104. // function that sends out the presence stanza.
  105. sendPresence: function (type, status_message, jid) {
  106. // The "converse" object is available via the __super__
  107. // attribute.
  108. var converse = this.__super__.converse;
  109. // Custom code can come here
  110. // ...
  111. // You can call the original overridden method, by
  112. // accessing it via the __super__ attribute.
  113. // When calling it, you need to apply the proper
  114. // context as reference by the "this" variable.
  115. this.__super__.sendPresence.apply(this, arguments);
  116. }
  117. },
  118. }
  119. });
  120. }));