Commit b041e5e4 authored by ale's avatar ale

save/restore state from hashtag; autolink tags

parent 855b6d70
......@@ -27,6 +27,15 @@ lens.util.timestampToString = function(ts) {
return lens.util.timestampToDate(ts).strftime('%Y/%m/%d %H:%M:%S');
};
// Template helper functions.
lens.util.replaceAttrsInMsg(log) {
var msg = log.msg.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;');
$.each(doc, function(key, value) {
if (key.substring(0, 5) == "attr_") {
msg = msg.replace(value, '<a class="attr" name="' + key + '">'+ value + '</a>');
}
});
};
/**
* Lens object.
......@@ -71,6 +80,79 @@ lens.Lens = function(config) {
this.error_template = $.template(null, lens.ERROR_TEMPLATE);
};
/**
* Save the current state to the hashtag.
*/
lens.Lens.prototype.saveState = function() {
var hashTag = ('#q:' + escape(lens.getQuery())
+ ',o:' + this.offset
+ ',p:' + this.page_size
+ ',trs:' + this.time_range.start
+ ',tre:' + this.time_range.end);
this.cur_hash = hashTag;
window.location.hash = hashTag;
};
/**
* Restore the current state from the location hashtag.
*/
lens.Lens.prototype.restoreState = function() {
var hashTag = window.location.hash;
// Return immediately if there's nothing to parse, or if we got called
// due to having set the hashtag ourselves.
if (!hashTag || hashTag == '#' || hashTag == this.cur_hash) {
return;
}
this.cur_hash = hashTag;
// Ugly parsing of the hash string.
var hashInfo = {};
$.each(hashTag.substr(1).split(','), function(idx, value) {
var kv = value.split(':');
hashInfo[kv[0]] = unescape(kv.slice(1).join(':'));
});
// Copy the parsed data into the lens state.
if (hashInfo.o) { this.offset = parseInt(hashInfo.o); }
if (hashInfo.p) { this.page_size = parseInt(hashInfo.p); }
if (hashInfo.trs) { this.rangeDays = parseInt(hashInfo.trs); }
if (hashInfo.tre) { this.rangeDays = parseInt(hashInfo.tre); }
if (hashInfo.q) { this.cur_query = hashInfo.q; }
this.refresh();
};
/**
* Add "onclick" handlers to the autogenerated filter links.
*/
lens.Lens.prototype.loadAttrLinks = function() {
var lensobj = this;
$('a.attr').click(function() {
var attrName = $(this).attr('name');
var attrValue = $(this).text();
lensobj.search('(' + attrName + ':"' + attrValue + '")');
});
};
/**
* Add "onclick" handlers to the standard filter links (host/prog/pid).
*/
lens.Lens.prototype.loadFilterLinks = function() {
var lensobj = this;
$('a.host').click(function() {
lensobj.appendToQuery('host:' + $(this).text());
});
$('a.prio').click(function() {
var facility = $(this).text().split('.')[0];
lensobj.appendToQuery('facility:' + facility);
});
$('a.prog').click(function() {
lensobj.appendToQuery('program:"' + $(this).text() + '"');
});
$('a.pid').click(function() {
lensobj.appendToQuery('pid:' + $(this).text());
});
};
/**
* Render some logs into an element.
*
......@@ -84,6 +166,8 @@ lens.Lens.prototype.renderLogs = function(logs) {
out += $.tmpl(template, elem);
});
this.results_view_elem.html(out);
this.loadAttrLinks();
this.loadFilterLinks();
};
lens.Lens.prototype.renderPagination = function(logs) {
......@@ -150,6 +234,7 @@ lens.Lens.prototype.refresh = function() {
lensobj.total_results = data.total_results;
lensobj.render(data.results);
lensobj.clearLoading();
lensobj.saveState();
},
error: this.ajaxError
});
......@@ -164,7 +249,12 @@ lens.Lens.prototype.search = function(query_str) {
this.offset = 0;
this.time_range = {start:0, end: 0};
this.refresh();
}
};
// Functions to manipulate the current query.
lens.Lens.prototype.appendToQuery = function(add_str) {
this.search(this.cur_query + ' ' + add_str);
};
/**
* General AJAX error callback.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment