Skip to content

Commit

Permalink
WOBS-1076: Implement search UI
Browse files Browse the repository at this point in the history
Set proper template type.
  • Loading branch information
petrjasek committed Mar 30, 2012
1 parent d1bc7c6 commit fe4ed7f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
2 changes: 2 additions & 0 deletions newscoop/application/controllers/SearchController.php
Expand Up @@ -155,6 +155,8 @@ private function decodeSolrResponse(\Zend_Http_Response $response)
{
$decoded = json_decode($response->getBody(), true);
$decoded['responseHeader']['params']['q'] = $this->_getParam('q'); // this might be modified, keep users query
$decoded['responseHeader']['params']['date'] = $this->_getParam('date');
$decoded['responseHeader']['params']['type'] = $this->_getParam('type');
return $decoded;
}
}
6 changes: 5 additions & 1 deletion newscoop/application/views/scripts/search_index.tpl
Expand Up @@ -48,7 +48,7 @@
</ul>
</section>

<script type="text/template" id="document-news-template">
<script type="text/template" id="document-article-template">
<img src="<%= doc.get('image') %>" alt="" width="90" />
<h3><a href="#"><%= doc.escape('title') %></a></h3>
<p><%= doc.escape('lead') %></p>
Expand All @@ -65,6 +65,10 @@
<p><%= doc.escape('bio') %></p>
</script>

<script type="text/template" id="document-event-template">
<h3><a href="#"><%= doc.escape('title') %></a></h3>
</script>

<script src="{{ $view->baseUrl('js/jquery/jquery-1.6.4.min.js') }}"></script>
<script src="{{ $view->baseUrl('js/underscore.js') }}"></script>
<script src="{{ $view->baseUrl('js/backbone.js') }}"></script>
Expand Down
31 changes: 27 additions & 4 deletions newscoop/js/apps/search.js
Expand Up @@ -6,8 +6,20 @@ var Document = Backbone.Model.extend({
month: 3600 * 24 * 30,
year: 3600 * 24 * 365,

types: {
'news': 'article',
'newswire': 'article',
'dossier': 'article',
'user': 'user',
'tweet': 'twitter',
'event': 'event'
},

/**
* Get relative date
*
* @param {string} property
* @return {string}
*/
relDate: function(property) {
var date = new Date(this.get(property));
Expand All @@ -27,6 +39,15 @@ var Document = Backbone.Model.extend({
} else {
return 'vor ' + (diff / this.year).toFixed() + ' Jahr';
}
},

/**
* Get template type
*
* @return {string}
*/
getTemplateType: function() {
return this.types[this.get('type')];
}
});

Expand Down Expand Up @@ -71,6 +92,8 @@ var DocumentCollection = Backbone.Collection.extend({
this.count = parseInt(response.response.numFound);
this.start = parseInt(response.response.start);
this.rows = parseInt(response.responseHeader.params.rows);
this.type = response.responseHeader.params.type;
this.date = response.responseHeader.params.date;
return response.response.docs;
}
});
Expand All @@ -83,11 +106,11 @@ var DocumentView = Backbone.View.extend({
tagName: 'li',

initialize: function() {
this.template = _.template($('#document-' + this.model.get('type') + '-template').html());
this.template = _.template($('#document-' + this.model.getTemplateType() + '-template').html());
},

render: function() {
$(this.el).html(this.template({doc: this.model})).addClass(this.model.get('type'));
$(this.el).html(this.template({doc: this.model})).addClass(this.model.getTemplateType());
return this;
}
});
Expand Down Expand Up @@ -197,7 +220,6 @@ var PaginationView = Backbone.View.extend({

initialize: function() {
this.collection.bind('reset', this.render, this);
console.log(this.collection);
},

render: function() {
Expand All @@ -216,10 +238,11 @@ var PaginationView = Backbone.View.extend({
goTo: function(e) {
e.preventDefault();

var lastStart = (Math.ceil(this.collection.count / this.collection.rows) - 1) * this.collection.rows;
if ($(e.target).closest('li').hasClass('prev')) {
var start = Math.max(0, this.collection.start - this.collection.rows);
} else {
var start = Math.min(Math.ceil(this.collection.count / this.collection.rows) - 1, this.collection.start + this.collection.rows);
var start = Math.min(lastStart, this.collection.start + this.collection.rows);
}

if (start == this.collection.start) {
Expand Down

0 comments on commit fe4ed7f

Please sign in to comment.