Skip to content
This repository has been archived by the owner on Dec 23, 2017. It is now read-only.

Some cleanup and fixing nav search bar #17

Merged
merged 2 commits into from
Jan 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 50 additions & 48 deletions static/js/modules/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,61 +72,63 @@ var loadSingleEntity = function(e) {
})
}

module.exports = {
init: function() {
events.on('search:submitted', function(e) {
var entities = [],
promises = [],
i,
len = entitiesArray.length;

for (i = 0; i < len; i++) {
promises.push(callAPI('rest/' + entitiesArray[i] + '?q=' + encodeURIComponent(e.query) + '&per_page=5'));
var loadSearchResultsList = function(e) {
var entities = [],
promises = [],
i,
len = entitiesArray.length;

for (i = 0; i < len; i++) {
promises.push(callAPI('rest/' + entitiesArray[i] + '?q=' + encodeURIComponent(e.query) + '&per_page=5'));
}


$.when.apply($, promises).done(function() {
var i,
len = arguments.length;
e.results = {};

for (i = 0; i < len; i++) {
if (typeof arguments[i][0] === 'string') {
arguments[i][0] = JSON.parse(arguments[i][0]);
}
e.results[entitiesArray[i]] = arguments[i][0].results;
}

events.emit('render:searchResultsList', e);
}).fail(function() {
events.emit('err:load:searchResultsList');
});
}

$.when.apply($, promises).done(function() {
var i,
len = arguments.length;
e.results = {};

for (i = 0; i < len; i++) {
if (typeof arguments[i][0] === 'string') {
arguments[i][0] = JSON.parse(arguments[i][0]);
}
e.results[entitiesArray[i]] = arguments[i][0].results;
}

events.emit('render:searchResultsList', e);
}).fail(function() {
events.emit('err:load:searchResultsList');
});
});

events.on('load:browse', function(e) {
var promise = callAPI('/rest/' + entityMap[e.category] + '?fields=*');

promise.done(function(data) {
promiseResolved(data, 'render:browse', e);
}).fail(function() {
events.emit('err:load:browse');
});
});

events.on('load:searchResults', function(e) {
var promise = callAPI(buildURL(e));

promise.done(function(data) {
promiseResolved(data, 'render:searchResults', e);
}).fail(function() {
events.emit('err:load:searchResults');
});;
});
var loadSearchResults = function(e) {
var promise = callAPI(buildURL(e));

promise.done(function(data) {
promiseResolved(data, 'render:searchResults', e);
}).fail(function() {
events.emit('err:load:searchResults');
});
}

var loadBrowse = function(e) {
var promise = callAPI('/rest/' + entityMap[e.category] + '?fields=*');

promise.done(function(data) {
promiseResolved(data, 'render:browse', e);
}).fail(function() {
events.emit('err:load:browse');
});
}

module.exports = {
init: function() {
events.on('search:submitted', loadSearchResultsList);
events.on('load:browse', loadBrowse);
events.on('load:searchResults', loadSearchResults);
events.on('selected:filter', filterLoadHandler);
events.on('deselected:filter', filterLoadHandler);
events.on('nav:pagination', filterLoadHandler);

events.on('load:singleEntity', loadSingleEntity);
},

Expand Down
39 changes: 21 additions & 18 deletions static/js/modules/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,27 @@ var enableSearchForm = function() {

$submitButton.removeAttr('disabled');
$searchBox.removeAttr('disabled');
};
}

var bindSearch = function() {
$('#main').on('click', '.results-header a', function() {
event.preventDefault();

var context = {},
$target = $(event.target);

context.category = $target.data('category');
context.query = $target.data('query');

$('#main').data('section', context.category);

events.emit('load:searchResults', context);
});
}

module.exports = {
init: function() {
$('#main').on('submit', '#search, #large-search', function(e) {
$(document).on('submit', '#search, #large-search', function(e) {
e.preventDefault();
var $form = $(e.target),
$submitButton = $form.find('input[type=submit]'),
Expand All @@ -29,23 +45,10 @@ module.exports = {
$('#main').html('');
});

events.on('bind:search', function() {
$('.results-header a').on('click', function() {
event.preventDefault();

var context = {},
$target = $(event.target);

context.category = $target.data('category');
context.query = $target.data('query');

$('#main').data('section', context.category);

events.emit('load:searchResults', context);
});
});

events.on('bind:search', bindSearch);
events.on('err:load:search', enableSearchForm);
events.on('render:searchResultsList', enableSearchForm);

bindSearch();
}
};