Skip to content

Commit

Permalink
Add browser history support.
Browse files Browse the repository at this point in the history
  • Loading branch information
msindwan committed Oct 22, 2017
1 parent 3923764 commit 1866b15
Show file tree
Hide file tree
Showing 13 changed files with 319 additions and 163 deletions.
6 changes: 3 additions & 3 deletions src/main/java/io/sherlock/core/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ public void start() {
// Serve static files from the dist folder.
StaticHandler staticHandler = StaticHandler.create("dist");
staticHandler.setCachingEnabled(cacheEnabled);
router.route().handler(staticHandler);
router.route("/dist/*").handler(staticHandler);

// Handle errors.
router.route().failureHandler(routingContext -> {
// Default to the index page.
router.route().handler(routingContext -> {
HttpServerResponse response = routingContext.response();
response.sendFile("dist/index.html");
});
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/io/sherlock/core/handlers/Search.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ public static void getFile(RoutingContext routingContext) {
}
}

routingContext.response().sendFile(root.getAbsolutePath());
routingContext.response()
.putHeader("content-type", "text/plain")
.sendFile(root.getAbsolutePath());
}

}
4 changes: 2 additions & 2 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="description" content="">
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="/app.css" type="text/css" />
<link rel="stylesheet" href="/dist/app.css" type="text/css" />
</head>
<body>
<!-- App container -->
<div id="app">
</div>

<!-- Scripts -->
<script type="text/javascript" src="/app.js"></script>
<script type="text/javascript" src="/dist/app.js"></script>
</body>
</html>
67 changes: 67 additions & 0 deletions static/javascript/views/common/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Sherlock API
*
* @Author : Mayank Sindwani
* @Date : 2017-10-21
*
* Description : Defines a set of api functions.
**/

import SherlockUtils from './util';
import axios from 'axios';

class SherlockAPI {

/**
* Fetch Files
*
* Description: Fetches files from the director in the path provided.
* @param {path} // An arary of strings representing the path to a directory.
* @param {success} // The success callback.
* @param {failure} // The failure callback.
*/
static fetchFiles(path, success, failure) {
let uri;
if (path.length > 0) {
uri = `/api/search/files?path=${JSON.stringify(path)}`;
} else {
uri = '/api/search/files';
}
axios.get(encodeURI(uri))
.then(success)
.catch(failure);
}

/**
* Fetch File
*
* Description: Fetches the contents of a file and returns it as a string.
* @param {path} // An arary of strings representing the path to a file.
* @param {success} // The success callback.
* @param {failure} // The failure callback.
*/
static fetchFile(path, success, failure) {
const uri = `/api/search/file?path=${JSON.stringify(path)}`;
axios.get(encodeURI(uri), { headers: { 'Accept': 'text/plain' } })
.then(success)
.catch(failure);
}

/**
* Search Files
*
* Description: Searches files for the given search string.
* @param {searchText} // The text to search for.
* @param {success} // The success callback.
* @param {failure} // The failure callback.
*/
static searchFiles(searchText, success, failure) {
const uri = `/api/search/?query=${searchText}`;
axios.get(encodeURI(uri))
.then(success)
.catch(failure);
}

}

export default SherlockAPI;
43 changes: 43 additions & 0 deletions static/javascript/views/common/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,49 @@ class SherlockUtils {
return ext;
}


static saveHistory(queries=[]) {
let search = '';
var len = queries.length;

for (var i = 0; i < len; i++) {
let key = queries[i][0];
let value = queries[i][1];

if (i == 0) {
search += `?${key}=${value}`;
} else {
search += `&${key}=${value}`;
}

}
search = encodeURI(search);
if (window.location.search !== search) {
history.pushState({}, '', `/${search}`);
}
}

static replaceHistory(queries=[]) {
let search = '';
var len = queries.length;

for (var i = 0; i < len; i++) {
let key = queries[i][0];
let value = queries[i][1];

if (i == 0) {
search += `?${key}=${value}`;
} else {
search += `&${key}=${value}`;
}

}
search = encodeURI(search);
if (window.location.search !== search) {
history.replaceState({}, '', `/${search}`);
}
}

}


Expand Down
Loading

0 comments on commit 1866b15

Please sign in to comment.