[extensions-web] js: Move away from the horrible dynamic template building system



commit e88c6bf9d88e72fbc70e9d7dbf6586d56fb952de
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Tue Sep 25 23:17:21 2012 -0300

    js: Move away from the horrible dynamic template building system
    
    Instead, do an explicit grab. Note that we're also changing the
    extension separator in preparation for future changes.

 sweettooth/static/js/extensions.js                 |   12 ++++----
 sweettooth/static/js/main.js                       |    2 +-
 sweettooth/static/js/paginator.js                  |    2 +-
 sweettooth/static/js/templates.js                  |   27 +++++++++++--------
 .../js/templates/extensions/comments_list.mustache |    2 +-
 .../static/js/templates/extensions/info.mustache   |    2 +-
 .../js/templates/extensions/info_list.mustache     |    2 +-
 sweettooth/static/js/templates/templatedata.js     |    6 ++--
 8 files changed, 30 insertions(+), 25 deletions(-)
---
diff --git a/sweettooth/static/js/extensions.js b/sweettooth/static/js/extensions.js
index 6cd7985..83760d9 100644
--- a/sweettooth/static/js/extensions.js
+++ b/sweettooth/static/js/extensions.js
@@ -34,7 +34,7 @@ function($, messages, dbusProxy, extensionUtils, templates) {
     if (dbusProxy.IsDummy) {
         // We don't have a proper DBus proxy -- it's probably an old
         // version of GNOME3 or the Shell.
-        messages.addError(templates.messages.dummy_proxy());
+        messages.addError(templates.get('messages/dummy_proxy')());
 
         $.fn.addExtensionSwitch = function() {
             // Don't show our switches -- CSS styles define a clickable
@@ -43,13 +43,13 @@ function($, messages, dbusProxy, extensionUtils, templates) {
         };
 
         $.fn.addLocalExtensions = function() {
-            return this.append(templates.messages.cannot_list_local());
+            return this.append(templates.get('messages/cannot_list_local')());
         };
 
         $.fn.fillInErrors = function() {
             var $textarea = this.find('textarea[name=error]');
             var $hidden = this.find('input:hidden[name=has_errors]');
-            $textarea.text(templates.messages.cannot_list_errors()).
+            $textarea.text(templates.get('messages/cannot_list_errors')()).
                 addClass('no-errors').attr('disabled', 'disabled');
             $hidden.val('');
             return this;
@@ -207,7 +207,7 @@ function($, messages, dbusProxy, extensionUtils, templates) {
                             dbusProxy.UninstallExtension(uuid).done(function(result) {
                                 if (result) {
                                     $elem.fadeOut({ queue: false }).slideUp({ queue: false });
-                                    messages.addInfo(templates.extensions.uninstall(extension));
+                                    messages.addInfo(templates.get('extension/uninstall')(extension));
                                 }
                             });
                         }
@@ -221,7 +221,7 @@ function($, messages, dbusProxy, extensionUtils, templates) {
                             if (extension.description)
                                 extension.first_line_of_description = extension.description.split('\n')[0];
 
-                            $elem = $(templates.extensions.info(extension)).replaceAll($elem);
+                            $elem = $(templates.get('extension/info')(extension)).replaceAll($elem);
                             $elem.find('.uninstall').on('click', uninstall);
 
                             addExtensionSwitch(uuid, $elem, extension);
@@ -262,7 +262,7 @@ function($, messages, dbusProxy, extensionUtils, templates) {
                                     ev: (meta && meta.version) ? meta.version : null,
                                     errors: errors };
 
-                    $textarea.text(templates.extensions.error_report_template(context));
+                    $textarea.text(templates.get('extensions/error_report_template')(context));
                 });
             });
         });
