blob: 10daaee7ff467c42726c873a623e2b6c6afb0036 [file] [log] [blame]
Nils Diewaldf0c4f112015-05-05 12:56:59 +00001/**
2 * Add string values to the virtual collection
3 */
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 () {
10 var regex = false;
11 var value = '';
12 if (arguments.length == 2) {
13 regex = arguments[1];
14 };
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 };
19 return Object.create(this)._init(value, regex);
20 },
21
Nils Diewaldc4c4b832015-05-05 16:00:08 +000022
23 // Initialize the string value
Nils Diewaldf0c4f112015-05-05 12:56:59 +000024 _init : function (value, regex) {
25 this.element();
26 this.value(value);
27 this.regex(regex);
28 return this;
29 },
30
Nils Diewaldc4c4b832015-05-05 16:00:08 +000031
32 /**
33 * Get or set the regex boolean value.
34 *
35 * @param bool Either true or false
36 */
Nils Diewaldf0c4f112015-05-05 12:56:59 +000037 regex : function (bool) {
38 if (arguments.length === 1) {
39 if (bool) {
Akron8778f5d2017-06-30 21:25:55 +020040 this._regex = true;
Nils Diewaldf0c4f112015-05-05 12:56:59 +000041 }
42 else {
Akron8778f5d2017-06-30 21:25:55 +020043 this._regex = false;
Nils Diewaldf0c4f112015-05-05 12:56:59 +000044 };
45 this._update();
46 };
47 return this._regex;
48 },
49
Nils Diewaldc4c4b832015-05-05 16:00:08 +000050
51 /**
52 * Toggle the regex, make it either true,
53 * if it is false, or make it false, if it is true.
54 */
Nils Diewaldf0c4f112015-05-05 12:56:59 +000055 toggleRegex : function () {
56 if (this._regex === false)
57 this.regex(true);
58 else
59 this.regex(false);
60 },
61
Nils Diewaldc4c4b832015-05-05 16:00:08 +000062 /**
63 * Get or set the string value.
64 */
Nils Diewaldf0c4f112015-05-05 12:56:59 +000065 value : function (val) {
66 if (arguments.length === 1) {
67 this._value = val;
Nils Diewald7991a3f2015-05-19 14:12:37 +000068 this._input.value = val;
Nils Diewaldf0c4f112015-05-05 12:56:59 +000069 this._update();
70 };
71 return this._value;
72 },
Nils Diewaldc4c4b832015-05-05 16:00:08 +000073
74
75 // Update dom element
Nils Diewaldf0c4f112015-05-05 12:56:59 +000076 _update : function () {
Nils Diewald7991a3f2015-05-19 14:12:37 +000077 this._value = this._input.value;
78
Nils Diewaldf0c4f112015-05-05 12:56:59 +000079 if (this._regex) {
80 this._element.classList.add('regex');
81 }
82 else {
Nils Diewaldc4c4b832015-05-05 16:00:08 +000083 this._element.classList.remove('regex');
Nils Diewaldf0c4f112015-05-05 12:56:59 +000084 };
85 },
86
Nils Diewaldc4c4b832015-05-05 16:00:08 +000087
88 /**
89 * Store the string value.
90 * This method should be overwritten.
Akron12971a02018-01-03 16:38:10 +010091 * The method receives the value and the regex.
Nils Diewaldc4c4b832015-05-05 16:00:08 +000092 */
93 store : function (v,r) {},
94
95
Akron12971a02018-01-03 16:38:10 +010096 /**
97 * Put focus on element
98 */
Nils Diewaldc4c4b832015-05-05 16:00:08 +000099 focus : function () {
100 this._element.children[0].focus();
101 },
102
103 /**
104 * Get the associated dom element.
105 */
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000106 element : function () {
107 if (this._element !== undefined)
108 return this._element;
109
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000110 // Create element
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000111 this._element = document.createElement('div');
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000112 var e = this._element;
113 e.setAttribute('tabindex', 0);
114 e.style.outline = 0;
115
116 var cl = e.classList;
117 cl.add('value');
118 if (this.regex() === true)
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000119 cl.add('regex');
120
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000121 // Add input field
Akron0b489ad2018-02-02 16:49:32 +0100122 this._input = e.addE('input');
Akron8db5e3a2018-05-28 19:25:26 +0200123
124 if (this.value() !== undefined) {
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000125 this._input.value = this.value();
Akron8db5e3a2018-05-28 19:25:26 +0200126 };
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000127
Nils Diewald9c125062015-05-05 23:54:17 +0000128 // Add regex button
Akron0b489ad2018-02-02 16:49:32 +0100129 var re = e.addE('div');
Nils Diewald9c125062015-05-05 23:54:17 +0000130 re.addEventListener(
131 'click',
Akron12971a02018-01-03 16:38:10 +0100132 function (ev) {
Akron8778f5d2017-06-30 21:25:55 +0200133 this.toggleRegex();
Akron12971a02018-01-03 16:38:10 +0100134 // ev.halt();
Nils Diewald9c125062015-05-05 23:54:17 +0000135 }.bind(this),
136 true
137 );
Akron0b489ad2018-02-02 16:49:32 +0100138 re.addT('RE');
Nils Diewald9c125062015-05-05 23:54:17 +0000139
Akron12971a02018-01-03 16:38:10 +0100140 // If the focus is not on the text field anymore,
141 // delegate focus to
142 this._input.addEventListener(
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000143 'blur',
Akron8db5e3a2018-05-28 19:25:26 +0200144 function (ev) {
Akron12971a02018-01-03 16:38:10 +0100145 if (!this._inField) {
146 this.value(this._input.value);
147 this.store(this.value(), this.regex());
148 };
149 ev.halt();
150 }.bind(this)
151 );
152
153 // Workaround to check the click is in the field
154 e.addEventListener(
155 'mousedown',
156 function (ev) {
157 this._inField = true;
158 }.bind(this)
159 );
160
161 e.addEventListener(
162 'mouseup',
163 function (ev) {
164 this._inField = false;
165 this._input.focus();
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000166 }.bind(this)
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000167 );
168
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000169 this._input.addEventListener(
170 'keypress',
Akron12971a02018-01-03 16:38:10 +0100171 function (ev) {
172 if (ev.keyCode == 13) {
Akron8778f5d2017-06-30 21:25:55 +0200173 this.value(this._input.value);
174 this.store(this.value(), this.regex());
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000175 return false;
Akron8778f5d2017-06-30 21:25:55 +0200176 };
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000177 }.bind(this)
178 );
179
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000180 // Add store button
181 /*
182 e.appendChild(
183 document.createElement('div')
184 ).addEventListener('click', function () {
185 this.store(this.value(), this.regex());
186 }.bind(this));
187 */
188 return e;
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000189 }
190});