blob: 7145919cef2b608e7f72b001426eba3ddb69e5da [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 /**
61 * Update the mirror content.
62 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000063 update : function () {
Nils Diewald7148c6f2015-05-04 15:07:53 +000064 this._mirror.firstChild.textContent = this._split()[0];
65 this._container.style.left = this._rightPos() + 'px';
Akron308db382016-05-30 22:34:07 +020066 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +000067 },
68
Akron308db382016-05-30 22:34:07 +020069
Nils Diewald7148c6f2015-05-04 15:07:53 +000070 /**
71 * Insert text into the mirror.
72 * This is a prefix of the input field's
73 * value.
74 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000075 insert : function (text) {
Nils Diewald7148c6f2015-05-04 15:07:53 +000076 var splittedText = this._split();
Nils Diewald0e6992a2015-04-14 20:13:52 +000077 var s = this._element;
78 s.value = splittedText[0] + text + splittedText[1];
79 s.selectionStart = (splittedText[0] + text).length;
80 s.selectionEnd = s.selectionStart;
Akron308db382016-05-30 22:34:07 +020081
82 // Maybe update?
Nils Diewald0e6992a2015-04-14 20:13:52 +000083 this._mirror.firstChild.textContent = splittedText[0] + text;
Akron308db382016-05-30 22:34:07 +020084 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +000085 },
Akron308db382016-05-30 22:34:07 +020086
87 /**
88 * Move hinthelper to given character position
89 */
90 moveto : function (charpos) {
91 this._element.selectionStart = charpos;
92 this._element.selectionEnd = charpos;
93 this._element.focus();
94 return this.update();
95 },
Nils Diewald0e6992a2015-04-14 20:13:52 +000096
Nils Diewald7148c6f2015-05-04 15:07:53 +000097 /**
98 * Reposition the input mirror directly
99 * below the input box.
100 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000101 reposition : function () {
102 var inputClientRect = this._element.getBoundingClientRect();
103 var inputStyle = window.getComputedStyle(this._element, null);
104
105 var bodyClientRect =
106 document.getElementsByTagName('body')[0].getBoundingClientRect();
107
108 // Reset position
109 var mirrorStyle = this._mirror.style;
110 mirrorStyle.left = inputClientRect.left + "px";
111 mirrorStyle.top = (inputClientRect.bottom - bodyClientRect.top) + "px";
112 mirrorStyle.width = inputStyle.getPropertyValue("width");
113
114 // These may be relevant in case of media depending css
115 mirrorStyle.paddingLeft = inputStyle.getPropertyValue("padding-left");
116 mirrorStyle.marginLeft = inputStyle.getPropertyValue("margin-left");
117 mirrorStyle.borderLeftWidth = inputStyle.getPropertyValue("border-left-width");
118 mirrorStyle.borderLeftStyle = inputStyle.getPropertyValue("border-left-style");
119 mirrorStyle.fontSize = inputStyle.getPropertyValue("font-size");
120 mirrorStyle.fontFamily = inputStyle.getPropertyValue("font-family");
121 },
Nils Diewald7148c6f2015-05-04 15:07:53 +0000122
Akron308db382016-05-30 22:34:07 +0200123
Nils Diewald7148c6f2015-05-04 15:07:53 +0000124 /**
125 * Get the context, which is the input
126 * field's value bounded to the
127 * cursor position.
128 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000129 context : function () {
Nils Diewald7148c6f2015-05-04 15:07:53 +0000130 return this._split()[0];
131 },
132
Akron308db382016-05-30 22:34:07 +0200133
134 // Initialize new input field
135 _init : function (element) {
136 this._element = element;
137
138 // Create mirror for searchField
139 // This is important for positioning
Akron00cd4d12016-05-31 21:01:11 +0200140 // if ((this._mirror = document.getElementById("searchMirror")) === null) {
Akron308db382016-05-30 22:34:07 +0200141 this._mirror = document.createElement("div");
Akron00cd4d12016-05-31 21:01:11 +0200142 this._mirror.classList.add('hint', 'mirror');
Akron308db382016-05-30 22:34:07 +0200143 this._mirror.appendChild(document.createElement("span"));
144 this._container = this._mirror.appendChild(document.createElement("div"));
145 this._mirror.style.height = "0px";
146 document.getElementsByTagName("body")[0].appendChild(this._mirror);
Akron00cd4d12016-05-31 21:01:11 +0200147// };
Akron308db382016-05-30 22:34:07 +0200148
149 // Update position of the mirror
150 window.addEventListener('resize', this.reposition.bind(this));
151 this._element.addEventListener('onfocus', this.reposition.bind(this));
152 this.reposition();
153 return this;
154 },
155
156 // Get the right position
157 _rightPos : function () {
158 var box = this._mirror.firstChild.getBoundingClientRect();
159 return box.right - box.left;
160 },
161
162
Nils Diewald7148c6f2015-05-04 15:07:53 +0000163 /*
164 * Return two substrings,
Akron308db382016-05-30 22:34:07 +0200165 * splitted at a given position
166 * or at the current cursor position.
Nils Diewald7148c6f2015-05-04 15:07:53 +0000167 */
Akron308db382016-05-30 22:34:07 +0200168 _split : function (start) {
Nils Diewald7148c6f2015-05-04 15:07:53 +0000169 var s = this._element;
170 var value = s.value;
Akron308db382016-05-30 22:34:07 +0200171
172 // Get start from cursor position
173 if (arguments.length === 0)
174 start = s.selectionStart;
175
Nils Diewald7148c6f2015-05-04 15:07:53 +0000176 return new Array(
177 value.substring(0, start),
178 value.substring(start, value.length)
179 );
Nils Diewald0e6992a2015-04-14 20:13:52 +0000180 }
181});