development.rst 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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. **Waiting until server support has been determined**
  173. The query method will only work if converse.js has been able to determine that
  174. the server supports MAM queries, otherwise the following error will be raised:
  175. - *This server does not support XEP-0313, Message Archive Management*
  176. The very first time converse.js loads in a browser tab, if you call the query
  177. API too quickly, the above error might appear because service discovery has not
  178. yet been completed.
  179. To work solve this problem, you can first listen for the ``serviceDiscovered`` event,
  180. through which you can be informed once support for MAM has been determined.
  181. For example:
  182. .. code-block:: javascript
  183. converse.listen.on('serviceDiscovered', function (event, feature) {
  184. if (feature.get('var') === converse.env.Strophe.NS.MAM) {
  185. converse.archive.query()
  186. }
  187. });
  188. **Requesting all archived messages for a particular contact or room**
  189. To query for messages sent between the current user and another user or room,
  190. the query options need to contain the the JID (Jabber ID) of the user or
  191. room under the ``with`` key.
  192. .. code-block:: javascript
  193. // For a particular user
  194. converse.archive.query({'with': 'john@doe.net'}, callback, errback);)
  195. // For a particular room
  196. converse.archive.query({'with': 'discuss@conference.doglovers.net'}, callback, errback);)
  197. **Requesting all archived messages before or after a certain date**
  198. The ``start`` and ``end`` parameters are used to query for messages
  199. within a certain timeframe. The passed in date values may either be ISO8601
  200. formatted date strings, or Javascript Date objects.
  201. .. code-block:: javascript
  202. var options = {
  203. 'with': 'john@doe.net',
  204. 'start': '2010-06-07T00:00:00Z',
  205. 'end': '2010-07-07T13:23:54Z'
  206. };
  207. converse.archive.query(options, callback, errback);
  208. **Limiting the amount of messages returned**
  209. The amount of returned messages may be limited with the ``max`` parameter.
  210. By default, the messages are returned from oldest to newest.
  211. .. code-block:: javascript
  212. // Return maximum 10 archived messages
  213. converse.archive.query({'with': 'john@doe.net', 'max':10}, callback, errback);
  214. **Paging forwards through a set of archived messages**
  215. When limiting the amount of messages returned per query, you might want to
  216. repeatedly make a further query to fetch the next batch of messages.
  217. To simplify this usecase for you, the callback method receives not only an array
  218. with the returned archived messages, but also a special RSM (*Result Set
  219. Management*) object which contains the query parameters you passed in, as well
  220. as two utility methods ``next``, and ``previous``.
  221. When you call one of these utility methods on the returned RSM object, and then
  222. pass the result into a new query, you'll receive the next or previous batch of
  223. archived messages. Please note, when calling these methods, pass in an integer
  224. to limit your results.
  225. .. code-block:: javascript
  226. var callback = function (messages, rsm) {
  227. // Do something with the messages, like showing them in your webpage.
  228. // ...
  229. // You can now use the returned "rsm" object, to fetch the next batch of messages:
  230. converse.archive.query(rsm.next(10), callback, errback))
  231. }
  232. converse.archive.query({'with': 'john@doe.net', 'max':10}, callback, errback);
  233. **Paging backwards through a set of archived messages**
  234. To page backwards through the archive, you need to know the UID of the message
  235. which you'd like to page backwards from and then pass that as value for the
  236. ``before`` parameter. If you simply want to page backwards from the most recent
  237. message, pass in the ``before`` parameter with an empty string value ``''``.
  238. .. code-block:: javascript
  239. converse.archive.query({'before': '', 'max':5}, function (message, rsm) {
  240. // Do something with the messages, like showing them in your webpage.
  241. // ...
  242. // You can now use the returned "rsm" object, to fetch the previous batch of messages:
  243. rsm.previous(5); // Call previous method, to update the object's parameters,
  244. // passing in a limit value of 5.
  245. // Now we query again, to get the previous batch.
  246. converse.archive.query(rsm, callback, errback);
  247. }
  248. The "user" grouping
  249. -------------------
  250. This grouping collects API functions related to the current logged in user.
  251. logout
  252. ~~~~~~
  253. Log the user out of the current XMPP session.
  254. .. code-block:: javascript
  255. converse.user.logout();
  256. The "status" sub-grouping
  257. ~~~~~~~~~~~~~~~~~~~~~~~~~
  258. Set and get the user's chat status, also called their *availability*.
  259. get
  260. ^^^
  261. Return the current user's availability status:
  262. .. code-block:: javascript
  263. converse.user.status.get(); // Returns for example "dnd"
  264. set
  265. ^^^
  266. The user's status can be set to one of the following values:
  267. * **away**
  268. * **dnd**
  269. * **offline**
  270. * **online**
  271. * **unavailable**
  272. * **xa**
  273. For example:
  274. .. code-block:: javascript
  275. converse.user.status.set('dnd');
  276. Because the user's availability is often set together with a custom status
  277. message, this method also allows you to pass in a status message as a
  278. second parameter:
  279. .. code-block:: javascript
  280. converse.user.status.set('dnd', 'In a meeting');
  281. The "message" sub-grouping
  282. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  283. The ``user.status.message`` sub-grouping exposes methods for setting and
  284. retrieving the user's custom status message.
  285. .. code-block:: javascript
  286. converse.user.status.message.set('In a meeting');
  287. converse.user.status.message.get(); // Returns "In a meeting"
  288. The "contacts" grouping
  289. -----------------------
  290. get
  291. ~~~
  292. This method is used to retrieve roster contacts.
  293. To get a single roster contact, call the method with the contact's JID (Jabber ID):
  294. .. code-block:: javascript
  295. converse.contacts.get('buddy@example.com')
  296. To get multiple contacts, pass in an array of JIDs:
  297. .. code-block:: javascript
  298. converse.contacts.get(['buddy1@example.com', 'buddy2@example.com'])
  299. To return all contacts, simply call ``get`` without any parameters:
  300. .. code-block:: javascript
  301. converse.contacts.get()
  302. The returned roster contact objects have these attributes:
  303. +----------------+-----------------------------------------------------------------------------------------------------------------+
  304. | Attribute | |
  305. +================+=================================================================================================================+
  306. | ask | If ask === 'subscribe', then we have asked this person to be our chat buddy. |
  307. +----------------+-----------------------------------------------------------------------------------------------------------------+
  308. | fullname | The person's full name. |
  309. +----------------+-----------------------------------------------------------------------------------------------------------------+
  310. | jid | The person's Jabber/XMPP username. |
  311. +----------------+-----------------------------------------------------------------------------------------------------------------+
  312. | requesting | If true, then this person is asking to be our chat buddy. |
  313. +----------------+-----------------------------------------------------------------------------------------------------------------+
  314. | subscription | The subscription state between the current user and this chat buddy. Can be `none`, `to`, `from` or `both`. |
  315. +----------------+-----------------------------------------------------------------------------------------------------------------+
  316. | id | A unique id, same as the jid. |
  317. +----------------+-----------------------------------------------------------------------------------------------------------------+
  318. | chat_status | The person's chat status. Can be `online`, `offline`, `busy`, `xa` (extended away) or `away`. |
  319. +----------------+-----------------------------------------------------------------------------------------------------------------+
  320. | user_id | The user id part of the JID (the part before the `@`). |
  321. +----------------+-----------------------------------------------------------------------------------------------------------------+
  322. | resources | The known resources for this chat buddy. Each resource denotes a separate and connected chat client. |
  323. +----------------+-----------------------------------------------------------------------------------------------------------------+
  324. | groups | The roster groups in which this chat buddy was placed. |
  325. +----------------+-----------------------------------------------------------------------------------------------------------------+
  326. | status | Their human readable custom status message. |
  327. +----------------+-----------------------------------------------------------------------------------------------------------------+
  328. | image_type | The image's file type. |
  329. +----------------+-----------------------------------------------------------------------------------------------------------------+
  330. | image | The Base64 encoded image data. |
  331. +----------------+-----------------------------------------------------------------------------------------------------------------+
  332. | url | The buddy's website URL, as specified in their VCard data. |
  333. +----------------+-----------------------------------------------------------------------------------------------------------------+
  334. | vcard_updated | When last the buddy's VCard was updated. |
  335. +----------------+-----------------------------------------------------------------------------------------------------------------+
  336. add
  337. ~~~
  338. Add a contact.
  339. Provide the JID of the contact you want to add:
  340. .. code-block:: javascript
  341. converse.contacts.add('buddy@example.com')
  342. You may also provide the fullname. If not present, we use the jid as fullname:
  343. .. code-block:: javascript
  344. converse.contacts.add('buddy@example.com', 'Buddy')
  345. The "chats" grouping
  346. --------------------
  347. get
  348. ~~~
  349. Returns an object representing a chat box.
  350. To return a single chat box, provide the JID of the contact you're chatting
  351. with in that chat box:
  352. .. code-block:: javascript
  353. converse.chats.get('buddy@example.com')
  354. To return an array of chat boxes, provide an array of JIDs:
  355. .. code-block:: javascript
  356. converse.chats.get(['buddy1@example.com', 'buddy2@example.com'])
  357. To return all open chat boxes, call the method without any JIDs::
  358. converse.chats.get()
  359. open
  360. ~~~~
  361. Opens a chat box and returns an object representing a chat box.
  362. To open a single chat box, provide the JID of the contact:
  363. .. code-block:: javascript
  364. converse.chats.open('buddy@example.com')
  365. To return an array of chat boxes, provide an array of JIDs:
  366. .. code-block:: javascript
  367. converse.chats.open(['buddy1@example.com', 'buddy2@example.com'])
  368. *The returned chat box object contains the following methods:*
  369. +-------------+------------------------------------------+
  370. | Method | Description |
  371. +=============+==========================================+
  372. | endOTR | End an OTR (Off-the-record) session. |
  373. +-------------+------------------------------------------+
  374. | get | Get an attribute (i.e. accessor). |
  375. +-------------+------------------------------------------+
  376. | initiateOTR | Start an OTR (off-the-record) session. |
  377. +-------------+------------------------------------------+
  378. | maximize | Minimize the chat box. |
  379. +-------------+------------------------------------------+
  380. | minimize | Maximize the chat box. |
  381. +-------------+------------------------------------------+
  382. | set | Set an attribute (i.e. mutator). |
  383. +-------------+------------------------------------------+
  384. | close | Close the chat box. |
  385. +-------------+------------------------------------------+
  386. | open | Opens the chat box. |
  387. +-------------+------------------------------------------+
  388. *The get and set methods can be used to retrieve and change the following attributes:*
  389. +-------------+-----------------------------------------------------+
  390. | Attribute | Description |
  391. +=============+=====================================================+
  392. | height | The height of the chat box. |
  393. +-------------+-----------------------------------------------------+
  394. | url | The URL of the chat box heading. |
  395. +-------------+-----------------------------------------------------+
  396. The "rooms" grouping
  397. --------------------
  398. get
  399. ~~~
  400. Returns an object representing a multi user chat box (room).
  401. Similar to chats.get API
  402. open
  403. ~~~~
  404. Opens a multi user chat box and returns an object representing it.
  405. Similar to chats.get API
  406. To open a single multi user chat box, provide the JID of the room:
  407. .. code-block:: javascript
  408. converse.rooms.open('group@muc.example.com')
  409. To return an array of rooms, provide an array of room JIDs:
  410. .. code-block:: javascript
  411. converse.rooms.open(['group1@muc.example.com', 'group2@muc.example.com'])
  412. To setup a custom nickname when joining the room, provide the optional nick argument:
  413. .. code-block:: javascript
  414. converse.rooms.open('group@muc.example.com', 'mycustomnick')
  415. The "settings" grouping
  416. -----------------------
  417. This grouping allows you to get or set the configuration settings of converse.js.
  418. get(key)
  419. ~~~~~~~~
  420. Returns the value of a configuration settings. For example:
  421. .. code-block:: javascript
  422. converse.settings.get("play_sounds"); // default value returned would be false;
  423. set(key, value) or set(object)
  424. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  425. Set one or many configuration settings. For example:
  426. .. code-block:: javascript
  427. converse.settings.set("play_sounds", true);
  428. or :
  429. .. code-block:: javascript
  430. converse.settings.set({
  431. "play_sounds", true,
  432. "hide_offline_users" true
  433. });
  434. Note, this is not an alternative to calling ``converse.initialize``, which still needs
  435. to be called. Generally, you'd use this method after converse.js is already
  436. running and you want to change the configuration on-the-fly.
  437. The "tokens" grouping
  438. ---------------------
  439. get
  440. ~~~
  441. Returns a token, either the RID or SID token depending on what's asked for.
  442. Example:
  443. .. code-block:: javascript
  444. converse.tokens.get('rid')
  445. The "listen" grouping
  446. ---------------------
  447. Converse.js emits events to which you can subscribe from your own Javascript.
  448. Concerning events, the following methods are available under the "listen"
  449. grouping:
  450. * **on(eventName, callback)**:
  451. Calling the ``on`` method allows you to subscribe to an event.
  452. Every time the event fires, the callback method specified by ``callback`` will be
  453. called.
  454. Parameters:
  455. * ``eventName`` is the event name as a string.
  456. * ``callback`` is the callback method to be called when the event is emitted.
  457. For example:
  458. .. code-block:: javascript
  459. converse.listen.on('message', function (event, messageXML) { ... });
  460. * **once(eventName, callback)**:
  461. Calling the ``once`` method allows you to listen to an event
  462. exactly once.
  463. Parameters:
  464. * ``eventName`` is the event name as a string.
  465. * ``callback`` is the callback method to be called when the event is emitted.
  466. For example:
  467. .. code-block:: javascript
  468. converse.listen.once('message', function (event, messageXML) { ... });
  469. * **not(eventName, callback)**
  470. To stop listening to an event, you can use the ``not`` method.
  471. Parameters:
  472. * ``eventName`` is the event name as a string.
  473. * ``callback`` refers to the function that is to be no longer executed.
  474. For example:
  475. .. code-block:: javascript
  476. converse.listen.not('message', function (event, messageXML) { ... });
  477. Events
  478. ======
  479. .. note:: see also the `"listen" grouping`_ API section above.
  480. Event Types
  481. -----------
  482. Here are the different events that are emitted:
  483. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  484. | Event Type | When is it triggered? | Example |
  485. +=================================+===================================================================================================+======================================================================================================+
  486. | **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) { ... });`` |
  487. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  488. | **chatBoxOpened** | When a chat box has been opened. | ``converse.listen.on('chatBoxOpened', function (event, chatbox) { ... });`` |
  489. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  490. | **chatRoomOpened** | When a chat room has been opened. | ``converse.listen.on('chatRoomOpened', function (event, chatbox) { ... });`` |
  491. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  492. | **chatBoxClosed** | When a chat box has been closed. | ``converse.listen.on('chatBoxClosed', function (event, chatbox) { ... });`` |
  493. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  494. | **chatBoxFocused** | When the focus has been moved to a chat box. | ``converse.listen.on('chatBoxFocused', function (event, chatbox) { ... });`` |
  495. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  496. | **chatBoxToggled** | When a chat box has been minimized or maximized. | ``converse.listen.on('chatBoxToggled', function (event, chatbox) { ... });`` |
  497. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  498. | **contactStatusChanged** | When a chat buddy's chat status has changed. | ``converse.listen.on('contactStatusChanged', function (event, buddy, status) { ... });`` |
  499. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  500. | **contactStatusMessageChanged** | When a chat buddy's custom status message has changed. | ``converse.listen.on('contactStatusMessageChanged', function (event, buddy, messageText) { ... });`` |
  501. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  502. | **message** | When a message is received. | ``converse.listen.on('message', function (event, messageXML) { ... });`` |
  503. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  504. | **messageSend** | When a message will be sent out. | ``storage_memoryconverse.listen.on('messageSend', function (event, messageText) { ... });`` |
  505. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  506. | **noResumeableSession** | When keepalive=true but there aren't any stored prebind tokens. | ``converse.listen.on('noResumeableSession', function (event) { ... });`` |
  507. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  508. | **initialized** | Once converse.js has been initialized. | ``converse.listen.on('initialized', function (event) { ... });`` |
  509. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  510. | **ready** | After connection has been established and converse.js has got all its ducks in a row. | ``converse.listen.on('ready', function (event) { ... });`` |
  511. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  512. | **reconnect** | After the connection has dropped. Converse.js will attempt to reconnect when not in prebind mode. | ``converse.listen.on('reconnect', function (event) { ... });`` |
  513. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  514. | **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) { ... });`` |
  515. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  516. | **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) { ... });`` |
  517. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  518. | **roster** | When the roster is updated. | ``converse.listen.on('roster', function (event, items) { ... });`` |
  519. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  520. | **statusChanged** | When own chat status has changed. | ``converse.listen.on('statusChanged', function (event, status) { ... });`` |
  521. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  522. | **statusMessageChanged** | When own custom status message has changed. | ``converse.listen.on('statusMessageChanged', function (event, message) { ... });`` |
  523. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  524. | **serviceDiscovered** | When converse.js has learned of a service provided by the XMPP server. See XEP-0030. | ``converse.listen.on('serviceDiscovered', function (event, service) { ... });`` |
  525. +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
  526. Writing a converse.js plugin
  527. ============================
  528. Converse.js exposes a plugin mechanism which allows developers to extend and
  529. override its functionality.
  530. You register a plugin as follows:
  531. .. code-block:: javascript
  532. converse.plugins.add('myplugin', {
  533. // Your plugin code goes in here
  534. });
  535. Security and access to the inner workings
  536. -----------------------------------------
  537. The globally available ``converse`` object, which exposes the API methods, such
  538. as ``initialize`` and ``plugins.add``, is a wrapper that encloses and protects
  539. a sensitive inner object.
  540. This inner object contains all the Backbone models and views, as well as
  541. various other attributes and functions.
  542. Within a plugin, you will have access to this internal
  543. `"closured" <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures>`_
  544. converse object, which is normally not exposed in the global variable scope. The
  545. hiding of this inner object is due to the fact that it contains sensitive information,
  546. such as the user's JID and password (if they logged in manually). You should
  547. therefore make sure NOT to expose this object globally.
  548. An example plugin
  549. -----------------
  550. .. code-block:: javascript
  551. (function (root, factory) {
  552. if (typeof define === 'function' && define.amd) {
  553. define("myplugin", ["jquery", "strophe", "utils", "converse"], factory);
  554. }
  555. }(this, function ($, strophe, utils, converse_api) {
  556. // Wrap your UI strings with the __ function for translation support.
  557. var __ = $.proxy(utils.__, this);
  558. // Strophe methods for building stanzas
  559. var Strophe = strophe.Strophe;
  560. $iq = strophe.$iq;
  561. $msg = strophe.$msg;
  562. $build = strophe.$build;
  563. // The following line registers your plugin.
  564. converse_api.plugins.add('myplugin', {
  565. myFunction: function () {
  566. // This is a function which does not override anything in
  567. // converse.js itself, but in which you still have access to
  568. // the protected "inner" converse object.
  569. var converse = this.converse;
  570. // Custom code comes here
  571. // ...
  572. },
  573. overrides: {
  574. // If you want to override some function or a Backbone model or
  575. // view defined inside converse, then you do that under this
  576. // "overrides" namespace.
  577. // For example, the inner protected *converse* object has a
  578. // method "onConnected". You can override that method as follows:
  579. onConnected: function () {
  580. // Override the onConnected method in converse.js
  581. // You have access to the protected converse object via the
  582. // _super attribute.
  583. var converse = this._super.converse;
  584. // Your custom code comes here.
  585. // ...
  586. // You can access the original function being overridden
  587. // vai the _super attribute.
  588. this._super.onConnected();
  589. },
  590. XMPPStatus: {
  591. // Override converse.js's XMPPStatus Backbone model so that we can override the
  592. // function that sends out the presence stanza.
  593. sendPresence: function (type, status_message, jid) {
  594. var converse = this._super.converse;
  595. // Custom code can come here
  596. // ...
  597. this._super.sendPresence(type, status_message, jid);
  598. }
  599. },
  600. }
  601. });
  602. }));