Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Add string values to the virtual collection |
| 3 | */ |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 4 | define(['util'], { |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 5 | |
| 6 | /** |
| 7 | * Create new string value helper. |
| 8 | */ |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 9 | create : function () { |
| 10 | var regex = false; |
| 11 | var value = ''; |
| 12 | if (arguments.length == 2) { |
| 13 | regex = arguments[1]; |
| 14 | }; |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 15 | if (arguments.length >= 1) { |
Akron | 8db5e3a | 2018-05-28 19:25:26 +0200 | [diff] [blame^] | 16 | if (arguments[0] !== undefined) |
| 17 | value = arguments[0]; |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 18 | }; |
| 19 | return Object.create(this)._init(value, regex); |
| 20 | }, |
| 21 | |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 22 | |
| 23 | // Initialize the string value |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 24 | _init : function (value, regex) { |
| 25 | this.element(); |
| 26 | this.value(value); |
| 27 | this.regex(regex); |
| 28 | return this; |
| 29 | }, |
| 30 | |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Get or set the regex boolean value. |
| 34 | * |
| 35 | * @param bool Either true or false |
| 36 | */ |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 37 | regex : function (bool) { |
| 38 | if (arguments.length === 1) { |
| 39 | if (bool) { |
Akron | 8778f5d | 2017-06-30 21:25:55 +0200 | [diff] [blame] | 40 | this._regex = true; |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 41 | } |
| 42 | else { |
Akron | 8778f5d | 2017-06-30 21:25:55 +0200 | [diff] [blame] | 43 | this._regex = false; |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 44 | }; |
| 45 | this._update(); |
| 46 | }; |
| 47 | return this._regex; |
| 48 | }, |
| 49 | |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Toggle the regex, make it either true, |
| 53 | * if it is false, or make it false, if it is true. |
| 54 | */ |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 55 | toggleRegex : function () { |
| 56 | if (this._regex === false) |
| 57 | this.regex(true); |
| 58 | else |
| 59 | this.regex(false); |
| 60 | }, |
| 61 | |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 62 | /** |
| 63 | * Get or set the string value. |
| 64 | */ |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 65 | value : function (val) { |
| 66 | if (arguments.length === 1) { |
| 67 | this._value = val; |
Nils Diewald | 7991a3f | 2015-05-19 14:12:37 +0000 | [diff] [blame] | 68 | this._input.value = val; |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 69 | this._update(); |
| 70 | }; |
| 71 | return this._value; |
| 72 | }, |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 73 | |
| 74 | |
| 75 | // Update dom element |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 76 | _update : function () { |
Nils Diewald | 7991a3f | 2015-05-19 14:12:37 +0000 | [diff] [blame] | 77 | this._value = this._input.value; |
| 78 | |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 79 | if (this._regex) { |
| 80 | this._element.classList.add('regex'); |
| 81 | } |
| 82 | else { |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 83 | this._element.classList.remove('regex'); |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 84 | }; |
| 85 | }, |
| 86 | |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 87 | |
| 88 | /** |
| 89 | * Store the string value. |
| 90 | * This method should be overwritten. |
Akron | 12971a0 | 2018-01-03 16:38:10 +0100 | [diff] [blame] | 91 | * The method receives the value and the regex. |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 92 | */ |
| 93 | store : function (v,r) {}, |
| 94 | |
| 95 | |
Akron | 12971a0 | 2018-01-03 16:38:10 +0100 | [diff] [blame] | 96 | /** |
| 97 | * Put focus on element |
| 98 | */ |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 99 | focus : function () { |
| 100 | this._element.children[0].focus(); |
| 101 | }, |
| 102 | |
| 103 | /** |
| 104 | * Get the associated dom element. |
| 105 | */ |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 106 | element : function () { |
| 107 | if (this._element !== undefined) |
| 108 | return this._element; |
| 109 | |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 110 | // Create element |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 111 | this._element = document.createElement('div'); |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 112 | 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 Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 119 | cl.add('regex'); |
| 120 | |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 121 | // Add input field |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 122 | this._input = e.addE('input'); |
Akron | 8db5e3a | 2018-05-28 19:25:26 +0200 | [diff] [blame^] | 123 | |
| 124 | if (this.value() !== undefined) { |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 125 | this._input.value = this.value(); |
Akron | 8db5e3a | 2018-05-28 19:25:26 +0200 | [diff] [blame^] | 126 | }; |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 127 | |
Nils Diewald | 9c12506 | 2015-05-05 23:54:17 +0000 | [diff] [blame] | 128 | // Add regex button |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 129 | var re = e.addE('div'); |
Nils Diewald | 9c12506 | 2015-05-05 23:54:17 +0000 | [diff] [blame] | 130 | re.addEventListener( |
| 131 | 'click', |
Akron | 12971a0 | 2018-01-03 16:38:10 +0100 | [diff] [blame] | 132 | function (ev) { |
Akron | 8778f5d | 2017-06-30 21:25:55 +0200 | [diff] [blame] | 133 | this.toggleRegex(); |
Akron | 12971a0 | 2018-01-03 16:38:10 +0100 | [diff] [blame] | 134 | // ev.halt(); |
Nils Diewald | 9c12506 | 2015-05-05 23:54:17 +0000 | [diff] [blame] | 135 | }.bind(this), |
| 136 | true |
| 137 | ); |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 138 | re.addT('RE'); |
Nils Diewald | 9c12506 | 2015-05-05 23:54:17 +0000 | [diff] [blame] | 139 | |
Akron | 12971a0 | 2018-01-03 16:38:10 +0100 | [diff] [blame] | 140 | // If the focus is not on the text field anymore, |
| 141 | // delegate focus to |
| 142 | this._input.addEventListener( |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 143 | 'blur', |
Akron | 8db5e3a | 2018-05-28 19:25:26 +0200 | [diff] [blame^] | 144 | function (ev) { |
Akron | 12971a0 | 2018-01-03 16:38:10 +0100 | [diff] [blame] | 145 | 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 Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 166 | }.bind(this) |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 167 | ); |
| 168 | |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 169 | this._input.addEventListener( |
| 170 | 'keypress', |
Akron | 12971a0 | 2018-01-03 16:38:10 +0100 | [diff] [blame] | 171 | function (ev) { |
| 172 | if (ev.keyCode == 13) { |
Akron | 8778f5d | 2017-06-30 21:25:55 +0200 | [diff] [blame] | 173 | this.value(this._input.value); |
| 174 | this.store(this.value(), this.regex()); |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 175 | return false; |
Akron | 8778f5d | 2017-06-30 21:25:55 +0200 | [diff] [blame] | 176 | }; |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 177 | }.bind(this) |
| 178 | ); |
| 179 | |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 180 | // 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 Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 189 | } |
| 190 | }); |