blob: adb74b60910e3b98d0d028a6dd4c04b76b74f33a [file] [log] [blame]
Nils Diewald0e6992a2015-04-14 20:13:52 +00001// Input field for queries
Akron308db382016-05-30 22:34:07 +02002/*
3 * TODO: Support allert for query problems.
4 */
5
Nils Diewald0e6992a2015-04-14 20:13:52 +00006define({
Nils Diewald7148c6f2015-05-04 15:07:53 +00007
8 /**
9 * Create a new input field.
10 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000011 create : function (element) {
12 return Object.create(this)._init(element);
13 },
Nils Diewald0e6992a2015-04-14 20:13:52 +000014
Nils Diewald0e6992a2015-04-14 20:13:52 +000015
Nils Diewald7148c6f2015-05-04 15:07:53 +000016 /**
17 * Get the mirrored input field.
18 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000019 mirror : function () {
20 return this._mirror;
21 },
22
Nils Diewald7148c6f2015-05-04 15:07:53 +000023
24 /**
25 * Get the container element.
Akron308db382016-05-30 22:34:07 +020026 * This contains the hint helper / menus
27 * and probably an
28 * error message.
Nils Diewald7148c6f2015-05-04 15:07:53 +000029 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000030 container : function () {
31 return this._container;
32 },
33
Nils Diewald7148c6f2015-05-04 15:07:53 +000034
35 /**
36 * Get the input element the
37 * hint helper is attached to.
38 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000039 element : function () {
40 return this._element;
41 },
42
Akron308db382016-05-30 22:34:07 +020043
Nils Diewald7148c6f2015-05-04 15:07:53 +000044 /**
45 * Get the value of the input field
46 * the hint helper is attached to.
47 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000048 value : function () {
49 return this._element.value;
50 },
51
Akron00cd4d12016-05-31 21:01:11 +020052 /**
53 * Get the value of the input field mirror.
54 */
55 mirrorValue : function () {
56 return this._mirror.firstChild.textContent;
57 },
58
Nils Diewald7148c6f2015-05-04 15:07:53 +000059
60 /**
Akron95abaf42018-04-26 15:33:22 +020061 * Reset the input value
62 */
63 reset : function () {
64 this._element.value = "";
65 },
66
67
68 /**
Nils Diewald7148c6f2015-05-04 15:07:53 +000069 * Update the mirror content.
70 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000071 update : function () {
Nils Diewald7148c6f2015-05-04 15:07:53 +000072 this._mirror.firstChild.textContent = this._split()[0];
73 this._container.style.left = this._rightPos() + 'px';
Akron308db382016-05-30 22:34:07 +020074 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +000075 },
76
Akron308db382016-05-30 22:34:07 +020077
Nils Diewald7148c6f2015-05-04 15:07:53 +000078 /**
79 * Insert text into the mirror.
80 * This is a prefix of the input field's
81 * value.
82 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000083 insert : function (text) {
Nils Diewald7148c6f2015-05-04 15:07:53 +000084 var splittedText = this._split();
Nils Diewald0e6992a2015-04-14 20:13:52 +000085 var s = this._element;
86 s.value = splittedText[0] + text + splittedText[1];
87 s.selectionStart = (splittedText[0] + text).length;
88 s.selectionEnd = s.selectionStart;
Akron308db382016-05-30 22:34:07 +020089
90 // Maybe update?
Nils Diewald0e6992a2015-04-14 20:13:52 +000091 this._mirror.firstChild.textContent = splittedText[0] + text;
Akron308db382016-05-30 22:34:07 +020092 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +000093 },
Akron308db382016-05-30 22:34:07 +020094
95 /**
96 * Move hinthelper to given character position
97 */
98 moveto : function (charpos) {
99 this._element.selectionStart = charpos;
100 this._element.selectionEnd = charpos;
101 this._element.focus();
102 return this.update();
103 },
Nils Diewald0e6992a2015-04-14 20:13:52 +0000104
Nils Diewald7148c6f2015-05-04 15:07:53 +0000105 /**
106 * Reposition the input mirror directly
107 * below the input box.
108 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000109 reposition : function () {
110 var inputClientRect = this._element.getBoundingClientRect();
111 var inputStyle = window.getComputedStyle(this._element, null);
112
113 var bodyClientRect =
114 document.getElementsByTagName('body')[0].getBoundingClientRect();
115
116 // Reset position
117 var mirrorStyle = this._mirror.style;
118 mirrorStyle.left = inputClientRect.left + "px";
119 mirrorStyle.top = (inputClientRect.bottom - bodyClientRect.top) + "px";
120 mirrorStyle.width = inputStyle.getPropertyValue("width");
121
122 // These may be relevant in case of media depending css
123 mirrorStyle.paddingLeft = inputStyle.getPropertyValue("padding-left");
124 mirrorStyle.marginLeft = inputStyle.getPropertyValue("margin-left");
125 mirrorStyle.borderLeftWidth = inputStyle.getPropertyValue("border-left-width");
126 mirrorStyle.borderLeftStyle = inputStyle.getPropertyValue("border-left-style");
127 mirrorStyle.fontSize = inputStyle.getPropertyValue("font-size");
128 mirrorStyle.fontFamily = inputStyle.getPropertyValue("font-family");
129 },
Nils Diewald7148c6f2015-05-04 15:07:53 +0000130
Akron308db382016-05-30 22:34:07 +0200131
Nils Diewald7148c6f2015-05-04 15:07:53 +0000132 /**
133 * Get the context, which is the input
134 * field's value bounded to the
135 * cursor position.
136 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000137 context : function () {
Nils Diewald7148c6f2015-05-04 15:07:53 +0000138 return this._split()[0];
139 },
140
Akron308db382016-05-30 22:34:07 +0200141
142 // Initialize new input field
143 _init : function (element) {
144 this._element = element;
145
146 // Create mirror for searchField
147 // This is important for positioning
Akron00cd4d12016-05-31 21:01:11 +0200148 // if ((this._mirror = document.getElementById("searchMirror")) === null) {
Akron308db382016-05-30 22:34:07 +0200149 this._mirror = document.createElement("div");
Akron00cd4d12016-05-31 21:01:11 +0200150 this._mirror.classList.add('hint', 'mirror');
Akron308db382016-05-30 22:34:07 +0200151 this._mirror.appendChild(document.createElement("span"));
152 this._container = this._mirror.appendChild(document.createElement("div"));
153 this._mirror.style.height = "0px";
154 document.getElementsByTagName("body")[0].appendChild(this._mirror);
Akron00cd4d12016-05-31 21:01:11 +0200155// };
Akron308db382016-05-30 22:34:07 +0200156
157 // Update position of the mirror
158 window.addEventListener('resize', this.reposition.bind(this));
159 this._element.addEventListener('onfocus', this.reposition.bind(this));
160 this.reposition();
161 return this;
162 },
163
164 // Get the right position
165 _rightPos : function () {
166 var box = this._mirror.firstChild.getBoundingClientRect();
167 return box.right - box.left;
168 },
169
170
Nils Diewald7148c6f2015-05-04 15:07:53 +0000171 /*
172 * Return two substrings,
Akron308db382016-05-30 22:34:07 +0200173 * splitted at a given position
174 * or at the current cursor position.
Nils Diewald7148c6f2015-05-04 15:07:53 +0000175 */
Akron308db382016-05-30 22:34:07 +0200176 _split : function (start) {
Nils Diewald7148c6f2015-05-04 15:07:53 +0000177 var s = this._element;
178 var value = s.value;
Akron308db382016-05-30 22:34:07 +0200179
180 // Get start from cursor position
181 if (arguments.length === 0)
182 start = s.selectionStart;
183
Nils Diewald7148c6f2015-05-04 15:07:53 +0000184 return new Array(
185 value.substring(0, start),
186 value.substring(start, value.length)
187 );
Nils Diewald0e6992a2015-04-14 20:13:52 +0000188 }
189});