nbot

A clone of the eBot by @deStrO fully based on node.js with build in Web Page - WIP
git clone git://archive.git.mtrnord.blog/MTRNord/nbot.git
Log | Files | Refs | README | LICENSE

api-search.js (2924B)


      1 YUI.add('api-search', function (Y) {
      2 
      3 var Lang   = Y.Lang,
      4     Node   = Y.Node,
      5     YArray = Y.Array;
      6 
      7 Y.APISearch = Y.Base.create('apiSearch', Y.Base, [Y.AutoCompleteBase], {
      8     // -- Public Properties ----------------------------------------------------
      9     RESULT_TEMPLATE:
     10         '<li class="result {resultType}">' +
     11             '<a href="{url}">' +
     12                 '<h3 class="title">{name}</h3>' +
     13                 '<span class="type">{resultType}</span>' +
     14                 '<div class="description">{description}</div>' +
     15                 '<span class="className">{class}</span>' +
     16             '</a>' +
     17         '</li>',
     18 
     19     // -- Initializer ----------------------------------------------------------
     20     initializer: function () {
     21         this._bindUIACBase();
     22         this._syncUIACBase();
     23     },
     24 
     25     // -- Protected Methods ----------------------------------------------------
     26     _apiResultFilter: function (query, results) {
     27         // Filter components out of the results.
     28         return YArray.filter(results, function (result) {
     29             return result.raw.resultType === 'component' ? false : result;
     30         });
     31     },
     32 
     33     _apiResultFormatter: function (query, results) {
     34         return YArray.map(results, function (result) {
     35             var raw  = Y.merge(result.raw), // create a copy
     36                 desc = raw.description || '';
     37 
     38             // Convert description to text and truncate it if necessary.
     39             desc = Node.create('<div>' + desc + '</div>').get('text');
     40 
     41             if (desc.length > 65) {
     42                 desc = Y.Escape.html(desc.substr(0, 65)) + ' &hellip;';
     43             } else {
     44                 desc = Y.Escape.html(desc);
     45             }
     46 
     47             raw['class'] || (raw['class'] = '');
     48             raw.description = desc;
     49 
     50             // Use the highlighted result name.
     51             raw.name = result.highlighted;
     52 
     53             return Lang.sub(this.RESULT_TEMPLATE, raw);
     54         }, this);
     55     },
     56 
     57     _apiTextLocator: function (result) {
     58         return result.displayName || result.name;
     59     }
     60 }, {
     61     // -- Attributes -----------------------------------------------------------
     62     ATTRS: {
     63         resultFormatter: {
     64             valueFn: function () {
     65                 return this._apiResultFormatter;
     66             }
     67         },
     68 
     69         resultFilters: {
     70             valueFn: function () {
     71                 return this._apiResultFilter;
     72             }
     73         },
     74 
     75         resultHighlighter: {
     76             value: 'phraseMatch'
     77         },
     78 
     79         resultListLocator: {
     80             value: 'data.results'
     81         },
     82 
     83         resultTextLocator: {
     84             valueFn: function () {
     85                 return this._apiTextLocator;
     86             }
     87         },
     88 
     89         source: {
     90             value: '/api/v1/search?q={query}&count={maxResults}'
     91         }
     92     }
     93 });
     94 
     95 }, '3.4.0', {requires: [
     96     'autocomplete-base', 'autocomplete-highlighters', 'autocomplete-sources',
     97     'escape'
     98 ]});