| Akron | d7ad907 | 2019-12-09 07:08:20 +0100 | [diff] [blame] | 1 | /** | 
 | 2 |  * Create a pipe object, that holds a list of services | 
 | 3 |  * meant to transform the KQ passed before it's finally | 
 | 4 |  * passed to the search engine. | 
 | 5 |  * | 
 | 6 |  * @author Nils Diewald | 
 | 7 |  */ | 
 | 8 | define(function () { | 
 | 9 |  | 
 | 10 |   "use strict"; | 
 | 11 |  | 
 | 12 |   const notNullRe = new RegExp("[a-zA-Z0-9]"); | 
 | 13 |  | 
 | 14 |   // Trim and check | 
 | 15 |   function _notNull (service) { | 
 | 16 |     service = service.trim(); | 
 | 17 |     if (service.match(notNullRe)) { | 
 | 18 |       return service; | 
 | 19 |     }; | 
 | 20 |     return null; | 
 | 21 |   } | 
 | 22 |    | 
 | 23 |   return { | 
 | 24 |  | 
 | 25 |     /** | 
 | 26 |      * Constructor | 
 | 27 |      */ | 
 | 28 |     create : function () { | 
 | 29 |       let obj = Object.create(this); | 
 | 30 |       obj._pipe = []; | 
 | 31 |       return obj; | 
 | 32 |     }, | 
 | 33 |  | 
 | 34 |     /** | 
 | 35 |      * Append service to pipe. | 
 | 36 |      */ | 
 | 37 |     append : function (service) { | 
 | 38 |       service = _notNull(service); | 
| Akron | ba7a049 | 2019-12-17 20:33:11 +0100 | [diff] [blame] | 39 |       if (service) { | 
| Akron | d7ad907 | 2019-12-09 07:08:20 +0100 | [diff] [blame] | 40 |         this._pipe.push(service); | 
| Akron | ba7a049 | 2019-12-17 20:33:11 +0100 | [diff] [blame] | 41 |         this._update(); | 
 | 42 |       }; | 
| Akron | d7ad907 | 2019-12-09 07:08:20 +0100 | [diff] [blame] | 43 |     }, | 
 | 44 |  | 
 | 45 |     /** | 
 | 46 |      * Prepend service to pipe. | 
 | 47 |      */ | 
 | 48 |     prepend : function (service) { | 
 | 49 |       service = _notNull(service); | 
| Akron | ba7a049 | 2019-12-17 20:33:11 +0100 | [diff] [blame] | 50 |       if (service) { | 
| Akron | d7ad907 | 2019-12-09 07:08:20 +0100 | [diff] [blame] | 51 |         this._pipe.unshift(service); | 
| Akron | ba7a049 | 2019-12-17 20:33:11 +0100 | [diff] [blame] | 52 |         this._update(); | 
 | 53 |       }; | 
| Akron | d7ad907 | 2019-12-09 07:08:20 +0100 | [diff] [blame] | 54 |     }, | 
 | 55 |  | 
 | 56 |     /** | 
 | 57 |      * Remove service from the pipe. | 
 | 58 |      */ | 
 | 59 |     remove : function (service) { | 
 | 60 |       let i = this._pipe.indexOf(service); | 
 | 61 |       if (i != -1) { | 
 | 62 |         this._pipe.splice(i, 1); | 
| Akron | ba7a049 | 2019-12-17 20:33:11 +0100 | [diff] [blame] | 63 |         this._update(); | 
| Akron | d7ad907 | 2019-12-09 07:08:20 +0100 | [diff] [blame] | 64 |       }; | 
 | 65 |     }, | 
 | 66 |  | 
 | 67 |     /** | 
 | 68 |      * The number of services in the pipe. | 
 | 69 |      */ | 
 | 70 |     size : function () { | 
 | 71 |       return this._pipe.length; | 
 | 72 |     }, | 
 | 73 |  | 
 | 74 |     /** | 
 | 75 |      * Return the pipe as a string. | 
 | 76 |      */ | 
 | 77 |     toString : function () { | 
 | 78 |       return this._pipe.join(','); | 
| Akron | ba7a049 | 2019-12-17 20:33:11 +0100 | [diff] [blame] | 79 |     }, | 
 | 80 |  | 
 | 81 |     /** | 
 | 82 |      * Update the pipe value. | 
 | 83 |      */ | 
 | 84 |     _update : function () { | 
 | 85 |       if (this.e != null) { | 
 | 86 |         this.e.setAttribute("value", this.toString()); | 
 | 87 |       }; | 
 | 88 |     }, | 
 | 89 |      | 
 | 90 |     /** | 
 | 91 |      * Return the pipe element. | 
 | 92 |      */ | 
 | 93 |     element : function () { | 
 | 94 |       if (this.e == null) { | 
 | 95 |         this.e = document.createElement('input'); | 
 | 96 |         this.e.setAttribute("type","text"); | 
 | 97 |         this.e.setAttribute("name","pipe"); | 
 | 98 |         this.e.classList.add("pipe"); | 
 | 99 |       }; | 
 | 100 |       return this.e; | 
| Akron | d7ad907 | 2019-12-09 07:08:20 +0100 | [diff] [blame] | 101 |     } | 
 | 102 |   }; | 
 | 103 | }); |