Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add requirejs example

  • Loading branch information...
commit 9ad43101fcca6efbf79475b058f8cc4a4381fd9c 1 parent 43061d9
@jpmckinney jpmckinney authored
View
160 examples/reuters-requirejs/css/reuters.css
@@ -0,0 +1,160 @@
+* {
+ padding: 0;
+ margin: 0;
+}
+
+body {
+ background: #fff url(../images/header.gif) repeat-x;
+ color: #333;
+ font-family: Verdana, Arial, Helvetica, sans-serif;
+ font-size: 12px;
+ line-height: 18px;
+}
+img {
+ border: none;
+}
+a {
+ color: #6998f0;
+ text-decoration: none;
+}
+a:hover {
+ color: #000;
+ text-decoration: underline;
+}
+
+#wrap {
+ width: 1000px;
+ margin: 0px auto;
+}
+.clear {
+ clear: both;
+}
+
+/* header */
+
+#header {
+ height: 90px;
+}
+#header h1 {
+ padding: 20px 0 5px 10px;
+ font-size: 20px;
+ letter-spacing: -2px;
+}
+#header h2 {
+ padding-left: 10px;
+ color: #666;
+ font-size: 14px;
+ font-weight: 100;
+}
+
+/* right */
+
+.right {
+ float: right;
+ width: 538px;
+ padding: 10px 20px 10px 40px;
+ border-left: 1px solid #aaa;
+ text-align: justify;
+}
+.right h2 {
+ padding: 15px 0 5px 0;
+ color: #B4241B;
+ font-size: 20px;
+ font-weight: 100;
+ letter-spacing: -2px;
+}
+#result a {
+ margin-right: 5px;
+}
+#navigation {
+ border-bottom: 1px solid #000;
+}
+#pager,
+#pager-header {
+ display: inline;
+}
+#pager li,
+.links li {
+ list-style-type: none;
+ display: inline;
+}
+#selection {
+ padding: 10px 15px 0;
+}
+#selection li {
+ list-style-type: none;
+}
+#docs {
+ padding-top: 1px;
+}
+
+/* left */
+
+.left {
+ float: left;
+ width: 390px;
+ padding: 0 0 10px 10px;
+}
+.left h2 {
+ clear: left;
+ padding-top: 20px;
+ color: #333;
+ font-size: 13px;
+}
+#search input {
+ width: 350px;
+}
+#calendar {
+ width: 160px;
+}
+#search_help {
+ font-size: 80%;
+}
+.tagcloud {
+ padding: 10px 15px 0;
+}
+.tagcloud_item {
+ float: left;
+ display: block;
+ margin-right: 4px;
+ color: #6998f0;
+}
+.tagcloud_item:hover {
+ color: #f00;
+}
+
+/* tagcloud */
+
+a.tagcloud_size_0 {
+font-size: 80%;
+}
+a.tagcloud_size_1 {
+font-size: 90%;
+}
+a.tagcloud_size_2 {
+font-size: 100%;
+}Rst
+a.tagcloud_size_3 {
+font-size: 110%;
+}
+a.tagcloud_size_4 {
+font-size: 120%;
+}
+a.tagcloud_size_5 {
+font-size: 130%;
+}
+a.tagcloud_size_6 {
+font-size: 140%;
+}
+a.tagcloud_size_7 {
+font-size: 150%
+}
+a.tagcloud_size_8 {
+font-size: 160%;
+}
+a.tagcloud_size_9 {
+font-size: 170%;
+}
+a.tagcloud_size_10 {
+font-size: 180%;
+}
View
BIN  examples/reuters-requirejs/images/ajax-loader.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  examples/reuters-requirejs/images/header.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
60 examples/reuters-requirejs/index.html
@@ -0,0 +1,60 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <meta charset="utf-8">
+ <title>AJAX Solr</title>
+ <link rel="stylesheet" href="css/reuters.css">
+ <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
+ <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.24/jquery-ui.min.js"></script>
+ <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.24/themes/smoothness/jquery-ui.css">
+ <script data-main="js/reuters" src="http://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.5/require.min.js"></script>
+</head>
+<body>
+ <div id="wrap">
+ <div id="header">
+ <h1>AJAX Solr Demonstration</h1>
+ <h2>Browse Reuters business news from 1987</h2>
+ </div>
+
+ <div class="right">
+ <div id="result">
+ <div id="navigation">
+ <ul id="pager"></ul>
+ <div id="pager-header"></div>
+ </div>
+ <div id="docs"></div>
+ </div>
+ </div>
+
+ <div class="left">
+ <h2>Current Selection</h2>
+ <ul id="selection"></ul>
+
+ <h2>Search</h2>
+ <span id="search_help">(press ESC to close suggestions)</span>
+ <ul id="search">
+ <input type="text" id="query" name="query" autocomplete="off">
+ </ul>
+
+ <h2>Top Topics</h2>
+ <div class="tagcloud" id="topics"></div>
+
+ <h2>Top Organisations</h2>
+ <div class="tagcloud" id="organisations"></div>
+
+ <h2>Top Exchanges</h2>
+ <div class="tagcloud" id="exchanges"></div>
+
+ <h2>By Country</h2>
+ <div id="countries"></div>
+ <div id="preview"></div>
+
+ <h2>By Date</h2>
+ <div id="calendar"></div>
+
+ <div class="clear"></div>
+ </div>
+ <div class="clear"></div>
+ </div>
+</body>
+</html>
View
102 examples/reuters-requirejs/js/reuters.js
@@ -0,0 +1,102 @@
+var Manager;
+
+require.config({
+ paths: {
+ core: '../../../core',
+ managers: '../../../managers',
+ widgets: '../../../widgets',
+ reuters: '../widgets'
+ },
+ urlArgs: "bust=" + (new Date()).getTime()
+});
+
+(function ($) {
+
+define([
+ 'managers/Manager.jquery',
+ 'core/ParameterStore',
+ 'reuters/ResultWidget',
+ 'reuters/TagcloudWidget',
+ 'reuters/CurrentSearchWidget.9',
+ 'reuters/AutocompleteWidget',
+ 'reuters/CountryCodeWidget',
+ 'reuters/CalendarWidget',
+ 'widgets/jquery/PagerWidget'
+], function () {
+ $(function () {
+ Manager = new AjaxSolr.Manager({
+ solrUrl: 'http://evolvingweb.ca/solr/reuters/'
+ });
+ Manager.addWidget(new AjaxSolr.ResultWidget({
+ id: 'result',
+ target: '#docs'
+ }));
+ Manager.addWidget(new AjaxSolr.PagerWidget({
+ id: 'pager',
+ target: '#pager',
+ prevLabel: '&lt;',
+ nextLabel: '&gt;',
+ innerWindow: 1,
+ renderHeader: function (perPage, offset, total) {
+ $('#pager-header').html($('<span></span>').text('displaying ' + Math.min(total, offset + 1) + ' to ' + Math.min(total, offset + perPage) + ' of ' + total));
+ }
+ }));
+ var fields = [ 'topics', 'organisations', 'exchanges' ];
+ for (var i = 0, l = fields.length; i < l; i++) {
+ Manager.addWidget(new AjaxSolr.TagcloudWidget({
+ id: fields[i],
+ target: '#' + fields[i],
+ field: fields[i]
+ }));
+ }
+ Manager.addWidget(new AjaxSolr.CurrentSearchWidget({
+ id: 'currentsearch',
+ target: '#selection'
+ }));
+ Manager.addWidget(new AjaxSolr.AutocompleteWidget({
+ id: 'text',
+ target: '#search',
+ fields: [ 'topics', 'organisations', 'exchanges' ]
+ }));
+ Manager.addWidget(new AjaxSolr.CountryCodeWidget({
+ id: 'countries',
+ target: '#countries',
+ field: 'countryCodes'
+ }));
+ Manager.addWidget(new AjaxSolr.CalendarWidget({
+ id: 'calendar',
+ target: '#calendar',
+ field: 'date'
+ }));
+ Manager.init();
+ Manager.store.addByValue('q', '*:*');
+ var params = {
+ facet: true,
+ 'facet.field': [ 'topics', 'organisations', 'exchanges', 'countryCodes' ],
+ 'facet.limit': 20,
+ 'facet.mincount': 1,
+ 'f.topics.facet.limit': 50,
+ 'f.countryCodes.facet.limit': -1,
+ 'facet.date': 'date',
+ 'facet.date.start': '1987-02-26T00:00:00.000Z/DAY',
+ 'facet.date.end': '1987-10-20T00:00:00.000Z/DAY+1DAY',
+ 'facet.date.gap': '+1DAY',
+ 'json.nl': 'map'
+ };
+ for (var name in params) {
+ Manager.store.addByValue(name, params[name]);
+ }
+ Manager.doRequest();
+ });
+
+ $.fn.showIf = function (condition) {
+ if (condition) {
+ return this.show();
+ }
+ else {
+ return this.hide();
+ }
+ }
+});
+
+})(jQuery);
View
57 examples/reuters-requirejs/widgets/AutocompleteWidget.7.0.js
@@ -0,0 +1,57 @@
+(function (callback) {
+ if (typeof define === 'function' && define.amd) {
+ define(['core/AbstractTextWidget'], callback);
+ }
+ else {
+ callback();
+ }
+}(function () {
+
+(function ($) {
+
+AjaxSolr.AutocompleteWidget = AjaxSolr.AbstractTextWidget.extend({
+ afterRequest: function () {
+ $(this.target).find('input').unbind().removeData('events').val('');
+
+ var self = this;
+
+ var list = [];
+ for (var i = 0; i < this.fields.length; i++) {
+ var field = this.fields[i];
+ for (var facet in this.manager.response.facet_counts.facet_fields[field]) {
+ list.push({
+ field: field,
+ value: facet,
+ label: facet + ' (' + this.manager.response.facet_counts.facet_fields[field][facet] + ') - ' + field
+ });
+ }
+ }
+
+ this.requestSent = false;
+ $(this.target).find('input').autocomplete('destroy').autocomplete({
+ source: list,
+ select: function(event, ui) {
+ if (ui.item) {
+ self.requestSent = true;
+ if (self.manager.store.addByValue('fq', ui.item.field + ':' + AjaxSolr.Parameter.escapeValue(ui.item.value))) {
+ self.doRequest();
+ }
+ }
+ }
+ });
+
+ // This has lower priority so that requestSent is set.
+ $(this.target).find('input').bind('keydown', function(e) {
+ if (self.requestSent === false && e.which == 13) {
+ var value = $(this).val();
+ if (value && self.set(value)) {
+ self.doRequest();
+ }
+ }
+ });
+ }
+});
+
+})(jQuery);
+
+}));
View
70 examples/reuters-requirejs/widgets/AutocompleteWidget.js
@@ -0,0 +1,70 @@
+(function (callback) {
+ if (typeof define === 'function' && define.amd) {
+ define(['core/AbstractTextWidget'], callback);
+ }
+ else {
+ callback();
+ }
+}(function () {
+
+(function ($) {
+
+AjaxSolr.AutocompleteWidget = AjaxSolr.AbstractTextWidget.extend({
+ afterRequest: function () {
+ $(this.target).find('input').unbind().removeData('events').val('');
+
+ var self = this;
+
+ var callback = function (response) {
+ var list = [];
+ for (var i = 0; i < self.fields.length; i++) {
+ var field = self.fields[i];
+ for (var facet in response.facet_counts.facet_fields[field]) {
+ list.push({
+ field: field,
+ value: facet,
+ label: facet + ' (' + response.facet_counts.facet_fields[field][facet] + ') - ' + field
+ });
+ }
+ }
+
+ self.requestSent = false;
+ $(self.target).find('input').autocomplete('destroy').autocomplete({
+ source: list,
+ select: function(event, ui) {
+ if (ui.item) {
+ self.requestSent = true;
+ if (self.manager.store.addByValue('fq', ui.item.field + ':' + AjaxSolr.Parameter.escapeValue(ui.item.value))) {
+ self.doRequest();
+ }
+ }
+ }
+ });
+
+ // This has lower priority so that requestSent is set.
+ $(self.target).find('input').bind('keydown', function(e) {
+ if (self.requestSent === false && e.which == 13) {
+ var value = $(this).val();
+ if (value && self.set(value)) {
+ self.doRequest();
+ }
+ }
+ });
+ } // end callback
+
+ var params = [ 'rows=0&facet=true&facet.limit=-1&facet.mincount=1&json.nl=map' ];
+ for (var i = 0; i < this.fields.length; i++) {
+ params.push('facet.field=' + this.fields[i]);
+ }
+ var values = this.manager.store.values('fq');
+ for (var i = 0; i < values.length; i++) {
+ params.push('fq=' + encodeURIComponent(values[i]));
+ }
+ params.push('q=' + this.manager.store.get('q').val());
+ $.getJSON(this.manager.solrUrl + 'select?' + params.join('&') + '&wt=json&json.wrf=?', {}, callback);
+ }
+});
+
+})(jQuery);
+
+}));
View
38 examples/reuters-requirejs/widgets/CalendarWidget.js
@@ -0,0 +1,38 @@
+(function (callback) {
+ if (typeof define === 'function' && define.amd) {
+ define(['core/AbstractFacetWidget'], callback);
+ }
+ else {
+ callback();
+ }
+}(function () {
+
+(function ($) {
+
+AjaxSolr.CalendarWidget = AjaxSolr.AbstractFacetWidget.extend({
+ afterRequest: function () {
+ var self = this;
+ $(this.target).datepicker('destroy').datepicker({
+ dateFormat: 'yy-mm-dd',
+ defaultDate: new Date(1987, 2, 1),
+ maxDate: $.datepicker.parseDate('yy-mm-dd', this.manager.store.get('facet.date.end').val().substr(0, 10)),
+ minDate: $.datepicker.parseDate('yy-mm-dd', this.manager.store.get('facet.date.start').val().substr(0, 10)),
+ nextText: '&gt;',
+ prevText: '&lt;',
+ beforeShowDay: function (date) {
+ var value = $.datepicker.formatDate('yy-mm-dd', date) + 'T00:00:00Z';
+ var count = self.manager.response.facet_counts.facet_dates[self.field][value];
+ return [ parseInt(count) > 0, '', count + ' documents found!' ];
+ },
+ onSelect: function (dateText, inst) {
+ if (self.add('[' + dateText + 'T00:00:00Z TO ' + dateText + 'T23:59:59Z]')) {
+ self.doRequest();
+ }
+ }
+ });
+ }
+});
+
+})(jQuery);
+
+}));
View
50 examples/reuters-requirejs/widgets/CountryCodeWidget.8.0.js
@@ -0,0 +1,50 @@
+(function (callback) {
+ if (typeof define === 'function' && define.amd) {
+ define(['core/AbstractFacetWidget'], callback);
+ }
+ else {
+ callback();
+ }
+}(function () {
+
+(function ($) {
+
+AjaxSolr.CountryCodeWidget = AjaxSolr.AbstractFacetWidget.extend({
+ afterRequest: function () {
+ var self = this;
+
+ $(this.target).empty();
+
+ var maxCount = 0;
+ var options = { '': '--select--' };
+ for (var facet in this.manager.response.facet_counts.facet_fields[this.field]) {
+ if (facet.length == 2) { // only display country codes
+ var count = this.manager.response.facet_counts.facet_fields[this.field][facet];
+ if (count > maxCount) {
+ maxCount = count;
+ }
+ options[facet] = facet + ' (' + count + ')';
+ }
+ }
+ $(this.target).append(this.template('country', options));
+
+ $(this.target).find('#country').change(function () {
+ var value = $(this).val();
+ if (value && self.add(value)) {
+ self.doRequest();
+ }
+ });
+ },
+
+ template: function (name, container) {
+ var options = [];
+ for (var value in container) {
+ options.push('<option value="' + value +'">' + container[value] + '</option>');
+ }
+ return '<select id="' + name + '" name="' + name + '">' + options.join('\n') + '</select>';
+ }
+});
+
+})(jQuery);
+
+}));
View
79 examples/reuters-requirejs/widgets/CountryCodeWidget.js
@@ -0,0 +1,79 @@
+(function (callback) {
+ if (typeof define === 'function' && define.amd) {
+ define(['core/AbstractFacetWidget'], callback);
+ }
+ else {
+ callback();
+ }
+}(function () {
+
+(function ($) {
+
+AjaxSolr.CountryCodeWidget = AjaxSolr.AbstractFacetWidget.extend({
+ afterRequest: function () {
+ var self = this;
+
+ $(this.target).empty();
+
+ var maps = {
+ world: 'view the World',
+ africa: 'view Africa',
+ asia: 'view Asia',
+ europe: 'view Europe',
+ middle_east: 'view the Middle East',
+ south_america: 'view South America',
+ usa: 'view North America'
+ };
+ $(this.target).append(this.template('region', maps));
+
+ $(this.target).find('#region').change(function () {
+ $(self.target).find('img').hide();
+ $('#' + self.id + $(this).val()).show();
+ });
+
+ var maxCount = 0;
+ var options = { '': '--select--' };
+ for (var facet in this.manager.response.facet_counts.facet_fields[this.field]) {
+ if (facet.length == 2) { // only display country codes
+ var count = this.manager.response.facet_counts.facet_fields[this.field][facet];
+ if (count > maxCount) {
+ maxCount = count;
+ }
+ options[facet] = facet + ' (' + count + ')';
+ }
+ }
+ $(this.target).append(this.template('country', options));
+
+ $(this.target).find('#country').change(function () {
+ var value = $(this).val();
+ if (value && self.add(value)) {
+ self.doRequest();
+ }
+ });
+
+ var chd = [];
+ var chld = '';
+ for (var facet in this.manager.response.facet_counts.facet_fields[this.field]) {
+ if (facet.length == 2) { // only display country codes
+ chd.push(parseInt(this.manager.response.facet_counts.facet_fields[this.field][facet] / maxCount * 100) + '.0');
+ chld += facet;
+ }
+ }
+ for (var value in maps) {
+ var src = 'http://chart.apis.google.com/chart?chco=f5f5f5,edf0d4,6c9642,365e24,13390a&chd=t:' + chd.join(',') + '&chf=bg,s,eaf7fe&chtm=' + value + '&chld=' + chld + '&chs=350x180&cht=t';
+ $('<img>').attr('id', this.id + value).showIf(value == 'world').attr('src', src).appendTo(this.target);
+ }
+ },
+
+ template: function (name, container) {
+ var options = [];
+ for (var value in container) {
+ options.push('<option value="' + value +'">' + container[value] + '</option>');
+ }
+ return '<select id="' + name + '" name="' + name + '">' + options.join('\n') + '</select>';
+ }
+});
+
+})(jQuery);
+
+}));
View
74 examples/reuters-requirejs/widgets/CurrentSearchWidget.9.js
@@ -0,0 +1,74 @@
+(function (callback) {
+ if (typeof define === 'function' && define.amd) {
+ define(['core/AbstractWidget'], callback);
+ }
+ else {
+ callback();
+ }
+}(function () {
+
+(function ($) {
+
+AjaxSolr.CurrentSearchWidget = AjaxSolr.AbstractWidget.extend({
+ start: 0,
+
+ afterRequest: function () {
+ var self = this;
+ var links = [];
+
+ var q = this.manager.store.get('q').val();
+ if (q != '*:*') {
+ links.push($('<a href="#"></a>').text('(x) ' + q).click(function () {
+ self.manager.store.get('q').val('*:*');
+ self.doRequest();
+ return false;
+ }));
+ }
+
+ var fq = this.manager.store.values('fq');
+ for (var i = 0, l = fq.length; i < l; i++) {
+ if (fq[i].match(/[\[\{]\S+ TO \S+[\]\}]/)) {
+ var field = fq[i].match(/^\w+:/)[0];
+ var value = fq[i].substr(field.length + 1, 10);
+ links.push($('<a href="#"></a>').text('(x) ' + field + value).click(self.removeFacet(fq[i])));
+ }
+ else {
+ links.push($('<a href="#"></a>').text('(x) ' + fq[i]).click(self.removeFacet(fq[i])));
+ }
+ }
+
+ if (links.length > 1) {
+ links.unshift($('<a href="#"></a>').text('remove all').click(function () {
+ self.manager.store.get('q').val('*:*');
+ self.manager.store.remove('fq');
+ self.doRequest();
+ return false;
+ }));
+ }
+
+ if (links.length) {
+ var $target = $(this.target);
+ $target.empty();
+ for (var i = 0, l = links.length; i < l; i++) {
+ $target.append($('<li></li>').append(links[i]));
+ }
+ }
+ else {
+ $(this.target).html('<li>Viewing all documents!</li>');
+ }
+ },
+
+ removeFacet: function (facet) {
+ var self = this;
+ return function () {
+ if (self.manager.store.removeByValue('fq', facet)) {
+ self.doRequest();
+ }
+ return false;
+ };
+ }
+});
+
+})(jQuery);
+
+}));
View
67 examples/reuters-requirejs/widgets/CurrentSearchWidget.js
@@ -0,0 +1,67 @@
+(function (callback) {
+ if (typeof define === 'function' && define.amd) {
+ define(['core/AbstractWidget'], callback);
+ }
+ else {
+ callback();
+ }
+}(function () {
+
+(function ($) {
+
+AjaxSolr.CurrentSearchWidget = AjaxSolr.AbstractWidget.extend({
+ start: 0,
+
+ afterRequest: function () {
+ var self = this;
+ var links = [];
+
+ var q = this.manager.store.get('q').val();
+ if (q != '*:*') {
+ links.push($('<a href="#"></a>').text('(x) ' + q).click(function () {
+ self.manager.store.get('q').val('*:*');
+ self.doRequest();
+ return false;
+ }));
+ }
+
+ var fq = this.manager.store.values('fq');
+ for (var i = 0, l = fq.length; i < l; i++) {
+ links.push($('<a href="#"></a>').text('(x) ' + fq[i]).click(self.removeFacet(fq[i])));
+ }
+
+ if (links.length > 1) {
+ links.unshift($('<a href="#"></a>').text('remove all').click(function () {
+ self.manager.store.get('q').val('*:*');
+ self.manager.store.remove('fq');
+ self.doRequest();
+ return false;
+ }));
+ }
+
+ if (links.length) {
+ var $target = $(this.target);
+ $target.empty();
+ for (var i = 0, l = links.length; i < l; i++) {
+ $target.append($('<li></li>').append(links[i]));
+ }
+ }
+ else {
+ $(this.target).html('<li>Viewing all documents!</li>');
+ }
+ },
+
+ removeFacet: function (facet) {
+ var self = this;
+ return function () {
+ if (self.manager.store.removeByValue('fq', facet)) {
+ self.doRequest();
+ }
+ return false;
+ };
+ }
+});
+
+})(jQuery);
+
+}));
View
41 examples/reuters-requirejs/widgets/ResultWidget.2.0.js
@@ -0,0 +1,41 @@
+(function (callback) {
+ if (typeof define === 'function' && define.amd) {
+ define(['core/AbstractWidget'], callback);
+ }
+ else {
+ callback();
+ }
+}(function () {
+
+(function ($) {
+
+AjaxSolr.ResultWidget = AjaxSolr.AbstractWidget.extend({
+ afterRequest: function () {
+ $(this.target).empty();
+ for (var i = 0, l = this.manager.response.response.docs.length; i < l; i++) {
+ var doc = this.manager.response.response.docs[i];
+ $(this.target).append(this.template(doc));
+ }
+ },
+
+ template: function (doc) {
+ var snippet = '';
+ if (doc.text.length > 300) {
+ snippet += doc.dateline + ' ' + doc.text.substring(0, 300);
+ snippet += '<span style="display:none;">' + doc.text.substring(300);
+ snippet += '</span> <a href="#" class="more">more</a>';
+ }
+ else {
+ snippet += doc.dateline + ' ' + doc.text;
+ }
+
+ var output = '<div><h2>' + doc.title + '</h2>';
+ output += '<p id="links_' + doc.id + '" class="links"></p>';
+ output += '<p>' + snippet + '</p></div>';
+ return output;
+ }
+});
+
+})(jQuery);
+
+}));
View
106 examples/reuters-requirejs/widgets/ResultWidget.f.js
@@ -0,0 +1,106 @@
+(function (callback) {
+ if (typeof define === 'function' && define.amd) {
+ define(['core/AbstractWidget'], callback);
+ }
+ else {
+ callback();
+ }
+}(function () {
+
+(function ($) {
+
+AjaxSolr.ResultWidget = AjaxSolr.AbstractWidget.extend({
+ start: 0,
+
+ beforeRequest: function () {
+ $(this.target).html($('<img>').attr('src', 'images/ajax-loader.gif'));
+ },
+
+ facetLinks: function (facet_field, facet_values) {
+ var links = [];
+ if (facet_values) {
+ for (var i = 0, l = facet_values.length; i < l; i++) {
+ if (facet_values[i] !== undefined) {
+ links = links.concat([
+ facet_field + ':',
+ $('<a href="#"></a>')
+ .text(facet_values[i])
+ .click(this.facetHandler(facet_field, facet_values[i]))
+ ]);
+ }
+ else {
+ links.push('no items found in current selection');
+ }
+ }
+ }
+ return links;
+ },
+
+ facetHandler: function (facet_field, facet_value) {
+ var self = this;
+ return function () {
+ self.manager.store.remove('fq');
+ self.manager.store.addByValue('fq', facet_field + ':' + AjaxSolr.Parameter.escapeValue(facet_value));
+ self.doRequest();
+ return false;
+ };
+ },
+
+ afterRequest: function () {
+ $(this.target).empty();
+ for (var i = 0, l = this.manager.response.response.docs.length; i < l; i++) {
+ var doc = this.manager.response.response.docs[i];
+ $(this.target).append(this.template(doc));
+
+ var items = [];
+ items = items.concat(this.facetLinks('topics', doc.topics));
+ items = items.concat(this.facetLinks('organisations', doc.organisations));
+ items = items.concat(this.facetLinks('exchanges', doc.exchanges));
+
+ var $links = $('#links_' + doc.id);
+ $links.empty();
+ for (var j = 0, m = items.length; j < m; j++) {
+ $links.append($('<li></li>').append(items[j]));
+ }
+ }
+ },
+
+ template: function (doc) {
+ var snippet = '';
+ if (doc.text.length > 300) {
+ snippet += doc.dateline + ' ' + doc.text.substring(0, 300);
+ snippet += '<span style="display:none;">' + doc.text.substring(300);
+ snippet += '</span> <a href="#" class="more">more</a>';
+ }
+ else {
+ snippet += doc.dateline + ' ' + doc.text;
+ }
+
+ var output = '<div><h2>' + doc.title + '</h2>';
+ output += '<p id="links_' + doc.id + '" class="links"></p>';
+ output += '<p>' + snippet + '</p></div>';
+ return output;
+ },
+
+ init: function () {
+ $(document).on('click', 'a.more', function () {
+ var $this = $(this),
+ span = $this.parent().find('span');
+
+ if (span.is(':visible')) {
+ span.hide();
+ $this.text('more');
+ }
+ else {
+ span.show();
+ $this.text('less');
+ }
+
+ return false;
+ });
+ }
+});
+
+})(jQuery);
+
+}));
View
105 examples/reuters-requirejs/widgets/ResultWidget.js
@@ -0,0 +1,105 @@
+(function (callback) {
+ if (typeof define === 'function' && define.amd) {
+ define(['core/AbstractWidget'], callback);
+ }
+ else {
+ callback();
+ }
+}(function () {
+
+(function ($) {
+
+AjaxSolr.ResultWidget = AjaxSolr.AbstractWidget.extend({
+ start: 0,
+
+ beforeRequest: function () {
+ $(this.target).html($('<img>').attr('src', 'images/ajax-loader.gif'));
+ },
+
+ facetLinks: function (facet_field, facet_values) {
+ var links = [];
+ if (facet_values) {
+ for (var i = 0, l = facet_values.length; i < l; i++) {
+ if (facet_values[i] !== undefined) {
+ links.push(
+ $('<a href="#"></a>')
+ .text(facet_values[i])
+ .click(this.facetHandler(facet_field, facet_values[i]))
+ );
+ }
+ else {
+ links.push('no items found in current selection');
+ }
+ }
+ }
+ return links;
+ },
+
+ facetHandler: function (facet_field, facet_value) {
+ var self = this;
+ return function () {
+ self.manager.store.remove('fq');
+ self.manager.store.addByValue('fq', facet_field + ':' + AjaxSolr.Parameter.escapeValue(facet_value));
+ self.doRequest();
+ return false;
+ };
+ },
+
+ afterRequest: function () {
+ $(this.target).empty();
+ for (var i = 0, l = this.manager.response.response.docs.length; i < l; i++) {
+ var doc = this.manager.response.response.docs[i];
+ $(this.target).append(this.template(doc));
+
+ var items = [];
+ items = items.concat(this.facetLinks('topics', doc.topics));
+ items = items.concat(this.facetLinks('organisations', doc.organisations));
+ items = items.concat(this.facetLinks('exchanges', doc.exchanges));
+
+ var $links = $('#links_' + doc.id);
+ $links.empty();
+ for (var j = 0, m = items.length; j < m; j++) {
+ $links.append($('<li></li>').append(items[j]));
+ }
+ }
+ },
+
+ template: function (doc) {
+ var snippet = '';
+ if (doc.text.length > 300) {
+ snippet += doc.dateline + ' ' + doc.text.substring(0, 300);
+ snippet += '<span style="display:none;">' + doc.text.substring(300);
+ snippet += '</span> <a href="#" class="more">more</a>';
+ }
+ else {
+ snippet += doc.dateline + ' ' + doc.text;
+ }
+
+ var output = '<div><h2>' + doc.title + '</h2>';
+ output += '<p id="links_' + doc.id + '" class="links"></p>';
+ output += '<p>' + snippet + '</p></div>';
+ return output;
+ },
+
+ init: function () {
+ $(document).on('click', 'a.more', function () {
+ var $this = $(this),
+ span = $this.parent().find('span');
+
+ if (span.is(':visible')) {
+ span.hide();
+ $this.text('more');
+ }
+ else {
+ span.show();
+ $this.text('less');
+ }
+
+ return false;
+ });
+ }
+});
+
+})(jQuery);
+
+}));
View
47 examples/reuters-requirejs/widgets/TagcloudWidget.js
@@ -0,0 +1,47 @@
+(function (callback) {
+ if (typeof define === 'function' && define.amd) {
+ define(['core/AbstractFacetWidget'], callback);
+ }
+ else {
+ callback();
+ }
+}(function () {
+
+(function ($) {
+
+AjaxSolr.TagcloudWidget = AjaxSolr.AbstractFacetWidget.extend({
+ afterRequest: function () {
+ if (this.manager.response.facet_counts.facet_fields[this.field] === undefined) {
+ $(this.target).html('no items found in current selection');
+ return;
+ }
+
+ var maxCount = 0;
+ var objectedItems = [];
+ for (var facet in this.manager.response.facet_counts.facet_fields[this.field]) {
+ var count = parseInt(this.manager.response.facet_counts.facet_fields[this.field][facet]);
+ if (count > maxCount) {
+ maxCount = count;
+ }
+ objectedItems.push({ facet: facet, count: count });
+ }
+ objectedItems.sort(function (a, b) {
+ return a.facet < b.facet ? -1 : 1;
+ });
+
+ $(this.target).empty();
+ for (var i = 0, l = objectedItems.length; i < l; i++) {
+ var facet = objectedItems[i].facet;
+ $(this.target).append(
+ $('<a href="#" class="tagcloud_item"></a>')
+ .text(facet)
+ .addClass('tagcloud_size_' + parseInt(objectedItems[i].count / maxCount * 10))
+ .click(this.clickHandler(facet))
+ );
+ }
+ }
+});
+
+})(jQuery);
+
+}));
View
32 examples/reuters-requirejs/widgets/TextWidget.js
@@ -0,0 +1,32 @@
+(function (callback) {
+ if (typeof define === 'function' && define.amd) {
+ define(['core/AbstractTextWidget'], callback);
+ }
+ else {
+ callback();
+ }
+}(function () {
+
+(function ($) {
+
+AjaxSolr.TextWidget = AjaxSolr.AbstractTextWidget.extend({
+ init: function () {
+ var self = this;
+ $(this.target).find('input').bind('keydown', function(e) {
+ if (e.which == 13) {
+ var value = $(this).val();
+ if (value && self.set(value)) {
+ self.doRequest();
+ }
+ }
+ });
+ },
+
+ afterRequest: function () {
+ $(this.target).find('input').val('');
+ }
+});
+
+})(jQuery);
+
+}));
Please sign in to comment.
Something went wrong with that request. Please try again.