Move OC.EventSource to the server bundle

Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
This commit is contained in:
Christoph Wurst 2019-01-29 09:23:38 +01:00
parent 8e9d259074
commit 3695d02575
No known key found for this signature in database
GPG key ID: CC42AC2A7F0E56D8
6 changed files with 57 additions and 53 deletions

View file

@ -21,7 +21,6 @@
"sharedialogshareelistview.js",
"octemplate.js",
"contactsmenu_templates.js",
"eventsource.js",
"public/appconfig.js",
"public/comments.js",
"public/publicpage.js",

BIN
core/js/dist/main.js vendored

Binary file not shown.

Binary file not shown.

View file

@ -4,7 +4,6 @@
"js.js",
"l10n.js",
"octemplate.js",
"eventsource.js",
"public/appconfig.js",
"public/comments.js",
"public/whatsnew.js",

View file

@ -30,70 +30,72 @@
/* global EventSource */
import $ from 'jquery'
/**
* Create a new event source
* @param {string} src
* @param {object} [data] to be send as GET
*
* @constructs OC.EventSource
* @constructs OCEventSource
*/
OC.EventSource=function(src,data){
var dataStr='';
const OCEventSource = function (src, data) {
var dataStr = '';
var name;
var joinChar;
this.typelessListeners=[];
this.typelessListeners = [];
this.closed = false;
this.listeners={};
if(data){
for(name in data){
dataStr+=name+'='+encodeURIComponent(data[name])+'&';
this.listeners = {};
if (data) {
for (name in data) {
dataStr += name + '=' + encodeURIComponent(data[name]) + '&';
}
}
dataStr+='requesttoken='+encodeURIComponent(oc_requesttoken);
if(!this.useFallBack && typeof EventSource !== 'undefined'){
dataStr += 'requesttoken=' + encodeURIComponent(oc_requesttoken);
if (!this.useFallBack && typeof EventSource !== 'undefined') {
joinChar = '&';
if(src.indexOf('?') === -1) {
if (src.indexOf('?') === -1) {
joinChar = '?';
}
this.source= new EventSource(src+joinChar+dataStr);
this.source.onmessage=function(e){
for(var i=0;i<this.typelessListeners.length;i++){
this.source = new EventSource(src + joinChar + dataStr);
this.source.onmessage = function (e) {
for (var i = 0; i < this.typelessListeners.length; i++) {
this.typelessListeners[i](JSON.parse(e.data));
}
}.bind(this);
}else{
var iframeId='oc_eventsource_iframe_'+OC.EventSource.iframeCount;
OC.EventSource.fallBackSources[OC.EventSource.iframeCount]=this;
this.iframe=$('<iframe/>');
this.iframe.attr('id',iframeId);
} else {
var iframeId = 'oc_eventsource_iframe_' + OCEventSource.iframeCount;
OCEventSource.fallBackSources[OCEventSource.iframeCount] = this;
this.iframe = $('<iframe/>');
this.iframe.attr('id', iframeId);
this.iframe.hide();
joinChar = '&';
if(src.indexOf('?') === -1) {
if (src.indexOf('?') === -1) {
joinChar = '?';
}
this.iframe.attr('src',src+joinChar+'fallback=true&fallback_id='+OC.EventSource.iframeCount+'&'+dataStr);
this.iframe.attr('src', src + joinChar + 'fallback=true&fallback_id=' + OCEventSource.iframeCount + '&' + dataStr);
$('body').append(this.iframe);
this.useFallBack=true;
OC.EventSource.iframeCount++;
this.useFallBack = true;
OCEventSource.iframeCount++;
}
//add close listener
this.listen('__internal__',function(data){
if(data === 'close'){
this.listen('__internal__', function (data) {
if (data === 'close') {
this.close();
}
}.bind(this));
};
OC.EventSource.fallBackSources=[];
OC.EventSource.iframeCount=0;//number of fallback iframes
OC.EventSource.fallBackCallBack=function(id,type,data){
OC.EventSource.fallBackSources[id].fallBackCallBack(type,data);
OCEventSource.fallBackSources = [];
OCEventSource.iframeCount = 0;//number of fallback iframes
OCEventSource.fallBackCallBack = function (id, type, data) {
OCEventSource.fallBackSources[id].fallBackCallBack(type, data);
};
OC.EventSource.prototype={
typelessListeners:[],
iframe:null,
listeners:{},//only for fallback
useFallBack:false,
OCEventSource.prototype = {
typelessListeners: [],
iframe: null,
listeners: {},//only for fallback
useFallBack: false,
/**
* Fallback callback for browsers that don't have the
* native EventSource object.
@ -104,50 +106,50 @@ OC.EventSource.prototype={
* @param {String} type event type
* @param {Object} data received data
*/
fallBackCallBack:function(type,data){
fallBackCallBack: function (type, data) {
var i;
// ignore messages that might appear after closing
if (this.closed) {
return;
}
if(type){
if (type) {
if (typeof this.listeners.done !== 'undefined') {
for(i=0;i<this.listeners[type].length;i++){
for (i = 0; i < this.listeners[type].length; i++) {
this.listeners[type][i](data);
}
}
}else{
for(i=0;i<this.typelessListeners.length;i++){
} else {
for (i = 0; i < this.typelessListeners.length; i++) {
this.typelessListeners[i](data);
}
}
},
lastLength:0,//for fallback
lastLength: 0,//for fallback
/**
* Listen to a given type of events.
*
* @param {String} type event type
* @param {Function} callback event callback
*/
listen:function(type,callback){
if(callback && callback.call){
listen: function (type, callback) {
if (callback && callback.call) {
if(type){
if(this.useFallBack){
if(!this.listeners[type]){
this.listeners[type]=[];
if (type) {
if (this.useFallBack) {
if (!this.listeners[type]) {
this.listeners[type] = [];
}
this.listeners[type].push(callback);
}else{
this.source.addEventListener(type,function(e){
} else {
this.source.addEventListener(type, function (e) {
if (typeof e.data !== 'undefined') {
callback(JSON.parse(e.data));
} else {
callback('');
}
},false);
}, false);
}
}else{
} else {
this.typelessListeners.push(callback);
}
}
@ -155,10 +157,12 @@ OC.EventSource.prototype={
/**
* Closes this event source.
*/
close:function(){
close: function () {
this.closed = true;
if (typeof this.source !== 'undefined') {
this.source.close();
}
}
};
export default OCEventSource;

View file

@ -24,6 +24,7 @@ import Backbone from 'backbone';
import Apps from './apps'
import AppConfig from './appconfig'
import ContactsMenu from './contactsmenu';
import EventSource from './eventsource'
import {davCall, davSync} from './backbone-webdav';
// Patch Backbone for DAV
@ -37,5 +38,6 @@ export default {
Apps,
AppConfig,
Backbone,
EventSource,
ContactsMenu,
};