Skip to content

Commit

Permalink
Frontend: Enable global search
Browse files Browse the repository at this point in the history
  • Loading branch information
smurfpandey committed Mar 16, 2015
1 parent f75b941 commit f2071a7
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 21 deletions.
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func NewClientFromURL(url string) (*Client, error) {
return nil, err
}

user, host := getHostUser(url)
user, host := getHostUserFromConnString(url)

return &Client{db: db, host: host, user: user}, nil
}
Expand Down
7 changes: 5 additions & 2 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@
<div class="wrap">
<div class="title">Database <i id="refresh-list" class="fa fa-refresh"></i></div>
<div class="search">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search for...">
<div class="input-group">
<input type="text" class="form-control" id="txtSearch" placeholder="Search for...">
<span class="input-group-btn">
<button class="btn btn-default" id="btnSearchDB" type="button">Go!</button>
</span>
</div><!-- /input-group -->
</div>
<div id="database-tree">
Expand Down
137 changes: 120 additions & 17 deletions static/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ function getViewDefiniton(dbName, viewName, cb) {
function closeConnection(cb) {
apiCall("delete", "/disconnect", {}, cb);
}
function apiSearchDatabase(query, cb) {
apiCall("get", "/search/" + query, {}, cb);
}


var fnGetSelectedTable = function() {
Expand Down Expand Up @@ -298,7 +301,6 @@ var showAlterDBPopup = function(nodeName) {
}
});
});

};

var showDropDBPopup = function(dbName) {
Expand Down Expand Up @@ -1118,7 +1120,7 @@ function initEditor(editorId, editorData) {
exec: function(editor) {
runQuery(editor);
}
}, {
}, {
name: "explain_query",
bindKey: {
win: "Ctrl-E",
Expand Down Expand Up @@ -1362,20 +1364,20 @@ $(document).ready(function() {
apiCall("post", "/connect", {
url: url
}, function(resp) {
button.prop("disabled", false).text("Connect");
button.prop("disabled", false).text("Connect");

if (resp.error) {
connected = false;
$("#connection_error").text(resp.error).show();
} else {
connected = true;
$("#connection_window").hide();
loadDatabases();
$("#main").show();
//
fnSetConnectionInfo();
}
});
if (resp.error) {
connected = false;
$("#connection_error").text(resp.error).show();
} else {
connected = true;
$("#connection_window").hide();
loadDatabases();
$("#main").show();
//
fnSetConnectionInfo();
}
});
});

$('#lnkAddQueryTab').on('click', function(e) {
Expand Down Expand Up @@ -1468,7 +1470,7 @@ $(document).ready(function() {
}
});
break;
}
}
case "FUNC": {
editFunction(dbName, procName, procDef, function(data) {
if (data.error) {
Expand All @@ -1490,7 +1492,7 @@ $(document).ready(function() {
}
});
break;
}
}
}


Expand All @@ -1500,6 +1502,107 @@ $(document).ready(function() {
loadDatabases();
});

$('#btnSearchDB').on('click', function(e) {
var searchQuery = $.trim($('#txtSearch').val());

apiSearchDatabase(searchQuery, function(data) {
if (data.error) {
return;
}

var objData = {};
var arrDB = [];

data.rows.forEach(function(val) {
var dbName = val[1];

var objDBObject = {};
objDBObject.name = val[0];
objDBObject.type = val[2];

var a = objData[dbName];

if (typeof (a) === "undefined") {
objData[dbName] = [];
arrDB.push(dbName);
}

objData[dbName].push(objDBObject);
});

var objTree = [];
arrDB.forEach(function(dbName) {
var dbTable = {
label: 'Table',
type: 'tbl-holder',
children: []
};
var dbProc = {
label: 'Procedure',
children: [],
type: 'sp-holder'
};
var dbFunc = {
label: 'Function',
children: [],
type: 'fn-holder'
};
var dbView = {
label: 'View',
children: [],
type: 'vw-holder'
};


objData[dbName].forEach(function(val) {
if (val.type === 'TBL') {
dbTable.children.push({
label: val.name,
type: 'table'
});
} else if (val.type === 'PROC') {
dbProc.children.push({
label: val.name,
type: 'procedure'
});
} else if (val.type === 'FUNC') {
dbFunc.children.push({
label: val.name,
type: 'function'
});
}
});

objTree.push({
label: dbName,
type: 'database',
children: [dbTable, dbProc, dbFunc]
});
});
console.log(objTree);

$tree.tree('loadData', objTree);

//Open all nodes
//https://github.com/mbraak/jqTree/issues/206
var tree = $tree.tree('getTree');

tree.iterate(
function(node, level) {
if (!node.hasChildren()) {
// This will open the folder
$tree.tree('selectNode', node);
return false;
}

return true;
}
);

$tree.tree('selectNode', null);
});
});

initModals();

initEditor("custom_query");
Expand Down
2 changes: 1 addition & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func splice(s string, idx int, rem int, sAdd string) string {
return (s[0:idx] + sAdd + s[(idx+rem):len(s)])
}

func getHostUser(url string) (string, string) {
func getHostUserFromConnString(url string) (string, string) {
colonIndx := strings.Index(url, ":")
userName := url[0:colonIndx]

Expand Down

0 comments on commit f2071a7

Please sign in to comment.