Add support for query.one, mixins, and DOM queries, and make the Riot query optional

master
Sven Slootweg 8 years ago
parent b083fa75df
commit 17b7fbe58d

@ -29,52 +29,95 @@ function recurseTags(tag) {
function createQuery(query) { function createQuery(query) {
let {riotQuery, domQuery} = queryParser.parse(query); let {riotQuery, domQuery} = queryParser.parse(query);
let flattenedQueries = flatten(riotQuery); let tagObtainer;
tagQueries(flattenedQueries);
return function traverse(tag) { if (riotQuery != null) {
let results = []; let flattenedQueries = flatten(riotQuery);
tagQueries(flattenedQueries);
tagObtainer = function(tag) {
let results = [];
flattenedQueries.forEach((subQuery) => {
let lastFound = [tag];
flattenedQueries.forEach((subQuery) => { subQuery.forEach((segment) => {
let lastFound = [tag]; let nextLastFound = [];
subQuery.forEach((segment) => { lastFound.forEach((subTag) => {
let nextLastFound = []; //console.log("subtag", subQuery, segment, subTag);
let found;
lastFound.forEach((subTag) => { if (segment.type === "identifier") {
//console.log("subtag", subQuery, segment, subTag); found = ensureArray(subTag.tags[segment.value]);
let found; } else if (segment.type === "wildcard") {
found = recurseTags(subTag);
}
if (segment.type === "identifier") { if (found.length > 0) {
found = ensureArray(subTag.tags[segment.value]); nextLastFound = nextLastFound.concat(found)
} else if (segment.type === "wildcard") { }
found = recurseTags(subTag); });
}
if (found.length > 0) { lastFound = nextLastFound;
nextLastFound = nextLastFound.concat(found)
}
}); });
lastFound = nextLastFound; results = results.concat(lastFound);
}); });
results = results.concat(lastFound); return dedupe(results, result => result._riot_id);
}); }
} else {
tagObtainer = function(tag) {
return [tag];
}
}
return function traverse(tag) {
let riotResults = tagObtainer(tag);
return dedupe(results, result => result._riot_id); if (domQuery == null) {
return riotResults;
} else {
let domElements = [];
riotResults.forEach((foundTag) => {
domElements = domElements.concat(Array.from(foundTag.root.querySelectorAll(domQuery)));
});
return domElements;
}
} }
} }
let queryCache = {}; let queryCache = {};
module.exports = function(tag, query) { function query(tag, queryString) {
if (queryCache[query] == null) { if (queryCache[queryString] == null) {
queryCache[query] = createQuery(query); queryCache[queryString] = createQuery(queryString);
} }
// TODO: DOM traversal // TODO: DOM traversal
return queryCache[query](tag); return queryCache[queryString](tag);
}
query.one = function queryOne(tag, queryString) {
let results = query(tag, queryString);
if (results.length === 0) {
throw new Error(`No matches found for query ${queryString}`);
} else {
return results[0];
}
}
/* For use with `riot.mixin(query.mixin)`: */
query.mixin = {
init: function() {
this.query = query.bind(null, this);
this.queryOne = query.one.bind(null, this);
}
} }
module.exports = query;

@ -19,7 +19,7 @@
} }
start start
= riotQuery:riotQuery domQuery:domQuerySuffix? { return combineFinal(riotQuery, domQuery) } = riotQuery:riotQuery? domQuery:domQuerySuffix? { return combineFinal(riotQuery, domQuery) }
riotQuery riotQuery
= segment:riotQuerySegment subSegments:("/" riotQuerySegment)* { return concatRepeat(segment, subSegments, 1); } = segment:riotQuerySegment subSegments:("/" riotQuerySegment)* { return concatRepeat(segment, subSegments, 1); }

Loading…
Cancel
Save