Skip to content

Commit

Permalink
first POC of ES6 module
Browse files Browse the repository at this point in the history
  • Loading branch information
zadam committed Mar 24, 2018
1 parent 1612e90 commit e3e2dc9
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 63 deletions.
14 changes: 14 additions & 0 deletions src/public/javascripts/bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import searchTree from './search_tree.js';

const $toggleSearchButton = $("#toggle-search-button");

$toggleSearchButton.click(searchTree.toggleSearch);
bindShortcut('ctrl+s', searchTree.toggleSearch);

function bindShortcut(keyboardShortcut, handler) {
$(document).bind('keydown', keyboardShortcut, e => {
handler();

e.preventDefault();
});
}
114 changes: 52 additions & 62 deletions src/public/javascripts/search_tree.js
Original file line number Diff line number Diff line change
@@ -1,83 +1,73 @@
"use strict";

const searchTree = (function() {
const $tree = $("#tree");
const $searchInput = $("input[name='search-text']");
const $resetSearchButton = $("#reset-search-button");
const $doSearchButton = $("#do-search-button");
const $saveSearchButton = $("#save-search-button");
const $searchBox = $("#search-box");
const $toggleSearchButton = $("#toggle-search-button");

$resetSearchButton.click(resetSearch);

function toggleSearch() {
if ($searchBox.is(":hidden")) {
$searchBox.show();
$searchInput.focus();
}
else {
resetSearch();

$searchBox.hide();
}
const $tree = $("#tree");
const $searchInput = $("input[name='search-text']");
const $resetSearchButton = $("#reset-search-button");
const $doSearchButton = $("#do-search-button");
const $saveSearchButton = $("#save-search-button");
const $searchBox = $("#search-box");

function toggleSearch() {
if ($searchBox.is(":hidden")) {
$searchBox.show();
$searchInput.focus();
}
else {
resetSearch();

function resetSearch() {
$searchInput.val("");

getTree().clearFilter();
$searchBox.hide();
}
}

function getTree() {
return $tree.fancytree('getTree');
}
function resetSearch() {
$searchInput.val("");

async function doSearch() {
const searchText = $searchInput.val();
getTree().clearFilter();
}

const noteIds = await server.get('search/' + encodeURIComponent(searchText));
function getTree() {
return $tree.fancytree('getTree');
}

for (const noteId of noteIds) {
await noteTree.expandToNote(noteId, {noAnimation: true, noEvents: true});
}
async function doSearch() {
const searchText = $searchInput.val();

// Pass a string to perform case insensitive matching
getTree().filterBranches(node => noteIds.includes(node.data.noteId));
}
const noteIds = await server.get('search/' + encodeURIComponent(searchText));

$searchInput.keyup(e => {
const searchText = $searchInput.val();
for (const noteId of noteIds) {
await noteTree.expandToNote(noteId, {noAnimation: true, noEvents: true});
}

if (e && e.which === $.ui.keyCode.ESCAPE || $.trim(searchText) === "") {
$resetSearchButton.click();
return;
}
// Pass a string to perform case insensitive matching
getTree().filterBranches(node => noteIds.includes(node.data.noteId));
}

if (e && e.which === $.ui.keyCode.ENTER) {
doSearch();
}
}).focus();
async function saveSearch() {
const {noteId} = await server.post('search/' + encodeURIComponent($searchInput.val()));

$doSearchButton.click(doSearch);
await noteTree.reload();

$saveSearchButton.click(async () => {
const {noteId} = await server.post('search/' + encodeURIComponent($searchInput.val()));
await noteTree.activateNode(noteId);
}

await noteTree.reload();
$searchInput.keyup(e => {
const searchText = $searchInput.val();

await noteTree.activateNode(noteId);
});
if (e && e.which === $.ui.keyCode.ESCAPE || $.trim(searchText) === "") {
$resetSearchButton.click();
return;
}

$(document).bind('keydown', 'ctrl+s', e => {
toggleSearch();
if (e && e.which === $.ui.keyCode.ENTER) {
doSearch();
}
}).focus();

e.preventDefault();
});
$doSearchButton.click(doSearch);
$resetSearchButton.click(resetSearch);

$toggleSearchButton.click(toggleSearch);
$saveSearchButton.click(saveSearch);

return {
toggleSearch
};
})();
export default {
toggleSearch
};
3 changes: 2 additions & 1 deletion src/views/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,8 @@

<link href="stylesheets/style.css" rel="stylesheet">

<script src="javascripts/bootstrap.js" type="module"></script>

<script src="javascripts/utils.js"></script>
<script src="javascripts/init.js"></script>
<script src="javascripts/server.js"></script>
Expand All @@ -530,7 +532,6 @@
<script src="javascripts/tree_utils.js"></script>
<script src="javascripts/drag_and_drop.js"></script>
<script src="javascripts/context_menu.js"></script>
<script src="javascripts/search_tree.js"></script>
<script src="javascripts/export.js"></script>

<!-- Note detail -->
Expand Down

0 comments on commit e3e2dc9

Please sign in to comment.