Commit 181fd3b3 authored by Romain Courteaud's avatar Romain Courteaud

erp5_core/graph_editor: add compatibility with renderJS.loopEventListener

parent 33e02c26
/// FIXME: merge into the gadget using these utilities and remove this file /// FIXME: merge into the gadget using these utilities and remove this file
/*global RSVP, FileReader */ /*global RSVP, FileReader, renderJS */
/*jslint unparam: true */ /*jslint unparam: true */
(function(window, RSVP, FileReader) { (function(window, RSVP, FileReader, renderJS) {
"use strict"; "use strict";
window.loopEventListener = function(target, type, useCapture, callback, allowDefault) { window.loopEventListener = renderJS.loopEventListener;
//////////////////////////
// Infinite event listener (promise is never resolved)
// eventListener is removed when promise is cancelled/rejected
//////////////////////////
var handle_event_callback, callback_promise;
function cancelResolver() {
if (callback_promise !== undefined && typeof callback_promise.cancel === "function") {
callback_promise.cancel();
}
}
function canceller() {
if (handle_event_callback !== undefined) {
target.removeEventListener(type, handle_event_callback, useCapture);
}
cancelResolver();
}
function itsANonResolvableTrap(resolve, reject) {
handle_event_callback = function(evt) {
evt.stopPropagation();
if (allowDefault !== true) {
evt.preventDefault();
}
cancelResolver();
callback_promise = new RSVP.Queue().push(function() {
return callback(evt);
}).push(undefined, function(error) {
if (!(error instanceof RSVP.CancellationError)) {
canceller();
reject(error);
}
});
};
target.addEventListener(type, handle_event_callback, useCapture);
}
return new RSVP.Promise(itsANonResolvableTrap, canceller);
};
window.promiseEventListener = function(target, type, useCapture) { window.promiseEventListener = function(target, type, useCapture) {
////////////////////////// //////////////////////////
// Resolve the promise as soon as the event is triggered // Resolve the promise as soon as the event is triggered
...@@ -74,4 +38,4 @@ ...@@ -74,4 +38,4 @@
reader.readAsText(file); reader.readAsText(file);
}); });
}; };
})(window, RSVP, FileReader); })(window, RSVP, FileReader, renderJS);
\ No newline at end of file \ No newline at end of file
/*global window, RSVP, FileReader */ /*global window, RSVP, FileReader, renderJS */
/*jslint indent: 2, maxerr: 3, unparam: true */ /*jslint indent: 2, maxerr: 3, unparam: true */
(function (window, RSVP, FileReader) { (function (window, RSVP, FileReader, renderJS) {
"use strict"; "use strict";
...@@ -62,62 +62,8 @@ ...@@ -62,62 +62,8 @@
} }
window.asBoolean = asBoolean; window.asBoolean = asBoolean;
window.loopEventListener = function (target, type, useCapture, callback, // Compatibility with gadgets not accessing this function from renderJS
prevent_default) { window.loopEventListener = renderJS.loopEventListener;
//////////////////////////
// Infinite event listener (promise is never resolved)
// eventListener is removed when promise is cancelled/rejected
//////////////////////////
var handle_event_callback,
callback_promise;
if (prevent_default === undefined) {
prevent_default = true;
}
function cancelResolver() {
if ((callback_promise !== undefined) &&
(typeof callback_promise.cancel === "function")) {
callback_promise.cancel();
}
}
function canceller() {
if (handle_event_callback !== undefined) {
target.removeEventListener(type, handle_event_callback, useCapture);
}
cancelResolver();
}
function itsANonResolvableTrap(resolve, reject) {
var result;
handle_event_callback = function (evt) {
if (prevent_default) {
evt.stopPropagation();
evt.preventDefault();
}
cancelResolver();
try {
result = callback(evt);
} catch (e) {
result = reject(e);
}
callback_promise = new RSVP.Queue(result)
.push(undefined, function handleEventCallbackError(error) {
// Prevent rejecting the loop, if the result cancelled itself
if (!(error instanceof RSVP.CancellationError)) {
canceller();
reject(error);
}
});
};
target.addEventListener(type, handle_event_callback, useCapture);
}
return new RSVP.Promise(itsANonResolvableTrap, canceller);
};
window.promiseEventListener = function (target, type, useCapture) { window.promiseEventListener = function (target, type, useCapture) {
////////////////////////// //////////////////////////
...@@ -182,4 +128,4 @@ ...@@ -182,4 +128,4 @@
}); });
}; };
}(window, RSVP, FileReader)); }(window, RSVP, FileReader, renderJS));
\ No newline at end of file \ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment