Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame^] | 1 | // Input field for queries |
| 2 | define({ |
| 3 | create : function (element) { |
| 4 | return Object.create(this)._init(element); |
| 5 | }, |
| 6 | |
| 7 | _init : function (element) { |
| 8 | this._element = element; |
| 9 | |
| 10 | // Create mirror for searchField |
| 11 | if ((this._mirror = document.getElementById("searchMirror")) === null) { |
| 12 | this._mirror = document.createElement("div"); |
| 13 | this._mirror.setAttribute("id", "searchMirror"); |
| 14 | this._mirror.appendChild(document.createElement("span")); |
| 15 | this._container = this._mirror.appendChild(document.createElement("div")); |
| 16 | this._mirror.style.height = "0px"; |
| 17 | document.getElementsByTagName("body")[0].appendChild(this._mirror); |
| 18 | }; |
| 19 | |
| 20 | // Update position of the mirror |
| 21 | var that = this; |
| 22 | var repos = function () { |
| 23 | that.reposition(); |
| 24 | }; |
| 25 | window.addEventListener('resize', repos); |
| 26 | this._element.addEventListener('onfocus', repos); |
| 27 | that.reposition(); |
| 28 | |
| 29 | return this; |
| 30 | }, |
| 31 | |
| 32 | rightPos : function () { |
| 33 | var box = this._mirror.firstChild.getBoundingClientRect(); |
| 34 | return box.right - box.left; |
| 35 | }, |
| 36 | |
| 37 | mirror : function () { |
| 38 | return this._mirror; |
| 39 | }, |
| 40 | |
| 41 | container : function () { |
| 42 | return this._container; |
| 43 | }, |
| 44 | |
| 45 | element : function () { |
| 46 | return this._element; |
| 47 | }, |
| 48 | |
| 49 | value : function () { |
| 50 | return this._element.value; |
| 51 | }, |
| 52 | |
| 53 | update : function () { |
| 54 | this._mirror.firstChild.textContent = this.split()[0]; |
| 55 | this._container.style.left = this.rightPos() + 'px'; |
| 56 | }, |
| 57 | |
| 58 | insert : function (text) { |
| 59 | var splittedText = this.split(); |
| 60 | var s = this._element; |
| 61 | s.value = splittedText[0] + text + splittedText[1]; |
| 62 | s.selectionStart = (splittedText[0] + text).length; |
| 63 | s.selectionEnd = s.selectionStart; |
| 64 | this._mirror.firstChild.textContent = splittedText[0] + text; |
| 65 | }, |
| 66 | |
| 67 | // Return two substrings, splitted at current cursor position |
| 68 | split : function () { |
| 69 | var s = this._element; |
| 70 | var value = s.value; |
| 71 | var start = s.selectionStart; |
| 72 | return new Array( |
| 73 | value.substring(0, start), |
| 74 | value.substring(start, value.length) |
| 75 | ); |
| 76 | }, |
| 77 | |
| 78 | // Position the input mirror directly below the input box |
| 79 | reposition : function () { |
| 80 | var inputClientRect = this._element.getBoundingClientRect(); |
| 81 | var inputStyle = window.getComputedStyle(this._element, null); |
| 82 | |
| 83 | var bodyClientRect = |
| 84 | document.getElementsByTagName('body')[0].getBoundingClientRect(); |
| 85 | |
| 86 | // Reset position |
| 87 | var mirrorStyle = this._mirror.style; |
| 88 | mirrorStyle.left = inputClientRect.left + "px"; |
| 89 | mirrorStyle.top = (inputClientRect.bottom - bodyClientRect.top) + "px"; |
| 90 | mirrorStyle.width = inputStyle.getPropertyValue("width"); |
| 91 | |
| 92 | // These may be relevant in case of media depending css |
| 93 | mirrorStyle.paddingLeft = inputStyle.getPropertyValue("padding-left"); |
| 94 | mirrorStyle.marginLeft = inputStyle.getPropertyValue("margin-left"); |
| 95 | mirrorStyle.borderLeftWidth = inputStyle.getPropertyValue("border-left-width"); |
| 96 | mirrorStyle.borderLeftStyle = inputStyle.getPropertyValue("border-left-style"); |
| 97 | mirrorStyle.fontSize = inputStyle.getPropertyValue("font-size"); |
| 98 | mirrorStyle.fontFamily = inputStyle.getPropertyValue("font-family"); |
| 99 | }, |
| 100 | context : function () { |
| 101 | return this.split()[0]; |
| 102 | } |
| 103 | }); |