Commit 4a0db3ff authored by Ivan Tyagov's avatar Ivan Tyagov

Use only one global RenderJs variable to access different modules instead of...

Use only one global RenderJs variable to access different modules instead of many global variable here and there.
parent 43c8d0b5
......@@ -12,7 +12,7 @@
</item>
<item>
<key> <string>_EtagSupport__etag</string> </key>
<value> <string>ts37261763.42</string> </value>
<value> <string>ts37323241.04</string> </value>
</item>
<item>
<key> <string>__name__</string> </key>
......@@ -35,146 +35,6 @@ if (typeof console === "undefined" || typeof console.log === "undefined") {\n
var is_ready;\n
is_ready = false; // XXX: dirty flag to be removed (indicates if ready event has been handled)\n
\n
\n
\n
/*\n
* Generic cache implementation that can fall back to local namespace storage\n
* if no "modern" storage like localStorage is available\n
*/ \n
var Cache = (function() {\n
\n
return {\n
\n
ROOT_CACHE_ID: \'APP_CACHE\',\n
\n
getCacheId: function (cache_id) {\n
/* We should have a way to \'purge\' localStorage by setting a ROOT_CACHE_ID in all browser\n
* instances\n
*/\n
return this.ROOT_CACHE_ID + cache_id;\n
},\n
\n
hasLocalStorage: function() {\n
/*\n
* Feature test if localStorage is supported\n
*/\n
var mod;\n
mod = \'localstorage_test_12345678\';\n
try {\n
localStorage.setItem(mod, mod);\n
localStorage.removeItem(mod);\n
return true;\n
}\n
catch (e) {\n
return false;\n
}\n
},\n
\n
get: function (cache_id, default_value) {\n
/* Get cache key value */\n
cache_id = this.getCacheId(cache_id);\n
if (this.hasLocalStorage()) {\n
return this.LocalStorageCachePlugin.get(cache_id, default_value);\n
}\n
//fallback to javscript namespace cache\n
return this.NameSpaceStorageCachePlugin.get(cache_id, default_value);\n
},\n
\n
set: function (cache_id, data) {\n
/* Set cache key value */\n
cache_id = this.getCacheId(cache_id);\n
if (this.hasLocalStorage()){\n
this.LocalStorageCachePlugin.set(cache_id, data);}\n
else{\n
this.NameSpaceStorageCachePlugin.set(cache_id, data);}\n
},\n
\n
LocalStorageCachePlugin : (function() {\n
/*\n
* This plugin saves using HTML5 localStorage.\n
*/\n
return {\n
get: function (cache_id, default_value) {\n
/* Get cache key value */\n
return $.jStorage.get(cache_id, default_value);\n
},\n
\n
set: function (cache_id, data) {\n
/* Set cache key value */\n
$.jStorage.set(cache_id, data);\n
}\n
}}()),\n
\n
NameSpaceStorageCachePlugin: (function() {\n
/*\n
* This plugin saves within current page namespace.\n
*/\n
\n
var namespace = {};\n
\n
return {\n
get: function (cache_id, default_value) {\n
/* Get cache key value */\n
return namespace[cache_id];\n
},\n
\n
set: function (cache_id, data) {\n
/* Set cache key value */\n
namespace[cache_id] = data;\n
}\n
\n
}}())\n
\n
}}());\n
\n
/*\n
* Generic tabular gadget\n
*/\n
var TabbularGadget = (function () {\n
\n
return {\n
\n
toggleVisibility: function(visible_dom) {\n
/*\n
* Set tab as active visually and mark as not active rest.\n
*/\n
$(".selected").addClass("not_selected"); $(".selected").removeClass("selected");\n
visible_dom.addClass("selected");\n
visible_dom.removeClass("not_selected");\n
},\n
\n
addNewTabGadget: function(dom_id, gadget, gadget_data_handler, gadget_data_source) {\n
/*\n
* add new gadget and render it\n
*/\n
var html_string;\n
tab_container=$(\'#\'+dom_id);\n
tab_container.empty();\n
html_string =[\'<div class="gadget" \',\n
\'gadget="\' + gadget + \'"\',\n
\'gadget:data-handler="\' + gadget_data_handler + \'" \',\n
\'gadget:data-source="\' + gadget_data_source +\'"></div>\'].join(\'\\n\');\n
\n
tab_container.append(html_string);\n
tab_gadget = tab_container.find(".gadget");\n
\n
// XXX: we should unregister all gadgets (if any we replace now in DOM)\n
\n
// render new gadget\n
is_ready = false;\n
RenderJs.loadGadgetFromUrl(tab_gadget);\n
// clear previous events\n
GadgetIndex.getRootGadget().getDom().bind("ready", function (){\n
if (!is_ready){\n
RenderJs.updateGadgetData(tab_gadget);\n
is_ready = true;\n
}\n
});\n
}\n
\n
}}());\n
\n
\n
\n
/*\n
* Javascript Gadget representation\n
......@@ -216,131 +76,6 @@ Gadget.prototype.getParent = function() {\n
};\n
\n
\n
/*\n
* Generic gadget index placeholder\n
*/\n
var GadgetIndex = (function () {\n
\n
var gadget_list = [];\n
\n
return {\n
\n
getGadgetList: function() {\n
/*\n
* Return list of registered gadgets\n
*/\n
return gadget_list;\n
},\n
\n
registerGadget: function(gadget) {\n
/*\n
* Register gadget\n
*/\n
gadget_list.push(gadget);\n
},\n
\n
unregisterGadget: function(gadget) {\n
/*\n
* Unregister gadget\n
*/\n
var index = $.inArray(gadget, gadget_list);\n
if (index!==-1) {\n
gadget_list.splice(index, 1);\n
}\n
},\n
\n
getGadgetById: function (gadget_id) {\n
/*\n
* Get gadget javascript representation by its Id\n
*/\n
var gadget;\n
gadget = undefined;\n
$(GadgetIndex.getGadgetList()).each(\n
function (index, value) {\n
if (value.getId()===gadget_id) {\n
gadget = value;\n
}});\n
return gadget;\n
},\n
\n
getRootGadget: function () {\n
/*\n
* Return root gadget (always first one in list)\n
*/\n
return this.getGadgetList()[0];\n
},\n
\n
isGadgetListLoaded: function () {\n
/*\n
* Return True if all gadgets were loaded from network or cache\n
*/\n
var result;\n
result = true;\n
$(this.getGadgetList()).each(\n
function (index, value) {\n
if (value.isReady()===false) {\n
result = false;\n
}\n
});\n
return result;\n
},\n
\n
getSelf: function (){\n
/*\n
* Return Gadget\'s Javascript representation\n
*/\n
// XXX:\n
}\n
\n
}}());\n
\n
\n
/*\n
* Basic gadget interaction gadget implementation.\n
*/\n
var InteractionGadget = (function () {\n
\n
return {\n
\n
bind: function (gadget_dom){\n
/*\n
* Bind event between gadgets.\n
*/\n
gadget_id = gadget_dom.attr("id");\n
gadget_dom.find("connect").each(function (key, value){\n
var source, source_gadget_id, source_method_id, source_gadget, destination, destination_gadget_id,\n
destination_method_id, destination_gadget, func_body, func;\n
source = $(value).attr("source").split(".");\n
source_gadget_id = source[0];\n
source_method_id = source[1];\n
source_gadget = GadgetIndex.getGadgetById(source_gadget_id);\n
\n
destination = $(value).attr("destination").split(".");\n
destination_gadget_id = destination[0];\n
destination_method_id = destination[1];\n
destination_gadget = GadgetIndex.getGadgetById(destination_gadget_id);\n
\n
if (source_gadget.hasOwnProperty(source_method_id)){\n
// direct javascript use case\n
func_body = \'GadgetIndex.getGadgetById("\' + source_gadget_id + \'")["original_\' + source_method_id + \'"]();\';\n
func_body = func_body + \'\\nGadgetIndex.getGadgetById("\' + destination_gadget_id + \'")["\' + destination_method_id + \'"]();\';\n
func = new Function(func_body);\n
source_gadget["original_" + source_method_id] = source_gadget[source_method_id];\n
source_gadget[source_method_id] = func;\n
}\n
else{\n
// this is a custom event attached to HTML gadget representation\n
func_body = \'GadgetIndex.getGadgetById("\' + destination_gadget_id + \'")["\' + destination_method_id + \'"]();\';\n
func = new Function(func_body);\n
source_gadget.dom.bind(source_method_id, func);\n
}\n
console.log(source_gadget_id, \'.\', source_method_id, \'-->\', destination_gadget_id, \'.\', destination_method_id);\n
}\n
);\n
}\n
}}());\n
\n
\n
/*\n
* Generic Gadget library renderer\n
*/\n
......@@ -380,7 +115,7 @@ var RenderJs = (function () {\n
\n
// register gadget in javascript namespace\n
gadget_js = new Gadget(gadget_id, gadget);\n
GadgetIndex.registerGadget(gadget_js);\n
RenderJs.GadgetIndex.registerGadget(gadget_js);\n
\n
if (url!==undefined && url!==""){\n
gadget_property = gadget.attr("gadget:property");\n
......@@ -393,7 +128,7 @@ var RenderJs = (function () {\n
if (cacheable) {\n
// get from cache if possible, use last part from URL as cache_key\n
cache_id = gadget_property.cache_id;\n
app_cache = Cache.get(cache_id, undefined);\n
app_cache = RenderJs.Cache.get(cache_id, undefined);\n
if(app_cache===undefined || app_cache===null){\n
// not in cache so we pull from network and cache\n
//console.log("not in cache: " + cache_id + " " + url);\n
......@@ -403,8 +138,8 @@ var RenderJs = (function () {\n
cache_id = this.yourCustomData.cache_id;\n
gadget_id = this.yourCustomData.gadget_id;\n
//console.log("set in cache: " + cache_id);\n
Cache.set(cache_id, data);\n
GadgetIndex.getGadgetById(gadget_id).setReady();\n
RenderJs.Cache.set(cache_id, data);\n
RenderJs.GadgetIndex.getGadgetById(gadget_id).setReady();\n
RenderJs.updateAndRecurse(gadget, data);\n
RenderJs.checkAndTriggerReady();\n
}});\n
......@@ -424,7 +159,7 @@ var RenderJs = (function () {\n
yourCustomData: {"gadget_id": gadget_id},\n
success: function (data) {\n
gadget_id = this.yourCustomData.gadget_id;\n
GadgetIndex.getGadgetById(gadget_id).setReady();\n
RenderJs.GadgetIndex.getGadgetById(gadget_id).setReady();\n
RenderJs.updateAndRecurse(gadget, data);\n
RenderJs.checkAndTriggerReady();\n
}});\n
......@@ -442,11 +177,11 @@ var RenderJs = (function () {\n
* Trigger "ready" event only if all gadgets were marked as "ready"\n
*/\n
var is_gadget_list_loaded;\n
is_gadget_list_loaded = GadgetIndex.isGadgetListLoaded();\n
is_gadget_list_loaded = RenderJs.GadgetIndex.isGadgetListLoaded();\n
if (is_gadget_list_loaded){\n
if (!is_ready) {\n
//console.log("trigger");\n
GadgetIndex.getRootGadget().getDom().trigger("ready");\n
RenderJs.GadgetIndex.getRootGadget().getDom().trigger("ready");\n
}\n
is_ready = true;\n
}\n
......@@ -477,7 +212,265 @@ var RenderJs = (function () {\n
dataType: "json",\n
yourCustomData: {"data_handler": data_handler},\n
success: RenderJs.updateGadgetWithDataHandler});}\n
}\n
},\n
\n
Cache : (function() {\n
/*\n
* Generic cache implementation that can fall back to local namespace storage\n
* if no "modern" storage like localStorage is available\n
*/\n
\n
return {\n
\n
ROOT_CACHE_ID: \'APP_CACHE\',\n
\n
getCacheId: function (cache_id) {\n
/* We should have a way to \'purge\' localStorage by setting a ROOT_CACHE_ID in all browser\n
* instances\n
*/\n
return this.ROOT_CACHE_ID + cache_id;\n
},\n
\n
hasLocalStorage: function() {\n
/*\n
* Feature test if localStorage is supported\n
*/\n
var mod;\n
mod = \'localstorage_test_12345678\';\n
try {\n
localStorage.setItem(mod, mod);\n
localStorage.removeItem(mod);\n
return true;\n
}\n
catch (e) {\n
return false;\n
}\n
},\n
\n
get: function (cache_id, default_value) {\n
/* Get cache key value */\n
cache_id = this.getCacheId(cache_id);\n
if (this.hasLocalStorage()) {\n
return this.LocalStorageCachePlugin.get(cache_id, default_value);\n
}\n
//fallback to javscript namespace cache\n
return this.NameSpaceStorageCachePlugin.get(cache_id, default_value);\n
},\n
\n
set: function (cache_id, data) {\n
/* Set cache key value */\n
cache_id = this.getCacheId(cache_id);\n
if (this.hasLocalStorage()){\n
this.LocalStorageCachePlugin.set(cache_id, data);}\n
else{\n
this.NameSpaceStorageCachePlugin.set(cache_id, data);}\n
},\n
\n
LocalStorageCachePlugin : (function() {\n
/*\n
* This plugin saves using HTML5 localStorage.\n
*/\n
return {\n
get: function (cache_id, default_value) {\n
/* Get cache key value */\n
return $.jStorage.get(cache_id, default_value);\n
},\n
\n
set: function (cache_id, data) {\n
/* Set cache key value */\n
$.jStorage.set(cache_id, data);\n
}\n
}}()),\n
\n
NameSpaceStorageCachePlugin: (function() {\n
/*\n
* This plugin saves within current page namespace.\n
*/\n
\n
var namespace = {};\n
\n
return {\n
get: function (cache_id, default_value) {\n
/* Get cache key value */\n
return namespace[cache_id];\n
},\n
\n
set: function (cache_id, data) {\n
/* Set cache key value */\n
namespace[cache_id] = data;\n
}\n
\n
}}())\n
\n
}}()),\n
\n
TabbularGadget : (function () {\n
/*\n
* Generic tabular gadget\n
*/\n
return {\n
\n
toggleVisibility: function(visible_dom) {\n
/*\n
* Set tab as active visually and mark as not active rest.\n
*/\n
$(".selected").addClass("not_selected"); $(".selected").removeClass("selected");\n
visible_dom.addClass("selected");\n
visible_dom.removeClass("not_selected");\n
},\n
\n
addNewTabGadget: function(dom_id, gadget, gadget_data_handler, gadget_data_source) {\n
/*\n
* add new gadget and render it\n
*/\n
var html_string;\n
tab_container=$(\'#\'+dom_id);\n
tab_container.empty();\n
html_string =[\'<div class="gadget" \',\n
\'gadget="\' + gadget + \'"\',\n
\'gadget:data-handler="\' + gadget_data_handler + \'" \',\n
\'gadget:data-source="\' + gadget_data_source +\'"></div>\'].join(\'\\n\');\n
\n
tab_container.append(html_string);\n
tab_gadget = tab_container.find(".gadget");\n
\n
// XXX: we should unregister all gadgets (if any we replace now in DOM)\n
\n
// render new gadget\n
is_ready = false;\n
RenderJs.loadGadgetFromUrl(tab_gadget);\n
// clear previous events\n
RenderJs.GadgetIndex.getRootGadget().getDom().bind("ready", function (){\n
if (!is_ready){\n
RenderJs.updateGadgetData(tab_gadget);\n
is_ready = true;\n
}\n
});\n
}\n
\n
}}()),\n
\n
\n
GadgetIndex : (function () {\n
/*\n
* Generic gadget index placeholder\n
*/\n
var gadget_list = [];\n
\n
return {\n
\n
getGadgetList: function() {\n
/*\n
* Return list of registered gadgets\n
*/\n
return gadget_list;\n
},\n
\n
registerGadget: function(gadget) {\n
/*\n
* Register gadget\n
*/\n
gadget_list.push(gadget);\n
},\n
\n
unregisterGadget: function(gadget) {\n
/*\n
* Unregister gadget\n
*/\n
var index = $.inArray(gadget, gadget_list);\n
if (index!==-1) {\n
gadget_list.splice(index, 1);\n
}\n
},\n
\n
getGadgetById: function (gadget_id) {\n
/*\n
* Get gadget javascript representation by its Id\n
*/\n
var gadget;\n
gadget = undefined;\n
$(RenderJs.GadgetIndex.getGadgetList()).each(\n
function (index, value) {\n
if (value.getId()===gadget_id) {\n
gadget = value;\n
}});\n
return gadget;\n
},\n
\n
getRootGadget: function () {\n
/*\n
* Return root gadget (always first one in list)\n
*/\n
return this.getGadgetList()[0];\n
},\n
\n
isGadgetListLoaded: function () {\n
/*\n
* Return True if all gadgets were loaded from network or cache\n
*/\n
var result;\n
result = true;\n
$(this.getGadgetList()).each(\n
function (index, value) {\n
if (value.isReady()===false) {\n
result = false;\n
}\n
});\n
return result;\n
},\n
\n
getSelf: function (){\n
/*\n
* Return Gadget\'s Javascript representation\n
*/\n
// XXX:\n
}\n
\n
}}()),\n
\n
InteractionGadget : (function () {\n
/*\n
* Basic gadget interaction gadget implementation.\n
*/\n
return {\n
\n
bind: function (gadget_dom){\n
/*\n
* Bind event between gadgets.\n
*/\n
gadget_id = gadget_dom.attr("id");\n
gadget_dom.find("connect").each(function (key, value){\n
var source, source_gadget_id, source_method_id, source_gadget, destination, destination_gadget_id,\n
destination_method_id, destination_gadget, func_body, func;\n
source = $(value).attr("source").split(".");\n
source_gadget_id = source[0];\n
source_method_id = source[1];\n
source_gadget = RenderJs.GadgetIndex.getGadgetById(source_gadget_id);\n
\n
destination = $(value).attr("destination").split(".");\n
destination_gadget_id = destination[0];\n
destination_method_id = destination[1];\n
destination_gadget = RenderJs.GadgetIndex.getGadgetById(destination_gadget_id);\n
\n
if (source_gadget.hasOwnProperty(source_method_id)){\n
// direct javascript use case\n
func_body = \'RenderJs.GadgetIndex.getGadgetById("\' + source_gadget_id + \'")["original_\' + source_method_id + \'"]();\';\n
func_body = func_body + \'\\nRenderJs.GadgetIndex.getGadgetById("\' + destination_gadget_id + \'")["\' + destination_method_id + \'"]();\';\n
func = new Function(func_body);\n
source_gadget["original_" + source_method_id] = source_gadget[source_method_id];\n
source_gadget[source_method_id] = func;\n
}\n
else{\n
// this is a custom event attached to HTML gadget representation\n
func_body = \'RenderJs.GadgetIndex.getGadgetById("\' + destination_gadget_id + \'")["\' + destination_method_id + \'"]();\';\n
func = new Function(func_body);\n
source_gadget.dom.bind(source_method_id, func);\n
}\n
console.log(source_gadget_id, \'.\', source_method_id, \'-->\', destination_gadget_id, \'.\', destination_method_id);\n
}\n
);\n
}\n
}}())\n
\n
}}());
......@@ -489,7 +482,7 @@ var RenderJs = (function () {\n
</item>
<item>
<key> <string>size</string> </key>
<value> <int>15882</int> </value>
<value> <int>18703</int> </value>
</item>
<item>
<key> <string>title</string> </key>
......
......@@ -8,7 +8,7 @@
<dictionary>
<item>
<key> <string>_EtagSupport__etag</string> </key>
<value> <string>ts37256303.27</string> </value>
<value> <string>ts37322419.16</string> </value>
</item>
<item>
<key> <string>__name__</string> </key>
......@@ -30,40 +30,40 @@ function setupRenderJSTest(){\n
test(\'Cache\', function(){\n
cache_id = \'my_test\';\n
data = {\'gg\':1};\n
Cache.set(cache_id, data);\n
deepEqual(data, Cache.get(cache_id));\n
RenderJs.Cache.set(cache_id, data);\n
deepEqual(data, RenderJs.Cache.get(cache_id));\n
});\n
\n
\n
module("TabularGadget");\n
test(\'addNewTabGadget\', function(){\n
TabbularGadget.addNewTabGadget("qunit-fixture", "Person_view/Form_asRenderJSGadget", "ERP5Form.update", "Form_asJSON?form_id=Person_view");\n
RenderJs.TabbularGadget.addNewTabGadget("qunit-fixture", "Person_view/Form_asRenderJSGadget", "ERP5Form.update", "Form_asJSON?form_id=Person_view");\n
equal($("#qunit-fixture").children(".gadget").length, 1);\n
equal(GadgetIndex.getGadgetList().length, 1);\n
equal(RenderJs.GadgetIndex.getGadgetList().length, 1);\n
\n
});\n
\n
module("GadgetIndex");\n
test(\'GadgetIndex\', function(){\n
// re-init GadgetIndex\n
$.each(GadgetIndex.getGadgetList(), function () {\n
GadgetIndex.unregisterGadget(this);\n
$.each(RenderJs.GadgetIndex.getGadgetList(), function () {\n
RenderJs.GadgetIndex.unregisterGadget(this);\n
});\n
\n
$("#qunit-fixture").append(\'<div gadget="" id="new">XXXXXXXXXXXX</div>\');\n
RenderJs.bootstrap($("#qunit-fixture"));\n
GadgetIndex.getRootGadget().getDom().one("ready", function (){\n
RenderJs.GadgetIndex.getRootGadget().getDom().one("ready", function (){\n
RenderJs.update($("#qunit-fixture"));\n
});\n
equal(GadgetIndex.getGadgetList().length, 2);\n
equal(true, GadgetIndex.isGadgetListLoaded());\n
equal($("#qunit-fixture").attr("id"), GadgetIndex.getRootGadget().getDom().attr("id"));\n
equal(GadgetIndex.getGadgetById("qunit-fixture"), GadgetIndex.getRootGadget());\n
equal(RenderJs.GadgetIndex.getGadgetList().length, 2);\n
equal(true, RenderJs.GadgetIndex.isGadgetListLoaded());\n
equal($("#qunit-fixture").attr("id"), RenderJs.GadgetIndex.getRootGadget().getDom().attr("id"));\n
equal(RenderJs.GadgetIndex.getGadgetById("qunit-fixture"), RenderJs.GadgetIndex.getRootGadget());\n
\n
// unregister gadget\n
GadgetIndex.unregisterGadget(GadgetIndex.getGadgetById("qunit-fixture"));\n
equal(GadgetIndex.getGadgetList().length, 1);\n
equal(GadgetIndex.getGadgetById("new"), GadgetIndex.getRootGadget());\n
RenderJs.GadgetIndex.unregisterGadget(RenderJs.GadgetIndex.getGadgetById("qunit-fixture"));\n
equal(RenderJs.GadgetIndex.getGadgetList().length, 1);\n
equal(RenderJs.GadgetIndex.getGadgetById("new"), RenderJs.GadgetIndex.getRootGadget());\n
\n
\n
});\n
......@@ -80,7 +80,7 @@ function setupRenderJSTest(){\n
</item>
<item>
<key> <string>size</string> </key>
<value> <int>1809</int> </value>
<value> <int>1962</int> </value>
</item>
<item>
<key> <string>title</string> </key>
......
17
\ No newline at end of file
18
\ 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