1 // $Id$
  2 
  3 /**
  4  * Baseclass for all free-text widgets.
  5  *
  6  * @class AbstractTextWidget
  7  * @augments AjaxSolr.AbstractWidget
  8  */
  9 AjaxSolr.AbstractTextWidget = AjaxSolr.AbstractWidget.extend(
 10   /** @lends AjaxSolr.AbstractTextWidget.prototype */
 11   {
 12   /**
 13    * Sets the main Solr query to the given string.
 14    *
 15    * @param {String} q The new Solr query.
 16    * @returns {Boolean} Whether the selection changed.
 17    */
 18   set: function (q) {
 19     return this.changeSelection(function () {
 20       this.manager.store.get('q').val(q);
 21     });
 22   },
 23 
 24   /**
 25    * Sets the main Solr query to the empty string.
 26    *
 27    * @returns {Boolean} Whether the selection changed.
 28    */
 29   clear: function () {
 30     return this.changeSelection(function () {
 31       this.manager.store.remove('q');
 32     });
 33   },
 34 
 35   /**
 36    * Helper for selection functions.
 37    *
 38    * @param {Function} Selection function to call.
 39    * @returns {Boolean} Whether the selection changed.
 40    */
 41   changeSelection: function (func) {
 42     var before = this.manager.store.get('q').val();
 43     func.apply(this);
 44     var after = this.manager.store.get('q').val();
 45     if (after !== before) {
 46       this.afterChangeSelection(after);
 47     }
 48     return after !== before;
 49   },
 50 
 51   /**
 52    * An abstract hook for child implementations.
 53    *
 54    * <p>This method is executed after the main Solr query changes.</p>
 55    *
 56    * @param {String} value The current main Solr query.
 57    */
 58   afterChangeSelection: function (value) {},
 59 
 60   /**
 61    * Returns a function to unset the main Solr query.
 62    *
 63    * @returns {Function}
 64    */
 65   unclickHandler: function () {
 66     var self = this;
 67     return function () {
 68       if (self.clear()) {
 69         self.manager.doRequest(0);
 70       }
 71       return false;
 72     }
 73   },
 74 
 75   /**
 76    * Returns a function to set the main Solr query.
 77    *
 78    * @param {String} value The new Solr query.
 79    * @returns {Function}
 80    */
 81   clickHandler: function (q) {
 82     var self = this;
 83     return function () {
 84       if (self.set(q)) {
 85         self.manager.doRequest(0);
 86       }
 87       return false;
 88     }
 89   }
 90 });
 91