Skip to content

Commit

Permalink
+ rewritten JS, extracted view components outwards, replaced Localiza…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
floere committed Feb 25, 2012
1 parent d49df07 commit e1509f2
Show file tree
Hide file tree
Showing 17 changed files with 371 additions and 214 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.
97 changes: 37 additions & 60 deletions client/javascripts/source/picky.allocation_renderer.js
Expand Up @@ -3,23 +3,20 @@ function AllocationRenderer(allocationChosenCallback, config) {


var locale = config.locale; var locale = config.locale;


var qualifiers = Localization.qualifiers && Localization.qualifiers[locale] || {}; // TODO Maybe make dynamic.
var explanations = Localization.explanations && Localization.explanations[locale] || {}; //
var explanation_delimiter = Localization.explanation_delimiters[locale]; var qualifiers = config.qualifiers && config.qualifiers[locale] || {};

var explanations = config.explanations && config.explanations[locale] || {};
var choiceGroups = config.groups || []; var choiceGroups = config.groups || [];
var choices = Localization.choices && Localization.choices[locale] || {}; var choices = config.choices && config.choices[locale] || {};
var nonPartial = config['nonPartial'] || [];


// Those are of interest to the public. // Those are of interest to the public.
// //
this.text = ''; this.text = '';
this.query = ''; this.query = '';
this.explanation = ''; this.explanation = '';


// TODO parametrize.
//
var no_ellipses = ['street_number', 'zipcode'];

// Contracts the originals of the zipped. // Contracts the originals of the zipped.
// //
function contract(zipped) { function contract(zipped) {
Expand All @@ -46,18 +43,25 @@ function AllocationRenderer(allocationChosenCallback, config) {
for (i = remove.length-1; i >= 0; i--) { for (i = remove.length-1; i >= 0; i--) {
zipped.remove(remove[i]); zipped.remove(remove[i]);
} }

return zipped; return zipped;
}; };
this.contract = contract; this.contract = contract;


// Renders the given combinations according to the // Renders the given combinations according to the
// choice formatting defined in the config. // choice formatting defined in the config.
// //
function makeUpMissingFormat(key) { function makeUpMissingFormat(keys) {
return $.map(key, function(element, i) { var result = [];
return '%' + (i+1) + '$s'; keys.each(function(i, _) {
}).join(' '); result.push('%' + (i+1) + '$s');
});
return result.join(' ');
}; };
this.makeUpMissingFormat = makeUpMissingFormat;

//
//
function rendered(zipped) { function rendered(zipped) {
// Return an empty string if there are no combinations. // Return an empty string if there are no combinations.
// //
Expand All @@ -72,33 +76,33 @@ function AllocationRenderer(allocationChosenCallback, config) {


// Now that it's sorted, get the right string. // Now that it's sorted, get the right string.
// //
var key = []; var keys = [];
for (var i = 0, l = key_ary.length; i < l; i++) { 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. // Get the right formatting or make up a simple one.
// //
// var result = choices[key] || (choices[key] = makeUpMissingFormat(key)); // var result = choices[key] || (choices[key] = makeUpMissingFormat(key));


var single = key.length == 1; var single = keys.length == 1;


// Get the formatting to be replaced. // 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 someone uses the simple format, change into complex format.
// //
if ($.type(formatting) === "string") { if (typeof formatting === "string") {
choices[key] = { format: formatting }; choices[keys] = { format: formatting };
formatting = choices[key]; formatting = choices[keys];
}; };


var j = 1; var j = 1;
var result = formatting.format; var result = formatting.format;


// Replace each word into the formatting string. // 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 category = original_token[0];
var word = original_token[2]; var word = original_token[2];


Expand All @@ -110,15 +114,10 @@ function AllocationRenderer(allocationChosenCallback, config) {
return result; return result;
} }


var regexp = new RegExp("%" + j + "\\$s", "g"); var regexp = new RegExp("%" + (i+1) + "\\$s", "g");
result = result.replace(regexp, word); result = result.replace(regexp, word);

j += 1;

return j;
}); });



return result; return result;
}; };
this.rendered = rendered; this.rendered = rendered;
Expand Down Expand Up @@ -180,13 +179,16 @@ function AllocationRenderer(allocationChosenCallback, config) {


// And append ellipses. // 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. // Render each group and return the resulting rendered array.
// //
return $.map(groups, function(group) { var result = [];
return rendered(group); groups.each(function(i, group) {
result.push(rendered(group));
}); });

return result;
}; };
this.groupify = groupify; this.groupify = groupify;


