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) { |
| 39 | this._regex = true; |
| 40 | } |
| 41 | else { |
| 42 | this._regex = false; |
| 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; |
| 67 | this._update(); |
| 68 | }; |
| 69 | return this._value; |
| 70 | }, |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 71 | |
| 72 | |
| 73 | // Update dom element |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 74 | _update : function () { |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 75 | this._input.value = this._value; |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 76 | if (this._regex) { |
| 77 | this._element.classList.add('regex'); |
| 78 | } |
| 79 | else { |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 80 | this._element.classList.remove('regex'); |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 81 | }; |
| 82 | }, |
| 83 | |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 84 | |
| 85 | /** |
| 86 | * Store the string value. |
| 87 | * This method should be overwritten. |
| 88 | * The method receives the the value and the regex. |
| 89 | */ |
| 90 | store : function (v,r) {}, |
| 91 | |
| 92 | |
| 93 | focus : function () { |
| 94 | this._element.children[0].focus(); |
| 95 | }, |
| 96 | |
| 97 | /** |
| 98 | * Get the associated dom element. |
| 99 | */ |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 100 | element : function () { |
| 101 | if (this._element !== undefined) |
| 102 | return this._element; |
| 103 | |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 104 | // Create element |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 105 | this._element = document.createElement('div'); |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 106 | var e = this._element; |
| 107 | e.setAttribute('tabindex', 0); |
| 108 | e.style.outline = 0; |
| 109 | |
| 110 | var cl = e.classList; |
| 111 | cl.add('value'); |
| 112 | if (this.regex() === true) |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 113 | cl.add('regex'); |
| 114 | |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 115 | // Add input field |
| 116 | this._input = e.appendChild( |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 117 | document.createElement('input') |
| 118 | ); |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 119 | if (this.value() !== undefined) |
| 120 | this._input.value = this.value(); |
| 121 | |
Nils Diewald | 9c12506 | 2015-05-05 23:54:17 +0000 | [diff] [blame] | 122 | // Add regex button |
| 123 | var re = e.appendChild( |
| 124 | document.createElement('div') |
| 125 | ); |
| 126 | re.addEventListener( |
| 127 | 'click', |
| 128 | function (e) { |
| 129 | this.toggleRegex(); |
| 130 | e.halt(); |
| 131 | }.bind(this), |
| 132 | true |
| 133 | ); |
| 134 | re.appendChild(document.createTextNode('RE')); |
| 135 | |
| 136 | e.addEventListener( |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 137 | 'blur', |
Nils Diewald | 9c12506 | 2015-05-05 23:54:17 +0000 | [diff] [blame] | 138 | function (e) { |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 139 | this.store(this.value(), this.regex()); |
Nils Diewald | 9c12506 | 2015-05-05 23:54:17 +0000 | [diff] [blame] | 140 | e.halt(); |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 141 | }.bind(this) |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 142 | ); |
| 143 | |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 144 | this._input.addEventListener( |
| 145 | 'keypress', |
| 146 | function (e) { |
| 147 | if (e.keyCode == 13) { |
| 148 | this.value(this._input.value); |
| 149 | this.store(this.value(), this.regex()); |
| 150 | return false; |
| 151 | }; |
| 152 | }.bind(this) |
| 153 | ); |
| 154 | |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 155 | // Add store button |
| 156 | /* |
| 157 | e.appendChild( |
| 158 | document.createElement('div') |
| 159 | ).addEventListener('click', function () { |
| 160 | this.store(this.value(), this.regex()); |
| 161 | }.bind(this)); |
| 162 | */ |
| 163 | return e; |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 164 | } |
| 165 | }); |