blob: f10853e61085cf1b91db940f3a73e984a953c832 [file] [log] [blame]
Akrond7ad9072019-12-09 07:08:20 +01001/**
2 * Create a pipe object, that holds a list of services
3 * meant to transform the KQ passed before it's finally
Akron910828a2025-06-27 15:38:48 +02004 * passed to the search engine or to transform the response
5 * afterwards.
Akrond7ad9072019-12-09 07:08:20 +01006 *
7 * @author Nils Diewald
8 */
Akronc854ce42020-10-19 14:08:58 +02009"use strict";
Akron910828a2025-06-27 15:38:48 +020010define(['util'], function () {
Akrond7ad9072019-12-09 07:08:20 +010011 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;
Akronc854ce42020-10-19 14:08:58 +020020 };
Akrond7ad9072019-12-09 07:08:20 +010021
22 return {
23
24 /**
25 * Constructor
26 */
Akron910828a2025-06-27 15:38:48 +020027 create : function (name) {
Akronc854ce42020-10-19 14:08:58 +020028 const obj = Object.create(this);
Akron910828a2025-06-27 15:38:48 +020029 obj._name = (name == undefined) ? 'pipe' : name;
Akrond7ad9072019-12-09 07:08:20 +010030 obj._pipe = [];
31 return obj;
32 },
33
Akronc854ce42020-10-19 14:08:58 +020034
Akrond7ad9072019-12-09 07:08:20 +010035 /**
36 * Append service to pipe.
37 */
38 append : function (service) {
39 service = _notNull(service);
Akronba7a0492019-12-17 20:33:11 +010040 if (service) {
Akrond7ad9072019-12-09 07:08:20 +010041 this._pipe.push(service);
Akronba7a0492019-12-17 20:33:11 +010042 this._update();
43 };
Akrond7ad9072019-12-09 07:08:20 +010044 },
45
Akronc854ce42020-10-19 14:08:58 +020046
Akrond7ad9072019-12-09 07:08:20 +010047 /**
48 * Prepend service to pipe.
49 */
50 prepend : function (service) {
51 service = _notNull(service);
Akronba7a0492019-12-17 20:33:11 +010052 if (service) {
Akrond7ad9072019-12-09 07:08:20 +010053 this._pipe.unshift(service);
Akronba7a0492019-12-17 20:33:11 +010054 this._update();
55 };
Akrond7ad9072019-12-09 07:08:20 +010056 },
57
Akronc854ce42020-10-19 14:08:58 +020058
Akrond7ad9072019-12-09 07:08:20 +010059 /**
60 * Remove service from the pipe.
61 */
62 remove : function (service) {
Akronc854ce42020-10-19 14:08:58 +020063 const i = this._pipe.indexOf(service);
Akrond7ad9072019-12-09 07:08:20 +010064 if (i != -1) {
65 this._pipe.splice(i, 1);
Akronba7a0492019-12-17 20:33:11 +010066 this._update();
Akrond7ad9072019-12-09 07:08:20 +010067 };
68 },
69
Akronc854ce42020-10-19 14:08:58 +020070
Akrond7ad9072019-12-09 07:08:20 +010071 /**
72 * The number of services in the pipe.
73 */
74 size : function () {
75 return this._pipe.length;
76 },
77
Akronc854ce42020-10-19 14:08:58 +020078
Akrond7ad9072019-12-09 07:08:20 +010079 /**
80 * Return the pipe as a string.
81 */
82 toString : function () {
83 return this._pipe.join(',');
Akronba7a0492019-12-17 20:33:11 +010084 },
85
Akron910828a2025-06-27 15:38:48 +020086 /**
87 * Return the pipe as a an URI compliant string.
88 */
89 toUriString : function () {
90 return encodeURIComponent(this.toString());
91 },
92
Akronc854ce42020-10-19 14:08:58 +020093
Akronba7a0492019-12-17 20:33:11 +010094 /**
95 * Update the pipe value.
96 */
97 _update : function () {
98 if (this.e != null) {
99 this.e.setAttribute("value", this.toString());
100 };
101 },
102
Akronc854ce42020-10-19 14:08:58 +0200103
Akronba7a0492019-12-17 20:33:11 +0100104 /**
105 * Return the pipe element.
106 */
107 element : function () {
Akronc854ce42020-10-19 14:08:58 +0200108 let e = this.e;
109 if (e == null) {
110 e = this.e = document.createElement('input');
111 e.setAttribute("type","text");
Akron910828a2025-06-27 15:38:48 +0200112 e.setAttribute("name",this._name);
Akronc854ce42020-10-19 14:08:58 +0200113 e.classList.add("pipe");
Akronba7a0492019-12-17 20:33:11 +0100114 };
Akronc854ce42020-10-19 14:08:58 +0200115 return e;
Akrond7ad9072019-12-09 07:08:20 +0100116 }
117 };
118});