blob: 1be3da8a61e7839fe8751bf30997e3ce29ce0976 [file] [log] [blame]
(function (callback) {
if (typeof define === 'function' && define.amd) {
define(['core/AbstractWidget'], callback);
}
else {
callback();
}
}(function () {
/**
* Baseclass for all free-text widgets.
*
* @class AbstractTextWidget
* @augments AjaxSolr.AbstractWidget
*/
AjaxSolr.AbstractTextWidget = AjaxSolr.AbstractWidget.extend(
/** @lends AjaxSolr.AbstractTextWidget.prototype */
{
/**
* @param {Object} [attributes]
* @param {Number} [attributes.start] This widget will by default set the
* offset parameter to 0 on each request.
*/
constructor: function (attributes) {
AjaxSolr.AbstractTextWidget.__super__.constructor.apply(this, arguments);
AjaxSolr.extend(this, {
start: 0
}, attributes);
},
/**
* Sets the main Solr query to the given string.
*
* @param {String} q The new Solr query.
* @returns {Boolean} Whether the selection changed.
*/
set: function (q) {
return this.changeSelection(function () {
this.manager.store.get('q').val(q);
});
},
/**
* Sets the main Solr query to the empty string.
*
* @returns {Boolean} Whether the selection changed.
*/
clear: function () {
return this.changeSelection(function () {
this.manager.store.remove('q');
});
},
/**
* Helper for selection functions.
*
* @param {Function} Selection function to call.
* @returns {Boolean} Whether the selection changed.
*/
changeSelection: function (func) {
var before = this.manager.store.get('q').val();
func.apply(this);
var after = this.manager.store.get('q').val();
if (after !== before) {
this.afterChangeSelection(after);
}
return after !== before;
},
/**
* An abstract hook for child implementations.
*
* <p>This method is executed after the main Solr query changes.</p>
*
* @param {String} value The current main Solr query.
*/
afterChangeSelection: function (value) {},
/**
* Returns a function to unset the main Solr query.
*
* @returns {Function}
*/
unclickHandler: function () {
var self = this;
return function () {
if (self.clear()) {
self.doRequest();
}
return false;
}
},
/**
* Returns a function to set the main Solr query.
*
* @param {String} value The new Solr query.
* @returns {Function}
*/
clickHandler: function (q) {
var self = this;
return function () {
if (self.set(q)) {
self.doRequest();
}
return false;
}
}
});
}));