development.rst 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. .. raw:: html
  2. <div id="banner"><a href="https://github.com/jcbrand/converse.js/blob/master/docs/source/development.rst">Edit me on GitHub</a></div>
  3. .. _development:
  4. ===========
  5. Development
  6. ===========
  7. .. contents:: Table of Contents
  8. :depth: 2
  9. :local:
  10. If you want to work with the non-minified Javascript and CSS files you'll soon
  11. notice that there are references to a missing *components* folder. Please
  12. follow the instructions below to create this folder and fetch Converse's
  13. 3rd-party dependencies.
  14. .. note::
  15. Windows environment: We recommend installing the required tools using `Chocolatey <https://chocolatey.org/>`_
  16. You will need Node.js (nodejs.install), Git (git.install) and optionally to build using Makefile, GNU Make (make)
  17. If you have trouble setting up a development environment on Windows,
  18. please read `this post <http://librelist.com/browser//conversejs/2014/11/5/openfire-converse-and-visual-studio-questions/#b28387e7f8f126693b11598a8acbe810>`_
  19. in the mailing list.:
  20. Install the development and front-end dependencies
  21. ==================================================
  22. We use development tools (`Grunt <http://gruntjs.com>`_ and `Bower <http://bower.io>`_)
  23. which depend on Node.js and npm (the Node package manager).
  24. If you don't have Node.js installed, you can download and install the latest
  25. version `here <https://nodejs.org/download>`_.
  26. Also make sure you have ``Git`` installed. `Details <http://git-scm.com/book/en/Getting-Started-Installing-Git>`_.
  27. .. note::
  28. Windows users should use Chocolatey as recommended above.:
  29. .. note::
  30. Debian & Ubuntu users : apt-get install git npm nodejs-legacy
  31. Once you have *Node.js* and *git* installed, run the following command inside the Converse.js
  32. directory:
  33. ::
  34. make dev
  35. On Windows you need to specify Makefile.win to be used by running: ::
  36. make -f Makefile.win dev
  37. Or alternatively, if you don't have GNU Make:
  38. ::
  39. npm install
  40. bower update
  41. This will first install the Node.js development tools (like Grunt and Bower)
  42. and then use Bower to install all of Converse.js's front-end dependencies.
  43. The front-end dependencies are those javascript files on which
  44. Converse.js directly depends and which will be loaded in the browser.
  45. If you are curious to know what the different dependencies are:
  46. * Development dependencies:
  47. Take a look at whats under the *devDependencies* key in
  48. `package.json <https://github.com/jcbrand/converse.js/blob/master/package.json>`_.
  49. * Front-end dependencies:
  50. See *dependencies* in
  51. `bower.json <https://github.com/jcbrand/converse.js/blob/master/bower.json>`_.
  52. .. note::
  53. After running ```make dev```, you should now have a new directory *components*,
  54. which contains all the front-end dependencies of Converse.js.
  55. If this directory does NOT exist, something must have gone wrong.
  56. Double-check the output of ```make dev``` to see if there are any errors
  57. listed. For support, you can write to the mailing list: conversejs@librelist.com
  58. With AMD and require.js (recommended)
  59. =====================================
  60. Converse.js uses `require.js <http://requirejs.org>`_ to asynchronously load dependencies.
  61. If you want to develop or customize converse.js, you'll want to load the
  62. non-minified javascript files.
  63. Add the following two lines to the *<head>* section of your webpage:
  64. .. code-block:: html
  65. <link rel="stylesheet" type="text/css" media="screen" href="converse.css">
  66. <script data-main="main" src="components/requirejs/require.js"></script>
  67. require.js will then let the main.js file be parsed (because of the *data-main*
  68. attribute on the *script* tag), which will in turn cause converse.js to be
  69. parsed.
  70. Without AMD and require.js
  71. ==========================
  72. Converse.js can also be used without require.js. If you for some reason prefer
  73. to use it this way, please refer to
  74. `non_amd.html <https://github.com/jcbrand/converse.js/blob/master/non_amd.html>`_
  75. for an example of how and in what order all the Javascript files that converse.js
  76. depends on need to be loaded.
  77. Before submitting a pull request
  78. ================================
  79. Please follow the usual github workflow. Create your own local fork of this repository,
  80. make your changes and then submit a pull request.
  81. Before submitting a pull request
  82. --------------------------------
  83. Please read the `style guide </docs/html/style_guide.html>`_ and make sure that your code follows it.
  84. Add tests for your bugfix or feature
  85. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  86. Add a test for any bug fixed or feature added. We use Jasmine
  87. for testing.
  88. Take a look at `tests.html <https://github.com/jcbrand/converse.js/blob/master/tests.html>`_
  89. and the `spec files <https://github.com/jcbrand/converse.js/blob/master/tests.html>`_
  90. to see how tests are implemented.
  91. Check that the tests pass
  92. ~~~~~~~~~~~~~~~~~~~~~~~~~
  93. Check that all tests complete sucessfully.
  94. Run ``make check`` in your terminal or open `tests.html <https://github.com/jcbrand/converse.js/blob/master/tests.html>`_
  95. in your browser.
  96. Developer API
  97. =============
  98. .. note:: The API documented here is available in Converse.js 0.8.4 and higher.
  99. Earlier versions of Converse.js might have different API methods or none at all.
  100. In the Converse.js API, you traverse towards a logical grouping, from
  101. which you can then call certain standardised accessors and mutators, such as::
  102. .get
  103. .set
  104. .add
  105. .remove
  106. This is done to increase readability and to allow intuitive method chaining.
  107. For example, to get a contact, you would do the following::
  108. converse.contacts.get('jid@example.com');
  109. To get multiple contacts, just pass in an array of jids::
  110. converse.contacts.get(['jid1@example.com', 'jid2@example.com']);
  111. To get all contacts, simply call ``get`` without any jids::
  112. converse.contacts.get();
  113. **Here follows now a breakdown of all API groupings and methods**:
  114. initialize
  115. ----------
  116. .. note:: This method is the one exception of a method which is not logically grouped
  117. as explained above.
  118. Initializes converse.js. This method must always be called when using
  119. converse.js.
  120. The `initialize` method takes a map (also called a hash or dictionary) of
  121. :ref:`configuration-variables`.
  122. Example:
  123. .. code-block:: javascript
  124. converse.initialize({
  125. allow_otr: true,
  126. auto_list_rooms: false,
  127. auto_subscribe: false,
  128. bosh_service_url: 'https://bind.example.com',
  129. hide_muc_server: false,
  130. i18n: locales['en'],
  131. keepalive: true,
  132. play_sounds: true,
  133. prebind: false,
  134. show_controlbox_by_default: true,
  135. debug: false,
  136. roster_groups: true
  137. });
  138. The "archive" grouping
  139. ----------------------
  140. Converse.js supports the *Message Archive Management*
  141. (`XEP-0313 <https://xmpp.org/extensions/xep-0313.html>`_) protocol,
  142. through which it is able to query an XMPP server for archived messages.
  143. See also the **message_archiving** option in the :ref:`configuration-variables` section, which you'll usually
  144. want to in conjunction with this API.
  145. query
  146. ~~~~~
  147. The ``query`` method is used to query for archived messages.
  148. It accepts the following optional parameters:
  149. * **options** an object containing the query parameters. Valid query parameters
  150. are ``with``, ``start``, ``end``, ``first``, ``last``, ``after``, ``before``, ``index`` and ``count``.
  151. * **callback** is the callback method that will be called when all the messages
  152. have been received.
  153. * **errback** is the callback method to be called when an error is returned by
  154. the XMPP server, for example when it doesn't support message archiving.
  155. Examples
  156. ^^^^^^^^
  157. **Requesting all archived messages**
  158. The simplest query that can be made is to simply not pass in any parameters.
  159. Such a query will return all archived messages for the current user.
  160. Generally, you'll however always want to pass in a callback method, to receive
  161. the returned messages.
  162. .. code-block:: javascript
  163. var errback = function (iq) {
  164. // The query was not successful, perhaps inform the user?
  165. // The IQ stanza returned by the XMPP server is passed in, so that you
  166. // may inspect it and determine what the problem was.
  167. }
  168. var callback = function (messages) {
  169. // Do something with the messages, like showing them in your webpage.
  170. }
  171. converse.archive.query(callback, errback))
  172. **Requesting all archived messages for a particular contact or room**
  173. To query for messages sent between the current user and another user or room,
  174. the query options need to contain the the JID (Jabber ID) of the user or
  175. room under the ``with`` key.
  176. .. code-block:: javascript
  177. // For a particular user
  178. converse.archive.query({'with': 'john@doe.net'}, callback, errback);)
  179. // For a particular room
  180. converse.archive.query({'with': 'discuss@conference.doglovers.net'}, callback, errback);)
  181. **Requesting all archived messages before or after a certain date**
  182. The ``start`` and ``end`` parameters are used to query for messages
  183. within a certain timeframe. The passed in date values may either be ISO8601
  184. formatted date strings, or Javascript Date objects.
  185. .. code-block:: javascript
  186. var options = {
  187. 'with': 'john@doe.net',
  188. 'start': '2010-06-07T00:00:00Z',
  189. 'end': '2010-07-07T13:23:54Z'
  190. };
  191. converse.archive.query(options, callback, errback);
  192. **Limiting the amount of messages returned**
  193. The amount of returned messages may be limited with the ``max`` parameter.
  194. By default, the messages are returned from oldest to newest.
  195. .. code-block:: javascript
  196. // Return maximum 10 archived messages
  197. converse.archive.query({'with': 'john@doe.net', 'max':10}, callback, errback);
  198. **Paging forwards through a set of archived messages**
  199. When limiting the amount of messages returned per query, you might want to
  200. repeatedly make a further query to fetch the next batch of messages.
  201. To simplify this usecase for you, the callback method receives not only an array
  202. with the returned archived messages, but also a special RSM (*Result Set
  203. Management*) object which contains the query parameters you passed in, as well
  204. as two utility methods ``next``, and ``previous``.
  205. When you call one of these utility methods on the returned RSM object, and then
  206. pass the result into a new query, you'll receive the next or previous batch of
  207. archived messages. Please note, when calling these methods, pass in an integer
  208. to limit your results.
  209. .. code-block:: javascript
  210. var callback = function (messages, rsm) {
  211. // Do something with the messages, like showing them in your webpage.
  212. // ...
  213. // You can now use the returned "rsm" object, to fetch the next batch of messages:
  214. converse.archive.query(rsm.next(10), callback, errback))
  215. }
  216. converse.archive.query({'with': 'john@doe.net', 'max':10}, callback, errback);
  217. **Paging backwards through a set of archived messages**
  218. To page backwards through the archive, you need to know the UID of the message
  219. which you'd like to page backwards from and then pass that as value for the
  220. ``before`` parameter. If you simply want to page backwards from the most recent
  221. message, pass in the ``before`` parameter with an empty string value ``''``.
  222. .. code-block:: javascript
  223. converse.archive.query({'before': '', 'max':5}, function (message, rsm) {
  224. // Do something with the messages, like showing them in your webpage.
  225. // ...
  226. // You can now use the returned "rsm" object, to fetch the previous batch of messages:
  227. rsm.previous(5); // Call previous method, to update the object's parameters,
  228. // passing in a limit value of 5.
  229. // Now we query again, to get the previous batch.
  230. converse.archive.query(rsm, callback, errback);
  231. }
  232. The "user" grouping
  233. -------------------
  234. This grouping collects API functions related to the current logged in user.
  235. logout
  236. ~~~~~~
  237. Log the user out of the current XMPP session.
  238. .. code-block:: javascript
  239. converse.user.logout();
  240. The "status" sub-grouping
  241. ~~~~~~~~~~~~~~~~~~~~~~~~~
  242. Set and get the user's chat status, also called their *availability*.
  243. get
  244. ^^^
  245. Return the current user's availability status:
  246. .. code-block:: javascript
  247. converse.user.status.get(); // Returns for example "dnd"
  248. set
  249. ^^^
  250. The user's status can be set to one of the following values:
  251. * **away**
  252. * **dnd**
  253. * **offline**
  254. * **online**
  255. * **unavailable**
  256. * **xa**
  257. For example:
  258. .. code-block:: javascript
  259. converse.user.status.set('dnd');
  260. Because the user's availability is often set together with a custom status
  261. message, this method also allows you to pass in a status message as a
  262. second parameter:
  263. .. code-block:: javascript
  264. converse.user.status.set('dnd', 'In a meeting');
  265. The "message" sub-grouping
  266. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  267. The ``user.status.message`` sub-grouping exposes methods for setting and
  268. retrieving the user's custom status message.
  269. .. code-block:: javascript
  270. converse.user.status.message.set('In a meeting');
  271. converse.user.status.message.get(); // Returns "In a meeting"
  272. The "contacts" grouping
  273. -----------------------
  274. get
  275. ~~~
  276. This method is used to retrieve roster contacts.
  277. To get a single roster contact, call the method with the contact's JID (Jabber ID):
  278. .. code-block:: javascript
  279. converse.contacts.get('buddy@example.com')
  280. To get multiple contacts, pass in an array of JIDs:
  281. .. code-block:: javascript
  282. converse.contacts.get(['buddy1@example.com', 'buddy2@example.com'])
  283. To return all contacts, simply call ``get`` without any parameters:
  284. .. code-block:: javascript
  285. converse.contacts.get()
  286. The returned roster contact objects have these attributes:
  287. +----------------+-----------------------------------------------------------------------------------------------------------------+
  288. | Attribute | |
  289. +================+=================================================================================================================+
  290. | ask | If ask === 'subscribe', then we have asked this person to be our chat buddy. |
  291. +----------------+-----------------------------------------------------------------------------------------------------------------+
  292. | fullname | The person's full name. |
  293. +----------------+-----------------------------------------------------------------------------------------------------------------+
  294. | jid | The person's Jabber/XMPP username. |
  295. +----------------+-----------------------------------------------------------------------------------------------------------------+
  296. | requesting | If true, then this person is asking to be our chat buddy. |
  297. +----------------+-----------------------------------------------------------------------------------------------------------------+
  298. | subscription | The subscription state between the current user and this chat buddy. Can be `none`, `to`, `from` or `both`. |
  299. +----------------+-----------------------------------------------------------------------------------------------------------------+
  300. | id | A unique id, same as the jid. |
  301. +----------------+-----------------------------------------------------------------------------------------------------------------+
  302. | chat_status | The person's chat status. Can be `online`, `offline`, `busy`, `xa` (extended away) or `away`. |
  303. +----------------+-----------------------------------------------------------------------------------------------------------------+
  304. | user_id | The user id part of the JID (the part before the `@`). |
  305. +----------------+-----------------------------------------------------------------------------------------------------------------+
  306. | resources | The known resources for this chat buddy. Each resource denotes a separate and connected chat client. |
  307. +----------------+-----------------------------------------------------------------------------------------------------------------+
  308. | groups | The roster groups in which this chat buddy was placed. |
  309. +----------------+-----------------------------------------------------------------------------------------------------------------+
  310. | status | Their human readable custom status message. |
  311. +----------------+-----------------------------------------------------------------------------------------------------------------+
  312. | image_type | The image's file type. |
  313. +----------------+-----------------------------------------------------------------------------------------------------------------+
  314. | image | The Base64 encoded image data. |
  315. +----------------+-----------------------------------------------------------------------------------------------------------------+
  316. | url | The buddy's website URL, as specified in their VCard data. |
  317. +----------------+-----------------------------------------------------------------------------------------------------------------+
  318. | vcard_updated | When last the buddy's VCard was updated. |
  319. +----------------+-----------------------------------------------------------------------------------------------------------------+
  320. add
  321. ~~~
  322. Add a contact.
  323. Provide the JID of the contact you want to add:
  324. .. code-block:: javascript
  325. converse.contacts.add('buddy@example.com')
  326. You may also provide the fullname. If not present, we use the jid as fullname:
  327. .. code-block:: javascript
  328. converse.contacts.add('buddy@example.com', 'Buddy')
  329. The "chats" grouping
  330. --------------------
  331. get
  332. ~~~
  333. Returns an object representing a chat box.
  334. To return a single chat box, provide the JID of the contact you're chatting
  335. with in that chat box:
  336. .. code-block:: javascript
  337. converse.chats.get('buddy@example.com')
  338. To return an array of chat boxes, provide an array of JIDs:
  339. .. code-block:: javascript
  340. converse.chats.get(['buddy1@example.com', 'buddy2@example.com'])
  341. To return all open chat boxes, call the method without any JIDs::
  342. converse.chats.get()
  343. open
  344. ~~~~
  345. Opens a chat box and returns an object representing a chat box.
  346. To open a single chat box, provide the JID of the contact:
  347. .. code-block:: javascript
  348. converse.chats.open('buddy@example.com')
  349. To return an array of chat boxes, provide an array of JIDs:
  350. .. code-block:: javascript
  351. converse.chats.open(['buddy1@example.com', 'buddy2@example.com'])
  352. *The returned chat box object contains the following methods:*
  353. +-------------+------------------------------------------+
  354. | Method | Description |
  355. +=============+==========================================+
  356. | endOTR | End an OTR (Off-the-record) session. |
  357. +-------------+------------------------------------------+
  358. | get | Get an attribute (i.e. accessor). |
  359. +-------------+------------------------------------------+
  360. | initiateOTR | Start an OTR (off-the-record) session. |
  361. +-------------+------------------------------------------+
  362. | maximize | Minimize the chat box. |
  363. +-------------+------------------------------------------+
  364. | minimize | Maximize the chat box. |
  365. +-------------+------------------------------------------+
  366. | set | Set an attribute (i.e. mutator). |
  367. +-------------+------------------------------------------+
  368. | close | Close the chat box. |
  369. +-------------+------------------------------------------+
  370. | open | Opens the chat box. |
  371. +-------------+------------------------------------------+
  372. *The get and set methods can be used to retrieve and change the following attributes:*
  373. +-------------+-----------------------------------------------------+
  374. | Attribute | Description |
  375. +=============+=====================================================+
  376. | height | The height of the chat box. |
  377. +-------------+-----------------------------------------------------+
  378. | url | The URL of the chat box heading. |
  379. +-------------+-----------------------------------------------------+
  380. The "rooms" grouping
  381. --------------------
  382. get
  383. ~~~
  384. Returns an object representing a multi user chat box (room).
  385. Similar to chats.get API
  386. open
  387. ~~~~
  388. Opens a multi user chat box and returns an object representing it.
  389. Similar to chats.get API
  390. To open a single multi user chat box, provide the JID of the room:
  391. .. code-block:: javascript
  392. converse.rooms.open('group@muc.example.com')
  393. To return an array of rooms, provide an array of room JIDs:
  394. .. code-block:: javascript
  395. converse.rooms.open(['group1@muc.example.com', 'group2@muc.example.com'])
  396. To setup a custom nickname when joining the room, provide the optional nick argument:
  397. .. code-block:: javascript
  398. converse.rooms.open('group@muc.example.com', 'mycustomnick')
  399. The "settings" grouping
  400. -----------------------
  401. This grouping allows you to get or set the configuration settings of converse.js.
  402. get(key)
  403. ~~~~~~~~
  404. Returns the value of a configuration settings. For example:
  405. .. code-block:: javascript
  406. converse.settings.get("play_sounds"); // default value returned would be false;
  407. set(key, value) or set(object)
  408. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  409. Set one or many configuration settings. For example:
  410. .. code-block:: javascript
  411. converse.settings.set("play_sounds", true);
  412. or :
  413. .. code-block:: javascript
  414. converse.settings.set({
  415. "play_sounds", true,
  416. "hide_offline_users" true
  417. });
  418. Note, this is not an alternative to calling ``converse.initialize``, which still needs
  419. to be called. Generally, you'd use this method after converse.js is already
  420. running and you want to change the configuration on-the-fly.
  421. The "tokens" grouping
  422. ---------------------
  423. get
  424. ~~~
  425. Returns a token, either the RID or SID token depending on what's asked for.
  426. Example:
  427. .. code-block:: javascript
  428. converse.tokens.get('rid')
  429. The "listen" grouping
  430. ---------------------
  431. Converse.js emits events to which you can subscribe from your own Javascript.
  432. Concerning events, the following methods are available under the "listen"
  433. grouping:
  434. * **on(eventName, callback)**:
  435. Calling the ``on`` method allows you to subscribe to an event.
  436. Every time the event fires, the callback method specified by ``callback`` will be
  437. called.
  438. Parameters:
  439. * ``eventName`` is the event name as a string.
  440. * ``callback`` is the callback method to be called when the event is emitted.
  441. For example:
  442. .. code-block:: javascript
  443. converse.listen.on('message', function (event, messageXML) { ... });
  444. * **once(eventName, callback)**:
  445. Calling the ``once`` method allows you to listen to an event
  446. exactly once.
  447. Parameters:
  448. * ``eventName`` is the event name as a string.
  449. * ``callback`` is the callback method to be called when the event is emitted.
  450. For example:
  451. .. code-block:: javascript
  452. converse.listen.once('message', function (event, messageXML) { ... });
  453. * **not(eventName, callback)**
  454. To stop listening to an event, you can use the ``not`` method.
  455. Parameters:
  456. * ``eventName`` is the event name as a string.
  457. * ``callback`` refers to the function that is to be no longer executed.
  458. For example:
  459. .. code-block:: javascript
  460. converse.listen.not('message', function (event, messageXML) { ... });
  461. Events
  462. ======
  463. .. note:: see also the `"listen" grouping`_ API section above.
  464. Event Types
  465. -----------
  466. Here are the different events that are emitted:
  467. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  468. | Event Type | When is it triggered? | Example |
  469. +=================================+===================================================================================================+======================================================================================================+
  470. | **callButtonClicked** | When a call button (i.e. with class .toggle-call) on a chat box has been clicked. | ``converse.listen.on('callButtonClicked', function (event, connection, model) { ... });`` |
  471. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  472. | **chatBoxOpened** | When a chat box has been opened. | ``converse.listen.on('chatBoxOpened', function (event, chatbox) { ... });`` |
  473. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  474. | **chatRoomOpened** | When a chat room has been opened. | ``converse.listen.on('chatRoomOpened', function (event, chatbox) { ... });`` |
  475. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  476. | **chatBoxClosed** | When a chat box has been closed. | ``converse.listen.on('chatBoxClosed', function (event, chatbox) { ... });`` |
  477. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  478. | **chatBoxFocused** | When the focus has been moved to a chat box. | ``converse.listen.on('chatBoxFocused', function (event, chatbox) { ... });`` |
  479. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  480. | **chatBoxToggled** | When a chat box has been minimized or maximized. | ``converse.listen.on('chatBoxToggled', function (event, chatbox) { ... });`` |
  481. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  482. | **contactStatusChanged** | When a chat buddy's chat status has changed. | ``converse.listen.on('contactStatusChanged', function (event, buddy, status) { ... });`` |
  483. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  484. | **contactStatusMessageChanged** | When a chat buddy's custom status message has changed. | ``converse.listen.on('contactStatusMessageChanged', function (event, buddy, messageText) { ... });`` |
  485. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  486. | **message** | When a message is received. | ``converse.listen.on('message', function (event, messageXML) { ... });`` |
  487. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  488. | **messageSend** | When a message will be sent out. | ``storage_memoryconverse.listen.on('messageSend', function (event, messageText) { ... });`` |
  489. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  490. | **noResumeableSession** | When keepalive=true but there aren't any stored prebind tokens. | ``converse.listen.on('noResumeableSession', function (event) { ... });`` |
  491. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  492. | **initialized** | Once converse.js has been initialized. | ``converse.listen.on('initialized', function (event) { ... });`` |
  493. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  494. | **ready** | After connection has been established and converse.js has got all its ducks in a row. | ``converse.listen.on('ready', function (event) { ... });`` |
  495. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  496. | **reconnect** | After the connection has dropped. Converse.js will attempt to reconnect when not in prebind mode. | ``converse.listen.on('reconnect', function (event) { ... });`` |
  497. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  498. | **roomInviteSent** | After the user has sent out a direct invitation, to a roster contact, asking them to join a room. | ``converse.listen.on('roomInvite', function (event, roomview, invitee_jid, reason) { ... });`` |
  499. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  500. | **roomInviteReceived** | After the user has sent out a direct invitation, to a roster contact, asking them to join a room. | ``converse.listen.on('roomInvite', function (event, roomview, invitee_jid, reason) { ... });`` |
  501. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  502. | **roster** | When the roster is updated. | ``converse.listen.on('roster', function (event, items) { ... });`` |
  503. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  504. | **statusChanged** | When own chat status has changed. | ``converse.listen.on('statusChanged', function (event, status) { ... });`` |
  505. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  506. | **statusMessageChanged** | When own custom status message has changed. | ``converse.listen.on('statusMessageChanged', function (event, message) { ... });`` |
  507. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  508. | **serviceDiscovered** | When converse.js has learned of a service provided by the XMPP server. See XEP-0030. | ``converse.listen.on('serviceDiscovered', function (event, service) { ... });`` |
  509. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  510. Writing a converse.js plugin
  511. ============================
  512. Converse.js exposes a plugin mechanism which allows developers to extend and
  513. override its functionality.
  514. You register a plugin as follows:
  515. .. code-block:: javascript
  516. converse.plugins.add('myplugin', {
  517. // Your plugin code goes in here
  518. });
  519. Security and access to the inner workings
  520. -----------------------------------------
  521. The globally available ``converse`` object, which exposes the API methods, such
  522. as ``initialize`` and ``plugins.add``, is a wrapper that encloses and protects
  523. a sensitive inner object.
  524. This inner object contains all the Backbone models and views, as well as
  525. various other attributes and functions.
  526. Within a plugin, you will have access to this internal
  527. `"closured" <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures>`_
  528. converse object, which is normally not exposed in the global variable scope. The
  529. hiding of this inner object is due to the fact that it contains sensitive information,
  530. such as the user's JID and password (if they logged in manually). You should
  531. therefore make sure NOT to expose this object globally.
  532. An example plugin
  533. -----------------
  534. .. code-block:: javascript
  535. (function (root, factory) {
  536. if (typeof define === 'function' && define.amd) {
  537. define("myplugin", ["jquery", "strophe", "utils", "converse"], factory);
  538. }
  539. }(this, function ($, strophe, utils, converse_api) {
  540. // Wrap your UI strings with the __ function for translation support.
  541. var __ = $.proxy(utils.__, this);
  542. // Strophe methods for building stanzas
  543. var Strophe = strophe.Strophe;
  544. $iq = strophe.$iq;
  545. $msg = strophe.$msg;
  546. $build = strophe.$build;
  547. // The following line registers your plugin.
  548. converse_api.plugins.add('myplugin', {
  549. myFunction: function () {
  550. // This is a function which does not override anything in
  551. // converse.js itself, but in which you still have access to
  552. // the protected "inner" converse object.
  553. var converse = this.converse;
  554. // Custom code comes here
  555. // ...
  556. },
  557. overrides: {
  558. // If you want to override some function or a Backbone model or
  559. // view defined inside converse, then you do that under this
  560. // "overrides" namespace.
  561. // For example, the inner protected *converse* object has a
  562. // method "onConnected". You can override that method as follows:
  563. onConnected: function () {
  564. // Override the onConnected method in converse.js
  565. // You have access to the protected converse object via the
  566. // _super attribute.
  567. var converse = this._super.converse;
  568. // Your custom code comes here.
  569. // ...
  570. // You can access the original function being overridden
  571. // vai the _super attribute.
  572. this._super.onConnected();
  573. },
  574. XMPPStatus: {
  575. // Override converse.js's XMPPStatus Backbone model so that we can override the
  576. // function that sends out the presence stanza.
  577. sendPresence: function (type, status_message, jid) {
  578. var converse = this._super.converse;
  579. // Custom code can come here
  580. // ...
  581. this._super.sendPresence(type, status_message, jid);
  582. }
  583. },
  584. }
  585. });
  586. }));