blob: fde86adb6f97c56c6a9644407f79d7aab59e1a9d [file] [log] [blame]
Nils Diewaldf0c4f112015-05-05 12:56:59 +00001/**
Akroncd42a142019-07-12 18:55:37 +02002 * Add string values to the virtual corpus
Nils Diewaldf0c4f112015-05-05 12:56:59 +00003 */
Akron88d237e2020-10-21 08:05:18 +02004"use strict";
5
Akrondbbe5ee2020-12-03 12:39:46 +01006define(['util'], function () {
Nils Diewaldc4c4b832015-05-05 16:00:08 +00007
Akrondbbe5ee2020-12-03 12:39:46 +01008 const loc = KorAP.Locale;
9 loc.REGEX = loc.REGEX || 'RegEx';
10 loc.REGEX_DESC = loc.REGEX_DESC || 'Use a regular expression';
Akronb19803c2018-08-16 16:39:42 +020011
Akrondbbe5ee2020-12-03 12:39:46 +010012 return {
13 /**
14 * Create new string value helper.
15 */
16 create : function () {
17 const a = arguments;
18 let regexOp = true,
19 regex = false,
20 value = '';
Akronb19803c2018-08-16 16:39:42 +020021
Akrondbbe5ee2020-12-03 12:39:46 +010022 // Set value
23 if (a.length >= 1) {
24 if (a[0] !== undefined)
25 value = a[0];
26 };
Akronb19803c2018-08-16 16:39:42 +020027
Akrondbbe5ee2020-12-03 12:39:46 +010028 // Set regex
29 if (a.length >= 2) {
30 if (a[1] !== undefined)
31 regex = a[1];
32 };
Nils Diewaldc4c4b832015-05-05 16:00:08 +000033
Akrondbbe5ee2020-12-03 12:39:46 +010034 // Set regexOp
35 if (a.length >= 3) {
36 regexOp = a[2];
37 if (regexOp === false) {
38 regex = false;
39 }
40 };
41 return Object.create(this)._init(value, regex, regexOp);
42 },
Nils Diewaldf0c4f112015-05-05 12:56:59 +000043
Akron8db5e3a2018-05-28 19:25:26 +020044
Akrondbbe5ee2020-12-03 12:39:46 +010045 // Initialize the string value
46 _init : function (value, regex, regexOp) {
47 this.value(value);
48 this.regex(regex);
49 this._regexOp(regexOp);
50 return this;
51 },
Nils Diewaldc4c4b832015-05-05 16:00:08 +000052
Akrondbbe5ee2020-12-03 12:39:46 +010053
54 /**
55 * Get or set the regex boolean value.
56 *
57 * @param bool Either true or false
58 */
59 regex : function (bool) {
60 if (arguments.length === 1) {
61 this._regex = bool ? true : false;
62 this._update();
63 };
64 return this._regex;
65 },
66
67
68 _regexOp : function (regexOp) {
69 if (arguments.length === 1) {
70 this.__regexOp = regexOp ? true : false;
71 this._update();
72 };
73 return this.__regexOp;
74 },
75
76
77 /**
78 * Toggle the regex, make it either true,
79 * if it is false, or make it false, if it is true.
80 */
81 toggleRegex : function () {
82 this.regex(this._regex === false ? true : false);
83 },
84
85
86 /**
87 * Get or set the string value.
88 */
89 value : function (val) {
90 if (arguments.length === 1) {
91 this._value = val;
92 // this._input.value = val;
93 this._update();
94 };
95 return this._value;
96 },
97
98
99 // Update dom element
100 _update : function () {
101 if (this._el === undefined)
102 return;
103
104 this._value = this._input.value;
105
106 const cl = this._el.classList;
107
108 if (this._regexOp() && this._regex) {
109 cl.add('regex');
110 }
111 else {
112 cl.remove('regex');
113 };
114 },
115
116
117 /**
118 * Store the string value.
119 * This method should be overwritten.
120 * The method receives the value and the regex.
121 */
122 store : function (v,r) {},
123
124
125 /**
126 * Put focus on element
127 */
128 focus : function () {
129 this._el.children[0].focus();
130 },
131
132
133 /**
134 * Get the associated dom element.
135 */
136 element : function () {
137 if (this._el !== undefined)
138 return this._el;
139
140 // Create element
141 const e = this._el = document.createElement('div');
142 e.setAttribute('tabindex', 0);
143 e.style.outline = 0;
144
145 const cl = e.classList;
146 cl.add('value');
147 if (this.regex() === true)
148 cl.add('regex');
149
150 // Add input field
151 this._input = e.addE('input');
152
153 if (this.value() !== undefined) {
154 this._input.value = this.value();
155 };
156
157 // Add regex button
158 if (this._regexOp()) {
159 const re = e.addE('div');
160 re.addEventListener(
161 'click',
162 function () {
163 this.toggleRegex();
164 // ev.halt();
165 }.bind(this),
166 true
167 );
168 re.setAttribute("title", "Use as regular expression");
169 re.addT('RegEx');
170 };
171
172 // If the focus is not on the text field anymore,
173 // delegate focus to
174 this._input.addEventListener(
175 'blur',
176 function (ev) {
177 const t = this;
178 if (!t._inField) {
179 t.value(t._input.value);
180 t.store(t.value(), t.regex());
181 };
182 ev.halt();
183 }.bind(this)
Akronb19803c2018-08-16 16:39:42 +0200184 );
Nils Diewald9c125062015-05-05 23:54:17 +0000185
Akrondbbe5ee2020-12-03 12:39:46 +0100186 // Workaround to check the click is in the field
187 e.addEventListener(
188 'mousedown',
189 function () {
190 this._inField = true;
191 }.bind(this)
192 );
Akron12971a02018-01-03 16:38:10 +0100193
Akrondbbe5ee2020-12-03 12:39:46 +0100194 e.addEventListener(
195 'mouseup',
196 function () {
197 this._inField = false;
198 this._input.focus();
199 }.bind(this)
200 );
Akron12971a02018-01-03 16:38:10 +0100201
Akrondbbe5ee2020-12-03 12:39:46 +0100202 this._input.addEventListener(
203 'keypress',
204 function (ev) {
205 const t = this;
206 if (ev.keyCode == 13) {
207 t.value(t._input.value);
208 t.store(t.value(), t.regex());
209 return false;
210 };
211 }.bind(this)
212 );
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000213
Akrondbbe5ee2020-12-03 12:39:46 +0100214 // Add store button
215 /*
216 e.appendChild(
217 document.createElement('div')
218 ).addEventListener('click', function () {
219 this.store(this.value(), this.regex());
220 }.bind(this));
221 */
222 return e;
223 }
224 };
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000225});