blob: 9847b45067916ba593c1ef55cf72741d5bc243c8 [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
4 * passed to the search engine.
5 *
6 * @author Nils Diewald
7 */
8define(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);
Akronba7a0492019-12-17 20:33:11 +010039 if (service) {
Akrond7ad9072019-12-09 07:08:20 +010040 this._pipe.push(service);
Akronba7a0492019-12-17 20:33:11 +010041 this._update();
42 };
Akrond7ad9072019-12-09 07:08:20 +010043 },
44
45 /**
46 * Prepend service to pipe.
47 */
48 prepend : function (service) {
49 service = _notNull(service);
Akronba7a0492019-12-17 20:33:11 +010050 if (service) {
Akrond7ad9072019-12-09 07:08:20 +010051 this._pipe.unshift(service);
Akronba7a0492019-12-17 20:33:11 +010052 this._update();
53 };
Akrond7ad9072019-12-09 07:08:20 +010054 },
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);
Akronba7a0492019-12-17 20:33:11 +010063 this._update();
Akrond7ad9072019-12-09 07:08:20 +010064 };
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(',');
Akronba7a0492019-12-17 20:33:11 +010079 },
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;
Akrond7ad9072019-12-09 07:08:20 +0100101 }
102 };
103});