aboutsummaryrefslogtreecommitdiffstats
path: root/bookmarktoolbar-hint.js
blob: fcb7b152749e74b1b5c772fce5a916d747b7de37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// ==VimperatorPlugin==
// @name           BookmarksToolbar-Hint
// @description    Feature the BookmarksToolbar-Hint
// @description-ja ブックマークツールバーのヒント機能を提供
// @version        0.2a
// ==/VimperatorPlugin==
//
// Usage:
// 
// <Leader>f   -> open current tab
// <Leader>F   -> open new tab
//
// Note: <Leader>はデフォルトでは`\'になっています

liberator.plugins.bookmarkToolbarHints = (function(){
	function $(id){
		return document.getElementById(id);
	}
	function createTooltip(){
		var tooltip = document.createElement('tooltip');
		tooltip.setAttribute('style','padding:0');
		var label = document.createElement('label');
		label.setAttribute('value',tooltipbox.childNodes.length+1);
		tooltip.appendChild(label);
		tooltipbox.appendChild(tooltip);
		return tooltip;
	}
	function clearTooltips(){
		while(tooltipbox.hasChildNodes()){
			tooltipbox.firstChild.hidePopup();
			tooltipbox.removeChild(tooltipbox.firstChild);
		}
	}
	function getToolbar(){
		if (toolbar)
			return toolbar;
		else
			toolbar = $('bookmarksBarContent');

		return toolbar;
	}
	function onKeyPress(event){
		manager.onEvent(event);
	}
	function allHidePopup(node){
		if (node.hidePopup) node.hidePopup();
		if (node.parentNode) allHidePopup(node.parentNode);
	}
	function toolbarOpen(target){
		if (target.getAttribute('container') == 'true'){
			target.firstChild.showPopup();
			liberator.plugins.bookmarkToolbarHints.hide();
			liberator.plugins.bookmarkToolbarHints.show(target.firstChild);
			return true;
		} else {
			liberator.open(target.node.uri, where);
		}
		return false;
	}
	var hints = [];
	var toolbar;
	var current;
	var currentNum = 0;
	var focusNum = 0;
	var where = liberator.CURERNT_TAB;
	var manager = {
		get toolbar(){
			return getToolbar();
		},
		go : null,
		get where(){ return where; },
		set where(value){ where = value; },
		show:function(node){
			liberator.modes.set(liberator.modes.CUSTOM, liberator.modes.QUICK_HINT);
			hints = [];
			window.addEventListener('keypress',onKeyPress,true);
			current = node || getToolbar();
			for (var i=0; i<current.childNodes.length; i++){
				var button = current.childNodes[i];
				if (button.localName == "menuseparator") continue;
				hints.push(button);
				var tooltip = createTooltip();
				//tooltip.showPopup(button, button.boxObject.screenX, button.boxObject.screenY);
				tooltip.showPopup(button, -1, -1,"tooltip","topright");
			}
		},
		onEvent: function(event){
			var key = liberator.events.toString(event);
			var num;
			switch(key){
				case "<Esc>":
				case "<C-[>":
					allHidePopup(current);
					liberator.options.guioptions= liberator.plugins.bookmarkToolbarHints.go;
					break;
				case "<Return>":
					//if (toolbarOpen(current.childNodes[focusNum])) return;
					if (toolbarOpen(hints[focusNum])) return;
					break;
				case "<Tab>":
				case "<S-Tab>":
					break;
				default:
					if (/^[0-9]$/.test(key)){
						num = parseInt(key,10);
						if (currentNum) num += currentNum * 10;

						//if (current.childNodes.length >= num*10){
						if (hints.length >= num*10){
							currentNum = num;
							forcusNum = num;
							return;
						}
						//if (current.childNodes[num-1]){
						if (hints[num-1]){
							//if (toolbarOpen(current.childNodes[num-1])) return;
							if (toolbarOpen(hints[num-1])) return;
						}
					}
			}
			liberator.plugins.bookmarkToolbarHints.hide();

		},
		hide: function(){
			currentNum = 0;
			focusNum = 0;
			window.removeEventListener('keypress',onKeyPress,true);
			liberator.modes.reset(true);
			var tooltipbox = document.getElementById('liberator-tooltip-container');
			while(tooltipbox.hasChildNodes()){
				tooltipbox.firstChild.hidePopup();
				tooltipbox.removeChild(tooltipbox.firstChild);
			}
			//clearTooltips();
		}
	};
	var tooltipbox = document.createElement('box');
	tooltipbox.setAttribute('id','liberator-tooltip-container');
	$('liberator-container').appendChild(tooltipbox);
	return manager;
})();

liberator.modes.setCustomMode('BookmarksToolbar-Hint', plugins.bookmarkToolbarHints.onEvent, plugins.bookmarkToolbarHints.hide);
liberator.mappings.addUserMap([liberator.modes.NORMAL], ['<Leader>f'],
	'Start Toolbar-HINTS (open current tab)',
	function(){
		plugins.bookmarkToolbarHints.go = options.guioptions;
		options.guioptions += "b";
		plugins.bookmarkToolbarHints.where = liberator.CURRENT_TAB;
		plugins.bookmarkToolbarHints.show();
	}
);
liberator.mappings.addUserMap([liberator.modes.NORMAL], ['<Leader>F'],
	'Start Toolbar-HINTS (open new tab)',
	function(){
		plugins.bookmarkToolbarHints.go = options.guioptions;
		options.guioptions += "b";
		plugins.bookmarkToolbarHints.where = liberator.NEW_TAB;
		plugins.bookmarkToolbarHints.show();
	}
);
//$("liberator-container").* += <tooltip id="liberator-tooltip"><label id="liberator-tooltip-text" value=""/></tooltip>;