Browse Source

HUE-3003 [search] Allow linking to a collection with a predefine query string

Added pushState for first query term and query definition ID
Removed LZString file and dependency
Enrico Berti 10 years ago
parent
commit
8648a05

File diff suppressed because it is too large
+ 0 - 0
apps/search/src/search/static/search/js/lzstring.min.js


+ 19 - 2
apps/search/src/search/static/search/js/search.ko.js

@@ -627,8 +627,13 @@ var Collection = function (vm, collection) {
     qdefinition.hasChanged = ko.observable(false);
 
     vm.selectedQDefinition(qdefinition);
-    if (window.location.hash.indexOf("collection") == -1){
-      window.location.hash = "q=" + qdef.uuid();
+    if (window.location.hash.indexOf("collection") == -1) {
+      if (typeof history.pushState != "undefined" && location.getParameter("collection") != "") {
+        history.pushState(null, null, "?collection=" + location.getParameter("collection") + "&qd=" + qdef.uuid());
+      }
+      else {
+        window.location.hash = "qd=" + qdef.uuid();
+      }
     }
     vm.search();
     $(document).trigger("loadedQDefinition");
@@ -660,6 +665,14 @@ var Collection = function (vm, collection) {
     vm.query.fqs.removeAll();
     vm.query.start(0);
     vm.query.selectedMultiq([]);
+    if (window.location.hash.indexOf("collection") == -1) {
+      if (typeof history.pushState != "undefined" && location.getParameter("collection") != "") {
+        history.pushState(null, null, "?collection=" + location.getParameter("collection"));
+      }
+      else {
+        window.location.hash = "";
+      }
+    }
   }
 
   self.getQDefinition = function (qDefID) {
@@ -1321,6 +1334,10 @@ var SearchViewModel = function (collection_json, query_json, initial_json) {
         self.selectedQDefinition().hasChanged(true);
       }
     }
+    else if (typeof history.pushState != "undefined" && location.getParameter("collection") != "") {
+      var firstQuery = self.query.qs()[0].q();
+      history.pushState(null, null, "?collection=" + location.getParameter("collection") + (firstQuery ? "&q=" + firstQuery : ""));
+    }
 
     // Multi queries
     var multiQs = [];

+ 12 - 19
apps/search/src/search/templates/search.mako

@@ -1908,7 +1908,6 @@ ${ dashboard.layout_skeleton() }
 ${ dashboard.import_layout(True) }
 
 <script src="${ static('search/js/search.utils.js') }" type="text/javascript" charset="utf-8"></script>
-<script src="${ static('search/js/lzstring.min.js') }" type="text/javascript" charset="utf-8"></script>
 <script src="${ static('desktop/js/jquery.textsqueezer.js') }" type="text/javascript" charset="utf-8"></script>
 <script src="${ static('desktop/ext/js/bootstrap-editable.min.js') }" type="text/javascript" charset="utf-8"></script>
 <script src="${ static('desktop/js/ko.editable.js') }" type="text/javascript" charset="utf-8"></script>
@@ -2421,18 +2420,6 @@ $(document).ready(function () {
   });
 
   var _query = ${ query | n,unicode };
-  if (window.location.hash != ""){
-    if (window.location.hash.indexOf("collection") == -1){
-      try {
-        var _decompress = LZString.decompressFromBase64(window.location.hash.substr(1));
-        if (_decompress != null && $.trim(_decompress) != ""){
-          _query = ko.mapping.fromJSON(LZString.decompressFromBase64(window.location.hash.substr(1)));
-        }
-      }
-      catch (e){}
-    }
-  }
-
   viewModel = new SearchViewModel(${ collection.get_json(user) | n,unicode }, _query, ${ initial | n,unicode });
 
   viewModel.timelineChartTypes = ko.observableArray([
@@ -2538,14 +2525,20 @@ $(document).ready(function () {
     }
   });
 
-  if (window.location.hash != "") {
-    if (window.location.hash.indexOf("q=") > -1) {
-      var _qdef = viewModel.collection.getQDefinition(window.location.hash.substr(1).replace(/(<([^>]+)>)/ig, "").split("=")[1]);
-      if (_qdef != null){
-        viewModel.collection.loadQDefinition(_qdef);
-      }
+  function loadQueryDefinition(id) {
+    var _qdef = viewModel.collection.getQDefinition(id);
+    if (_qdef != null) {
+      viewModel.collection.loadQDefinition(_qdef);
     }
   }
+
+  if (window.location.hash != "" && window.location.hash.indexOf("qd=") > -1) {
+    loadQueryDefinition(window.location.hash.substr(1).replace(/(<([^>]+)>)/ig, "").split("=")[1]);
+  }
+  else if (_query.qd) {
+    loadQueryDefinition(_query.qd);
+  }
+
 });
 
 

+ 8 - 1
apps/search/src/search/views.py

@@ -59,9 +59,16 @@ def index(request):
 
   query = {'qs': [{'q': ''}], 'fqs': [], 'start': 0}
 
+  if request.method == 'GET':
+    if 'q' in request.GET:
+      query['qs'][0]['q'] = request.GET.get('q')
+    if 'qd' in request.GET:
+      query['qd'] = request.GET.get('qd')
+
+
   return render('search.mako', request, {
     'collection': collection,
-    'query': query,
+    'query': json.dumps(query),
     'initial': json.dumps({'collections': [], 'layout': [], 'is_latest': LATEST.get()}),
     'is_owner': collection_doc.doc.get().can_write(request.user),
     'can_edit_index': can_edit_index(request.user)

+ 20 - 9
desktop/core/src/desktop/static/desktop/js/hue.utils.js

@@ -26,8 +26,8 @@ if (!('clean' in Array.prototype)) {
     return this;
   };
 }
-
 if (!('move' in Array.prototype)) {
+
   Array.prototype.move = function (old_index, new_index) {
     if (new_index >= this.length) {
       var k = new_index - this.length;
@@ -39,8 +39,8 @@ if (!('move' in Array.prototype)) {
     return this;
   };
 }
-
 if (!('indexOf' in Array.prototype)) {
+
   Array.prototype.indexOf = function (needle) {
     for (var i = 0; i < this.length; i++) {
       if (this[i] === needle) {
@@ -50,14 +50,16 @@ if (!('indexOf' in Array.prototype)) {
     return -1;
   };
 }
-
 // adding missing .filter for IE8
+
 if (!('filter' in Array.prototype)) {
   Array.prototype.filter = function (filter, that /*opt*/) {
     var other = [], v;
-    for (var i = 0, n = this.length; i < n; i++)
-      if (i in this && filter.call(that, v = this[i], i, this))
+    for (var i = 0, n = this.length; i < n; i++) {
+      if (i in this && filter.call(that, v = this[i], i, this)) {
         other.push(v);
+      }
+    }
     return other;
   };
 }
@@ -67,10 +69,10 @@ Array.prototype.diff = function (a) {
     return a.indexOf(i) < 0;
   });
 };
-
 /*
  * Add utility methods to the HUE object
 */
+
 (function (hue) {
   'use strict';
 
@@ -116,8 +118,8 @@ Array.prototype.diff = function (a) {
 
 }(hue = window.hue || {}));
 
-
 if (!Object.keys) {
+
   Object.keys = (function () {
     'use strict';
     var hasOwnProperty = Object.prototype.hasOwnProperty,
@@ -169,7 +171,6 @@ function s4() {
 function UUID() {
   return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
 }
-
 // Based on original pub/sub implementation from http://davidwalsh.name/pubsub-javascript
 var huePubSub = (function () {
   var topics = {};
@@ -208,6 +209,7 @@ var huePubSub = (function () {
   };
 })();
 
+
 Number.prototype.toHHMMSS = function () {
   var _s = this;
   var _ms = _s % 1000;
@@ -216,6 +218,15 @@ Number.prototype.toHHMMSS = function () {
   _s = (_s - _secs) / 60;
   var _mins = _s % 60;
   var _hrs = (_s - _mins) / 60;
-
   return (_hrs > 0 ? _hrs + "h, " : "") + (_mins > 0 ? _mins + "m, " : "") + _secs + "." + _ms + "s";
+
+}
+
+if (!('getParameter' in window.location)) {
+  window.location.getParameter = function (name) {
+    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
+    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
+        results = regex.exec(window.location.search);
+    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
+  };
 }

Some files were not shown because too many files changed in this diff