diff --git a/sweettooth/static/js/main.js b/sweettooth/static/js/main.js
index ee5048a..1ac71c3 100644
--- a/sweettooth/static/js/main.js
+++ b/sweettooth/static/js/main.js
@@ -181,7 +181,7 @@ function($, messages, modal, hashParamUtils, templates) {
                     data: { pk: pk },
                     url: '/comments/all/',
                 }).done(function(comments) {
-                    var $newContent = $(templates.extensions.comments_list(comments));
+                    var $newContent = $(templates.get('extensions/comments_list')(comments));
                     $newContent.find('time').timeago();
                     $loadingText.replaceWith($newContent);
                 });
diff --git a/sweettooth/static/js/paginator.js b/sweettooth/static/js/paginator.js
index 222a492..5689c95 100644
--- a/sweettooth/static/js/paginator.js
+++ b/sweettooth/static/js/paginator.js
@@ -59,7 +59,7 @@ define(['jquery', 'hashParamUtils', 'paginatorUtils', 'dbus!_', 'templates', 'jq
                         this.first_line_of_description = this.description.split('\n')[0];
                 });
 
-                var $newContent = $(templates.extensions.info_list(result));
+                var $newContent = $(templates.get('extensions/info_list')(result));
 
                 $elem.
                     removeClass('loading').
