blob: 45d2169eb4af7cd9eca8d66181b4279564c7a1f6 [file] [log] [blame]
Nils Diewald19ccee92014-12-08 11:30:08 +00001/**
Nils Diewald5c5a7472015-04-02 22:13:38 +00002 * Hint menu for Kalamar.
Nils Diewald47f366b2015-04-15 20:06:35 +00003 * Based on menu object.
Nils Diewald19ccee92014-12-08 11:30:08 +00004 *
5 * @author Nils Diewald
6 */
Nils Diewald0e6992a2015-04-14 20:13:52 +00007define([
8 'hint/input',
9 'hint/menu',
10 'hint/contextanalyzer',
11 'util'
12], function (inputClass,
13 menuClass,
14 analyzerClass) {
Nils Diewald19ccee92014-12-08 11:30:08 +000015 "use strict";
16
Nils Diewald19ccee92014-12-08 11:30:08 +000017 /**
18 * @define {regex} Regular expression for context
19 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000020 KorAP.context = KorAP.context ||
Nils Diewald19ccee92014-12-08 11:30:08 +000021 "(?:^|[^-_a-zA-Z0-9])" + // Anchor
22 "((?:[-_a-zA-Z0-9]+?)\/" + // Foundry
23 "(?:" +
24 "(?:[-_a-zA-Z0-9]+?)=" + // Layer
Akron113cc1a2016-01-22 21:17:57 +010025 "(?:"+
26 "(?:[^:=\/ ]+?):|" + // Key
27 "(?:[^-=\/ ]+?)-" + // Node
28 ")?" +
Nils Diewald19ccee92014-12-08 11:30:08 +000029 ")?" +
30 ")$";
Nils Diewald19ccee92014-12-08 11:30:08 +000031 KorAP.hintArray = KorAP.hintArray || {};
32
Nils Diewald0e6992a2015-04-14 20:13:52 +000033 /**
34 * Return keycode based on event
35 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000036
37 // Initialize hint array
Nils Diewald5c5a7472015-04-02 22:13:38 +000038
39 /**
40 * KorAP.Hint.create({
41 * inputField : node,
42 * context : context regex
43 * });
44 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000045 return {
Nils Diewald5c5a7472015-04-02 22:13:38 +000046
47 // Some variables
48 // _firstTry : true,
49 active : false,
50
Nils Diewald0e6992a2015-04-14 20:13:52 +000051 /**
52 * Create new hint helper.
53 */
Nils Diewald5c5a7472015-04-02 22:13:38 +000054 create : function (param) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000055 return Object.create(this)._init(param);
Nils Diewald5c5a7472015-04-02 22:13:38 +000056 },
57
Nils Diewald0e6992a2015-04-14 20:13:52 +000058 // Initialize hint helper
Nils Diewald5c5a7472015-04-02 22:13:38 +000059 _init : function (param) {
60 param = param || {};
61
62 // Holds all menus per prefix context
63 this._menu = {};
64
65 // Get input field
Nils Diewald0e6992a2015-04-14 20:13:52 +000066 var qfield = param["inputField"] || document.getElementById("q-field");
67 if (!qfield)
68 return null;
69
70 this._inputField = inputClass.create(qfield);
Nils Diewald5c5a7472015-04-02 22:13:38 +000071
72 var inputFieldElement = this._inputField.element();
73
74 var that = this;
75
76 // Add event listener for key pressed down
77 inputFieldElement.addEventListener(
Nils Diewald47f366b2015-04-15 20:06:35 +000078 "keydown", function (e) {
Nils Diewald5c5a7472015-04-02 22:13:38 +000079 var code = _codeFromEvent(e);
80 if (code === 40) {
81 that.show(false);
82 e.halt();
83 };
84 }, false
85 );
86
Nils Diewald47f366b2015-04-15 20:06:35 +000087 this._inputField.container().addEventListener('click', function (e) {
88 if (!this.classList.contains('active')) {
89 that.show(false);
90 };
91 });
92
93 var _up = function (e) {
94 var input = that._inputField;
95 input.update();
96 };
97
98 // Move infobox
99 inputFieldElement.addEventListener("keyup", _up);
100 inputFieldElement.addEventListener("click", _up);
Nils Diewald5c5a7472015-04-02 22:13:38 +0000101
102 // Set Analyzer for context
Nils Diewald0e6992a2015-04-14 20:13:52 +0000103 this._analyzer = analyzerClass.create(
Nils Diewald5c5a7472015-04-02 22:13:38 +0000104 param["context"] || KorAP.context
105 );
Nils Diewald19ccee92014-12-08 11:30:08 +0000106 return this;
107 },
108
Nils Diewald5c5a7472015-04-02 22:13:38 +0000109 inputField : function () {
110 return this._inputField;
111 },
Nils Diewald19ccee92014-12-08 11:30:08 +0000112
Nils Diewald5c5a7472015-04-02 22:13:38 +0000113 /**
Nils Diewald5c5a7472015-04-02 22:13:38 +0000114 * Return hint menu and probably init based on an action
115 */
116 menu : function (action) {
117
118 if (this._menu[action] === undefined) {
119
120 // No matching hint menu
121 if (KorAP.hintArray[action] === undefined)
122 return;
123
124 // Create matching hint menu
Nils Diewald0e6992a2015-04-14 20:13:52 +0000125 this._menu[action] = menuClass.create(
Nils Diewald5c5a7472015-04-02 22:13:38 +0000126 this, action, KorAP.hintArray[action]
Nils Diewald19ccee92014-12-08 11:30:08 +0000127 );
Nils Diewald5c5a7472015-04-02 22:13:38 +0000128 };
129
130 // Return matching hint menu
131 return this._menu[action];
132 },
133
134 /**
135 * Get the correct menu based on the context
136 */
137 contextMenu : function (ifContext) {
138 var context = this._inputField.context();
139 if (context === undefined || context.length == 0)
140 return ifContext ? undefined : this.menu("-");
141
142 context = this._analyzer.test(context);
143 if (context === undefined || context.length == 0)
144 return ifContext ? undefined : this.menu("-");
145
146 return this.menu(context);
147 },
148
149
150 /**
151 * Show the menu
152 */
153 show : function (ifContext) {
154
155 // Menu is already active
156 if (this.active)
157 return;
158
Nils Diewald5c5a7472015-04-02 22:13:38 +0000159 // Get the menu
160 var menu;
161 if (menu = this.contextMenu(ifContext)) {
Nils Diewald2488d052015-04-09 21:46:02 +0000162 var c = this._inputField.container();
163 c.classList.add('active');
164 c.appendChild(menu.element());
Nils Diewald5c5a7472015-04-02 22:13:38 +0000165 menu.show('');
166 menu.focus();
Nils Diewald5c5a7472015-04-02 22:13:38 +0000167 // Focus on input field
168 // this.inputField.element.focus();
Nils Diewald19ccee92014-12-08 11:30:08 +0000169 };
170 }
171 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000172});