Commit dc9702f4 authored by Tristan Cavelier's avatar Tristan Cavelier

RenderJs source code re-indented correctly

parent 42731279
...@@ -2,484 +2,529 @@ ...@@ -2,484 +2,529 @@
// still it's possible to override this and use explicit gadget rendering // still it's possible to override this and use explicit gadget rendering
var RENDERJS_ENABLE_IMPLICIT_GADGET_RENDERING = true; var RENDERJS_ENABLE_IMPLICIT_GADGET_RENDERING = true;
// Add required by RenderJs jstorage library only if used HTML application uses requirejs // Add required by RenderJs jstorage library only if used HTML application
// uses requirejs
if (typeof require !== 'undefined') { if (typeof require !== 'undefined') {
require(["../../../../lib/jstorage/jstorage.js"], function(util) { require(["../../../../lib/jstorage/jstorage.js"], function(util) {
}); });
} }
// fallback for IE // fallback for IE
if (typeof console === "undefined" || typeof console.log === "undefined") { if (typeof console === "undefined" || typeof console.log === "undefined") {
console = {}; console = {};
console.log = function() {}; console.log = function() {};
} }
/* /*
* Generic Gadget library renderer * Generic Gadget library renderer
*/ */
var RenderJs = (function () { var RenderJs = (function () {
// a variable indicating if current gadget loading is over or not
// a variable indicating if current gadget loading is over or not var is_ready = false;
var is_ready = false;
return {
return { bootstrap: function (root) {
/* initial load application gadget */
bootstrap: function (root){ RenderJs.loadGadgetFromUrl(root);
/* initial load application gadget */ RenderJs.load(root);
RenderJs.loadGadgetFromUrl(root); },
RenderJs.load(root);
}, load: function (root) {
/* Load gadget layout by traversing DOM */
load: function (root) { var gadget_list;
/* Load gadget layout by traversing DOM */ gadget_list = root.find("[data-gadget]");
var gadget_list; // Load chilren
gadget_list = root.find("[data-gadget]"); gadget_list.each(function() {
// Load chilren RenderJs.loadGadgetFromUrl($(this));
gadget_list.each(function() { });
RenderJs.loadGadgetFromUrl($(this)); },
});
}, updateAndRecurse: function(gadget, data) {
/* Update current gadget and recurse down */
updateAndRecurse: function(gadget, data){ gadget.append(data);
/* Update current gadget and recurse down */ // a gadget may contain sub gadgets
gadget.append(data); this.load(gadget);
// a gadget may contain sub gadgets },
this.load(gadget);
}, loadGadgetFromUrl: function(gadget) {
/* Load gadget's SPECs from URL */
loadGadgetFromUrl: function(gadget) { var url, gadget_id, gadget_property, cacheable, cache_id,
/* Load gadget's SPECs from URL */ app_cache, data, gadget_js;
var url, gadget_id, gadget_property, cacheable, cache_id, app_cache, data, gadget_js; url = gadget.attr("data-gadget");
url = gadget.attr("data-gadget"); gadget_id = gadget.attr("id");
gadget_id = gadget.attr("id");
// register gadget in javascript namespace
// register gadget in javascript namespace gadget_js = new RenderJs.Gadget(gadget_id, gadget);
gadget_js = new RenderJs.Gadget(gadget_id, gadget); RenderJs.GadgetIndex.registerGadget(gadget_js);
RenderJs.GadgetIndex.registerGadget(gadget_js);
// XXX: update Gadget's instance with contents of "data-gadget-property"
// XXX: update Gadget's instance with contents of "data-gadget-property"
if (url !== undefined && url !== ""){
if (url!==undefined && url!==""){ cacheable = gadget.attr("data-gadget-cacheable");
cacheable = gadget.attr("data-gadget-cacheable"); cache_id = gadget.attr("data-gadget-cache-id");
cache_id = gadget.attr("data-gadget-cache-id"); if (cacheable !== undefined && cache_id !== undefined){
if (cacheable!==undefined && cache_id!==undefined){ cacheable = Boolean(parseInt(cacheable));
cacheable = Boolean(parseInt(cacheable)); }
} //cacheable = false ; // to develop faster
//cacheable = false ; // to develop faster if (cacheable) {
if (cacheable) { // get from cache if possible, use last part from URL as
// get from cache if possible, use last part from URL as cache_key // cache_key
app_cache = RenderJs.Cache.get(cache_id, undefined); app_cache = RenderJs.Cache.get(cache_id, undefined);
if(app_cache===undefined || app_cache===null){ if(app_cache === undefined || app_cache === null){
// not in cache so we pull from network and cache // not in cache so we pull from network and cache
$.ajax({url:url, $.ajax({
yourCustomData: {"gadget_id": gadget_id, "cache_id": cache_id}, url:url,
success: function (data) { yourCustomData: {
cache_id = this.yourCustomData.cache_id; "gadget_id": gadget_id, "cache_id": cache_id
gadget_id = this.yourCustomData.gadget_id; },
RenderJs.Cache.set(cache_id, data); success: function (data) {
RenderJs.GadgetIndex.getGadgetById(gadget_id).setReady(); cache_id = this.yourCustomData.cache_id;
RenderJs.updateAndRecurse(gadget, data); gadget_id = this.yourCustomData.gadget_id;
RenderJs.checkAndTriggerReady(); RenderJs.Cache.set(cache_id, data);
}}); RenderJs.GadgetIndex.getGadgetById(gadget_id).
} setReady();
else { RenderJs.updateAndRecurse(gadget, data);
// get from cache RenderJs.checkAndTriggerReady();
data = app_cache; }
gadget_js.setReady(); });
this.updateAndRecurse(gadget, data); } else {
this.checkAndTriggerReady(); // get from cache
} data = app_cache;
} gadget_js.setReady();
else { this.updateAndRecurse(gadget, data);
// not to be cached this.checkAndTriggerReady();
$.ajax({url:url,
yourCustomData: {"gadget_id": gadget_id},
success: function (data) {
gadget_id = this.yourCustomData.gadget_id;
RenderJs.GadgetIndex.getGadgetById(gadget_id).setReady();
RenderJs.updateAndRecurse(gadget, data);
RenderJs.checkAndTriggerReady();
}});
}
}
else {
// gadget is an inline one so no need to load it from network
gadget_js.setReady();
RenderJs.checkAndTriggerReady();
} }
}, } else {
// not to be cached
$.ajax({
url:url,
yourCustomData: {"gadget_id": gadget_id},
success: function (data) {
gadget_id = this.yourCustomData.gadget_id;
RenderJs.GadgetIndex.getGadgetById(gadget_id).
setReady();
RenderJs.updateAndRecurse(gadget, data);
RenderJs.checkAndTriggerReady();
}
});
}
} else {
// gadget is an inline one so no need to load it from network
gadget_js.setReady();
RenderJs.checkAndTriggerReady();
}
},
isReady: function (value) { isReady: function (value) {
/* /*
* Get rendering status * Get rendering status
*/ */
return is_ready; return is_ready;
}, },
setReady: function (value) { setReady: function (value) {
/* /*
* Update rendering status * Update rendering status
*/ */
is_ready = value; is_ready = value;
}, },
checkAndTriggerReady: function() { checkAndTriggerReady: function () {
/* /*
* Trigger "ready" event only if all gadgets were marked as "ready" * Trigger "ready" event only if all gadgets were marked as "ready"
*/ */
var is_gadget_list_loaded; var is_gadget_list_loaded;
is_gadget_list_loaded = RenderJs.GadgetIndex.isGadgetListLoaded(); is_gadget_list_loaded = RenderJs.GadgetIndex.isGadgetListLoaded();
if (is_gadget_list_loaded){ if (is_gadget_list_loaded) {
if (!RenderJs.isReady()) { if (!RenderJs.isReady()) {
RenderJs.GadgetIndex.getRootGadget().getDom().trigger("ready"); RenderJs.GadgetIndex.getRootGadget().getDom().
RenderJs.setReady(true); trigger("ready");
} RenderJs.setReady(true);
//is_ready = true; }
//is_ready = true;
} }
return is_gadget_list_loaded; return is_gadget_list_loaded;
}, },
update: function (root) { update: function (root) {
/* update gadget with data from remote source */ /* update gadget with data from remote source */
root.find("[gadget]").each(function(i,v){RenderJs.updateGadgetData($(this));}); root.find("[gadget]").each(function (i,v) {
}, RenderJs.updateGadgetData($(this));
});
updateGadgetWithDataHandler: function (result) { },
var data_handler;
data_handler = this.yourCustomData.data_handler; updateGadgetWithDataHandler: function (result) {
if (data_handler!==undefined){ var data_handler;
eval(data_handler+ "(result)"); data_handler = this.yourCustomData.data_handler;
} if (data_handler !== undefined){
}, eval(data_handler+ "(result)");
}
updateGadgetData: function(gadget) { },
/* Do real gagdet update here */
var data_source, data_handler; updateGadgetData: function (gadget) {
data_source = gadget.attr("data-gadget-source"); /* Do real gagdet update here */
data_handler = gadget.attr("data-gadget-handler"); var data_source, data_handler;
// acquire data and pass it to method handler data_source = gadget.attr("data-gadget-source");
if (data_source!==undefined && data_source!==""){ data_handler = gadget.attr("data-gadget-handler");
$.ajax({url:data_source, // acquire data and pass it to method handler
dataType: "json", if (data_source !== undefined && data_source !== ""){
yourCustomData: {"data_handler": data_handler}, $.ajax({
success: RenderJs.updateGadgetWithDataHandler});} url:data_source,
}, dataType: "json",
yourCustomData: {"data_handler": data_handler},
addGadget: function(dom_id, gadget, gadget_data_handler, gadget_data_source) { success: RenderJs.updateGadgetWithDataHandler
/* });
* add new gadget and render it }
*/ },
var html_string;
tab_container=$('#'+dom_id);
tab_container.empty();
html_string =['<div class="gadget" ',
'data-gadget="' + gadget + '"',
'data-gadget-handler="' + gadget_data_handler + '" ',
'data-gadget-source="' + gadget_data_source +'"></div>'].join('\n');
tab_container.append(html_string);
tab_gadget = tab_container.find(".gadget");
// render new gadget
RenderJs.setReady(false);
RenderJs.loadGadgetFromUrl(tab_gadget);
// clear previous events
RenderJs.GadgetIndex.getRootGadget().getDom().bind("ready", function (){
if (!is_ready){
RenderJs.updateGadgetData(tab_gadget);
is_ready = true;
}
});
return tab_gadget;
},
Cache : (function() {
/*
* Generic cache implementation that can fall back to local namespace storage
* if no "modern" storage like localStorage is available
*/
return {
ROOT_CACHE_ID: 'APP_CACHE',
getCacheId: function (cache_id) {
/* We should have a way to 'purge' localStorage by setting a ROOT_CACHE_ID in all browser
* instances
*/
return this.ROOT_CACHE_ID + cache_id;
},
hasLocalStorage: function() {
/*
* Feature test if localStorage is supported
*/
var mod;
mod = 'localstorage_test_12345678';
try {
localStorage.setItem(mod, mod);
localStorage.removeItem(mod);
return true;
}
catch (e) {
return false;
}
},
get: function (cache_id, default_value) { addGadget: function(dom_id, gadget, gadget_data_handler,
/* Get cache key value */ gadget_data_source) {
cache_id = this.getCacheId(cache_id); /*
if (this.hasLocalStorage()) { * add new gadget and render it
return this.LocalStorageCachePlugin.get(cache_id, default_value); */
} var html_string;
//fallback to javscript namespace cache tab_container = $('#'+dom_id);
return this.NameSpaceStorageCachePlugin.get(cache_id, default_value); tab_container.empty();
}, html_string = [
'<div class="gadget" ',
'data-gadget="' + gadget + '"',
'data-gadget-handler="' + gadget_data_handler + '" ',
'data-gadget-source="' + gadget_data_source +'"></div>'
].join('\n');
tab_container.append(html_string);
tab_gadget = tab_container.find(".gadget");
// render new gadget
RenderJs.setReady(false);
RenderJs.loadGadgetFromUrl(tab_gadget);
// clear previous events
RenderJs.GadgetIndex.getRootGadget().getDom().bind(
"ready", function (){
if (!is_ready){
RenderJs.updateGadgetData(tab_gadget);
is_ready = true;
}
}
);
return tab_gadget;
},
set: function (cache_id, data) { Cache: (function() {
/* Set cache key value */ /*
cache_id = this.getCacheId(cache_id); * Generic cache implementation that can fall back to local
if (this.hasLocalStorage()){ * namespace storage if no "modern" storage like localStorage
this.LocalStorageCachePlugin.set(cache_id, data);} * is available
else{ */
this.NameSpaceStorageCachePlugin.set(cache_id, data);} return {
}, ROOT_CACHE_ID: 'APP_CACHE',
getCacheId: function (cache_id) {
/*
* We should have a way to 'purge' localStorage by setting a
* ROOT_CACHE_ID in all browser instances
*/
return this.ROOT_CACHE_ID + cache_id;
},
LocalStorageCachePlugin : (function() { hasLocalStorage: function() {
/*
* This plugin saves using HTML5 localStorage.
*/
return {
get: function (cache_id, default_value) {
/* Get cache key value */
return $.jStorage.get(cache_id, default_value);
},
set: function (cache_id, data) {
/* Set cache key value */
$.jStorage.set(cache_id, data);
}
}}()),
NameSpaceStorageCachePlugin: (function() {
/*
* This plugin saves within current page namespace.
*/
var namespace = {};
return {
get: function (cache_id, default_value) {
/* Get cache key value */
return namespace[cache_id];
},
set: function (cache_id, data) {
/* Set cache key value */
namespace[cache_id] = data;
}
}}())
}}()),
Gadget: ( function (id, dom) {
/*
* Javascript Gadget representation
*/
this.id = id;
this.dom = dom;
this.is_ready = false;
this.getId = function() {
return this.id;
};
this.getDom = function() {
return this.dom;
};
this.isReady = function() {
/* /*
* Return True if remote gadget is loaded into DOM. * Feature test if localStorage is supported
*/ */
return this.is_ready; var mod;
}; mod = 'localstorage_test_12345678';
try {
localStorage.setItem(mod, mod);
localStorage.removeItem(mod);
return true;
} catch (e) {
return false;
}
},
get: function (cache_id, default_value) {
/* Get cache key value */
cache_id = this.getCacheId(cache_id);
if (this.hasLocalStorage()) {
return this.LocalStorageCachePlugin.
get(cache_id, default_value);
}
//fallback to javscript namespace cache
return this.NameSpaceStorageCachePlugin.
get(cache_id, default_value);
},
set: function (cache_id, data) {
/* Set cache key value */
cache_id = this.getCacheId(cache_id);
if (this.hasLocalStorage()) {
this.LocalStorageCachePlugin.set(cache_id, data);
} else {
this.NameSpaceStorageCachePlugin.set(cache_id, data);
}
},
this.setReady = function() { LocalStorageCachePlugin: (function() {
/* /*
* Return True if remote gadget is loaded into DOM. * This plugin saves using HTML5 localStorage.
*/ */
this.is_ready = true; return {
}; get: function (cache_id, default_value) {
}), /* Get cache key value */
return $.jStorage.get(cache_id, default_value);
TabbularGadget : (function () {
/*
* Generic tabular gadget
*/
return {
toggleVisibility: function(visible_dom) {
/*
* Set tab as active visually and mark as not active rest.
*/
$(".selected").addClass("not_selected"); $(".selected").removeClass("selected");
visible_dom.addClass("selected");
visible_dom.removeClass("not_selected");
}, },
addNewTabGadget: function(dom_id, gadget, gadget_data_handler, gadget_data_source) { set: function (cache_id, data) {
/* /* Set cache key value */
* add new gadget and render it $.jStorage.set(cache_id, data);
*/ }
tab_gadget = RenderJs.addGadget(dom_id, gadget, gadget_data_handler, gadget_data_source) }
}()),
// XXX: we should unregister all gadgets (if any we replace now in DOM) NameSpaceStorageCachePlugin: (function() {
/*
* This plugin saves within current page namespace.
*/
var namespace = {};
return {
get: function (cache_id, default_value) {
/* Get cache key value */
return namespace[cache_id];
},
set: function (cache_id, data) {
/* Set cache key value */
namespace[cache_id] = data;
} }
}}()), }}())
}}()),
GadgetIndex : (function () { Gadget: (function (id, dom) {
/* /*
* Generic gadget index placeholder * Javascript Gadget representation
*/ */
var gadget_list = []; this.id = id;
this.dom = dom;
this.is_ready = false;
this.getId = function() {
return this.id;
};
this.getDom = function() {
return this.dom;
};
this.isReady = function() {
/*
* Return True if remote gadget is loaded into DOM.
*/
return this.is_ready;
};
this.setReady = function() {
/*
* Return True if remote gadget is loaded into DOM.
*/
this.is_ready = true;
};
}),
TabbularGadget: (function () {
/*
* Generic tabular gadget
*/
return {
toggleVisibility: function(visible_dom) {
/*
* Set tab as active visually and mark as not active rest.
*/
$(".selected").addClass("not_selected");
$(".selected").removeClass("selected");
visible_dom.addClass("selected");
visible_dom.removeClass("not_selected");
},
addNewTabGadget: function(dom_id, gadget, gadget_data_handler,
gadget_data_source) {
/*
* add new gadget and render it
*/
tab_gadget = RenderJs.addGadget(
dom_id, gadget, gadget_data_handler, gadget_data_source
);
// XXX: we should unregister all gadgets (if any we replace now in DOM)
}
}
}()),
return { GadgetIndex: (function () {
/*
* Generic gadget index placeholder
*/
var gadget_list = [];
getGadgetList: function() { return {
/* getGadgetList: function() {
* Return list of registered gadgets /*
*/ * Return list of registered gadgets
return gadget_list; */
}, return gadget_list;
},
registerGadget: function(gadget) { registerGadget: function(gadget) {
/* /*
* Register gadget * Register gadget
*/ */
gadget_list.push(gadget); gadget_list.push(gadget);
}, },
unregisterGadget: function(gadget) { unregisterGadget: function(gadget) {
/* /*
* Unregister gadget * Unregister gadget
*/ */
var index = $.inArray(gadget, gadget_list); var index = $.inArray(gadget, gadget_list);
if (index!==-1) { if (index !== -1) {
gadget_list.splice(index, 1); gadget_list.splice(index, 1);
} }
}, },
getGadgetById: function (gadget_id) { getGadgetById: function (gadget_id) {
/* /*
* Get gadget javascript representation by its Id * Get gadget javascript representation by its Id
*/ */
var gadget; var gadget;
gadget = undefined; gadget = undefined;
$(RenderJs.GadgetIndex.getGadgetList()).each( $(RenderJs.GadgetIndex.getGadgetList()).each(
function (index, value) { function (index, value) {
if (value.getId()===gadget_id) { if (value.getId() === gadget_id) {
gadget = value; gadget = value;
}}); }
return gadget; }
}, );
return gadget;
},
getRootGadget: function () { getRootGadget: function () {
/* /*
* Return root gadget (always first one in list) * Return root gadget (always first one in list)
*/ */
return this.getGadgetList()[0]; return this.getGadgetList()[0];
}, },
isGadgetListLoaded: function () { isGadgetListLoaded: function () {
/* /*
* Return True if all gadgets were loaded from network or cache * Return True if all gadgets were loaded from network or
*/ * cache
var result; */
result = true; var result;
$(this.getGadgetList()).each( result = true;
function (index, value) { $(this.getGadgetList()).each(
if (value.isReady()===false) { function (index, value) {
if (value.isReady() === false) {
result = false; result = false;
} }
});
return result;
},
getSelf: function (){
/*
* Return Gadget's Javascript representation
*/
// XXX:
} }
);
return result;
},
}}()), getSelf: function (){
/*
InteractionGadget : (function () { * Return Gadget's Javascript representation
/* */
* Basic gadget interaction gadget implementation. // XXX:
*/ }
return { }
}()),
bind: function (gadget_dom){
/* InteractionGadget : (function () {
* Bind event between gadgets. /*
*/ * Basic gadget interaction gadget implementation.
gadget_id = gadget_dom.attr("id"); */
return {
var createMethodInteraction = function (original_source_method_id, source_gadget_id, source_method_id, bind: function (gadget_dom){
destination_gadget_id, destination_method_id) { /*
var interaction = function() { * Bind event between gadgets.
RenderJs.GadgetIndex.getGadgetById(source_gadget_id)[original_source_method_id].apply(null, arguments); */
RenderJs.GadgetIndex.getGadgetById(destination_gadget_id)[destination_method_id](); var createMethodInteraction = function (
}; original_source_method_id, source_gadget_id,
return interaction; source_method_id, destination_gadget_id,
destination_method_id) {
var interaction = function() {
RenderJs.GadgetIndex.getGadgetById(
source_gadget_id)[original_source_method_id].
apply(null, arguments);
RenderJs.GadgetIndex.getGadgetById(
destination_gadget_id)[destination_method_id]();
}; };
var createTriggerInteraction = function (destination_gadget_id, destination_method_id) { return interaction;
var interaction = function() { },
RenderJs.GadgetIndex.getGadgetById(destination_gadget_id)[destination_method_id].apply(null, arguments); createTriggerInteraction = function (
}; destination_gadget_id, destination_method_id) {
return interaction; var interaction = function () {
RenderJs.GadgetIndex.getGadgetById(
destination_gadget_id)[destination_method_id].
apply(null, arguments);
}; };
gadget_dom.find("connect").each(function (key, value){ return interaction;
var source, source_gadget_id, source_method_id, source_gadget, destination, destination_gadget_id, };
destination_method_id, destination_gadget, func_body, func; gadget_id = gadget_dom.attr("id");
source = $(value).attr("source").split("."); gadget_dom.find("connect").each(function (key, value) {
source_gadget_id = source[0]; var source, source_gadget_id, source_method_id,
source_method_id = source[1]; source_gadget, destination, destination_gadget_id,
source_gadget = RenderJs.GadgetIndex.getGadgetById(source_gadget_id); destination_method_id, destination_gadget,
original_source_method_id,
destination = $(value).attr("destination").split("."); func_body, func;
destination_gadget_id = destination[0]; source = $(value).attr("source").split(".");
destination_method_id = destination[1]; source_gadget_id = source[0];
destination_gadget = RenderJs.GadgetIndex.getGadgetById(destination_gadget_id); source_method_id = source[1];
source_gadget = RenderJs.GadgetIndex.
if (source_gadget.hasOwnProperty(source_method_id)){ getGadgetById(source_gadget_id);
destination = $(value).attr("destination").split(".");
destination_gadget_id = destination[0];
destination_method_id = destination[1];
destination_gadget = RenderJs.GadgetIndex.
getGadgetById(destination_gadget_id);
if (source_gadget.hasOwnProperty(source_method_id)){
// direct javascript use case // direct javascript use case
var original_source_method_id = "original_" + source_method_id; original_source_method_id = "original_" +
source_gadget[original_source_method_id] = source_gadget[source_method_id]; source_method_id;
source_gadget[source_method_id] = createMethodInteraction(original_source_method_id, source_gadget_id, source_method_id, source_gadget[original_source_method_id] =
destination_gadget_id, destination_method_id); source_gadget[source_method_id];
} source_gadget[source_method_id] =
else{ createMethodInteraction(
// this is a custom event attached to HTML gadget representation original_source_method_id,
var interaction = createTriggerInteraction(destination_gadget_id, destination_method_id); source_gadget_id,
source_gadget.dom.bind(source_method_id, interaction); source_method_id,
} destination_gadget_id,
destination_method_id
);
} }
); else{
} // this is a custom event attached to HTML gadget
}}()) // representation
source_gadget.dom.bind(
}}()); source_method_id,
createTriggerInteraction(
destination_gadget_id, destination_method_id
)
);
}
});
}
}
}())
}
}());
// impliticly call RenderJs bootstrap // impliticly call RenderJs bootstrap
$(document).ready(function() { $(document).ready(function() {
if (RENDERJS_ENABLE_IMPLICIT_GADGET_RENDERING){ if (RENDERJS_ENABLE_IMPLICIT_GADGET_RENDERING){
RenderJs.bootstrap($('body')); RenderJs.bootstrap($('body'));
} }
}); });
\ 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