blob: 8b05787dacf44596913603c964758fd123d6ee10 [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 */
Akron0b489ad2018-02-02 16:49:32 +01004define(['util'], {
Nils Diewaldc4c4b832015-05-05 16:00:08 +00005
6 /**
7 * Create new string value helper.
8 */
Nils Diewaldf0c4f112015-05-05 12:56:59 +00009 create : function () {
Akronb19803c2018-08-16 16:39:42 +020010 var regexOp = true;
Nils Diewaldf0c4f112015-05-05 12:56:59 +000011 var regex = false;
12 var value = '';
Akronb19803c2018-08-16 16:39:42 +020013
14 // Set value
Nils Diewaldc4c4b832015-05-05 16:00:08 +000015 if (arguments.length >= 1) {
Akron8db5e3a2018-05-28 19:25:26 +020016 if (arguments[0] !== undefined)
17 value = arguments[0];
Nils Diewaldf0c4f112015-05-05 12:56:59 +000018 };
Akronb19803c2018-08-16 16:39:42 +020019
20 // Set regex
21 if (arguments.length >= 2) {
22 if (arguments[1] !== undefined)
23 regex = arguments[1];
24 };
25
26 // Set regexOp
27 if (arguments.length >= 3) {
28 regexOp = arguments[2];
29 if (regexOp === false) {
30 regex = false;
31 }
32 };
33 return Object.create(this)._init(value, regex, regexOp);
Nils Diewaldf0c4f112015-05-05 12:56:59 +000034 },
35
Nils Diewaldc4c4b832015-05-05 16:00:08 +000036
37 // Initialize the string value
Akronb19803c2018-08-16 16:39:42 +020038 _init : function (value, regex, regexOp) {
Nils Diewaldf0c4f112015-05-05 12:56:59 +000039 this.value(value);
40 this.regex(regex);
Akronb19803c2018-08-16 16:39:42 +020041 this._regexOp(regexOp);
Nils Diewaldf0c4f112015-05-05 12:56:59 +000042 return this;
43 },
44
Nils Diewaldc4c4b832015-05-05 16:00:08 +000045
46 /**
47 * Get or set the regex boolean value.
48 *
49 * @param bool Either true or false
50 */
Nils Diewaldf0c4f112015-05-05 12:56:59 +000051 regex : function (bool) {
52 if (arguments.length === 1) {
53 if (bool) {
Akron8778f5d2017-06-30 21:25:55 +020054 this._regex = true;
Nils Diewaldf0c4f112015-05-05 12:56:59 +000055 }
56 else {
Akron8778f5d2017-06-30 21:25:55 +020057 this._regex = false;
Nils Diewaldf0c4f112015-05-05 12:56:59 +000058 };
59 this._update();
60 };
61 return this._regex;
62 },
63
Akronb19803c2018-08-16 16:39:42 +020064 _regexOp : function (regexOp) {
65 if (arguments.length === 1) {
66 if (regexOp) {
67 this.__regexOp = true;
68 }
69 else {
70 this.__regexOp = false;
71 };
72 this._update();
73 };
74 return this.__regexOp;
75 },
Nils Diewaldc4c4b832015-05-05 16:00:08 +000076
77 /**
78 * Toggle the regex, make it either true,
79 * if it is false, or make it false, if it is true.
80 */
Nils Diewaldf0c4f112015-05-05 12:56:59 +000081 toggleRegex : function () {
82 if (this._regex === false)
83 this.regex(true);
84 else
85 this.regex(false);
86 },
87
Nils Diewaldc4c4b832015-05-05 16:00:08 +000088 /**
89 * Get or set the string value.
90 */
Nils Diewaldf0c4f112015-05-05 12:56:59 +000091 value : function (val) {
92 if (arguments.length === 1) {
93 this._value = val;
Akronb19803c2018-08-16 16:39:42 +020094 // this._input.value = val;
Nils Diewaldf0c4f112015-05-05 12:56:59 +000095 this._update();
96 };
97 return this._value;
98 },
Nils Diewaldc4c4b832015-05-05 16:00:08 +000099
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000100 // Update dom element
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000101 _update : function () {
Akronb19803c2018-08-16 16:39:42 +0200102 if (this._element === undefined)
103 return;
104
Nils Diewald7991a3f2015-05-19 14:12:37 +0000105 this._value = this._input.value;
106
Akronb19803c2018-08-16 16:39:42 +0200107 if (this._regexOp() && this._regex) {
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000108 this._element.classList.add('regex');
109 }
110 else {
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000111 this._element.classList.remove('regex');
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000112 };
113 },
114
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000115
116 /**
117 * Store the string value.
118 * This method should be overwritten.
Akron12971a02018-01-03 16:38:10 +0100119 * The method receives the value and the regex.
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000120 */
121 store : function (v,r) {},
122
Akron12971a02018-01-03 16:38:10 +0100123 /**
124 * Put focus on element
125 */
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000126 focus : function () {
127 this._element.children[0].focus();
128 },
129
130 /**
131 * Get the associated dom element.
132 */
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000133 element : function () {
134 if (this._element !== undefined)
135 return this._element;
136
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000137 // Create element
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000138 this._element = document.createElement('div');
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000139 var e = this._element;
140 e.setAttribute('tabindex', 0);
141 e.style.outline = 0;
142
143 var cl = e.classList;
144 cl.add('value');
145 if (this.regex() === true)
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000146 cl.add('regex');
147
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000148 // Add input field
Akron0b489ad2018-02-02 16:49:32 +0100149 this._input = e.addE('input');
Akron8db5e3a2018-05-28 19:25:26 +0200150
151 if (this.value() !== undefined) {
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000152 this._input.value = this.value();
Akron8db5e3a2018-05-28 19:25:26 +0200153 };
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000154
Nils Diewald9c125062015-05-05 23:54:17 +0000155 // Add regex button
Akronb19803c2018-08-16 16:39:42 +0200156 if (this._regexOp()) {
157 var re = e.addE('div');
158 re.addEventListener(
159 'click',
160 function (ev) {
161 this.toggleRegex();
162 // ev.halt();
163 }.bind(this),
164 true
165 );
166 re.addT('RE');
167 };
Nils Diewald9c125062015-05-05 23:54:17 +0000168
Akron12971a02018-01-03 16:38:10 +0100169 // If the focus is not on the text field anymore,
170 // delegate focus to
171 this._input.addEventListener(
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000172 'blur',
Akron8db5e3a2018-05-28 19:25:26 +0200173 function (ev) {
Akron12971a02018-01-03 16:38:10 +0100174 if (!this._inField) {
175 this.value(this._input.value);
176 this.store(this.value(), this.regex());
177 };
178 ev.halt();
179 }.bind(this)
180 );
181
182 // Workaround to check the click is in the field
183 e.addEventListener(
184 'mousedown',
185 function (ev) {
186 this._inField = true;
187 }.bind(this)
188 );
189
190 e.addEventListener(
191 'mouseup',
192 function (ev) {
193 this._inField = false;
194 this._input.focus();
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000195 }.bind(this)
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000196 );
197
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000198 this._input.addEventListener(
199 'keypress',
Akron12971a02018-01-03 16:38:10 +0100200 function (ev) {
201 if (ev.keyCode == 13) {
Akron8778f5d2017-06-30 21:25:55 +0200202 this.value(this._input.value);
203 this.store(this.value(), this.regex());
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000204 return false;
Akron8778f5d2017-06-30 21:25:55 +0200205 };
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000206 }.bind(this)
207 );
208
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000209 // Add store button
210 /*
211 e.appendChild(
212 document.createElement('div')
213 ).addEventListener('click', function () {
214 this.store(this.value(), this.regex());
215 }.bind(this));
216 */
217 return e;
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000218 }
219});