diff --git a/sweettooth/static/js/templates.js b/sweettooth/static/js/templates.js
index 57c68b3..d31284e 100644
--- a/sweettooth/static/js/templates.js
+++ b/sweettooth/static/js/templates.js
@@ -4,27 +4,32 @@ define(['templates/templatedata', 'mustache'], function(templatedata, mustache)
     "use strict";
 
     var exports = {};
-    exports._T = templatedata;
+    var cache = {};
+
+    function _processPartials(prefix, data) {
+        for (var prop in data) {
+            var value = data[prop];
+            var name;
 
-    function _compileTemplateData(data, out, prefix) {
-        for (var propname in data) {
-            var v = data[propname], pkey;
             if (prefix)
-                pkey = prefix + "." + propname;
+                name = prefix + "/" + prop;
             else
-                pkey = propname;
+                name = prop;
 
-            if (typeof(v) === typeof({})) {
+            if (typeof(value) === typeof({})) {
                 // Subdirectory. Recurse.
-                out[propname] = _compileTemplateData(v, {}, pkey);
+                _processPartials(name, value);
             } else {
                 // Template. Mustache will cache all partials for us.
-                out[propname] = mustache.compilePartial(pkey, v);
+                cache[name] = mustache.compilePartial(name, value);
             }
         }
-        return out;
+    }
+    _processPartials("", templatedata);
+
+    exports.get = function get(name) {
+        return cache[name];
     }
 
-    _compileTemplateData(templatedata, exports, "");
     return exports;
 });
diff --git a/sweettooth/static/js/templates/extensions/comments_list.mustache b/sweettooth/static/js/templates/extensions/comments_list.mustache
index 9738c7f..9ea88ae 100644
--- a/sweettooth/static/js/templates/extensions/comments_list.mustache
+++ b/sweettooth/static/js/templates/extensions/comments_list.mustache
@@ -1,5 +1,5 @@
 {{#.}}
-  {{>extensions.comment}}
+  {{>extensions/comment}}
   <hr>
 {{/.}}
 {{^.}}
diff --git a/sweettooth/static/js/templates/extensions/info.mustache b/sweettooth/static/js/templates/extensions/info.mustache
index 5fe63fa..6d5c403 100644
--- a/sweettooth/static/js/templates/extensions/info.mustache
+++ b/sweettooth/static/js/templates/extensions/info.mustache
@@ -1,3 +1,3 @@
 <div class="extension" data-uuid="{{uuid}}">
-  {{>extensions.info_contents}}
+  {{>extensions/info_contents}}
 </div>
diff --git a/sweettooth/static/js/templates/extensions/info_list.mustache b/sweettooth/static/js/templates/extensions/info_list.mustache
index 8edece8..6379ad2 100644
--- a/sweettooth/static/js/templates/extensions/info_list.mustache
+++ b/sweettooth/static/js/templates/extensions/info_list.mustache
@@ -1,7 +1,7 @@
 <ul class="extensions">
 {{#extensions}}
   <li class="extension" data-svm="{{shell_version_map}}">
-    {{>extensions.info_contents}}
+    {{>extensions/info_contents}}
   </li>
 {{/extensions}}
 </ul>
diff --git a/sweettooth/static/js/templates/templatedata.js b/sweettooth/static/js/templates/templatedata.js
index cf738bd..ea08ac3 100644
--- a/sweettooth/static/js/templates/templatedata.js
+++ b/sweettooth/static/js/templates/templatedata.js
@@ -4,11 +4,11 @@
 define({
   "extensions": {
     "comment": "<div class=\"comment\">\n  {{#is_extension_creator}}\n  <div class=\"extension-creator-badge\">Author</div>\n  {{/is_extension_creator}}\n  <img src=\"{{gravatar}}\" class=\"gravatar\">\n  <div class=\"rating-author\">\n    {{#has_rating}}\n      <div class=\"rating\" data-rating-value=\"{{rating}}\"></div> by\n    {{/has_rating}}\n    <a class=\"comment-author\" href=\"{{author.url}}\">{{author.username}}</a>\n    <p>{{comment}}</p>\n    <time datetime=\"{{date.timestamp}}Z\">{{date.standard}}</time>\n  </div>\n</div>", 
-    "comments_list": "{{#.}}\n  {{>extensions.comment}}\n  <hr>\n{{/.}}\n{{^.}}\n  <p>There are no comments. Be the first!</p>\n{{/.}}", 
+    "comments_list": "{{#.}}\n  {{>extensions/comment}}\n  <hr>\n{{/.}}\n{{^.}}\n  <p>There are no comments. Be the first!</p>\n{{/.}}", 
     "error_report_template": "What's wrong?\n\n\n\nWhat have you tried?\n\n\n\nAutomatically detected errors:\n\n{{#errors}}\n  {{.}}\n\n================\n{{/errors}}\n{{^errors}}\nGNOME Shell Extensions did not detect any errors with this extension.\n{{/errors}}\n\nVersion information:\n\n    Shell version: {{sv}}\n    Extension version: {{#ev}}{{ev}}{{/ev}}{{^ev}}Unknown{{/ev}}", 
-    "info": "<div class=\"extension\" data-uuid=\"{{uuid}}\">\n  {{>extensions.info_contents}}\n</div>", 
+    "info": "<div class=\"extension\" data-uuid=\"{{uuid}}\">\n  {{>extensions/info_contents}}\n</div>", 
     "info_contents": "<div class=\"switch\"></div>\n<div class=\"extra-buttons\">\n  <div class=\"upgrade-button\" title=\"Upgrade this extension\"></div>\n  <div class=\"configure-button\" title=\"Configure this extension\"></div>\n</div>\n<h3 class=\"extension-name\">\n  {{#link}}\n    <a href=\"{{link}}\" class=\"title-link\"> <img src=\"{{icon}}\" class=\"icon\"> {{name}} </a>\n  {{/link}}\n  {{^link}}\n  {{name}}\n  {{/link}}\n</h3>\n{{#creator}}\n  <span class=\"author\">by <a href=\"{{creator_url}}\"> {{creator}} </a></span>\n{{/creator}}\n<p class=\"description\">{{first_line_of_description}}</p>\n{{#want_uninstall}}\n  <button class=\"uninstall\" title=\"Uninstall\">Uninstall</button>\n{{/want_uninstall}}", 
-    "info_list": "<ul class=\"extensions\">\n{{#extensions}}\n  <li class=\"extension\" data-svm=\"{{shell_version_map}}\">\n    {{>extensions.info_contents}}\n  </li>\n{{/extensions}}\n</ul>", 
+    "info_list": "<ul class=\"extensions\">\n{{#extensions}}\n  <li class=\"extension\" data-svm=\"{{shell_version_map}}\">\n    {{>extensions/info_contents}}\n  </li>\n{{/extensions}}\n</ul>", 
     "uninstall": "You uninstalled <b>{{name}}</b>."
   }, 
   "messages": {



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]