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 | |
| 122 | this._input.addEventListener( |
| 123 | 'blur', |
| 124 | function () { |
| 125 | this.store(this.value(), this.regex()); |
| 126 | }.bind(this) |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 127 | ); |
| 128 | |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame^] | 129 | this._input.addEventListener( |
| 130 | 'keypress', |
| 131 | function (e) { |
| 132 | if (e.keyCode == 13) { |
| 133 | this.value(this._input.value); |
| 134 | this.store(this.value(), this.regex()); |
| 135 | return false; |
| 136 | }; |
| 137 | }.bind(this) |
| 138 | ); |
| 139 | |
| 140 | // Add regex button |
| 141 | var re = e.appendChild( |
| 142 | document.createElement('div') |
| 143 | ); |
| 144 | re.addEventListener('click', function (e) { |
| 145 | this.toggleRegex(); |
| 146 | e.halt(); |
| 147 | }.bind(this)); |
| 148 | re.appendChild(document.createTextNode('RE')); |
| 149 | |
| 150 | // Add store button |
| 151 | /* |
| 152 | e.appendChild( |
| 153 | document.createElement('div') |
| 154 | ).addEventListener('click', function () { |
| 155 | this.store(this.value(), this.regex()); |
| 156 | }.bind(this)); |
| 157 | */ |
| 158 | return e; |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 159 | } |
| 160 | }); |