[Kimchi-devel] [PATCH] Rename kimchi.template to kimchi.substitute

Hongliang Wang hlwang at linux.vnet.ibm.com
Mon May 26 09:46:30 UTC 2014


This function was designed to substitute variables in HTML template strings originally
so it was named as kimchi.template, and it can be used in other scenarios, e.g. we use
it when we load plugins we have a unified URL for plugin XML files:
  /plugins/{plugin}/ui/config/tab-ext.xml
and after we have the plugins list, we can easily generate the corresponding URLs like
  /plugins/pluginA/ui/config/tab-ext.xml,
  /plugins/pluginB/ui/config/tab-ext.xml.
by list ['pluginA', 'pluginB'].
  Though it is sometimes misunderstood to be related with VM templates.
  Renamed it in this patch.

Signed-off-by: Hongliang Wang <hlwang at linux.vnet.ibm.com>
---
 ui/js/src/kimchi.grid.js                 |  2 +-
 ui/js/src/kimchi.guest_add_main.js       |  2 +-
 ui/js/src/kimchi.guest_edit_main.js      |  4 +--
 ui/js/src/kimchi.guest_media_main.js     |  2 +-
 ui/js/src/kimchi.host.js                 |  2 +-
 ui/js/src/kimchi.main.js                 |  2 +-
 ui/js/src/kimchi.network.js              |  2 +-
 ui/js/src/kimchi.storage_main.js         |  4 +--
 ui/js/src/kimchi.storagepool_add_main.js |  2 +-
 ui/js/src/kimchi.substitute.js           | 45 ++++++++++++++++++++++++++++++++
 ui/js/src/kimchi.template.js             | 45 --------------------------------
 ui/js/src/kimchi.template_add_main.js    |  4 +--
 ui/js/src/kimchi.template_edit_main.js   |  2 +-
 ui/js/src/kimchi.template_main.js        |  2 +-
 14 files changed, 60 insertions(+), 60 deletions(-)
 create mode 100644 ui/js/src/kimchi.substitute.js
 delete mode 100644 ui/js/src/kimchi.template.js

