浏览代码

Add query string assembly.

Add:
    static/js/index.advanced-search-form.js
        -Add `assembleQuery()`, which constructs a query string from the
        contents of the fields of the advanced search form's search groups.
tags/v1.0^2
Severyn Kozak 10 年前
父节点
当前提交
aa3cd9d16b
共有 2 个文件被更改,包括 50 次插入5 次删除
  1. +43
    -4
      static/js/index.advanced-search-form.js
  2. +7
    -1
      templates/index.html

+ 43
- 4
static/js/index.advanced-search-form.js 查看文件

@@ -84,7 +84,7 @@ var searchGroups = $("div#search-groups");
$(".search-group#selected ." + fieldId).datepicker();
}
else
$("div.search-group#selected ." + fieldId).remove()
$("div.search-group#selected #" + fieldId).remove()
});
}());

@@ -97,16 +97,55 @@ function createSearchGroupInput(fieldId){
return [
"<div id='" + fieldId + "'>",
"<div>" + fieldId.replace(/-/g, " ") + "</div>",
"<input class='" + fieldId + "'type='text'/>",
"<input class='" + fieldId + "' name='" + fieldId + "'type='text'>",
"<input type='checkbox' name='regex'><span>Regex</span>",
"</div>"
].join("");
}

/*
* Create a query from advanced-search groups.
*/
function assembleQuery(){
var groups = searchGroups.children(".search-group");
var groupQueries = [];

for(var group = 0; group < groups.length; group++)
console.log(group);
for(var group = 0; group < groups.length; group++){
var inputs = groups[group].querySelectorAll("input[type=text]");
var groupQuery = []
for(var field = 0; field < inputs.length; field++)
if(inputs[field].value.length > 0)
groupQuery.push(genFieldQueryString(inputs[field]));

groupQueries.push(groupQuery.join(" AND "));
}

// console.log(groupQueries.join(" OR "));
}

/*
* Generate a processed query string for an input field's value.
*
* @param field An `input[type=text]` DOM element.
*/
function genFieldQueryString(field){
var terms = field.value.replace(/\\/g, "\\\\").replace(/\"/g, "\\\"");
if(field.value.indexOf('"') >= 0)
return '"' + field.getAttribute("name") + ":" + terms + '"';
return terms;
}

(function testQueryStringGeneration(){
var sampleStrings = [
"A B",
"A \\ B",
'"A \\" B"',
'"A \\" B" C D "A B"',
];
for(var string = 0; string < sampleStrings.length; string++)
console.log([
(sampleStrings[string] + " ").slice(0, 12),
":",
" " + genFieldQueryString($("<input>").val(sampleStrings[string])[0])
].join(""));
})();

+ 7
- 1
templates/index.html 查看文件

@@ -84,11 +84,17 @@
<div class="search-group" id="selected">
<div id="language">
<div>language</div>
<input class="language" type="text">
<input class="language" name="language" type="text">
<input type="checkbox" name="regex"><span class="regex">Regex</span>
</div>
</div>
</div>

<!-- Temporary. -->
<button onclick="assembleQuery()">
Assemble
</button>

</div>
</form>
</div>


正在加载...
取消
保存