2019-07-11 10:48:52 +02:00
|
|
|
/**
|
|
|
|
* @module converse-rsm
|
2020-01-26 16:21:20 +01:00
|
|
|
* @copyright The Converse.js contributors
|
2019-09-19 16:54:55 +02:00
|
|
|
* @license Mozilla Public License (MPLv2)
|
|
|
|
* @description XEP-0059 Result Set Management
|
|
|
|
* Some code taken from the Strophe RSM plugin, licensed under the MIT License
|
|
|
|
* Copyright 2006-2017 Strophe (https://github.com/strophe/strophejs)
|
2019-07-11 10:48:52 +02:00
|
|
|
*/
|
2020-04-23 13:22:31 +02:00
|
|
|
import { converse } from "./converse-core";
|
2019-05-29 17:40:16 +02:00
|
|
|
|
|
|
|
const { Strophe, $build } = converse.env;
|
|
|
|
|
|
|
|
Strophe.addNamespace('RSM', 'http://jabber.org/protocol/rsm');
|
|
|
|
|
|
|
|
|
|
|
|
converse.plugins.add('converse-rsm', {
|
|
|
|
initialize () {
|
|
|
|
const { _converse } = this;
|
|
|
|
const RSM_ATTRIBUTES = ['max', 'first', 'last', 'after', 'before', 'index', 'count'];
|
|
|
|
_converse.RSM_ATTRIBUTES = RSM_ATTRIBUTES;
|
|
|
|
|
|
|
|
|
|
|
|
class RSM {
|
|
|
|
constructor (options) {
|
|
|
|
if (typeof options.xml != 'undefined') {
|
|
|
|
this.fromXMLElement(options.xml);
|
|
|
|
} else {
|
|
|
|
for (let ii = 0; ii < RSM_ATTRIBUTES.length; ii++) {
|
|
|
|
const attrib = RSM_ATTRIBUTES[ii];
|
|
|
|
this[attrib] = options[attrib];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toXML () {
|
|
|
|
let xml = $build('set', {xmlns: Strophe.NS.RSM});
|
|
|
|
for (let ii = 0; ii < RSM_ATTRIBUTES.length; ii++) {
|
|
|
|
const attrib = RSM_ATTRIBUTES[ii];
|
|
|
|
if (typeof this[attrib] != 'undefined') {
|
|
|
|
xml = xml.c(attrib).t(this[attrib].toString()).up();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return xml.tree();
|
|
|
|
}
|
|
|
|
|
2019-08-06 22:54:51 +02:00
|
|
|
next (max, before) {
|
|
|
|
return new RSM({max: max, after: this.last, before});
|
2019-05-29 17:40:16 +02:00
|
|
|
}
|
|
|
|
|
2019-08-06 22:54:51 +02:00
|
|
|
previous (max, after) {
|
|
|
|
return new RSM({max: max, before: this.first, after});
|
2019-05-29 17:40:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fromXMLElement (xmlElement) {
|
|
|
|
for (var ii = 0; ii < RSM_ATTRIBUTES.length; ii++) {
|
|
|
|
const attrib = RSM_ATTRIBUTES[ii];
|
|
|
|
const elem = xmlElement.getElementsByTagName(attrib)[0];
|
|
|
|
if (typeof elem != 'undefined' && elem !== null) {
|
|
|
|
this[attrib] = Strophe.getText(elem);
|
|
|
|
if (attrib == 'first') {
|
|
|
|
this.index = elem.getAttribute('index');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_converse.RSM = RSM;
|
|
|
|
}
|
|
|
|
});
|