blob: 7f0f6be8c1125e23cde3efd703ff75744ea74e88 [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 */
Akronc854ce42020-10-19 14:08:58 +02008"use strict";
Akrond7ad9072019-12-09 07:08:20 +01009define(function () {
Akrond7ad9072019-12-09 07:08:20 +010010 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;
Akronc854ce42020-10-19 14:08:58 +020019 };
Akrond7ad9072019-12-09 07:08:20 +010020
21 return {
22
23 /**
24 * Constructor
25 */
26 create : function () {
Akronc854ce42020-10-19 14:08:58 +020027 const obj = Object.create(this);
Akrond7ad9072019-12-09 07:08:20 +010028 obj._pipe = [];
29 return obj;
30 },
31
Akronc854ce42020-10-19 14:08:58 +020032
Akrond7ad9072019-12-09 07:08:20 +010033 /**
34 * Append service to pipe.
35 */
36 append : function (service) {
37 service = _notNull(service);
Akronba7a0492019-12-17 20:33:11 +010038 if (service) {
Akrond7ad9072019-12-09 07:08:20 +010039 this._pipe.push(service);
Akronba7a0492019-12-17 20:33:11 +010040 this._update();
41 };
Akrond7ad9072019-12-09 07:08:20 +010042 },
43
Akronc854ce42020-10-19 14:08:58 +020044
Akrond7ad9072019-12-09 07:08:20 +010045 /**
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
Akronc854ce42020-10-19 14:08:58 +020056
Akrond7ad9072019-12-09 07:08:20 +010057 /**
58 * Remove service from the pipe.
59 */
60 remove : function (service) {
Akronc854ce42020-10-19 14:08:58 +020061 const i = this._pipe.indexOf(service);
Akrond7ad9072019-12-09 07:08:20 +010062 if (i != -1) {
63 this._pipe.splice(i, 1);
Akronba7a0492019-12-17 20:33:11 +010064 this._update();
Akrond7ad9072019-12-09 07:08:20 +010065 };
66 },
67
Akronc854ce42020-10-19 14:08:58 +020068
Akrond7ad9072019-12-09 07:08:20 +010069 /**
70 * The number of services in the pipe.
71 */
72 size : function () {
73 return this._pipe.length;
74 },
75
Akronc854ce42020-10-19 14:08:58 +020076
Akrond7ad9072019-12-09 07:08:20 +010077 /**
78 * Return the pipe as a string.
79 */
80 toString : function () {
81 return this._pipe.join(',');
Akronba7a0492019-12-17 20:33:11 +010082 },
83
Akronc854ce42020-10-19 14:08:58 +020084
Akronba7a0492019-12-17 20:33:11 +010085 /**
86 * Update the pipe value.
87 */
88 _update : function () {
89 if (this.e != null) {
90 this.e.setAttribute("value", this.toString());
91 };
92 },
93
Akronc854ce42020-10-19 14:08:58 +020094
Akronba7a0492019-12-17 20:33:11 +010095 /**
96 * Return the pipe element.
97 */
98 element : function () {
Akronc854ce42020-10-19 14:08:58 +020099 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");
Akronba7a0492019-12-17 20:33:11 +0100105 };
Akronc854ce42020-10-19 14:08:58 +0200106 return e;
Akrond7ad9072019-12-09 07:08:20 +0100107 }
108 };
109});