blob: 54f92ec3f260065aa18fc1a520145a5628b48bfb [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
Akron0b489ad2018-02-02 16:49:32 +01006define(['util'], {
Nils Diewaldc4c4b832015-05-05 16:00:08 +00007
8 /**
9 * Create new string value helper.
10 */
Nils Diewaldf0c4f112015-05-05 12:56:59 +000011 create : function () {
Akron88d237e2020-10-21 08:05:18 +020012 const a = arguments;
13 let regexOp = true,
14 regex = false,
15 value = '';
Akronb19803c2018-08-16 16:39:42 +020016
17 // Set value
Akron88d237e2020-10-21 08:05:18 +020018 if (a.length >= 1) {
19 if (a[0] !== undefined)
20 value = a[0];
Nils Diewaldf0c4f112015-05-05 12:56:59 +000021 };
Akronb19803c2018-08-16 16:39:42 +020022
23 // Set regex
Akron88d237e2020-10-21 08:05:18 +020024 if (a.length >= 2) {
25 if (a[1] !== undefined)
26 regex = a[1];
Akronb19803c2018-08-16 16:39:42 +020027 };
28
29 // Set regexOp
Akron88d237e2020-10-21 08:05:18 +020030 if (a.length >= 3) {
31 regexOp = a[2];
Akronb19803c2018-08-16 16:39:42 +020032 if (regexOp === false) {
33 regex = false;
34 }
35 };
36 return Object.create(this)._init(value, regex, regexOp);
Nils Diewaldf0c4f112015-05-05 12:56:59 +000037 },
38
Nils Diewaldc4c4b832015-05-05 16:00:08 +000039
40 // Initialize the string value
Akronb19803c2018-08-16 16:39:42 +020041 _init : function (value, regex, regexOp) {
Nils Diewaldf0c4f112015-05-05 12:56:59 +000042 this.value(value);
43 this.regex(regex);
Akronb19803c2018-08-16 16:39:42 +020044 this._regexOp(regexOp);
Nils Diewaldf0c4f112015-05-05 12:56:59 +000045 return this;
46 },
47
Nils Diewaldc4c4b832015-05-05 16:00:08 +000048
49 /**
50 * Get or set the regex boolean value.
51 *
52 * @param bool Either true or false
53 */
Nils Diewaldf0c4f112015-05-05 12:56:59 +000054 regex : function (bool) {
55 if (arguments.length === 1) {
Akron88d237e2020-10-21 08:05:18 +020056 this._regex = bool ? true : false;
Nils Diewaldf0c4f112015-05-05 12:56:59 +000057 this._update();
58 };
59 return this._regex;
60 },
61
Akron88d237e2020-10-21 08:05:18 +020062
Akronb19803c2018-08-16 16:39:42 +020063 _regexOp : function (regexOp) {
64 if (arguments.length === 1) {
Akron88d237e2020-10-21 08:05:18 +020065 this.__regexOp = regexOp ? true : false;
Akronb19803c2018-08-16 16:39:42 +020066 this._update();
67 };
68 return this.__regexOp;
69 },
Nils Diewaldc4c4b832015-05-05 16:00:08 +000070
Akron88d237e2020-10-21 08:05:18 +020071
Nils Diewaldc4c4b832015-05-05 16:00:08 +000072 /**
73 * Toggle the regex, make it either true,
74 * if it is false, or make it false, if it is true.
75 */
Nils Diewaldf0c4f112015-05-05 12:56:59 +000076 toggleRegex : function () {
Akron88d237e2020-10-21 08:05:18 +020077 this.regex(this._regex === false ? true : false);
Nils Diewaldf0c4f112015-05-05 12:56:59 +000078 },
79
Akron88d237e2020-10-21 08:05:18 +020080
Nils Diewaldc4c4b832015-05-05 16:00:08 +000081 /**
82 * Get or set the string value.
83 */
Nils Diewaldf0c4f112015-05-05 12:56:59 +000084 value : function (val) {
85 if (arguments.length === 1) {
86 this._value = val;
Akronb19803c2018-08-16 16:39:42 +020087 // this._input.value = val;
Nils Diewaldf0c4f112015-05-05 12:56:59 +000088 this._update();
89 };
90 return this._value;
91 },
Nils Diewaldc4c4b832015-05-05 16:00:08 +000092
Akron88d237e2020-10-21 08:05:18 +020093
Nils Diewaldc4c4b832015-05-05 16:00:08 +000094 // Update dom element
Nils Diewaldf0c4f112015-05-05 12:56:59 +000095 _update : function () {
Akron24aa0052020-11-10 11:00:34 +010096 if (this._el === undefined)
Akronb19803c2018-08-16 16:39:42 +020097 return;
98
Nils Diewald7991a3f2015-05-19 14:12:37 +000099 this._value = this._input.value;
100
Akron24aa0052020-11-10 11:00:34 +0100101 const cl = this._el.classList;
Akron88d237e2020-10-21 08:05:18 +0200102
Akronb19803c2018-08-16 16:39:42 +0200103 if (this._regexOp() && this._regex) {
Akron88d237e2020-10-21 08:05:18 +0200104 cl.add('regex');
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000105 }
106 else {
Akron88d237e2020-10-21 08:05:18 +0200107 cl.remove('regex');
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000108 };
109 },
110
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000111
112 /**
113 * Store the string value.
114 * This method should be overwritten.
Akron12971a02018-01-03 16:38:10 +0100115 * The method receives the value and the regex.
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000116 */
117 store : function (v,r) {},
118
Akron88d237e2020-10-21 08:05:18 +0200119
Akron12971a02018-01-03 16:38:10 +0100120 /**
121 * Put focus on element
122 */
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000123 focus : function () {
Akron24aa0052020-11-10 11:00:34 +0100124 this._el.children[0].focus();
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000125 },
126
Akron88d237e2020-10-21 08:05:18 +0200127
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000128 /**
129 * Get the associated dom element.
130 */
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000131 element : function () {
Akron24aa0052020-11-10 11:00:34 +0100132 if (this._el !== undefined)
133 return this._el;
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000134
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000135 // Create element
Akron24aa0052020-11-10 11:00:34 +0100136 const e = this._el = document.createElement('div');
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000137 e.setAttribute('tabindex', 0);
138 e.style.outline = 0;
139
Akron88d237e2020-10-21 08:05:18 +0200140 const cl = e.classList;
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000141 cl.add('value');
142 if (this.regex() === true)
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000143 cl.add('regex');
144
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000145 // Add input field
Akron0b489ad2018-02-02 16:49:32 +0100146 this._input = e.addE('input');
Akron8db5e3a2018-05-28 19:25:26 +0200147
148 if (this.value() !== undefined) {
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000149 this._input.value = this.value();
Akron8db5e3a2018-05-28 19:25:26 +0200150 };
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000151
Nils Diewald9c125062015-05-05 23:54:17 +0000152 // Add regex button
Akronb19803c2018-08-16 16:39:42 +0200153 if (this._regexOp()) {
Akron88d237e2020-10-21 08:05:18 +0200154 const re = e.addE('div');
Akronb19803c2018-08-16 16:39:42 +0200155 re.addEventListener(
156 'click',
Akron88d237e2020-10-21 08:05:18 +0200157 function () {
Akronb19803c2018-08-16 16:39:42 +0200158 this.toggleRegex();
159 // ev.halt();
160 }.bind(this),
161 true
162 );
163 re.addT('RE');
164 };
Nils Diewald9c125062015-05-05 23:54:17 +0000165
Akron12971a02018-01-03 16:38:10 +0100166 // If the focus is not on the text field anymore,
167 // delegate focus to
168 this._input.addEventListener(
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000169 'blur',
Akron88d237e2020-10-21 08:05:18 +0200170 function () {
171 const t = this;
172 if (!t._inField) {
173 t.value(t._input.value);
174 t.store(t.value(), t.regex());
Akron12971a02018-01-03 16:38:10 +0100175 };
176 ev.halt();
177 }.bind(this)
178 );
179
180 // Workaround to check the click is in the field
181 e.addEventListener(
182 'mousedown',
Akron88d237e2020-10-21 08:05:18 +0200183 function () {
Akron12971a02018-01-03 16:38:10 +0100184 this._inField = true;
185 }.bind(this)
186 );
187
188 e.addEventListener(
189 'mouseup',
Akron88d237e2020-10-21 08:05:18 +0200190 function () {
Akron12971a02018-01-03 16:38:10 +0100191 this._inField = false;
192 this._input.focus();
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000193 }.bind(this)
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000194 );
195
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000196 this._input.addEventListener(
197 'keypress',
Akron12971a02018-01-03 16:38:10 +0100198 function (ev) {
Akron88d237e2020-10-21 08:05:18 +0200199 const t = this;
Akron12971a02018-01-03 16:38:10 +0100200 if (ev.keyCode == 13) {
Akron88d237e2020-10-21 08:05:18 +0200201 t.value(t._input.value);
202 t.store(t.value(), t.regex());
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000203 return false;
Akron8778f5d2017-06-30 21:25:55 +0200204 };
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000205 }.bind(this)
206 );
207
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000208 // Add store button
209 /*
210 e.appendChild(
211 document.createElement('div')
212 ).addEventListener('click', function () {
213 this.store(this.value(), this.regex());
214 }.bind(this));
215 */
216 return e;
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000217 }
218});