Permalink
Browse files

+ rewritten JS, extracted view components outwards, replaced Localiza…

…tion
  • Loading branch information...
1 parent d49df07 commit e1509f25cff8bdb527c923c42671ecd3bc34412b @floere committed Feb 25, 2012
Oops, something went wrong.
Binary file not shown.
@@ -3,23 +3,20 @@ function AllocationRenderer(allocationChosenCallback, config) {
var locale = config.locale;
- var qualifiers = Localization.qualifiers && Localization.qualifiers[locale] || {};
- var explanations = Localization.explanations && Localization.explanations[locale] || {};
- var explanation_delimiter = Localization.explanation_delimiters[locale];
-
- var choiceGroups = config.groups || [];
- var choices = Localization.choices && Localization.choices[locale] || {};
+ // TODO Maybe make dynamic.
+ //
+ var qualifiers = config.qualifiers && config.qualifiers[locale] || {};
+ var explanations = config.explanations && config.explanations[locale] || {};
+ var choiceGroups = config.groups || [];
+ var choices = config.choices && config.choices[locale] || {};
+ var nonPartial = config['nonPartial'] || [];
// Those are of interest to the public.
//
this.text = '';
this.query = '';
this.explanation = '';
- // TODO parametrize.
- //
- var no_ellipses = ['street_number', 'zipcode'];
-
// Contracts the originals of the zipped.
//
function contract(zipped) {
@@ -46,18 +43,25 @@ function AllocationRenderer(allocationChosenCallback, config) {
for (i = remove.length-1; i >= 0; i--) {
zipped.remove(remove[i]);
}
+
return zipped;
};
this.contract = contract;
// Renders the given combinations according to the
// choice formatting defined in the config.
//
- function makeUpMissingFormat(key) {
- return $.map(key, function(element, i) {
- return '%' + (i+1) + '$s';
- }).join(' ');
+ function makeUpMissingFormat(keys) {
+ var result = [];
+ keys.each(function(i, _) {
+ result.push('%' + (i+1) + '$s');
+ });
+ return result.join(' ');
};
+ this.makeUpMissingFormat = makeUpMissingFormat;
+
+ //
+ //
function rendered(zipped) {
// Return an empty string if there are no combinations.
//
@@ -72,33 +76,33 @@ function AllocationRenderer(allocationChosenCallback, config) {
// Now that it's sorted, get the right string.
//
- var key = [];
+ var keys = [];
for (var i = 0, l = key_ary.length; i < l; i++) {
- key.push(key_ary[i][0]);
+ keys.push(key_ary[i][0]);
};
// Get the right formatting or make up a simple one.
//
// var result = choices[key] || (choices[key] = makeUpMissingFormat(key));
- var single = key.length == 1;
+ var single = keys.length == 1;
// Get the formatting to be replaced.
//
- var formatting = choices[key] || (choices[key] = makeUpMissingFormat(key));
+ var formatting = choices[keys.join(',')] || (choices[keys] = makeUpMissingFormat(keys));
// If someone uses the simple format, change into complex format.
//
- if ($.type(formatting) === "string") {
- choices[key] = { format: formatting };
- formatting = choices[key];
+ if (typeof formatting === "string") {
+ choices[keys] = { format: formatting };
+ formatting = choices[keys];
};
var j = 1;
var result = formatting.format;
// Replace each word into the formatting string.
//
- $.each(zipped, function(i, original_token) {
+ zipped.each(function(i, original_token) {
var category = original_token[0];
var word = original_token[2];
@@ -110,15 +114,10 @@ function AllocationRenderer(allocationChosenCallback, config) {
return result;
}
- var regexp = new RegExp("%" + j + "\\$s", "g");
+ var regexp = new RegExp("%" + (i+1) + "\\$s", "g");
result = result.replace(regexp, word);
-
- j += 1;
-
- return j;
});
-
return result;
};
this.rendered = rendered;
@@ -180,13 +179,16 @@ function AllocationRenderer(allocationChosenCallback, config) {
// And append ellipses.
//
- if (!no_ellipses.include(last_part[0])) { last_part[1] += '...'; } // TODO *
+ if (!nonPartial.include(last_part[0])) { last_part[1] += '...'; }
// Render each group and return the resulting rendered array.
//
- return $.map(groups, function(group) {
- return rendered(group);
+ var result = [];
+ groups.each(function(i, group) {
+ result.push(rendered(group));
});
+
+ return result;
};
this.groupify = groupify;
@@ -195,11 +197,13 @@ function AllocationRenderer(allocationChosenCallback, config) {
function querify(zipped) {
var query_parts = [];
var qualifier;
+
for (var i in zipped) {
qualifier = zipped[i][0];
qualifier = qualifiers[qualifier] || qualifier; // Use the returned qualifier if none is given.
query_parts[i] = qualifier + ':' + zipped[i][2];
};
+
return query_parts.join(' ');
};
this.querify = querify;
@@ -209,37 +213,10 @@ function AllocationRenderer(allocationChosenCallback, config) {
function suggestify(zipped) {
return groupify(zipped).join(' ');
};
-
-
- // Generates the text and the link.
- //
- var generate = function() {
- this.query = querify(combination);
- this.text = suggestify(combination);
-
- return self;
- };
-
- // TODO Extract.
- //
- var listItem = function(text, count) {
- return $('<li><div class="text">' + text + '</div><div class="count">' + count + '</div></li>');
- };
+ this.suggestify = suggestify;
var render = function(allocation) {
-
- var combination = allocation.combination;
- var type = allocation.type;
- var count = allocation.count;
-
- var query = querify(combination);
-
- var item = listItem(suggestify(combination), count);
-
- // TODO Move this outwards?
- //
- item.bind('click', { query: query }, allocationChosenCallback);
- return item;
+ return suggestify(allocation.combination);
};
this.render = render;
@@ -26,24 +26,34 @@ var PickyAllocationsCloud = function(view, config) {
//
//
- var allocationChosen = function(event) {
+ var allocationChosenCallback = function(event) {
hide();
view.allocationChosen(event);
};
- var allocationRenderer = new AllocationRenderer(allocationChosen, config);
+ var allocationRenderer = new AllocationRenderer(config);
+ // How an allocation renders as a list item.
+ //
+ var listItem = function(text, count) {
+ return $('<li><div class="text">' + text + '</div><div class="count">' + count + '</div></li>');
+ };
+
+ //
+ //
var createAllocationList = function(allocations) {
var shown = [];
+
allocations.each(function(i, allocation) {
- shown.push(allocationRenderer.render(allocation));
+ var rendered = allocationRenderer.render(allocation);
+ var query = allocationRenderer.querify(allocation.combination);
- // TODO Combine.
- // allocationRenderer.generate();
- // var listItem = renderListItem(allocationRenderer);
-
- // shown.push(listItem);
+ rendered = listItem(rendered, allocation.count);
+ rendered.bind('click', { query: query }, allocationChosenCallback);
+
+ shown.push(rendered);
});
+
return shown;
};
@@ -1,4 +1,3 @@
-var Localization = {};
var PickyI18n = { };
// The client handles parameters and
@@ -24,25 +23,31 @@ var PickyClient = function(config) {
//
// This needs to correspond to the parsing in the search engine.
//
- Localization.qualifiers = config.qualifiers || {};
+ config['qualifiers'] = config.qualifiers || {};
// This is used to explain the preceding word in the suggestion text.
//
// e.g. with locale it:
// ['title', 'ulysses', 'Ulysses'] => 'Ulysses (titolo)'
//
- Localization.explanations = config.explanations || {};
+ config['explanations'] = config.explanations || {};
// This is used to expain more complex combinations of categories
// in the choices.
//
// e.g. with locale en:{'author,title': '%1$s, who wrote %2$s'}
//
- Localization.choices = config.choices || {};
+ config['choices'] = config.choices || {};
// Delimiters for connecting explanations.
//
- Localization.explanation_delimiters = { de:'und', fr:'et', it:'e', en:'and', ch:'und' };
+ config['explanation_delimiters'] = {
+ ch:'und',
+ de:'und',
+ en:'and',
+ fr:'et',
+ it:'e'
+ };
// Either you pass it a backends hash with full and live,
// or you pass it full and live (urls), which will then
@@ -85,9 +90,9 @@ var PickyClient = function(config) {
// Results rendering.
//
config['results'] = $(config['resultsSelector'] || (enclosingSelector + ' div.results'));
- config['resultsDivider'] = config['resultsDivider'] || '';
- config['noAsterisks'] = config['noAsterisks'] || []; // e.g. ['category1', 'category2']
- config['wrapResults'] = config['wrapResults'] || '<ol class="results"></ol>';
+ config['resultsDivider'] = config['resultsDivider'] || '';
+ config['nonPartial'] = config['nonPartial'] || []; // e.g. ['category1', 'category2']
+ config['wrapResults'] = config['wrapResults'] || '<ol class="results"></ol>';
// The central Picky controller.
//
@@ -2,10 +2,9 @@ var PickyResultsRenderer = function(addination, config) {
var locale = config.locale;
- var results = config['results'];
var resultsDivider = config['resultsDivider'];
var allocationWrapper = config['wrapResults'];
- var noAsterisks = config['noAsterisks'];
+ var nonPartial = config['nonPartial'];
// Adds asterisks to the last token.
//
@@ -14,7 +13,7 @@ var PickyResultsRenderer = function(addination, config) {
if (last_part === undefined) { return []; }
var parts = combinations.slice(0, combinations.length-1);
if (parts == []) { parts = [parts]; }
- if (!noAsterisks.include(last_part[0])) {
+ if (!nonPartial.include(last_part[0])) {
// Replace with * unless there is already one or a tilde.
//
if (last_part[1].match(/[^\*~]$/)) { last_part[1] += '*'; }
@@ -120,7 +119,7 @@ var PickyResultsRenderer = function(addination, config) {
// Render results with the data.
//
- this.render = function(data) {
+ this.render = function(results, data) {
data.allocations.each(function(i, allocation) {
// Only render if there is something to render.
// TODO Move into methods.
@@ -117,7 +117,7 @@ var PickyView = function(picky_controller, config) {
var showResults = function(data) {
clean();
updateResultCounter(data.total);
- resultsRenderer.render(data);
+ resultsRenderer.render(results, data);
results.show();
showClearButton();
};
@@ -130,7 +130,7 @@ var PickyView = function(picky_controller, config) {
var position = $(moreSelector).position().top;
addination.remove(); // TODO Where should this be?
- resultsRenderer.render(data);
+ resultsRenderer.render(results, data);
scrollTo(position);
};
Oops, something went wrong.

0 comments on commit e1509f2

Please sign in to comment.