Akron | 5d4f2e4 | 2024-12-16 09:10:27 +0100 | [diff] [blame^] | 1 | /** |
| 2 | * Add integer values to the virtual corpus |
| 3 | */ |
| 4 | "use strict"; |
| 5 | |
| 6 | define(['util'], function () { |
| 7 | |
| 8 | return { |
| 9 | /** |
| 10 | * Create new integer value helper. |
| 11 | */ |
| 12 | create : function () { |
| 13 | const a = arguments; |
| 14 | let value = 0; |
| 15 | |
| 16 | // Set value |
| 17 | if (a.length >= 1) { |
| 18 | if (a[0] !== undefined) |
| 19 | value = a[0]; |
| 20 | }; |
| 21 | |
| 22 | return Object.create(this)._init(value); |
| 23 | }, |
| 24 | |
| 25 | |
| 26 | // Initialize the integer value |
| 27 | _init : function (value) { |
| 28 | this.value(value); |
| 29 | return this; |
| 30 | }, |
| 31 | |
| 32 | /** |
| 33 | * Get or set the integer value. |
| 34 | */ |
| 35 | value : function (val) { |
| 36 | if (arguments.length === 1) { |
| 37 | |
| 38 | if (typeof val != "number") |
| 39 | val = parseInt(val); |
| 40 | |
| 41 | if (isNaN(val)) |
| 42 | val = 0; |
| 43 | |
| 44 | this._value = val; |
| 45 | this._update(); |
| 46 | }; |
| 47 | return this._value; |
| 48 | }, |
| 49 | |
| 50 | |
| 51 | // Update dom element |
| 52 | _update : function () { |
| 53 | if (this._el === undefined) |
| 54 | return; |
| 55 | |
| 56 | this._value = this._input.value; |
| 57 | }, |
| 58 | |
| 59 | |
| 60 | /** |
| 61 | * Store the integer value. |
| 62 | * This method should be overwritten. |
| 63 | * The method receives the value. |
| 64 | */ |
| 65 | store : function (v) {}, |
| 66 | |
| 67 | |
| 68 | /** |
| 69 | * Put focus on element |
| 70 | */ |
| 71 | focus : function () { |
| 72 | this._el.children[0].focus(); |
| 73 | }, |
| 74 | |
| 75 | |
| 76 | /** |
| 77 | * Get the associated dom element. |
| 78 | */ |
| 79 | element : function () { |
| 80 | if (this._el !== undefined) |
| 81 | return this._el; |
| 82 | |
| 83 | // Create element |
| 84 | const e = this._el = document.createElement('div'); |
| 85 | e.setAttribute('tabindex', 0); |
| 86 | e.style.outline = 0; |
| 87 | |
| 88 | const cl = e.classList; |
| 89 | cl.add('value'); |
| 90 | |
| 91 | // Add input field |
| 92 | this._input = e.addE('input'); |
| 93 | this._input.setAttribute("type", "number"); |
| 94 | |
| 95 | if (this.value() !== undefined) { |
| 96 | this._input.value = this.value(); |
| 97 | }; |
| 98 | |
| 99 | // If the focus is not on the text field anymore, |
| 100 | // delegate focus to |
| 101 | this._input.addEventListener( |
| 102 | 'blur', |
| 103 | function (ev) { |
| 104 | const t = this; |
| 105 | if (!t._inField) { |
| 106 | t.value(t._input.value); |
| 107 | t.store(t.value()); |
| 108 | }; |
| 109 | ev.halt(); |
| 110 | }.bind(this) |
| 111 | ); |
| 112 | |
| 113 | // Workaround to check the click is in the field |
| 114 | e.addEventListener( |
| 115 | 'mousedown', |
| 116 | function () { |
| 117 | this._inField = true; |
| 118 | }.bind(this) |
| 119 | ); |
| 120 | |
| 121 | e.addEventListener( |
| 122 | 'mouseup', |
| 123 | function () { |
| 124 | this._inField = false; |
| 125 | this._input.focus(); |
| 126 | }.bind(this) |
| 127 | ); |
| 128 | |
| 129 | this._input.addEventListener( |
| 130 | 'keypress', |
| 131 | function (ev) { |
| 132 | const t = this; |
| 133 | if (ev.keyCode == 13) { |
| 134 | t.value(t._input.value); |
| 135 | t.store(t.value()); |
| 136 | return false; |
| 137 | }; |
| 138 | }.bind(this) |
| 139 | ); |
| 140 | |
| 141 | return e; |
| 142 | } |
| 143 | }; |
| 144 | }); |