blob: bd3a65c5c16855f79f5dcdf658f40dd386127e00 [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) {
Nils Diewaldf0c4f112015-05-05 12:56:59 +000016 value = arguments[0];
17 };
18 return Object.create(this)._init(value, regex);
19 },
20
Nils Diewaldc4c4b832015-05-05 16:00:08 +000021
22 // Initialize the string value
Nils Diewaldf0c4f112015-05-05 12:56:59 +000023 _init : function (value, regex) {
24 this.element();
25 this.value(value);
26 this.regex(regex);
27 return this;
28 },
29
Nils Diewaldc4c4b832015-05-05 16:00:08 +000030
31 /**
32 * Get or set the regex boolean value.
33 *
34 * @param bool Either true or false
35 */
Nils Diewaldf0c4f112015-05-05 12:56:59 +000036 regex : function (bool) {
37 if (arguments.length === 1) {
38 if (bool) {
Akron8778f5d2017-06-30 21:25:55 +020039 this._regex = true;
Nils Diewaldf0c4f112015-05-05 12:56:59 +000040 }
41 else {
Akron8778f5d2017-06-30 21:25:55 +020042 this._regex = false;
Nils Diewaldf0c4f112015-05-05 12:56:59 +000043 };
44 this._update();
45 };
46 return this._regex;
47 },
48
Nils Diewaldc4c4b832015-05-05 16:00:08 +000049
50 /**
51 * Toggle the regex, make it either true,
52 * if it is false, or make it false, if it is true.
53 */
Nils Diewaldf0c4f112015-05-05 12:56:59 +000054 toggleRegex : function () {
55 if (this._regex === false)
56 this.regex(true);
57 else
58 this.regex(false);
59 },
60
Nils Diewaldc4c4b832015-05-05 16:00:08 +000061 /**
62 * Get or set the string value.
63 */
Nils Diewaldf0c4f112015-05-05 12:56:59 +000064 value : function (val) {
65 if (arguments.length === 1) {
66 this._value = val;
Nils Diewald7991a3f2015-05-19 14:12:37 +000067 this._input.value = val;
Nils Diewaldf0c4f112015-05-05 12:56:59 +000068 this._update();
69 };
70 return this._value;
71 },
Nils Diewaldc4c4b832015-05-05 16:00:08 +000072
73
74 // Update dom element
Nils Diewaldf0c4f112015-05-05 12:56:59 +000075 _update : function () {
Nils Diewald7991a3f2015-05-19 14:12:37 +000076 this._value = this._input.value;
77
Nils Diewaldf0c4f112015-05-05 12:56:59 +000078 if (this._regex) {
79 this._element.classList.add('regex');
80 }
81 else {
Nils Diewaldc4c4b832015-05-05 16:00:08 +000082 this._element.classList.remove('regex');
Nils Diewaldf0c4f112015-05-05 12:56:59 +000083 };
84 },
85
Nils Diewaldc4c4b832015-05-05 16:00:08 +000086
87 /**
88 * Store the string value.
89 * This method should be overwritten.
Akron12971a02018-01-03 16:38:10 +010090 * The method receives the value and the regex.
Nils Diewaldc4c4b832015-05-05 16:00:08 +000091 */
92 store : function (v,r) {},
93
94
Akron12971a02018-01-03 16:38:10 +010095 /**
96 * Put focus on element
97 */
Nils Diewaldc4c4b832015-05-05 16:00:08 +000098 focus : function () {
99 this._element.children[0].focus();
100 },
101
102 /**
103 * Get the associated dom element.
104 */
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000105 element : function () {
106 if (this._element !== undefined)
107 return this._element;
108
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000109 // Create element
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000110 this._element = document.createElement('div');
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000111 var e = this._element;
112 e.setAttribute('tabindex', 0);
113 e.style.outline = 0;
114
115 var cl = e.classList;
116 cl.add('value');
117 if (this.regex() === true)
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000118 cl.add('regex');
119
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000120 // Add input field
Akron0b489ad2018-02-02 16:49:32 +0100121 this._input = e.addE('input');
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000122 if (this.value() !== undefined)
123 this._input.value = this.value();
124
Nils Diewald9c125062015-05-05 23:54:17 +0000125 // Add regex button
Akron0b489ad2018-02-02 16:49:32 +0100126 var re = e.addE('div');
Nils Diewald9c125062015-05-05 23:54:17 +0000127 re.addEventListener(
128 'click',
Akron12971a02018-01-03 16:38:10 +0100129 function (ev) {
Akron8778f5d2017-06-30 21:25:55 +0200130 this.toggleRegex();
Akron12971a02018-01-03 16:38:10 +0100131 // ev.halt();
Nils Diewald9c125062015-05-05 23:54:17 +0000132 }.bind(this),
133 true
134 );
Akron0b489ad2018-02-02 16:49:32 +0100135 re.addT('RE');
Nils Diewald9c125062015-05-05 23:54:17 +0000136
Akron12971a02018-01-03 16:38:10 +0100137 // If the focus is not on the text field anymore,
138 // delegate focus to
139 this._input.addEventListener(
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000140 'blur',
Akron12971a02018-01-03 16:38:10 +0100141 function (ev) {
142 if (!this._inField) {
143 this.value(this._input.value);
144 this.store(this.value(), this.regex());
145 };
146 ev.halt();
147 }.bind(this)
148 );
149
150 // Workaround to check the click is in the field
151 e.addEventListener(
152 'mousedown',
153 function (ev) {
154 this._inField = true;
155 }.bind(this)
156 );
157
158 e.addEventListener(
159 'mouseup',
160 function (ev) {
161 this._inField = false;
162 this._input.focus();
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000163 }.bind(this)
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000164 );
165
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000166 this._input.addEventListener(
167 'keypress',
Akron12971a02018-01-03 16:38:10 +0100168 function (ev) {
169 if (ev.keyCode == 13) {
Akron8778f5d2017-06-30 21:25:55 +0200170 this.value(this._input.value);
171 this.store(this.value(), this.regex());
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000172 return false;
Akron8778f5d2017-06-30 21:25:55 +0200173 };
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000174 }.bind(this)
175 );
176
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000177 // Add store button
178 /*
179 e.appendChild(
180 document.createElement('div')
181 ).addEventListener('click', function () {
182 this.store(this.value(), this.regex());
183 }.bind(this));
184 */
185 return e;
Nils Diewaldf0c4f112015-05-05 12:56:59 +0000186 }
187});