[Kimchi-devel] [PATCH] [Wok 6/7] Cache plugins information on UI to avoid multiple requests

Aline Manera alinefm at linux.vnet.ibm.com
Mon Feb 13 13:57:03 UTC 2017


Plugins information does not change a lot and once changed the UI needs
to reload the browser session to rebuild the UI, so there is no need to
do multiple requests to get the plugin information.

A single request would be enough to all matters.

So do it and update the code according to that change.

Signed-off-by: Aline Manera <alinefm at linux.vnet.ibm.com>
---
 ui/js/src/wok.logos.js | 79 ++++++++++++++++++++++++++-------------------
 ui/js/src/wok.main.js  | 87 ++++++++++++++++++++++++++------------------------
 ui/js/wok.user-log.js  | 45 ++++++++++++++------------
 3 files changed, 116 insertions(+), 95 deletions(-)

diff --git a/ui/js/src/wok.logos.js b/ui/js/src/wok.logos.js
index b4e2d75..1abab84 100644
--- a/ui/js/src/wok.logos.js
+++ b/ui/js/src/wok.logos.js
@@ -16,6 +16,15 @@
  * limitations under the License.
  */
 
+
+wok.plugins = undefined;
+wok.listPlugins(function(result) {
+    wok.plugins = result;
+}, function(data) {
+    wok.plugins = [];
+    wok.message.error(data.responseJSON.reason);
+});
+
 wok.logos = function(element, powered) {
     powered = (typeof powered === 'undefined') ? false : true;
     var genLogos  = function(obj){
@@ -73,44 +82,48 @@ wok.logos = function(element, powered) {
 
     var pluginUrl = 'plugins/{plugin}';
     var buildLogos = function() {
+        // Make wok.plugins is ready to be used
+        if (wok.plugins == undefined) {
+            setTimeout(buildLogos, 2000);
+            return;
+        }
+
         var logos = [];
         var  obj = {};
-        wok.listPlugins(function(plugins) {
-            if(plugins && plugins.length > 0) {
-                $(plugins).each(function(i, p) {
-                    if (p.enabled === false) {
-                        return true;
-                    }
-                    var url = wok.substitute(pluginUrl, {
-                        plugin: p.name
-                    });
-                    obj[i] = {
-                        name : p.name
-                    }
-                    var pluginVersions;
-                    pluginVersions = retrieveVersion(url);
-                    if(pluginVersions && pluginVersions.length > 0){
-                        obj[i].version = pluginVersions;
-                    }
-                    var imagepath = url+'/images/'+p.name;
-                    if(checkImage(imagepath+'.svg') == 200) {
-                        obj[i].image = imagepath+'.svg';
-                    }
-                    else if(checkImage(imagepath+'.png') == 200) {
-                        obj[i].image = imagepath+'.png';
-                    }
+        if(wok.plugins && wok.plugins.length > 0) {
+            $(wok.plugins).each(function(i, p) {
+                if (p.enabled === false) {
+                    return true;
+                }
+                var url = wok.substitute(pluginUrl, {
+                    plugin: p.name
                 });
-                var generatedLogos = genLogos(obj);
-                if(generatedLogos.length > 0) {
-                    $(element).append(generatedLogos);
-                    if(powered) {
-                        $(element).parentsUntil('.container').find('.powered').removeClass('hidden');
-                    }else {
-                        $(element).parentsUntil('.container').find('.powered').remove();
-                    }
+                obj[i] = {
+                    name : p.name
+                }
+                var pluginVersions;
+                pluginVersions = retrieveVersion(url);
+                if(pluginVersions && pluginVersions.length > 0){
+                    obj[i].version = pluginVersions;
+                }
+                var imagepath = url+'/images/'+p.name;
+                if(checkImage(imagepath+'.svg') == 200) {
+                    obj[i].image = wok.plugins[i].image = imagepath+'.svg';
+                }
+                else if(checkImage(imagepath+'.png') == 200) {
+                    obj[i].image = wok.plugins[i].image =  imagepath+'.png';
+                }
+            });
+            var generatedLogos = genLogos(obj);
+            if(generatedLogos.length > 0) {
+                $(element).append(generatedLogos);
+                if(powered) {
+                    $(element).parentsUntil('.container').find('.powered').removeClass('hidden');
+                }else {
+                    $(element).parentsUntil('.container').find('.powered').remove();
                 }
             }
-        });
+        }
     };
     buildLogos();
 
diff --git a/ui/js/src/wok.main.js b/ui/js/src/wok.main.js
index 6de8ea0..20c017e 100644
--- a/ui/js/src/wok.main.js
+++ b/ui/js/src/wok.main.js
@@ -139,55 +139,58 @@ wok.main = function() {
     var pluginI18nUrl = 'plugins/{plugin}/i18n.json';
     var DEFAULT_HASH;
     var buildTabs = function(callback) {
+        // Make wok.plugins is ready to be used
+        if (wok.plugins == undefined) {
+            setTimeout(buildTabs, 2000);
+            return;
+        }
+
         var tabs = retrieveTabs('wok', wokConfigUrl);
-        wok.listPlugins(function(plugins) {
-            $(plugins).each(function(i, p) {
-                if (p.enabled === false) {
-                    return true;
-                }
+        var plugins = wok.plugins;
+        $(plugins).each(function(i, p) {
+            if (p.enabled === false) {
+                return true;
+            }
 
-                var url = wok.substitute(pluginConfigUrl, {
-                    plugin: p.name
-                });
-                var i18nUrl = wok.substitute(pluginI18nUrl, {
-                    plugin: p.name
-                });
-                wok.getI18n(function(i18nObj){ $.extend(i18n, i18nObj)},
-                            function(i18nObj){ //i18n is not define by plugin
-                            }, i18nUrl, true);
-                var pluginTabs = retrieveTabs(p.name, url);
-                if(pluginTabs.length > 0){
-                    tabs.push.apply(tabs, pluginTabs);
-                }
+            var url = wok.substitute(pluginConfigUrl, {
+                plugin: p.name
             });
-
-            //sort second level tab based on their ordering number
-            var orderedTabs = tabs.slice(0);
-            orderedTabs.sort(function(a, b) {
-                return a.order - b.order;
+            var i18nUrl = wok.substitute(pluginI18nUrl, {
+                plugin: p.name
             });
-            //redirect to empty page when no plugin installed
-            if(tabs.length===0){
-                DEFAULT_HASH = 'wok-empty';
-            } else {
-                var defaultTab = orderedTabs[0]
-                var defaultTabPath = defaultTab && defaultTab['path']
+            wok.getI18n(function(i18nObj){ $.extend(i18n, i18nObj)},
+                        function(i18nObj){ //i18n is not define by plugin
+                        }, i18nUrl, true);
+            var pluginTabs = retrieveTabs(p.name, url);
+            if(pluginTabs.length > 0){
+                tabs.push.apply(tabs, pluginTabs);
+            }
+        });
 
-                // Remove file extension from 'defaultTabPath'
-                DEFAULT_HASH = defaultTabPath &&
-                    defaultTabPath.substring(0, defaultTabPath.lastIndexOf('.'))
-                }
+        //sort second level tab based on their ordering number
+        var orderedTabs = tabs.slice(0);
+        orderedTabs.sort(function(a, b) {
+            return a.order - b.order;
+        });
+        //redirect to empty page when no plugin installed
+        if(tabs.length===0){
+            DEFAULT_HASH = 'wok-empty';
+        } else {
+            var defaultTab = orderedTabs[0]
+            var defaultTabPath = defaultTab && defaultTab['path']
+
+            // Remove file extension from 'defaultTabPath'
+            DEFAULT_HASH = defaultTabPath &&
+                defaultTabPath.substring(0, defaultTabPath.lastIndexOf('.'))
+        }
 
-                genTabs(orderedTabs);
-                wok.getHostname();
-                wok.logos('ul#plugins',true);
-                wok.logos('ul#wok-about',false);
+        genTabs(orderedTabs);
+        wok.getHostname();
+        wok.logos('ul#plugins',true);
+        wok.logos('ul#wok-about',false);
 
-                callback && callback();
-            }, function(data) {
-               wok.message.error(data.responseJSON.reason);
-            }, true);
-    };
+        callback && callback();
+    }
 
     var onLanguageChanged = function(lang) {
         wok.lang.set(lang);
diff --git a/ui/js/wok.user-log.js b/ui/js/wok.user-log.js
index 80e67e1..0e8fb09 100644
--- a/ui/js/wok.user-log.js
+++ b/ui/js/wok.user-log.js
@@ -156,6 +156,12 @@ wok.initUserLogContent = function() {
 };
 
 wok.initUserLogWindow = function() {
+    // Make wok.plugins is ready to be used
+    if (wok.plugins == undefined) {
+        setTimeout(wok.initUserLogWindow, 2000);
+        return;
+    }
+
     var currentLocale = wok.lang.get_locale();
     currentLocale = currentLocale.substring(0, currentLocale.indexOf('-'));
     $("#request-type").selectpicker();
@@ -172,26 +178,25 @@ wok.initUserLogWindow = function() {
         }
     });
     var pluginsData = [];
-    wok.listPlugins(function(pluginReturn) {
-        $.each(pluginReturn, function(i, obj) {
-            pluginsData.push({"app": obj});
-        });
-        pluginsData.unshift({"app": "wok"});
-        var pluginsTt = new Bloodhound({
-            datumTokenizer: Bloodhound.tokenizers.obj.whitespace('app'),
-            queryTokenizer: Bloodhound.tokenizers.whitespace,
-            local: pluginsData
-        });
-        pluginsTt.initialize();
-
-        $('.typeahead').typeahead(
-            {
-                autoselect:  false
-            }, {
-                name: 'application-name',
-                displayKey: 'app',
-                source: pluginsTt.ttAdapter()
-        });
+    var pluginReturn = wok.plugins;
+    $.each(pluginReturn, function(i, obj) {
+        pluginsData.push({"app": obj});
+    });
+    pluginsData.unshift({"app": "wok"});
+    var pluginsTt = new Bloodhound({
+         datumTokenizer: Bloodhound.tokenizers.obj.whitespace('app'),
+         queryTokenizer: Bloodhound.tokenizers.whitespace,
+         local: pluginsData
+    });
+    pluginsTt.initialize();
+
+    $('.typeahead').typeahead(
+        {
+            autoselect:  false
+        }, {
+            name: 'application-name',
+            displayKey: 'app',
+            source: pluginsTt.ttAdapter()
     });
 
     $('#form-advanced-search').submit(function(event) {
-- 
2.9.3



More information about the Kimchi-devel mailing list