Commit 84717f1f authored by Romain Courteaud's avatar Romain Courteaud

Release version 0.7.1

Do not prevent calling declareGadget in ready functions.
Allow to directly declareGadget inside the HTML.
parent 5e541236
This diff is collapsed.
This diff is collapsed.
...@@ -671,7 +671,7 @@ ...@@ -671,7 +671,7 @@
javascript_registration_dict = {}, javascript_registration_dict = {},
stylesheet_registration_dict = {}, stylesheet_registration_dict = {},
gadget_loading_klass, gadget_loading_klass,
loading_gadget_promise, loading_klass_promise,
renderJS; renderJS;
function removeHash(url) { function removeHash(url) {
...@@ -701,7 +701,34 @@ ...@@ -701,7 +701,34 @@
g.__sub_gadget_dict = {}; g.__sub_gadget_dict = {};
} }
RenderJSGadget.__ready_list = [clearGadgetInternalParameters]; function loadSubGadgetDOMDeclaration(g) {
var element_list = g.__element.querySelectorAll('[data-gadget-scope]'),
element,
promise_list = [],
scope,
url,
sandbox,
i;
for (i = 0; i < element_list.length; i += 1) {
element = element_list[i];
scope = element.getAttribute("data-gadget-scope");
url = element.getAttribute("data-gadget-url");
sandbox = element.getAttribute("data-gadget-sandbox");
if ((scope !== null) && (url !== null)) {
promise_list.push(g.declareGadget(url, {
element: element,
scope: scope || undefined,
sandbox: sandbox || undefined
}));
}
}
return RSVP.all(promise_list);
}
RenderJSGadget.__ready_list = [clearGadgetInternalParameters,
loadSubGadgetDOMDeclaration];
RenderJSGadget.ready = function (callback) { RenderJSGadget.ready = function (callback) {
this.__ready_list.push(callback); this.__ready_list.push(callback);
return this; return this;
...@@ -1012,7 +1039,8 @@ ...@@ -1012,7 +1039,8 @@
.declareMethod('declareGadget', function (url, options) { .declareMethod('declareGadget', function (url, options) {
var queue, var queue,
parent_gadget = this, parent_gadget = this,
previous_loading_gadget_promise = loading_gadget_promise; local_loading_klass_promise,
previous_loading_klass_promise = loading_klass_promise;
if (options === undefined) { if (options === undefined) {
options = {}; options = {};
...@@ -1024,10 +1052,10 @@ ...@@ -1024,10 +1052,10 @@
// transform url to absolute url if it is relative // transform url to absolute url if it is relative
url = renderJS.getAbsoluteURL(url, this.__path); url = renderJS.getAbsoluteURL(url, this.__path);
// Change the global variable to update the loading queue // Change the global variable to update the loading queue
queue = new RSVP.Queue() loading_klass_promise = new RSVP.Queue()
// Wait for previous gadget loading to finish first // Wait for previous gadget loading to finish first
.push(function () { .push(function () {
return previous_loading_gadget_promise; return previous_loading_klass_promise;
}) })
.push(undefined, function () { .push(undefined, function () {
// Forget previous declareGadget error // Forget previous declareGadget error
...@@ -1047,9 +1075,25 @@ ...@@ -1047,9 +1075,25 @@
}) })
// Set the HTML context // Set the HTML context
.push(function (gadget_instance) { .push(function (gadget_instance) {
var i;
// Drop the current loading klass info used by selector // Drop the current loading klass info used by selector
gadget_loading_klass = undefined; gadget_loading_klass = undefined;
return gadget_instance;
})
.push(undefined, function (e) {
// Drop the current loading klass info used by selector
// even in case of error
gadget_loading_klass = undefined;
throw e;
});
local_loading_klass_promise = loading_klass_promise;
queue = new RSVP.Queue()
.push(function () {
return local_loading_klass_promise;
})
// Set the HTML context
.push(function (gadget_instance) {
var i;
// Trigger calling of all ready callback // Trigger calling of all ready callback
function ready_wrapper() { function ready_wrapper() {
return gadget_instance; return gadget_instance;
...@@ -1065,17 +1109,18 @@ ...@@ -1065,17 +1109,18 @@
// Store local reference to the gadget instance // Store local reference to the gadget instance
if (options.scope !== undefined) { if (options.scope !== undefined) {
parent_gadget.__sub_gadget_dict[options.scope] = gadget_instance; parent_gadget.__sub_gadget_dict[options.scope] = gadget_instance;
gadget_instance.__element.setAttribute("data-gadget-scope",
options.scope);
} }
// Put some attribute to ease page layout comprehension
gadget_instance.__element.setAttribute("data-gadget-url", url);
gadget_instance.__element.setAttribute("data-gadget-sandbox",
options.sandbox);
return gadget_instance; return gadget_instance;
})
.push(undefined, function (e) {
// Drop the current loading klass info used by selector
// even in case of error
gadget_loading_klass = undefined;
throw e;
}); });
loading_gadget_promise = queue; return queue;
return loading_gadget_promise;
}) })
.declareMethod('getDeclaredGadget', function (gadget_scope) { .declareMethod('getDeclaredGadget', function (gadget_scope) {
if (!this.__sub_gadget_dict.hasOwnProperty(gadget_scope)) { if (!this.__sub_gadget_dict.hasOwnProperty(gadget_scope)) {
...@@ -1324,7 +1369,7 @@ ...@@ -1324,7 +1369,7 @@
isAbsoluteURL = new RegExp('^(?:[a-z]+:)?//', 'i'); isAbsoluteURL = new RegExp('^(?:[a-z]+:)?//', 'i');
if (!url || !isAbsoluteURL.test(url)) { if (!url || !isAbsoluteURL.test(url)) {
throw new Error("The second parameter should be an absolute url"); throw new Error("The url should be absolute: " + url);
} }
if (document_element.nodeType === 9) { if (document_element.nodeType === 9) {
...@@ -1374,6 +1419,7 @@ ...@@ -1374,6 +1419,7 @@
var url = removeHash(window.location.href), var url = removeHash(window.location.href),
tmp_constructor, tmp_constructor,
root_gadget, root_gadget,
loading_gadget_promise = new RSVP.Queue(),
declare_method_count = 0, declare_method_count = 0,
embedded_channel, embedded_channel,
notifyReady, notifyReady,
...@@ -1385,7 +1431,7 @@ ...@@ -1385,7 +1431,7 @@
if (gadget_model_dict.hasOwnProperty(url)) { if (gadget_model_dict.hasOwnProperty(url)) {
throw new Error("bootstrap should not be called twice"); throw new Error("bootstrap should not be called twice");
} }
loading_gadget_promise = new RSVP.Promise(function (resolve, reject) { loading_klass_promise = new RSVP.Promise(function (resolve, reject) {
if (window.self === window.top) { if (window.self === window.top) {
last_acquisition_gadget = new RenderJSGadget(); last_acquisition_gadget = new RenderJSGadget();
...@@ -1536,8 +1582,7 @@ ...@@ -1536,8 +1582,7 @@
.then(function (all_list) { .then(function (all_list) {
var i, var i,
js_list = all_list[0], js_list = all_list[0],
css_list = all_list[1], css_list = all_list[1];
queue;
for (i = 0; i < js_list.length; i += 1) { for (i = 0; i < js_list.length; i += 1) {
javascript_registration_dict[js_list[i]] = null; javascript_registration_dict[js_list[i]] = null;
} }
...@@ -1545,7 +1590,24 @@ ...@@ -1545,7 +1590,24 @@
stylesheet_registration_dict[css_list[i]] = null; stylesheet_registration_dict[css_list[i]] = null;
} }
gadget_loading_klass = undefined; gadget_loading_klass = undefined;
queue = new RSVP.Queue(); return root_gadget;
}).then(resolve, function (e) {
reject(e);
/*global console */
console.error(e);
throw e;
});
}
document.addEventListener('DOMContentLoaded', init, false);
});
loading_gadget_promise
.push(function () {
return loading_klass_promise;
})
.push(function (root_gadget) {
var i;
function ready_wrapper() { function ready_wrapper() {
return root_gadget; return root_gadget;
} }
...@@ -1562,33 +1624,23 @@ ...@@ -1562,33 +1624,23 @@
}); });
} }
queue.push(ready_wrapper); loading_gadget_promise.push(ready_wrapper);
for (i = 0; i < tmp_constructor.__ready_list.length; i += 1) { for (i = 0; i < tmp_constructor.__ready_list.length; i += 1) {
// Put a timeout? // Put a timeout?
queue.push(tmp_constructor.__ready_list[i]) loading_gadget_promise
.push(tmp_constructor.__ready_list[i])
// Always return the gadget instance after ready function // Always return the gadget instance after ready function
.push(ready_wrapper); .push(ready_wrapper);
} }
queue.push(resolve, function (e) {
reject(e);
throw e;
}); });
return queue;
}).fail(function (e) {
reject(e);
/*global console */
console.error(e);
});
}
document.addEventListener('DOMContentLoaded', init, false);
});
if (window.self !== window.top) { if (window.self !== window.top) {
// Inform parent window that gadget is correctly loaded // Inform parent window that gadget is correctly loaded
loading_gadget_promise.then(function () { loading_gadget_promise
.then(function () {
gadget_ready = true; gadget_ready = true;
notifyReady(); notifyReady();
}).fail(function (e) { })
.fail(function (e) {
embedded_channel.notify({method: "failed", params: e.toString()}); embedded_channel.notify({method: "failed", params: e.toString()});
throw e; throw e;
}); });
......
This diff is collapsed.
{ {
"name": "renderjs", "name": "renderjs",
"version": "0.7.0", "version": "0.7.1",
"description": "RenderJs provides HTML5 gadgets", "description": "RenderJs provides HTML5 gadgets",
"main": "dist/renderjs-latest.js", "main": "dist/renderjs-latest.js",
"dependencies": { "dependencies": {
......
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