diff options
Diffstat (limited to 'scripts/modules/completion')
-rw-r--r-- | scripts/modules/completion | 134 |
1 files changed, 60 insertions, 74 deletions
diff --git a/scripts/modules/completion b/scripts/modules/completion index 3b955d1f..2ffb9418 100644 --- a/scripts/modules/completion +++ b/scripts/modules/completion @@ -1,14 +1,13 @@ -var util = namespace("util"); +var util = namespace("util"); +var gui = namespace("gui"); -var mWidget = null; -var shortCut = "tg"; -var label = "tabsearch:"; -var fontFamily = "monospace"; -var fontSize = 11; -var visibleItems = 11; -var lineSpacing = 2; -var margin = 3; -var _keyPress, _keyRelease; +var mWidget = new HiddenWebView(); + +mWidget.canFocus = false; +mWidget.transparent = true; + +gui.mainBox.packStart(mWidget, false, false, 0); +gui.mainBox.reorderChild(mWidget, 2); function htmlContent() { /*HEREDOC @@ -42,9 +41,8 @@ body { float:right; text-align:right; padding-left:5px; - display:inline-block; } -body > div { +#content { width : 100%; height : 100%; margin:0; @@ -56,14 +54,11 @@ body > div { } </style> <script type="text/javascript"> - var mSelectedIdx = -1, mElements, mContent; + var mSelectedIdx = -1, mElements; function clear() { - if (mContent) { - document.body.removeChild(mContent); - } + document.getElementById("content").innerHTML = ""; mElements = null; mSelectedIdx = -1; - mContent = null; } function getSelected() { return mElements[mSelectedIdx].dataset.id; @@ -99,10 +94,10 @@ body > div { } function update(data) { clear(); - mContent = document.createElement("div"); + var content = document.createDocumentFragment(); mElements = data.map(function(tab, i) { var className = "line "; - var element = createElement("div", mContent, { + var element = createElement("div", content, { className : "line", }); createElement("div", element, { @@ -117,7 +112,7 @@ body > div { return element; }); select(); - document.body.appendChild(mContent); + document.getElementById("content").appendChild(content); } function applyStyle(styles) { var selector, style, i, sheet, item; @@ -130,15 +125,17 @@ body > div { } } } - } </script> </head> <body> +<div id="content"></div> </body> HEREDOC*/ } +mWidget.loadString(util.hereDoc(htmlContent)); + function Completion(args) { if (!args.shortcut) { throw new Error("Completion no shortcut defined!"); @@ -155,21 +152,13 @@ function Completion(args) { } Object.defineProperties(Completion.prototype, { + // private _sigKeyPress : { value : null, writable : true }, _sigKeyRelease : { value : null, writable : true }, - _update : { value : null, writable : true }, _idLabelNotify : { value : -1, writable : true }, _lastText : { value : "", writable : true }, _data : { value : {}, writable : true }, - - onUpdate : { value : null, writable : true }, - onSelected : { value : null, writable : true }, - shortcut : { value : "", writable : true }, - label : { value : ":", writable : true }, - visibleItems : { value : 11, writable : true }, - fontSize : { value : 11, writable : true }, - fontFamily : { value : "monospace", writable : true }, - lineSpacing : { value : 2, writable : true }, + _height : { value : 0, writable : true }, _cleanup : { value : function() { @@ -180,6 +169,7 @@ Object.defineProperties(Completion.prototype, { mWidget.inject("clear()"); mWidget.visible = false; + this.onHide(); this._lastText = ""; this._data = null; @@ -228,30 +218,7 @@ Object.defineProperties(Completion.prototype, { if (e.isModifier) { return; } - this._update(); - } - }, - _updateHeight : { - value : function() { - if (!this.height) { - mWidget.heightRequest = Math.min(this._data.length, this.visibleItems) * (this.fontSize + this.lineSpacing); - } - } - }, - _start : { - value : function() { - this._data = this.onShow(); - this._updateHeight(); - if (this._data) { - mWidget.inject("update(" + JSON.stringify(this._data) + ")"); - } - } - }, - _update : { - value : function() { - var text, words, elements, data; - - text = gui.entry.text.trim(); + var text = gui.entry.text.trim(); if (text == this._lastText) { return; } @@ -265,6 +232,24 @@ Object.defineProperties(Completion.prototype, { } } }, + _updateHeight : { + value : function() { + if (!this.height) { + var oldHeight = this._height; + this._height = Math.min(this._data.length, this.visibleItems) * (this.fontSize + this.lineSpacing); + if (this._height == 0) { + mWidget.visible = false; + return; + } + if (oldHeight == 0) { + mWidget.visible = true; + } + if (oldHeight != this._height) { + mWidget.heightRequest = this._height; + } + } + } + }, _onUpdateLabel : { value : function() { if (gui.messageLabel.label != this.label) { @@ -277,8 +262,6 @@ Object.defineProperties(Completion.prototype, { value : function() { mWidget.inject("applyStyle(" + this._style + ")"); - //mWidget.heightRequest = this.height; - //update(); gui.messageLabel.label = this.label; gui.entry.visible = true; gui.entry.hasFocus = true; @@ -287,8 +270,12 @@ Object.defineProperties(Completion.prototype, { this._sigKeyPress.connect(); this._sigKeyRelease.connect(); + this._data = this.onShow(); + this._updateHeight(); + if (this._data) { + mWidget.inject("update(" + JSON.stringify(this._data) + ")"); + } mWidget.visible = true; - this._start(); } }, _startup : { @@ -310,7 +297,7 @@ Object.defineProperties(Completion.prototype, { ".label.left" : { "margin-left" : this.margin || "3", }, - "body > div" : { + "#content" : { "overflow-y" : this.overflow || "auto" } @@ -321,21 +308,20 @@ Object.defineProperties(Completion.prototype, { bind(this.shortcut, this._bindCallback.bind(this)); this.onShow = this.onShow || this.onUpdate; } - } + }, + // public + widget : { value : mWidget }, + // public overridable properties + onUpdate : { value : null, writable : true }, + onSelected : { value : null, writable : true }, + shortcut : { value : null, writable : true }, + label : { value : ":", writable : true }, + visibleItems : { value : 11, writable : true }, + fontSize : { value : 11, writable : true }, + fontFamily : { value : "monospace", writable : true }, + lineSpacing : { value : 2, writable : true }, + onHide : { value : function(){}, writable : true }, }); -function create(ctorArgs) { - return new Completion(ctorArgs); -} -mWidget = new HiddenWebView(); - -mWidget.canFocus = false; -mWidget.transparent = true; - -gui.mainBox.packStart(mWidget, false, false, 0); -gui.mainBox.reorderChild(mWidget, 2); - -mWidget.loadString(util.hereDoc(htmlContent)); - -return { create : create, ctor : Completion }; +return Completion; /* vim: set ft=javascript: */ |