[extensions-web/feature/static-manifests] static: export static images to javascript code.



commit b3c3d4b77d64215404818522239ae4576bfcbd48
Author: Yuri Konotopov <ykonotopov gnome org>
Date:   Mon Jan 30 19:33:08 2017 +0400

    static: export static images to javascript code.

 sweettooth/static/js/staticfiles.js               |   36 +++++++++++++++++++++
 sweettooth/templates/base.html                    |    2 +
 sweettooth/templates/templatetags/static_paths.py |   20 +++++++++++
 3 files changed, 58 insertions(+), 0 deletions(-)
---
diff --git a/sweettooth/static/js/staticfiles.js b/sweettooth/static/js/staticfiles.js
new file mode 100644
index 0000000..2a3a528
--- /dev/null
+++ b/sweettooth/static/js/staticfiles.js
@@ -0,0 +1,36 @@
+/*
+    GNOME Shell extensions repository
+    Copyright (C) 2017  Yuri Konotopov <ykonotopov gnome org>
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU Affero General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+ */
+
+define(function() {
+       "use strict";
+
+       return {
+               getImage: function(path) {
+                       if(!django_static_images || !django_static_images[path])
+                       {
+                               return;
+                       }
+
+                       return django_static_images[path];
+               },
+
+               getImageFile: function(path) {
+                       let image = this.getImage(path);
+
+                       if(image)
+                       {
+                               image = image.split('/');
+                               return image[image.length - 1];
+                       }
+
+                       return image;
+               }
+       }
+});
diff --git a/sweettooth/templates/base.html b/sweettooth/templates/base.html
index 6b9279b..81346db 100644
--- a/sweettooth/templates/base.html
+++ b/sweettooth/templates/base.html
@@ -15,6 +15,8 @@
             paths: {% static_js_paths %},
             waitSeconds: 30 // It's fails sometimes with default 7 secs
         };
+
+        var django_static_images = {% static_img_paths %};
     </script>
     <script src="{% static 'js/require.js' %}"></script>
     <script src="{% static 'js/jquery.js' %}"></script>
diff --git a/sweettooth/templates/templatetags/static_paths.py 
b/sweettooth/templates/templatetags/static_paths.py
index 92c5708..5397630 100644
--- a/sweettooth/templates/templatetags/static_paths.py
+++ b/sweettooth/templates/templatetags/static_paths.py
@@ -15,6 +15,7 @@ import json
 
 register = template.Library()
 js_paths = None
+img_paths = None
 
 
 @register.simple_tag
@@ -34,3 +35,22 @@ def static_js_paths():
         return js_paths
 
     return "{}"
+
+
+@register.simple_tag
+def static_img_paths():
+    global img_paths
+
+    if isinstance(staticfiles_storage, ManifestStaticFilesStorage):
+        if img_paths is None:
+            img_paths = {};
+
+            for base_file, hashed_file in staticfiles_storage.hashed_files.iteritems():
+                if base_file.startswith('images/'):
+                    img_paths[base_file] = hashed_file
+
+            img_paths = json.dumps(img_paths)
+
+        return img_paths
+
+    return "{}"


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