2012-01-30 23:19:51 +04:00
|
|
|
/**
|
|
|
|
* @copyright 2012 Robin Appelman icewind1991@gmail.com
|
|
|
|
*
|
2021-03-31 13:15:40 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
* @author John Molakvoæ <skjnldsv@protonmail.com>
|
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
* @author Vincent Petry <vincent@nextcloud.com>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
2012-01-30 23:19:51 +04:00
|
|
|
*
|
2021-03-31 13:15:40 +03:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
2012-01-30 23:19:51 +04:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2021-03-31 13:15:40 +03:00
|
|
|
* GNU Affero General Public License for more details.
|
2012-01-30 23:19:51 +04:00
|
|
|
*
|
2021-03-31 13:15:40 +03:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2012-01-30 23:19:51 +04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2021-03-31 13:15:40 +03:00
|
|
|
/* eslint-disable */
|
2019-01-29 11:23:38 +03:00
|
|
|
import $ from 'jquery'
|
|
|
|
|
2019-09-25 19:19:42 +03:00
|
|
|
import { getToken } from './requesttoken'
|
2019-05-10 15:18:08 +03:00
|
|
|
|
2012-01-31 02:26:01 +04:00
|
|
|
/**
|
2014-04-21 16:42:50 +04:00
|
|
|
* Create a new event source
|
|
|
|
* @param {string} src
|
|
|
|
* @param {object} [data] to be send as GET
|
2014-06-24 01:56:10 +04:00
|
|
|
*
|
2019-01-29 11:23:38 +03:00
|
|
|
* @constructs OCEventSource
|
2012-01-31 02:26:01 +04:00
|
|
|
*/
|
2019-09-25 19:19:42 +03:00
|
|
|
const OCEventSource = function(src, data) {
|
|
|
|
var dataStr = ''
|
|
|
|
var name
|
|
|
|
var joinChar
|
|
|
|
this.typelessListeners = []
|
|
|
|
this.closed = false
|
|
|
|
this.listeners = {}
|
2019-01-29 11:23:38 +03:00
|
|
|
if (data) {
|
|
|
|
for (name in data) {
|
2019-09-25 19:19:42 +03:00
|
|
|
dataStr += name + '=' + encodeURIComponent(data[name]) + '&'
|
2012-03-31 18:10:29 +04:00
|
|
|
}
|
2012-01-31 02:26:01 +04:00
|
|
|
}
|
2019-09-25 19:19:42 +03:00
|
|
|
dataStr += 'requesttoken=' + encodeURIComponent(getToken())
|
2019-01-29 11:23:38 +03:00
|
|
|
if (!this.useFallBack && typeof EventSource !== 'undefined') {
|
2019-09-25 19:19:42 +03:00
|
|
|
joinChar = '&'
|
2019-01-29 11:23:38 +03:00
|
|
|
if (src.indexOf('?') === -1) {
|
2019-09-25 19:19:42 +03:00
|
|
|
joinChar = '?'
|
2012-11-16 23:28:03 +04:00
|
|
|
}
|
2019-09-25 19:19:42 +03:00
|
|
|
this.source = new EventSource(src + joinChar + dataStr)
|
|
|
|
this.source.onmessage = function(e) {
|
2019-01-29 11:23:38 +03:00
|
|
|
for (var i = 0; i < this.typelessListeners.length; i++) {
|
2019-09-25 19:19:42 +03:00
|
|
|
this.typelessListeners[i](JSON.parse(e.data))
|
2012-01-30 23:19:51 +04:00
|
|
|
}
|
2019-09-25 19:19:42 +03:00
|
|
|
}.bind(this)
|
2019-01-29 11:23:38 +03:00
|
|
|
} else {
|
2019-09-25 19:19:42 +03:00
|
|
|
var iframeId = 'oc_eventsource_iframe_' + OCEventSource.iframeCount
|
|
|
|
OCEventSource.fallBackSources[OCEventSource.iframeCount] = this
|
|
|
|
this.iframe = $('<iframe/>')
|
|
|
|
this.iframe.attr('id', iframeId)
|
|
|
|
this.iframe.hide()
|
2012-11-16 23:28:03 +04:00
|
|
|
|
2019-09-25 19:19:42 +03:00
|
|
|
joinChar = '&'
|
2019-01-29 11:23:38 +03:00
|
|
|
if (src.indexOf('?') === -1) {
|
2019-09-25 19:19:42 +03:00
|
|
|
joinChar = '?'
|
2012-11-16 23:28:03 +04:00
|
|
|
}
|
2019-09-25 19:19:42 +03:00
|
|
|
this.iframe.attr('src', src + joinChar + 'fallback=true&fallback_id=' + OCEventSource.iframeCount + '&' + dataStr)
|
|
|
|
$('body').append(this.iframe)
|
|
|
|
this.useFallBack = true
|
|
|
|
OCEventSource.iframeCount++
|
2012-01-30 23:19:51 +04:00
|
|
|
}
|
2019-09-25 19:19:42 +03:00
|
|
|
// add close listener
|
|
|
|
this.listen('__internal__', function(data) {
|
2019-01-29 11:23:38 +03:00
|
|
|
if (data === 'close') {
|
2019-09-25 19:19:42 +03:00
|
|
|
this.close()
|
2012-01-31 02:19:43 +04:00
|
|
|
}
|
2019-09-25 19:19:42 +03:00
|
|
|
}.bind(this))
|
|
|
|
}
|
|
|
|
OCEventSource.fallBackSources = []
|
|
|
|
OCEventSource.iframeCount = 0// number of fallback iframes
|
|
|
|
OCEventSource.fallBackCallBack = function(id, type, data) {
|
|
|
|
OCEventSource.fallBackSources[id].fallBackCallBack(type, data)
|
|
|
|
}
|
2019-01-29 11:23:38 +03:00
|
|
|
OCEventSource.prototype = {
|
|
|
|
typelessListeners: [],
|
|
|
|
iframe: null,
|
2019-09-25 19:19:42 +03:00
|
|
|
listeners: {}, // only for fallback
|
2019-01-29 11:23:38 +03:00
|
|
|
useFallBack: false,
|
2014-06-24 01:56:10 +04:00
|
|
|
/**
|
|
|
|
* Fallback callback for browsers that don't have the
|
|
|
|
* native EventSource object.
|
|
|
|
*
|
|
|
|
* Calls the registered listeners.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {String} type event type
|
|
|
|
* @param {Object} data received data
|
|
|
|
*/
|
2019-09-25 19:19:42 +03:00
|
|
|
fallBackCallBack: function(type, data) {
|
|
|
|
var i
|
2014-07-02 23:31:11 +04:00
|
|
|
// ignore messages that might appear after closing
|
|
|
|
if (this.closed) {
|
2019-09-25 19:19:42 +03:00
|
|
|
return
|
2014-07-02 23:31:11 +04:00
|
|
|
}
|
2019-01-29 11:23:38 +03:00
|
|
|
if (type) {
|
2014-07-02 23:32:55 +04:00
|
|
|
if (typeof this.listeners.done !== 'undefined') {
|
2019-01-29 11:23:38 +03:00
|
|
|
for (i = 0; i < this.listeners[type].length; i++) {
|
2019-09-25 19:19:42 +03:00
|
|
|
this.listeners[type][i](data)
|
2013-02-14 15:17:14 +04:00
|
|
|
}
|
2012-01-30 23:19:51 +04:00
|
|
|
}
|
2019-01-29 11:23:38 +03:00
|
|
|
} else {
|
|
|
|
for (i = 0; i < this.typelessListeners.length; i++) {
|
2019-09-25 19:19:42 +03:00
|
|
|
this.typelessListeners[i](data)
|
2012-01-30 23:19:51 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2019-09-25 19:19:42 +03:00
|
|
|
lastLength: 0, // for fallback
|
2014-06-24 01:56:10 +04:00
|
|
|
/**
|
|
|
|
* Listen to a given type of events.
|
|
|
|
*
|
|
|
|
* @param {String} type event type
|
|
|
|
* @param {Function} callback event callback
|
|
|
|
*/
|
2019-09-25 19:19:42 +03:00
|
|
|
listen: function(type, callback) {
|
2019-01-29 11:23:38 +03:00
|
|
|
if (callback && callback.call) {
|
2012-11-15 21:26:08 +04:00
|
|
|
|
2019-01-29 11:23:38 +03:00
|
|
|
if (type) {
|
|
|
|
if (this.useFallBack) {
|
|
|
|
if (!this.listeners[type]) {
|
2019-09-25 19:19:42 +03:00
|
|
|
this.listeners[type] = []
|
2012-01-30 23:19:51 +04:00
|
|
|
}
|
2019-09-25 19:19:42 +03:00
|
|
|
this.listeners[type].push(callback)
|
2019-01-29 11:23:38 +03:00
|
|
|
} else {
|
2019-09-25 19:19:42 +03:00
|
|
|
this.source.addEventListener(type, function(e) {
|
2014-07-02 23:32:55 +04:00
|
|
|
if (typeof e.data !== 'undefined') {
|
2019-09-25 19:19:42 +03:00
|
|
|
callback(JSON.parse(e.data))
|
2013-07-11 02:00:01 +04:00
|
|
|
} else {
|
2019-09-25 19:19:42 +03:00
|
|
|
callback('')
|
2013-07-11 02:00:01 +04:00
|
|
|
}
|
2019-09-25 19:19:42 +03:00
|
|
|
}, false)
|
2012-01-30 23:19:51 +04:00
|
|
|
}
|
2019-01-29 11:23:38 +03:00
|
|
|
} else {
|
2019-09-25 19:19:42 +03:00
|
|
|
this.typelessListeners.push(callback)
|
2012-01-30 23:19:51 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2014-06-24 01:56:10 +04:00
|
|
|
/**
|
|
|
|
* Closes this event source.
|
|
|
|
*/
|
2019-09-25 19:19:42 +03:00
|
|
|
close: function() {
|
|
|
|
this.closed = true
|
2014-07-02 23:32:55 +04:00
|
|
|
if (typeof this.source !== 'undefined') {
|
2019-09-25 19:19:42 +03:00
|
|
|
this.source.close()
|
2013-02-14 15:17:14 +04:00
|
|
|
}
|
2012-01-30 23:19:51 +04:00
|
|
|
}
|
2019-09-25 19:19:42 +03:00
|
|
|
}
|
2019-01-29 11:23:38 +03:00
|
|
|
|
2019-09-25 19:19:42 +03:00
|
|
|
export default OCEventSource
|