2021-03-24 17:17:43 -03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
helper.multipleUsers = {
|
2021-03-31 02:38:05 -04:00
|
|
|
_user0: null,
|
|
|
|
_user1: null,
|
2021-03-24 17:17:43 -03:00
|
|
|
|
|
|
|
// open the same pad on different frames (allows concurrent editions to pad)
|
|
|
|
async init() {
|
2021-03-31 02:38:05 -04:00
|
|
|
this._user0 = {
|
2021-03-24 17:17:43 -03:00
|
|
|
$frame: $('#iframe-container iframe'),
|
2021-03-31 02:38:05 -04:00
|
|
|
token: getToken(),
|
2021-03-24 17:17:43 -03:00
|
|
|
// we'll switch between pads, need to store current values of helper.pad*
|
|
|
|
// to be able to restore those values later
|
|
|
|
padChrome$: helper.padChrome$,
|
|
|
|
padOuter$: helper.padOuter$,
|
|
|
|
padInner$: helper.padInner$,
|
|
|
|
};
|
2021-03-31 02:38:05 -04:00
|
|
|
this._user1 = {};
|
|
|
|
// Force generation of a new token.
|
|
|
|
clearToken();
|
2021-03-24 17:17:43 -03:00
|
|
|
// need to perform as the other user, otherwise we'll get the userdup error message
|
2021-03-31 02:38:05 -04:00
|
|
|
await this.performAsOtherUser(this._createUser1Frame.bind(this));
|
2021-03-24 17:17:43 -03:00
|
|
|
},
|
|
|
|
|
|
|
|
async performAsOtherUser(action) {
|
2021-03-31 02:38:05 -04:00
|
|
|
startActingLike(this._user1);
|
2021-03-24 17:17:43 -03:00
|
|
|
await action();
|
2021-03-31 02:38:05 -04:00
|
|
|
startActingLike(this._user0);
|
2021-03-24 17:17:43 -03:00
|
|
|
},
|
|
|
|
|
|
|
|
close() {
|
2021-03-31 02:38:05 -04:00
|
|
|
this._user0.$frame.attr('style', ''); // make the default ocopy the full height
|
|
|
|
this._user1.$frame.remove();
|
2021-03-24 17:17:43 -03:00
|
|
|
},
|
|
|
|
|
2021-03-31 02:38:05 -04:00
|
|
|
async _loadJQueryForUser1Frame() {
|
2021-07-22 13:33:48 -04:00
|
|
|
this._user1.padChrome$ = await helper.getFrameJQuery(this._user1.$frame, true);
|
2021-03-31 02:38:05 -04:00
|
|
|
this._user1.padOuter$ =
|
2021-07-22 13:33:48 -04:00
|
|
|
await helper.getFrameJQuery(this._user1.padChrome$('iframe[name="ace_outer"]'), false);
|
2021-03-31 02:38:05 -04:00
|
|
|
this._user1.padInner$ =
|
2021-07-22 13:33:48 -04:00
|
|
|
await helper.getFrameJQuery(this._user1.padOuter$('iframe[name="ace_inner"]'), true);
|
2021-03-24 17:17:43 -03:00
|
|
|
|
|
|
|
// update helper vars now that they are available
|
2021-03-31 02:38:05 -04:00
|
|
|
helper.padChrome$ = this._user1.padChrome$;
|
|
|
|
helper.padOuter$ = this._user1.padOuter$;
|
|
|
|
helper.padInner$ = this._user1.padInner$;
|
2021-03-24 17:17:43 -03:00
|
|
|
},
|
|
|
|
|
2021-03-31 02:38:05 -04:00
|
|
|
async _createUser1Frame() {
|
2021-03-24 17:17:43 -03:00
|
|
|
// create the iframe
|
2021-03-31 02:38:05 -04:00
|
|
|
const padUrl = this._user0.$frame.attr('src');
|
|
|
|
this._user1.$frame = $('<iframe>').attr('id', 'user1_pad').attr('src', padUrl);
|
2021-03-24 17:17:43 -03:00
|
|
|
|
|
|
|
// place one iframe (visually) below the other
|
2021-03-31 02:38:05 -04:00
|
|
|
this._user0.$frame.attr('style', 'height: 50%');
|
|
|
|
this._user1.$frame.attr('style', 'height: 50%; top: 50%');
|
|
|
|
this._user1.$frame.insertAfter(this._user0.$frame);
|
2021-03-24 17:17:43 -03:00
|
|
|
|
2021-03-31 02:38:05 -04:00
|
|
|
// wait for user1 pad to load
|
|
|
|
await new Promise((resolve) => this._user1.$frame.one('load', resolve));
|
2021-03-24 17:17:43 -03:00
|
|
|
|
2021-03-31 02:38:05 -04:00
|
|
|
const $editorLoadingMessage = this._user1.$frame.contents().find('#editorloadingbox');
|
|
|
|
const $errorMessageModal = this._user0.$frame.contents().find('#connectivity .userdup');
|
2021-03-24 17:17:43 -03:00
|
|
|
|
|
|
|
await helper.waitForPromise(() => {
|
2021-03-31 02:38:05 -04:00
|
|
|
const loaded = !$editorLoadingMessage.is(':visible');
|
2021-03-24 17:17:43 -03:00
|
|
|
// make sure we don't get the userdup by mistake
|
|
|
|
const didNotDetectUserDup = !$errorMessageModal.is(':visible');
|
2021-03-31 02:38:05 -04:00
|
|
|
return loaded && didNotDetectUserDup;
|
2021-03-24 17:17:43 -03:00
|
|
|
}, 50000);
|
|
|
|
|
2021-03-31 02:38:05 -04:00
|
|
|
// need to get values for this._user1.pad* vars
|
|
|
|
await this._loadJQueryForUser1Frame();
|
|
|
|
|
|
|
|
this._user1.token = getToken();
|
|
|
|
if (this._user0.token === this._user1.token) {
|
|
|
|
throw new Error('expected different token for user1');
|
|
|
|
}
|
2021-03-24 17:17:43 -03:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-03-31 02:38:05 -04:00
|
|
|
const getCookies =
|
|
|
|
() => helper.padChrome$.window.require('ep_etherpad-lite/static/js/pad_utils').Cookies;
|
2021-03-24 17:17:43 -03:00
|
|
|
|
2021-03-31 02:38:05 -04:00
|
|
|
const setToken = (token) => getCookies().set('token', token);
|
2021-03-24 17:17:43 -03:00
|
|
|
|
2021-03-31 02:38:05 -04:00
|
|
|
const getToken = () => getCookies().get('token');
|
2021-03-24 17:17:43 -03:00
|
|
|
|
2021-03-31 02:38:05 -04:00
|
|
|
const startActingLike = (user) => {
|
2021-03-24 17:17:43 -03:00
|
|
|
// update helper references, so other methods will act as if the main frame
|
|
|
|
// was the one we're using from now on
|
|
|
|
helper.padChrome$ = user.padChrome$;
|
|
|
|
helper.padOuter$ = user.padOuter$;
|
|
|
|
helper.padInner$ = user.padInner$;
|
|
|
|
|
2021-03-31 02:38:05 -04:00
|
|
|
if (helper.padChrome$) setToken(user.token);
|
2021-03-24 17:17:43 -03:00
|
|
|
};
|
|
|
|
|
2021-03-31 02:38:05 -04:00
|
|
|
const clearToken = () => getCookies().remove('token');
|