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