Expand All @@ -195,11 +197,13 @@ function AllocationRenderer(allocationChosenCallback, config) {
function querify(zipped) { function querify(zipped) {
var query_parts = []; var query_parts = [];
var qualifier; var qualifier;

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

return query_parts.join(' '); return query_parts.join(' ');
}; };
this.querify = querify; this.querify = querify;
Expand All @@ -209,37 +213,10 @@ function AllocationRenderer(allocationChosenCallback, config) {
function suggestify(zipped) { function suggestify(zipped) {
return groupify(zipped).join(' '); return groupify(zipped).join(' ');
}; };

this.suggestify = suggestify;

// 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>');
};


var render = function(allocation) { var render = function(allocation) {

return suggestify(allocation.combination);
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;
}; };
this.render = render; this.render = render;


Expand Down
26 changes: 18 additions & 8 deletions client/javascripts/source/picky.allocations_cloud.js
Expand Up @@ -26,24 +26,34 @@ var PickyAllocationsCloud = function(view, config) {


// //
// //
var allocationChosen = function(event) { var allocationChosenCallback = function(event) {
hide(); hide();
view.allocationChosen(event); 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 createAllocationList = function(allocations) {
var shown = []; var shown = [];

allocations.each(function(i, allocation) { allocations.each(function(i, allocation) {
shown.push(allocationRenderer.render(allocation)); var rendered = allocationRenderer.render(allocation);
var query = allocationRenderer.querify(allocation.combination);


// TODO Combine. rendered = listItem(rendered, allocation.count);
// allocationRenderer.generate(); rendered.bind('click', { query: query }, allocationChosenCallback);
// var listItem = renderListItem(allocationRenderer);

shown.push(rendered);
// shown.push(listItem);
}); });

return shown; return shown;
}; };


Expand Down
21 changes: 13 additions & 8 deletions client/javascripts/source/picky.client.js
@@ -1,4 +1,3 @@
var Localization = {};
var PickyI18n = { }; var PickyI18n = { };


// The client handles parameters and // The client handles parameters and
Expand All @@ -24,25 +23,31 @@ var PickyClient = function(config) {
// //
// This needs to correspond to the parsing in the search engine. // 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. // This is used to explain the preceding word in the suggestion text.
// //
// e.g. with locale it: // e.g. with locale it:
// ['title', 'ulysses', 'Ulysses'] => 'Ulysses (titolo)' // ['title', 'ulysses', 'Ulysses'] => 'Ulysses (titolo)'
// //
Localization.explanations = config.explanations || {}; config['explanations'] = config.explanations || {};


// This is used to expain more complex combinations of categories // This is used to expain more complex combinations of categories
// in the choices. // in the choices.
// //
// e.g. with locale en:{'author,title': '%1$s, who wrote %2$s'} // 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. // 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, // Either you pass it a backends hash with full and live,
// or you pass it full and live (urls), which will then // or you pass it full and live (urls), which will then
Expand Down Expand Up @@ -85,9 +90,9 @@ var PickyClient = function(config) {
// Results rendering. // Results rendering.
// //
config['results'] = $(config['resultsSelector'] || (enclosingSelector + ' div.results')); config['results'] = $(config['resultsSelector'] || (enclosingSelector + ' div.results'));
config['resultsDivider'] = config['resultsDivider'] || ''; config['resultsDivider'] = config['resultsDivider'] || '';
config['noAsterisks'] = config['noAsterisks'] || []; // e.g. ['category1', 'category2'] config['nonPartial'] = config['nonPartial'] || []; // e.g. ['category1', 'category2']
config['wrapResults'] = config['wrapResults'] || '<ol class="results"></ol>'; config['wrapResults'] = config['wrapResults'] || '<ol class="results"></ol>';


// The central Picky controller. // The central Picky controller.
// //
Expand Down
7 changes: 3 additions & 4 deletions client/javascripts/source/picky.results_renderer.js
Expand Up @@ -2,10 +2,9 @@ var PickyResultsRenderer = function(addination, config) {


var locale = config.locale; var locale = config.locale;


var results = config['results'];
var resultsDivider = config['resultsDivider']; var resultsDivider = config['resultsDivider'];
var allocationWrapper = config['wrapResults']; var allocationWrapper = config['wrapResults'];
var noAsterisks = config['noAsterisks']; var nonPartial = config['nonPartial'];


// Adds asterisks to the last token. // Adds asterisks to the last token.
// //
Expand All @@ -14,7 +13,7 @@ var PickyResultsRenderer = function(addination, config) {
if (last_part === undefined) { return []; } if (last_part === undefined) { return []; }
var parts = combinations.slice(0, combinations.length-1); var parts = combinations.slice(0, combinations.length-1);
if (parts == []) { parts = [parts]; } 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. // Replace with * unless there is already one or a tilde.
// //
if (last_part[1].match(/[^\*~]$/)) { last_part[1] += '*'; } if (last_part[1].match(/[^\*~]$/)) { last_part[1] += '*'; }
Expand Down Expand Up @@ -120,7 +119,7 @@ var PickyResultsRenderer = function(addination, config) {


// Render results with the data. // Render results with the data.
// //
this.render = function(data) { this.render = function(results, data) {
data.allocations.each(function(i, allocation) { data.allocations.each(function(i, allocation) {
// Only render if there is something to render. // Only render if there is something to render.
// TODO Move into methods. // TODO Move into methods.
Expand Down
4 changes: 2 additions & 2 deletions client/javascripts/source/picky.view.js
Expand Up @@ -117,7 +117,7 @@ var PickyView = function(picky_controller, config) {
var showResults = function(data) { var showResults = function(data) {
clean(); clean();
updateResultCounter(data.total); updateResultCounter(data.total);
resultsRenderer.render(data); resultsRenderer.render(results, data);
results.show(); results.show();
showClearButton(); showClearButton();
}; };
Expand All @@ -130,7 +130,7 @@ var PickyView = function(picky_controller, config) {
var position = $(moreSelector).position().top; var position = $(moreSelector).position().top;


addination.remove(); // TODO Where should this be? addination.remove(); // TODO Where should this be?
resultsRenderer.render(data); resultsRenderer.render(results, data);


scrollTo(position); scrollTo(position);
}; };
Expand Down

0 comments on commit e1509f2

Please sign in to comment.