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 | */ |
Akron | c854ce4 | 2020-10-19 14:08:58 +0200 | [diff] [blame^] | 8 | "use strict"; |
Akron | d7ad907 | 2019-12-09 07:08:20 +0100 | [diff] [blame] | 9 | define(function () { |
Akron | d7ad907 | 2019-12-09 07:08:20 +0100 | [diff] [blame] | 10 | const notNullRe = new RegExp("[a-zA-Z0-9]"); |
| 11 | |
| 12 | // Trim and check |
| 13 | function _notNull (service) { |
| 14 | service = service.trim(); |
| 15 | if (service.match(notNullRe)) { |
| 16 | return service; |
| 17 | }; |
| 18 | return null; |
Akron | c854ce4 | 2020-10-19 14:08:58 +0200 | [diff] [blame^] | 19 | }; |
Akron | d7ad907 | 2019-12-09 07:08:20 +0100 | [diff] [blame] | 20 | |
| 21 | return { |
| 22 | |
| 23 | /** |
| 24 | * Constructor |
| 25 | */ |
| 26 | create : function () { |
Akron | c854ce4 | 2020-10-19 14:08:58 +0200 | [diff] [blame^] | 27 | const obj = Object.create(this); |
Akron | d7ad907 | 2019-12-09 07:08:20 +0100 | [diff] [blame] | 28 | obj._pipe = []; |
| 29 | return obj; |
| 30 | }, |
| 31 | |
Akron | c854ce4 | 2020-10-19 14:08:58 +0200 | [diff] [blame^] | 32 | |
Akron | d7ad907 | 2019-12-09 07:08:20 +0100 | [diff] [blame] | 33 | /** |
| 34 | * Append service to pipe. |
| 35 | */ |
| 36 | append : function (service) { |
| 37 | service = _notNull(service); |
Akron | ba7a049 | 2019-12-17 20:33:11 +0100 | [diff] [blame] | 38 | if (service) { |
Akron | d7ad907 | 2019-12-09 07:08:20 +0100 | [diff] [blame] | 39 | this._pipe.push(service); |
Akron | ba7a049 | 2019-12-17 20:33:11 +0100 | [diff] [blame] | 40 | this._update(); |
| 41 | }; |
Akron | d7ad907 | 2019-12-09 07:08:20 +0100 | [diff] [blame] | 42 | }, |
| 43 | |
Akron | c854ce4 | 2020-10-19 14:08:58 +0200 | [diff] [blame^] | 44 | |
Akron | d7ad907 | 2019-12-09 07:08:20 +0100 | [diff] [blame] | 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 | |
Akron | c854ce4 | 2020-10-19 14:08:58 +0200 | [diff] [blame^] | 56 | |
Akron | d7ad907 | 2019-12-09 07:08:20 +0100 | [diff] [blame] | 57 | /** |
| 58 | * Remove service from the pipe. |
| 59 | */ |
| 60 | remove : function (service) { |
Akron | c854ce4 | 2020-10-19 14:08:58 +0200 | [diff] [blame^] | 61 | const i = this._pipe.indexOf(service); |
Akron | d7ad907 | 2019-12-09 07:08:20 +0100 | [diff] [blame] | 62 | if (i != -1) { |
| 63 | this._pipe.splice(i, 1); |
Akron | ba7a049 | 2019-12-17 20:33:11 +0100 | [diff] [blame] | 64 | this._update(); |
Akron | d7ad907 | 2019-12-09 07:08:20 +0100 | [diff] [blame] | 65 | }; |
| 66 | }, |
| 67 | |
Akron | c854ce4 | 2020-10-19 14:08:58 +0200 | [diff] [blame^] | 68 | |
Akron | d7ad907 | 2019-12-09 07:08:20 +0100 | [diff] [blame] | 69 | /** |
| 70 | * The number of services in the pipe. |
| 71 | */ |
| 72 | size : function () { |
| 73 | return this._pipe.length; |
| 74 | }, |
| 75 | |
Akron | c854ce4 | 2020-10-19 14:08:58 +0200 | [diff] [blame^] | 76 | |
Akron | d7ad907 | 2019-12-09 07:08:20 +0100 | [diff] [blame] | 77 | /** |
| 78 | * Return the pipe as a string. |
| 79 | */ |
| 80 | toString : function () { |
| 81 | return this._pipe.join(','); |
Akron | ba7a049 | 2019-12-17 20:33:11 +0100 | [diff] [blame] | 82 | }, |
| 83 | |
Akron | c854ce4 | 2020-10-19 14:08:58 +0200 | [diff] [blame^] | 84 | |
Akron | ba7a049 | 2019-12-17 20:33:11 +0100 | [diff] [blame] | 85 | /** |
| 86 | * Update the pipe value. |
| 87 | */ |
| 88 | _update : function () { |
| 89 | if (this.e != null) { |
| 90 | this.e.setAttribute("value", this.toString()); |
| 91 | }; |
| 92 | }, |
| 93 | |
Akron | c854ce4 | 2020-10-19 14:08:58 +0200 | [diff] [blame^] | 94 | |
Akron | ba7a049 | 2019-12-17 20:33:11 +0100 | [diff] [blame] | 95 | /** |
| 96 | * Return the pipe element. |
| 97 | */ |
| 98 | element : function () { |
Akron | c854ce4 | 2020-10-19 14:08:58 +0200 | [diff] [blame^] | 99 | let e = this.e; |
| 100 | if (e == null) { |
| 101 | e = this.e = document.createElement('input'); |
| 102 | e.setAttribute("type","text"); |
| 103 | e.setAttribute("name","pipe"); |
| 104 | e.classList.add("pipe"); |
Akron | ba7a049 | 2019-12-17 20:33:11 +0100 | [diff] [blame] | 105 | }; |
Akron | c854ce4 | 2020-10-19 14:08:58 +0200 | [diff] [blame^] | 106 | return e; |
Akron | d7ad907 | 2019-12-09 07:08:20 +0100 | [diff] [blame] | 107 | } |
| 108 | }; |
| 109 | }); |