Jelajahi Sumber

HUE-3294 [editor] Avoid reloading the same CSS and JS files

Enrico Berti 9 tahun lalu
induk
melakukan
421016f745

+ 1 - 2
apps/search/src/search/templates/common_search.mako

@@ -22,7 +22,7 @@ from django.utils.translation import ugettext as _
 
 <%namespace name="dashboard" file="common_dashboard.mako" />
 
-<%def name="page_structure(is_mobile=False)">
+<%def name="page_structure(is_mobile=False, is_embeddable=False)">
 
 <script type="text/javascript">
   SLIDER_LABELS = {
@@ -2668,7 +2668,6 @@ ${ dashboard.import_charts() }
 
 var viewModel;
 
-nv.dev = false;
 moment.suppressDeprecationWarnings = true;
 
 var HIT_OPTIONS = [

+ 2 - 2
apps/search/src/search/templates/search_embeddable.mako

@@ -23,5 +23,5 @@ from django.utils.translation import ugettext as _
 <%namespace name="common_search" file="common_search.mako" />
 
 <span id="searchComponents">
-${ common_search.page_structure() }
-</span>
+${ common_search.page_structure(is_embeddable=True) }
+</span>

+ 1 - 1
apps/search/src/search/templates/search_m.mako

@@ -25,7 +25,7 @@ from django.utils.translation import ugettext as _
 ${ commonheader_m(_('Search'), "search", user, request, "80px") | n,unicode }
 
 <span id="searchComponents">
-${ common_search.page_structure(True) }
+${ common_search.page_structure(is_mobile=True) }
 </span>
 
 ${ commonfooter_m(request, messages) | n,unicode }

+ 1 - 0
apps/search/src/search/urls.py

@@ -24,6 +24,7 @@ urlpatterns = patterns('search.views',
   url(r'^search$', 'search', name='search'),
   url(r'^save$', 'save', name='save'),
   url(r'^new_search', 'new_search', name='new_search'),
+  url(r'^embeddable/new_search', 'new_search_embeddable', name='new_search_embeddable'),
   url(r'^browse/(?P<name>.+)', 'browse', name='browse'),
   url(r'^browse_m/(?P<name>.+)', 'browse_m', name='browse_m'),
   url(r'^download$', 'download', name='download'),

+ 9 - 4
apps/search/src/search/views.py

@@ -92,8 +92,7 @@ def index_m(request):
 def index_embeddable(request):
   return index(request, False, True)
 
-
-def new_search(request):
+def new_search(request, is_embeddable=False):
   collections = SearchController(request.user).get_all_indexes()
   if not collections:
     return no_collections(request)
@@ -101,7 +100,11 @@ def new_search(request):
   collection = Collection2(user=request.user, name=collections[0])
   query = {'qs': [{'q': ''}], 'fqs': [], 'start': 0}
 
-  return render('search.mako', request, {
+  template = 'search.mako'
+  if is_embeddable:
+    template = 'search_embeddable.mako'
+
+  return render(template, request, {
     'collection': collection,
     'query': query,
     'initial': json.dumps({
@@ -122,6 +125,8 @@ def new_search(request):
     'can_edit_index': can_edit_index(request.user)
   })
 
+def new_search_embeddable(request):
+  return new_search(request, True)
 
 def browse(request, name, is_mobile=False):
   collections = SearchController(request.user).get_all_indexes()
@@ -600,7 +605,7 @@ def _create_facet(collection, user, facet_id, facet_label, facet_field, widget_t
         properties['type'] = facet_type
       if widget_type == 'hit-widget':
         facet_type = 'function'
-      else:        
+      else:
         facet_type = 'nested'
       properties['facets_form'] = {'field': '', 'mincount': 1, 'limit': 10, 'aggregate': {'function': 'unique', 'ops': []}}
       properties['facets'] = []

+ 31 - 1
desktop/core/src/desktop/templates/responsive.mako

@@ -418,13 +418,24 @@ ${ assist.assistPanel() }
     };
 
     (function () {
+      var LOADED_JS = [];
+      var LOADED_CSS = [];
+
+      $('script[src]').each(function(){
+        LOADED_JS.push($(this).attr('src'));
+      });
+
+      $('link[href]').each(function(){
+        LOADED_CSS.push($(this).attr('href'));
+      });
+
       var OnePageViewModel = function () {
         var self = this;
 
         self.EMBEDDABLE_PAGE_URLS = {
           editor: '/notebook/editor_embeddable',
           metastore: '/metastore/tables/?is_embeddable=true',
-          search: '/search/embeddable?collection=4'
+          search: '/search/embeddable/new_search'
         };
 
         self.embeddable_cache = {};
@@ -446,6 +457,25 @@ ${ assist.assistPanel() }
                 // hack to avoid css caching for development
                 var r = $(response);
                 r.find('link').each(function(){ $(this).attr('href', $(this).attr('href') + '?' + Math.random()) });
+                // load just CSS and JS files that are not loaded before
+                r.find('script[src]').each(function(){
+                  var jsFile = $(this).attr('src').split('?')[0];
+                  if (LOADED_JS.indexOf(jsFile) > -1){
+                    $(this).remove();
+                  }
+                  else {
+                    LOADED_JS.push(jsFile);
+                  }
+                });
+                r.find('link[href]').each(function(){
+                  var cssFile = $(this).attr('href').split('?')[0];
+                  if (LOADED_CSS.indexOf(cssFile) > -1){
+                    $(this).remove();
+                  }
+                  else {
+                    LOADED_CSS.push(cssFile);
+                  }
+                });
                 self.embeddable_cache[newVal] = r;
                 $('#embeddable_' + newVal).html(r);
                 self.isLoadingEmbeddable(false);