Pārlūkot izejas kodu

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 gadi atpakaļ
vecāks
revīzija
8648a0550b

Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 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);
     qdefinition.hasChanged = ko.observable(false);
 
 
     vm.selectedQDefinition(qdefinition);
     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();
     vm.search();
     $(document).trigger("loadedQDefinition");
     $(document).trigger("loadedQDefinition");
@@ -660,6 +665,14 @@ var Collection = function (vm, collection) {
     vm.query.fqs.removeAll();
     vm.query.fqs.removeAll();
     vm.query.start(0);
     vm.query.start(0);
     vm.query.selectedMultiq([]);
     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) {
   self.getQDefinition = function (qDefID) {
@@ -1321,6 +1334,10 @@ var SearchViewModel = function (collection_json, query_json, initial_json) {
         self.selectedQDefinition().hasChanged(true);
         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
     // Multi queries
     var multiQs = [];
     var multiQs = [];

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

@@ -1908,7 +1908,6 @@ ${ dashboard.layout_skeleton() }
 ${ dashboard.import_layout(True) }
 ${ dashboard.import_layout(True) }
 
 
 <script src="${ static('search/js/search.utils.js') }" type="text/javascript" charset="utf-8"></script>
 <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/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/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>
 <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 };
   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 = new SearchViewModel(${ collection.get_json(user) | n,unicode }, _query, ${ initial | n,unicode });
 
 
   viewModel.timelineChartTypes = ko.observableArray([
   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}
   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, {
   return render('search.mako', request, {
     'collection': collection,
     'collection': collection,
-    'query': query,
+    'query': json.dumps(query),
     'initial': json.dumps({'collections': [], 'layout': [], 'is_latest': LATEST.get()}),
     'initial': json.dumps({'collections': [], 'layout': [], 'is_latest': LATEST.get()}),
     'is_owner': collection_doc.doc.get().can_write(request.user),
     'is_owner': collection_doc.doc.get().can_write(request.user),
     'can_edit_index': can_edit_index(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;
     return this;
   };
   };
 }
 }
-
 if (!('move' in Array.prototype)) {
 if (!('move' in Array.prototype)) {
+
   Array.prototype.move = function (old_index, new_index) {
   Array.prototype.move = function (old_index, new_index) {
     if (new_index >= this.length) {
     if (new_index >= this.length) {
       var k = new_index - this.length;
       var k = new_index - this.length;
@@ -39,8 +39,8 @@ if (!('move' in Array.prototype)) {
     return this;
     return this;
   };
   };
 }
 }
-
 if (!('indexOf' in Array.prototype)) {
 if (!('indexOf' in Array.prototype)) {
+
   Array.prototype.indexOf = function (needle) {
   Array.prototype.indexOf = function (needle) {
     for (var i = 0; i < this.length; i++) {
     for (var i = 0; i < this.length; i++) {
       if (this[i] === needle) {
       if (this[i] === needle) {
@@ -50,14 +50,16 @@ if (!('indexOf' in Array.prototype)) {
     return -1;
     return -1;
   };
   };
 }
 }
-
 // adding missing .filter for IE8
 // adding missing .filter for IE8
+
 if (!('filter' in Array.prototype)) {
 if (!('filter' in Array.prototype)) {
   Array.prototype.filter = function (filter, that /*opt*/) {
   Array.prototype.filter = function (filter, that /*opt*/) {
     var other = [], v;
     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);
         other.push(v);
+      }
+    }
     return other;
     return other;
   };
   };
 }
 }
@@ -67,10 +69,10 @@ Array.prototype.diff = function (a) {
     return a.indexOf(i) < 0;
     return a.indexOf(i) < 0;
   });
   });
 };
 };
-
 /*
 /*
  * Add utility methods to the HUE object
  * Add utility methods to the HUE object
 */
 */
+
 (function (hue) {
 (function (hue) {
   'use strict';
   'use strict';
 
 
@@ -116,8 +118,8 @@ Array.prototype.diff = function (a) {
 
 
 }(hue = window.hue || {}));
 }(hue = window.hue || {}));
 
 
-
 if (!Object.keys) {
 if (!Object.keys) {
+
   Object.keys = (function () {
   Object.keys = (function () {
     'use strict';
     'use strict';
     var hasOwnProperty = Object.prototype.hasOwnProperty,
     var hasOwnProperty = Object.prototype.hasOwnProperty,
@@ -169,7 +171,6 @@ function s4() {
 function UUID() {
 function UUID() {
   return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
   return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
 }
 }
-
 // Based on original pub/sub implementation from http://davidwalsh.name/pubsub-javascript
 // Based on original pub/sub implementation from http://davidwalsh.name/pubsub-javascript
 var huePubSub = (function () {
 var huePubSub = (function () {
   var topics = {};
   var topics = {};
@@ -208,6 +209,7 @@ var huePubSub = (function () {
   };
   };
 })();
 })();
 
 
+
 Number.prototype.toHHMMSS = function () {
 Number.prototype.toHHMMSS = function () {
   var _s = this;
   var _s = this;
   var _ms = _s % 1000;
   var _ms = _s % 1000;
@@ -216,6 +218,15 @@ Number.prototype.toHHMMSS = function () {
   _s = (_s - _secs) / 60;
   _s = (_s - _secs) / 60;
   var _mins = _s % 60;
   var _mins = _s % 60;
   var _hrs = (_s - _mins) / 60;
   var _hrs = (_s - _mins) / 60;
-
   return (_hrs > 0 ? _hrs + "h, " : "") + (_mins > 0 ? _mins + "m, " : "") + _secs + "." + _ms + "s";
   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, " "));
+  };
 }
 }

Daži faili netika attēloti, jo izmaiņu fails ir pārāk liels