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