Commit 1c804868 authored by Cédric Le Ninivin's avatar Cédric Le Ninivin

Add Basic Grep to search among files

parent 1b2cb43d
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, user-scalable=no" />
<title>CribJS Header</title>
<!-- renderjs -->
<script src="../lib/rsvp.js" type="text/javascript"></script>
<script src="../lib/renderjs.js" type="text/javascript"></script>
<script src="./gadget_global.js" type="text/javascript"></script>
<!-- Custom -->
<script src="./gadget_cribjs_page_grep.js" type="text/javascript"></script>
</head>
<body>
<div class="nav_content url_list container">
<form class="crib-url-list-content">
<h3>Grep URL List</h3>
<label>Search:
<input name="search-pattern" class="search-pattern form-control" type="text" size="50" value="">
</label>
<button type="submit" name="list-contents" class="btn btn-default">Search</button>
<table class="table table-striped table-condensed">
<thead>
<tr>
<th>Found</th>
<th>Url</th>
<th>Go</th>
</tr>
</thead>
<tfoot>
</tfoot>
<tbody>
</tbody>
</table>
</form>
</div>
</body>
</html>
\ No newline at end of file
/*global window, rJS, RSVP, jIO, document, loopEventListener, RegExp */
/*jslint nomen: true, indent: 2, maxerr: 3*/
(function (window, rJS, jIO, loopEventListener) {
"use strict";
function getURLContentAsText(gadget, url) {
return new RSVP.Queue()
.push(function () {
return gadget.crib_sw_get(url);
})
.push(function (data) {
var type;
type = data.type;
return jIO.util.readBlobAsText(data, type);
})
.push(function (event) {
return event.target.result;
});
}
function displayURLList(gadget, event) {
var url_list = [],
grep_pattern = gadget.props.element.querySelector(".search-pattern").value;
return new RSVP.Queue()
.push(function () {
return gadget.crib_sw_allDocs();
})
.push(function (data) {
var promise_list = [],
tmp_url_list = [],
url;
if (data.hasOwnProperty("urls")) {
tmp_url_list = data.urls;
} else {
tmp_url_list = data;
}
for (url in tmp_url_list) {
if (tmp_url_list.hasOwnProperty(url)) {
// XXX-CLN Ugly way to filter
if (url.endsWith(".js") || url.endsWith(".html") ||
url.endsWith(".json") || url.endsWith(".css")) {
url_list.push(url);
promise_list.push(RSVP.all([
gadget.getUrlFor({page: 'editor', url: url}),
grep_pattern ? getURLContentAsText(gadget, url) : ""
]));
}
}
}
return RSVP.all(promise_list);
})
.push(function (result_list) {
var contentsElement = gadget.props.element.querySelector('.crib-url-list-content tbody'),
footer_element = gadget.props.element.querySelector('.crib-url-list-content tfoot'),
url_number, result_number = 0,
url, trElement, tdElement;
// Clear out the existing items from the list.
while (contentsElement.firstChild) {
contentsElement.removeChild(contentsElement.firstChild);
}
if (grep_pattern) {
grep_pattern = new RegExp(grep_pattern);
}
// Add each cached URL to the list, one by one.
for (url_number = 0; url_number < url_list.length; url_number++) {
url = url_list[url_number];
var element,
is_present = grep_pattern ?
grep_pattern.test(result_list[url_number][1]) : true;
if (is_present) {
trElement = document.createElement('tr');
tdElement = document.createElement('td');
tdElement.innerHTML = "<span>&#x2714;</span>";
trElement.appendChild(tdElement);
tdElement = document.createElement('td');
element = document.createElement('a');
element.setAttribute('href', result_list[url_number][0]);
element.textContent = url;
tdElement.appendChild(element);
trElement.appendChild(tdElement);
tdElement = document.createElement('td');
element = document.createElement('a');
element.textContent = "Go";
element.setAttribute('href', url);
element.setAttribute('target', "_blank");
element.setAttribute("class", "btn btn-primary btn-xs");
tdElement.appendChild(element);
trElement.appendChild(tdElement);
contentsElement.appendChild(trElement);
result_number += 1;
}
}
while (footer_element.firstChild) {
footer_element.removeChild(footer_element.firstChild);
}
trElement = document.createElement('tr');
tdElement = document.createElement('td');
tdElement.innerHTML = "<span>" + result_number + " &#x2714;</span>";
trElement.appendChild(tdElement);
tdElement = document.createElement('td');
tdElement.innerHTML = url_number + " URLs";
trElement.appendChild(tdElement);
tdElement = document.createElement('td');
trElement.appendChild(tdElement);
footer_element.appendChild(trElement);
});
}
rJS(window)
.ready(function (g) {
g.props = {};
return g.getElement()
.push(function (element) {
g.props.element = element;
g.props.start_deferred = RSVP.defer();
});
})
.declareAcquiredMethod("crib_sw_allDocs", "crib_sw_allDocs")
.declareAcquiredMethod("crib_sw_get", "crib_sw_get")
.declareAcquiredMethod("redirect", "redirect")
.declareAcquiredMethod("getUrlFor", "getUrlFor")
.declareMethod('render', function (options) {
var gadget = this;
if (options === undefined)
options = {};
gadget.props.options = options;
return new RSVP.Queue()
.push(function () {
var pattern = "";
if (options.grep_pattern !== undefined) {
pattern = options.grep_pattern;
}
gadget.props.element.querySelector(".search-pattern")
.value = pattern;
return displayURLList(gadget, undefined);
})
.push(function () {
return gadget.props.start_deferred.resolve();
});
})
.declareService(function () {
var gadget = this;
return new RSVP.Queue()
.push(function () {
return gadget.props.start_deferred.promise;
})
.push(function () {
return loopEventListener(
gadget.props.element.querySelector(".crib-url-list-content"),
'submit',
false,
function (event) {
gadget.redirect({
page: "grep",
grep_pattern: gadget.props.element.querySelector(".search-pattern")
.value
});
}
);
});
});
}(window, rJS, jIO, loopEventListener));
\ No newline at end of file
......@@ -20,6 +20,7 @@
<li>Check <a class="jslint" href="#page=jslint">JSLint</a></li>
<li><a class="mass_remove" href="#page=mass_remove">Remove</a> uncessary URLs</li>
<li><a class="select_site" href="#page=select_site">Select Site to Edit</a></li>
<li><a class="grep" href="#page=grep">Grep</a> among js and html files</li>
</ul>
</div>
</body>
......
......@@ -22,7 +22,7 @@
.declareAcquiredMethod("getUrlFor", "getUrlFor")
.declareMethod('render', function (options) {
var gadget = this,
page_list = ["select_site", "jslint", "mass_remove"],
page_list = ["select_site", "jslint", "mass_remove", "grep"],
promise_list = [];
page_list.forEach(function (page) {
......
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