Skip to content

Commit

Permalink
+ specs, ! choices -> queries and grouping of categories
Browse files Browse the repository at this point in the history
  • Loading branch information
floere committed Mar 5, 2012
1 parent 16f0531 commit 6251fb6
Show file tree
Hide file tree
Showing 10 changed files with 240 additions and 119 deletions.
36 changes: 18 additions & 18 deletions client/javascripts/picky.min.js

Large diffs are not rendered by default.

Binary file modified client/javascripts/picky.source.js.tar
Binary file not shown.
83 changes: 55 additions & 28 deletions client/javascripts/source/picky.allocation_renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ function AllocationRenderer(config) {
this.query = '';
this.explanation = '';

// Contracts the originals of the zipped.
// Contracts the originals/parsed of the zipped into
// an array of originals/parsed.
//
// Example:
// ['cat2', 'Orig1', 'parsed1'],
// ['cat1', 'Orig2', 'parsed2'],
// ['cat2', 'Orig3', 'parsed3']
// becomes
// ['cat2', ['Orig1', 'Orig3'], ['parsed1', 'parsed3']],
// ['cat1', ['Orig2'], ['parsed2']]
//
function contract(zipped) {
var originals = {}; // Remembers the combined values.
Expand All @@ -31,12 +40,12 @@ function AllocationRenderer(config) {
for (i = 0, l = zipped.length; i < l; i++) {
var key = zipped[i][0];
if (key in originals) {
originals[key] = originals[key] + ' ' + zipped[i][1];
// parsed[key] = parsed[key] + ' ' + zipped[i][2];
originals[key].push(zipped[i][1]);
parsed[key].push(zipped[i][2]);
remove.push(i);
} else {
originals[key] = zipped[i][1];
// parsed[key] = zipped[i][2];
originals[key] = [zipped[i][1]];
parsed[key] = [zipped[i][2]];
insert[i] = key;
}
}
Expand All @@ -45,7 +54,7 @@ function AllocationRenderer(config) {
//
for (i in insert) {
zipped[i][1] = originals[insert[i]];
// zipped[i][2] = parsed[insert[i]];
zipped[i][2] = parsed[insert[i]];
}

// Remove the ones from zipped we don't like. From the end.
Expand All @@ -62,22 +71,21 @@ function AllocationRenderer(config) {
// choice formatting defined in the config.
//
function makeUpMissingFormat(keys) {
var result = [];
keys.each(function(i, _) {
result.push('%' + (i+1) + '$s');
keys.map(function(i, _) {
return '%' + (i+1) + '$s';
});
return result.join(' ');
return keys.join(' ');
};
this.makeUpMissingFormat = makeUpMissingFormat;

//
//
function rendered(zipped) {
function rendered(allocation) {
// Return an empty string if there are no combinations.
//
if (zipped.length == 0) { return ''; };
if (allocation.length == 0) { return ''; };

zipped = contract(zipped);
var zipped = contract(allocation);

var key_ary = zipped;
key_ary.sort(function(zipped1, zipped2) {
Expand All @@ -99,11 +107,16 @@ function AllocationRenderer(config) {

// Get the formatting to be replaced.
//
var formatting = choices[keys.join(',')] || (choices[keys] = makeUpMissingFormat(keys));
var formatting = choices[keys.join(',')]
if (formatting === undefined) { formatting = (choices[keys] = makeUpMissingFormat(keys)) };

// If someone uses the simple format, change into complex format.
//
if (typeof formatting === "string") {
choices[keys] = { format: formatting };
choices[keys] = {
format: formatting,
ignoreSingle: true
};
formatting = choices[keys];
};

Expand All @@ -114,20 +127,28 @@ function AllocationRenderer(config) {
//
zipped.each(function(i, original_token) {
var category = original_token[0];
var word = original_token[2];
var words = original_token[2];

if (formatting.filter) { word = formatting.filter(word); }
if (formatting.filter) {
words.map(function(i, word) {
return formatting.filter(word);
});
}

var explanation = explanations[category] || category;
if (single && !(formatting && formatting.ignoreSingle)) {
result = word + '&nbsp;(' + explanation + ')';
var explanation = explanations[category] || category;
result = words.join('&nbsp;') + '&nbsp;(' + explanation + ')';
return result;
}

word = word.replace(/[\w,]+:(.+)/, "$1");

// Remove the category.
//
words.map(function(i, word) {
return word.replace(/[\w,]+:(.+)/, "$1");
});

var regexp = new RegExp("%" + (i+1) + "\\$s", "g");
result = result.replace(regexp, word);
result = result.replace(regexp, words.join('&nbsp;'));
});

return result;
Expand Down Expand Up @@ -188,7 +209,7 @@ function AllocationRenderer(config) {
//
last_part = last_part[last_part.length-1];

// And append ellipses. TODO Duplicate text!
// And append "ellipses".
//
if (!nonPartial.include(last_part[0])) { last_part[1] = last_part[1].valueOf() + '...'; }

Expand All @@ -198,14 +219,19 @@ function AllocationRenderer(config) {

// Creates a query string from combination and originals.
//
function querify(zipped) {
function querify(combination) {
var query_parts = [];
var qualifier;
var original;

for (var i in zipped) {
qualifier = zipped[i][0];
for (var i in combination) {
qualifier = combination[i][0];
qualifier = qualifiers[qualifier] || qualifier; // Use the returned qualifier if none is given.
query_parts[i] = qualifier + ':' + zipped[i][2]; // TODO Use original.
original = combination[i][1];
original = original || "";

var partial = original.charAt(original.length - 1) == '*' ? '*' : ''; // TODO This is not the way to do this!
query_parts[i] = qualifier + ':' + combination[i][2] + partial;
};

return query_parts.join(' ');
Expand All @@ -221,7 +247,8 @@ function AllocationRenderer(config) {
//
var result = [];
groups.each(function(i, group) {
result.push(rendered(group));
var render = rendered(group);
if (render) { result.push(render); }
});

return result.join(' ');
Expand Down
2 changes: 1 addition & 1 deletion client/javascripts/source/picky.allocations_cloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ var PickyAllocationsCloud = function(view, config) {
var shown = [];

allocations.each(function(i, allocation) {
var rendered = allocationRenderer.render(allocation);
var query = allocationRenderer.querify(allocation.combination);
var rendered = allocationRenderer.render(allocation); // Note: Render destroys the data.

rendered = listItem(rendered, allocation.count);
rendered.bind('click', { query: query }, allocationChosenCallback);
Expand Down
7 changes: 7 additions & 0 deletions client/javascripts/source/picky.extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,11 @@ Array.prototype.each = function(callback) {
callback(i, this[i]);
}
return this;
}

Array.prototype.map = function(callback) {
for(var i = 0, l = this.length; i < l; i++) {
this[i] = callback(i, this[i]);
}
return this;
}
103 changes: 78 additions & 25 deletions client/javascripts/spec/picky.allocation_renderer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ describe(
function() {
renderer = new AllocationRenderer({
locale: 'en',
groups: [['invisible']], // Invisible is first, always.
choices: {
en:{
'title': {
Expand All @@ -16,7 +17,9 @@ describe(
filter: function(text) { return text.toLowerCase(); },
ignoreSingle: true
},
'invisible': '', // Not visible.
'author,title': '%1$s, who wrote %2$s',
'invisible,title': 'only %2$s is visible'
}
}
});
Expand Down Expand Up @@ -47,8 +50,8 @@ describe(
['cat1', 'Orig2', 'parsed2'],
['cat2', 'Orig3', 'parsed3']
]).compare([
['cat2', 'Orig1 Orig3', 'parsed1 parsed3'], // TODO Should this be an array? Is just parsed1 ok? Do we care?
['cat1', 'Orig2', 'parsed2']
['cat2', ['Orig1', 'Orig3'], ['parsed1', 'parsed3']],
['cat1', ['Orig2'], ['parsed2']]
]);
});
it("is correct", function() {
Expand All @@ -58,30 +61,36 @@ describe(
['cat2', 'Orig3', 'parsed3'],
['cat1', 'Orig4', 'parsed4']
]).compare([
['cat2', 'Orig1 Orig3', 'parsed1 parsed3'], // TODO Is just parsed1 ok? Do we care?
['cat1', 'Orig2 Orig4', 'parsed2 parsed4']
['cat2', ['Orig1', 'Orig3'], ['parsed1', 'parsed3']],
['cat1', ['Orig2', 'Orig4'], ['parsed2', 'parsed4']]
]);
});
});
describe("rendered", null, function() {
it("is correct", function() {
return renderer.rendered([['cat1', 'Orig1 Orig2', 'parsed1']]) == "parsed1&nbsp;(cat1)";
});
it("is correct", function() {
return renderer.rendered([
['cat1', 'Orig1 Orig2', 'parsed1'],
['cat2', 'Orig3 Orig4', 'parsed2']
['cat1', 'Orig1', 'parsed1'],
['cat2', 'Orig2', 'parsed2']
]) == "parsed1 parsed2";
});
it("is correct", function() {
return renderer.rendered(
[
['cat1', 'Orig1', 'parsed1'],
['cat1', 'Orig2', 'parsed2']
]
) == "parsed1&nbsp;parsed2";
});
it("is correct", function() {
return renderer.rendered([
['title', 'Title1', 'title1']
]) == "TITLE1&nbsp;(title)";
});
it("is correct", function() {
return renderer.rendered([
['title', 'Title1 Title2', 'title1 title2']
]) == "TITLE1 TITLE2&nbsp;(title)";
['title', 'Title1', 'title1'],
['title', 'Title2', 'title2']
]) == "TITLE1&nbsp;TITLE2&nbsp;(title)";
});
it("is correct", function() {
return renderer.rendered([
Expand All @@ -90,8 +99,9 @@ describe(
});
it("is correct", function() {
return renderer.rendered([
['author', 'Author1 Author2', 'author1 author2']
]) == "<em>author1 author2</em>";
['author', 'Author1', 'author1'],
['author', 'Author2', 'author2']
]) == "<em>author1&nbsp;author2</em>";
});
it("is correct", function() {
return renderer.rendered([
Expand All @@ -105,32 +115,75 @@ describe(
['author', 'Author1', 'author:author1']
]) == "author1, who wrote title1";
});
it("is correct", function() {
return renderer.rendered([
['invisible', 'Invisible1', 'invisible:invisible1'],
['title', 'Title1', 'title:title1']
]) == "only title1 is visible";
});
it("is correct", function() {
return renderer.rendered([
['invisible', 'Invisible1', 'invisible:invisible1'],
['invisible', 'Invisible2', 'invisible:invisible2'],
['title', 'Title1', 'title:title1']
]) == "only title1 is visible";
});
it("is correct", function() {
return renderer.rendered([
['title', 'Title1', 'title:title1'],
['invisible', 'Invisible1', 'invisible:invisible1'],
['invisible', 'Invisible2', 'invisible:invisible2']
]) == "only title1 is visible";
});
});
describe("groupify", null, function() {
it("is correct", function() {
return renderer.groupify([
['cat1', 'Orig1', 'parsed1']
]).compare([['cat1', 'Orig1...', 'parsed1']]);
]).compare([
['cat1', 'Orig1...', 'parsed1']
]);
});
it("is correct", function() {
return renderer.groupify([
['cat1', 'Orig1', 'parsed1'],
['cat2', 'Orig2', 'parsed2']
]).compare([
['cat1', 'Orig1', 'parsed1'],
['cat2', 'Orig2...', 'parsed2']
]);
[],
[
['cat1', 'Orig1', 'parsed1'], // Both in the second group.
['cat2', 'Orig2...', 'parsed2']
]
]);
});
it("is correct", function() {
return renderer.groupify([
['cat1', 'Orig1', 'parsed1'],
['cat2', 'Orig2', 'parsed2'],
['cat1', 'Orig3', 'parsed3']
['cat1', 'Orig3', 'parsed3']
]).compare([
[],
[
['cat1', 'Orig1', 'parsed1'], // All in the second group.
['cat2', 'Orig2', 'parsed2'],
['cat1', 'Orig3...', 'parsed3']
]
]);
});
it("is correct", function() {
return renderer.groupify([
['cat1', 'Orig1', 'parsed1'],
['invisible', 'Invisible2', 'invisible2'],
['cat1', 'Orig2', 'parsed2']
]).compare([
['cat1', 'Orig1', 'parsed1'],
['cat2', 'Orig2', 'parsed2'],
['cat1', 'Orig3...', 'parsed3']
]);
[
['invisible', 'Invisible2', 'invisible2']
],
[
['cat1', 'Orig1', 'parsed1'],
['cat1', 'Orig2...', 'parsed2']
]
]);
});
});
describe("querify", null, function() {
Expand All @@ -141,7 +194,7 @@ describe(
});
it("is correct", function() {
return renderer.querify([
['cat1', 'Orig1*', 'parsed1*']
['cat1', 'Orig1*', 'parsed1']
]) == "cat1:parsed1*";
});
it("is correct", function() {
Expand All @@ -152,7 +205,7 @@ describe(
});
it("is correct", function() {
return renderer.querify([
['cat1', 'Orig1*', 'parsed1*'],
['cat1', 'Orig1*', 'parsed1'],
['cat2', 'Orig2', 'parsed2']
]) == "cat1:parsed1* cat2:parsed2";
});
Expand All @@ -161,7 +214,7 @@ describe(
it("is correct", function() {
return renderer.suggestify([
['cat1', 'Orig1', 'parsed1']
]) == "parsed1&nbsp;(cat1)";
]) == "parsed1";
});
it("is correct", function() {
return renderer.suggestify([
Expand Down
Loading

0 comments on commit 6251fb6

Please sign in to comment.