diff --git a/ui/js/src/kimchi.grid.js b/ui/js/src/kimchi.grid.js
index 055f487..2c70969 100644
--- a/ui/js/src/kimchi.grid.js
+++ b/ui/js/src/kimchi.grid.js
@@ -375,7 +375,7 @@ kimchi.widget.Grid.prototype = (function() {
         var container = $('#' + containerID);
         var gridID = this['opts']['id'];
         var rowSelection = this['opts']['rowSelection'] || 'single';
-        var domNode = $(kimchi.template(htmlStr, {
+        var domNode = $(kimchi.substitute(htmlStr, {
             id: gridID,
             loading: i18n['KCHGRD6001M'],
             message: i18n['KCHGRD6002M'],
diff --git a/ui/js/src/kimchi.guest_add_main.js b/ui/js/src/kimchi.guest_add_main.js
index 2113099..eefd59a 100644
--- a/ui/js/src/kimchi.guest_add_main.js
+++ b/ui/js/src/kimchi.guest_add_main.js
@@ -25,7 +25,7 @@ kimchi.guest_add_main = function() {
                 var html = '';
                 var tmpl = $('#tmpl-template').html();
                 $.each(result, function(index, value) {
-                    html += kimchi.template(tmpl, value);
+                    html += kimchi.substitute(tmpl, value);
                 });
                 $('#templateTile').html(html);
                 return;
diff --git a/ui/js/src/kimchi.guest_edit_main.js b/ui/js/src/kimchi.guest_edit_main.js
index 0236e2d..90dcb32 100644
--- a/ui/js/src/kimchi.guest_edit_main.js
+++ b/ui/js/src/kimchi.guest_edit_main.js
@@ -42,7 +42,7 @@ kimchi.guest_edit_main = function() {
             $.each(storages, function(index, storage) {
                 storage['vm'] = kimchi.selectedGuest;
                 rowHTML = $('#' + storage['type'] + '-row-tmpl').html();
-                var templated = kimchi.template(rowHTML, storage);
+                var templated = kimchi.substitute(rowHTML, storage);
                 container.append(templated);
             });
 
@@ -96,7 +96,7 @@ kimchi.guest_edit_main = function() {
             $(".action-area", item).toggleClass("hide");
         };
         var addItem = function(data) {
-            var itemNode = $.parseHTML(kimchi.template($('#interface-tmpl').html(),data));
+            var itemNode = $.parseHTML(kimchi.substitute($('#interface-tmpl').html(),data));
             $(".body", "#form-guest-edit-interface").append(itemNode);
             $("select", itemNode).append(networkOptions);
             if(data.network!==""){
diff --git a/ui/js/src/kimchi.guest_media_main.js b/ui/js/src/kimchi.guest_media_main.js
index 02009ec..d6dee2e 100644
--- a/ui/js/src/kimchi.guest_media_main.js
+++ b/ui/js/src/kimchi.guest_media_main.js
@@ -28,7 +28,7 @@ kimchi.guest_media_main = function() {
 
             $.each(storages, function(index, storage) {
                 storage['vm'] = kimchi.selectedGuest;
-                var templated = kimchi.template(rowHTML, storage);
+                var templated = kimchi.substitute(rowHTML, storage);
                 container.append(templated);
             });
 
diff --git a/ui/js/src/kimchi.host.js b/ui/js/src/kimchi.host.js
index 4c94fc1..ee59a7e 100644
--- a/ui/js/src/kimchi.host.js
+++ b/ui/js/src/kimchi.host.js
@@ -485,7 +485,7 @@ kimchi.host_main = function() {
         data['memory'] = kimchi.formatMeasurement(data['memory'], {
             fixed: 2
         });
-        var templated = kimchi.template(htmlTmpl, data);
+        var templated = kimchi.substitute(htmlTmpl, data);
         $('#host-content-container').html(templated);
 
         initPage();
diff --git a/ui/js/src/kimchi.main.js b/ui/js/src/kimchi.main.js
index 86fed5b..184029d 100644
--- a/ui/js/src/kimchi.main.js
+++ b/ui/js/src/kimchi.main.js
@@ -69,7 +69,7 @@ kimchi.main = function() {
         var tabs = retrieveTabs(tabConfigUrl);
         kimchi.listPlugins(function(plugins) {
             $(plugins).each(function(i, p) {
-                var url = kimchi.template(pluginConfigUrl, {
+                var url = kimchi.substitute(pluginConfigUrl, {
                     plugin: p
                 });
                 tabs.push.apply(tabs, retrieveTabs(url));
diff --git a/ui/js/src/kimchi.network.js b/ui/js/src/kimchi.network.js
index ca6d29b..875bc08 100644
--- a/ui/js/src/kimchi.network.js
+++ b/ui/js/src/kimchi.network.js
@@ -63,7 +63,7 @@ kimchi.getNetworkItemHtml = function(network) {
     }
 
     var disable_in_use = network.in_use ? "ui-state-disabled" : "";
-    var networkItem = kimchi.template($('#networkItem').html(), {
+    var networkItem = kimchi.substitute($('#networkItem').html(), {
         name : network.name,
         state : network.state,
         type : network.type,
diff --git a/ui/js/src/kimchi.storage_main.js b/ui/js/src/kimchi.storage_main.js
index 6052510..1493124 100644
--- a/ui/js/src/kimchi.storage_main.js
+++ b/ui/js/src/kimchi.storage_main.js
@@ -25,7 +25,7 @@ kimchi.doListStoragePools = function() {
                 value.capacity = kimchi.changetoProperUnit(value.capacity,1);
                 value.allocated = kimchi.changetoProperUnit(value.allocated,1);
                 if ('kimchi-iso' !== value.type) {
-                    listHtml += kimchi.template(storageHtml, value);
+                    listHtml += kimchi.substitute(storageHtml, value);
                 }
             });
             $('#storagepoolsList').html(listHtml);
@@ -172,7 +172,7 @@ kimchi.doListVolumes = function(poolObj) {
                     value.poolname = poolObj.data('name');
                     value.capacity = kimchi.changetoProperUnit(value.capacity,1);
                     value.allocation = kimchi.changetoProperUnit(value.allocation,1);
-                    listHtml += kimchi.template(volumeHtml, value);
+                    listHtml += kimchi.substitute(volumeHtml, value);
                 });
                 volumeDiv.html(listHtml);
             } else {
diff --git a/ui/js/src/kimchi.storagepool_add_main.js b/ui/js/src/kimchi.storagepool_add_main.js
index 86dbe7f..050aa90 100644
--- a/ui/js/src/kimchi.storagepool_add_main.js
+++ b/ui/js/src/kimchi.storagepool_add_main.js
@@ -30,7 +30,7 @@ kimchi.initStorageAddPage = function() {
             valid_types = ['part', 'disk', 'mpath'];
             $.each(data, function(index, value) {
                 if (valid_types.indexOf(value.type) != -1) {
-                    listHtml += kimchi.template(deviceHtml, value);
+                    listHtml += kimchi.substitute(deviceHtml, value);
                 }
             });
             $('.host-partition').html(listHtml);
diff --git a/ui/js/src/kimchi.substitute.js b/ui/js/src/kimchi.substitute.js
new file mode 100644
index 0000000..27ca8ed
--- /dev/null
+++ b/ui/js/src/kimchi.substitute.js
@@ -0,0 +1,45 @@
+/*
+ * Project Kimchi
+ *
+ * Copyright IBM, Corp. 2013
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+kimchi.substitute = function(templatedStr, data, tag) {
+    tag = tag || /\{([^\}]+)\}/g;
+
+    var escapeHtml = function(html) {
+        return String(html)
+            .replace(/&/g, '&amp;')
+            .replace(/</g, '&lt;')
+            .replace(/>/g, '&gt;')
+            .replace(/"/g, '&quot;')
+            .replace(/'/g, '&#39;');
+    };
+
+    return templatedStr.replace(tag, function(matchResult, express) {
+        var propertyArray = express.split('!');
+        var defaultValue = propertyArray[1] || '';
+        propertyArray = propertyArray[0].split('.');
+        var value = data, i = 0, l = propertyArray.length, property;
+        for (; i < l; i++) {
+            property = propertyArray[i];
+            if (value) {
+                value = value[property];
+            } else {
+                break;
+            }
+        }
+        return escapeHtml((value || value === 0) ? value : defaultValue);
+    });
+};
diff --git a/ui/js/src/kimchi.template.js b/ui/js/src/kimchi.template.js
deleted file mode 100644
index 082269f..0000000
--- a/ui/js/src/kimchi.template.js
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- * Project Kimchi
- *
- * Copyright IBM, Corp. 2013
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-kimchi.template = function(templateStr, data, tag) {
-    tag = tag || /\{([^\}]+)\}/g;
-
-    var escapeHtml = function(html) {
-        return String(html)
-            .replace(/&/g, '&amp;')
-            .replace(/</g, '&lt;')
-            .replace(/>/g, '&gt;')
-            .replace(/"/g, '&quot;')
-            .replace(/'/g, '&#39;');
-    };
-
-    return templateStr.replace(tag, function(matchResult, express) {
-        var propertyArray = express.split('!');
-        var defaultValue = propertyArray[1] || '';
-        propertyArray = propertyArray[0].split('.');
-        var value = data, i = 0, l = propertyArray.length, property;
-        for (; i < l; i++) {
-            property = propertyArray[i];
-            if (value) {
-                value = value[property];
-            } else {
-                break;
-            }
-        }
-        return escapeHtml((value || value === 0) ? value : defaultValue);
-    });
-};
diff --git a/ui/js/src/kimchi.template_add_main.js b/ui/js/src/kimchi.template_add_main.js
index dbb3952..e9adbac 100644
--- a/ui/js/src/kimchi.template_add_main.js
+++ b/ui/js/src/kimchi.template_add_main.js
@@ -152,7 +152,7 @@ kimchi.template_add_main = function() {
                 volume.isoId = isoId;
                 volume.capacity = kimchi.changetoProperUnit(volume.capacity, 1);
                 kimchi.isoInfo[isoId] = volume;
-                html += kimchi.template(template, volume);
+                html += kimchi.substitute(template, volume);
             }
         });
         $('#list-local-iso').append(html);
@@ -266,7 +266,7 @@ kimchi.template_add_main = function() {
                 if (!kimchi.isoInfo[isoId]) {
                     volume.isoId = isoId;
                     kimchi.isoInfo[isoId] = volume;
-                    html += kimchi.template(template, volume);
+                    html += kimchi.substitute(template, volume);
                 }
             });
             $('#list-remote-iso').html(html);
diff --git a/ui/js/src/kimchi.template_edit_main.js b/ui/js/src/kimchi.template_edit_main.js
index 9ff8bcc..cffc685 100644
--- a/ui/js/src/kimchi.template_edit_main.js
+++ b/ui/js/src/kimchi.template_edit_main.js
@@ -87,7 +87,7 @@ kimchi.template_edit_main = function() {
                 var html = '';
                 var tmpl = $('#tmpl-network').html();
                 $.each(result, function(index, network) {
-                    html += kimchi.template(tmpl, network);
+                    html += kimchi.substitute(tmpl, network);
                 });
                 $('#template-edit-network-list').html(html).show();
                 if(template.networks && template.networks.length > 0) {
diff --git a/ui/js/src/kimchi.template_main.js b/ui/js/src/kimchi.template_main.js
index 160ecc8..b90e3f3 100644
--- a/ui/js/src/kimchi.template_main.js
+++ b/ui/js/src/kimchi.template_main.js
@@ -22,7 +22,7 @@ kimchi.doListTemplates = function() {
             var listHtml = '';
             var templateHtml = $('#templateTmpl').html();
             $.each(result, function(index, value) {
-                listHtml += kimchi.template(templateHtml, value);
+                listHtml += kimchi.substitute(templateHtml, value);
             });
             $('#templateList').html(listHtml);
             kimchi.templateBindClick();
-- 
1.8.1.4




More information about the Kimchi-devel mailing list