Commit 1f31f7a6 authored by Romain Courteaud's avatar Romain Courteaud

[erp5_web_renderjs_ui] update renderJS to WIP

parent f9e2ea18
......@@ -689,6 +689,66 @@ if (typeof document.contains !== 'function') {
});
}
function loopEventListener(target, type, useCapture, callback,
prevent_default) {
//////////////////////////
// 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 = RSVP.reject(e);
}
callback_promise = result;
new RSVP.Queue()
.push(function () {
return result;
})
.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);
}
function ajax(url) {
var xhr;
function resolver(resolve, reject) {
......@@ -805,6 +865,10 @@ if (typeof document.contains !== 'function') {
paragraph.textContent = 'User-agent: ' + navigator.userAgent;
container.appendChild(paragraph);
paragraph = document.createElement("p");
paragraph.textContent = 'Date: ' + new Date(Date.now()).toISOString();
container.appendChild(paragraph);
body.appendChild(container);
for (i = 0; i < error_list.length; i += 1) {
......@@ -1051,12 +1115,26 @@ if (typeof document.contains !== 'function') {
this.__ready_list.push(callback);
return this;
};
RenderJSGadget.setState = function (state_dict) {
var json_state = JSON.stringify(state_dict);
return this.ready(function () {
this.state = JSON.parse(json_state);
});
};
RenderJSGadget.__service_list = [];
RenderJSGadget.declareService = function (callback) {
this.__service_list.push(callback);
return this;
};
RenderJSGadget.onEvent = function (type, callback, use_capture,
prevent_default) {
this.__service_list.push(function () {
return loopEventListener(this.__element, type, use_capture,
callback.bind(this), prevent_default);
});
return this;
};
function startService(gadget) {
gadget.__monitor.monitor(new RSVP.Queue()
......@@ -1114,6 +1192,27 @@ if (typeof document.contains !== 'function') {
throw new Error("No element defined");
}
return this.__element;
})
.declareMethod('render', function () {
return;
})
.declareMethod('updateDOM', function () {
return;
})
.declareMethod('changeState', function (state_dict) {
var key,
modified = false,
modification_dict = {};
for (key in state_dict) {
if (state_dict[key] !== this.state[key]) {
this.state[key] = state_dict[key];
modification_dict[key] = state_dict[key];
modified = true;
}
}
if (modified) {
return this.updateDOM(modification_dict);
}
});
/////////////////////////////////////////////////////////////////
......@@ -1202,8 +1301,12 @@ if (typeof document.contains !== 'function') {
RenderJSGadget.__service_list.slice();
RenderJSEmbeddedGadget.ready =
RenderJSGadget.ready;
RenderJSEmbeddedGadget.setState =
RenderJSGadget.setState;
RenderJSEmbeddedGadget.declareService =
RenderJSGadget.declareService;
RenderJSEmbeddedGadget.onEvent =
RenderJSGadget.onEvent;
RenderJSEmbeddedGadget.prototype = new RenderJSGadget();
RenderJSEmbeddedGadget.prototype.constructor = RenderJSEmbeddedGadget;
......@@ -1233,6 +1336,8 @@ if (typeof document.contains !== 'function') {
gadget_loading_klass = Klass;
gadget_instance = new Klass();
gadget_instance.__element = options.element;
gadget_instance.element = options.element;
gadget_instance.state = {};
for (i = 0; i < template_node_list.length; i += 1) {
gadget_instance.__element.appendChild(
template_node_list[i].cloneNode(true)
......@@ -1276,9 +1381,13 @@ if (typeof document.contains !== 'function') {
RenderJSIframeGadget.__ready_list = RenderJSGadget.__ready_list.slice();
RenderJSIframeGadget.ready =
RenderJSGadget.ready;
RenderJSIframeGadget.setState =
RenderJSGadget.setState;
RenderJSIframeGadget.__service_list = RenderJSGadget.__service_list.slice();
RenderJSIframeGadget.declareService =
RenderJSGadget.declareService;
RenderJSIframeGadget.onEvent =
RenderJSGadget.onEvent;
RenderJSIframeGadget.prototype = new RenderJSGadget();
RenderJSIframeGadget.prototype.constructor = RenderJSIframeGadget;
......@@ -1307,6 +1416,8 @@ if (typeof document.contains !== 'function') {
iframe.setAttribute("src", url);
gadget_instance.__path = url;
gadget_instance.__element = options.element;
gadget_instance.element = options.element;
gadget_instance.state = {};
// Attach it to the DOM
options.element.appendChild(iframe);
......@@ -1475,10 +1586,17 @@ if (typeof document.contains !== 'function') {
function ready_wrapper() {
return gadget_instance;
}
function ready_executable_wrapper(fct) {
return function (g) {
return fct.call(g, g);
};
}
for (i = 0; i < gadget_instance.constructor.__ready_list.length;
i += 1) {
// Put a timeout?
queue.push(gadget_instance.constructor.__ready_list[i]);
queue.push(ready_executable_wrapper(
gadget_instance.constructor.__ready_list[i]
));
// Always return the gadget instance after ready function
queue.push(ready_wrapper);
}
......@@ -1662,8 +1780,12 @@ if (typeof document.contains !== 'function') {
RenderJSGadget.allowPublicAcquisition;
tmp_constructor.ready =
RenderJSGadget.ready;
tmp_constructor.setState =
RenderJSGadget.setState;
tmp_constructor.declareService =
RenderJSGadget.declareService;
tmp_constructor.onEvent =
RenderJSGadget.onEvent;
tmp_constructor.prototype = new RenderJSGadget();
tmp_constructor.prototype.constructor = tmp_constructor;
tmp_constructor.prototype.__path = url;
......@@ -1835,9 +1957,12 @@ if (typeof document.contains !== 'function') {
RenderJSGadget.allowPublicAcquisition;
tmp_constructor.__ready_list = RenderJSGadget.__ready_list.slice();
tmp_constructor.ready = RenderJSGadget.ready;
tmp_constructor.setState = RenderJSGadget.setState;
tmp_constructor.__service_list = RenderJSGadget.__service_list.slice();
tmp_constructor.declareService =
RenderJSGadget.declareService;
tmp_constructor.onEvent =
RenderJSGadget.onEvent;
tmp_constructor.prototype = new RenderJSGadget();
tmp_constructor.prototype.constructor = tmp_constructor;
tmp_constructor.prototype.__path = url;
......@@ -1960,6 +2085,8 @@ if (typeof document.contains !== 'function') {
tmp_constructor.declareService =
RenderJSGadget.declareService;
tmp_constructor.onEvent =
RenderJSGadget.onEvent;
tmp_constructor.declareAcquiredMethod =
RenderJSGadget.declareAcquiredMethod;
tmp_constructor.allowPublicAcquisition =
......@@ -1983,6 +2110,8 @@ if (typeof document.contains !== 'function') {
}
tmp_constructor.__template_element = document.createElement("div");
root_gadget.__element = document.body;
root_gadget.element = document.body;
root_gadget.state = {};
for (j = 0; j < root_gadget.__element.childNodes.length; j += 1) {
tmp_constructor.__template_element.appendChild(
root_gadget.__element.childNodes[j].cloneNode(true)
......@@ -2089,7 +2218,11 @@ if (typeof document.contains !== 'function') {
function ready_wrapper() {
return root_gadget;
}
function ready_executable_wrapper(fct) {
return function (g) {
return fct.call(g, g);
};
}
tmp_constructor.ready(function (g) {
return startService(g);
});
......@@ -2098,7 +2231,7 @@ if (typeof document.contains !== 'function') {
for (i = 0; i < tmp_constructor.__ready_list.length; i += 1) {
// Put a timeout?
loading_gadget_promise
.push(tmp_constructor.__ready_list[i])
.push(ready_executable_wrapper(tmp_constructor.__ready_list[i]))
// Always return the gadget instance after ready function
.push(ready_wrapper);
}
......
......@@ -230,7 +230,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>952.32884.7041.46523</string> </value>
<value> <string>954.3971.11153.48145</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -248,7 +248,7 @@
</tuple>
<state>
<tuple>
<float>1469783555.9</float>
<float>1474983451.97</float>
<string>UTC</string>
</tuple>
</state>
......
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