blob: f2f314935316d5f3148d9eff1f11788db2b7703a [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
25 "(?:(?:[^:=\/ ]+?):)?" + // Key
26 ")?" +
27 ")$";
Nils Diewald19ccee92014-12-08 11:30:08 +000028 KorAP.hintArray = KorAP.hintArray || {};
29
Nils Diewald0e6992a2015-04-14 20:13:52 +000030 /**
31 * Return keycode based on event
32 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000033
34 // Initialize hint array
Nils Diewald5c5a7472015-04-02 22:13:38 +000035
36 /**
37 * KorAP.Hint.create({
38 * inputField : node,
39 * context : context regex
40 * });
41 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000042 return {
Nils Diewald5c5a7472015-04-02 22:13:38 +000043
44 // Some variables
45 // _firstTry : true,
46 active : false,
47
Nils Diewald0e6992a2015-04-14 20:13:52 +000048 /**
49 * Create new hint helper.
50 */
Nils Diewald5c5a7472015-04-02 22:13:38 +000051 create : function (param) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000052 return Object.create(this)._init(param);
Nils Diewald5c5a7472015-04-02 22:13:38 +000053 },
54
Nils Diewald0e6992a2015-04-14 20:13:52 +000055 // Initialize hint helper
Nils Diewald5c5a7472015-04-02 22:13:38 +000056 _init : function (param) {
57 param = param || {};
58
59 // Holds all menus per prefix context
60 this._menu = {};
61
62 // Get input field
Nils Diewald0e6992a2015-04-14 20:13:52 +000063 var qfield = param["inputField"] || document.getElementById("q-field");
64 if (!qfield)
65 return null;
66
67 this._inputField = inputClass.create(qfield);
Nils Diewald5c5a7472015-04-02 22:13:38 +000068
69 var inputFieldElement = this._inputField.element();
70
71 var that = this;
72
73 // Add event listener for key pressed down
74 inputFieldElement.addEventListener(
Nils Diewald47f366b2015-04-15 20:06:35 +000075 "keydown", function (e) {
Nils Diewald5c5a7472015-04-02 22:13:38 +000076 var code = _codeFromEvent(e);
77 if (code === 40) {
78 that.show(false);
79 e.halt();
80 };
81 }, false
82 );
83
Nils Diewald47f366b2015-04-15 20:06:35 +000084 this._inputField.container().addEventListener('click', function (e) {
85 if (!this.classList.contains('active')) {
86 that.show(false);
87 };
88 });
89
90 var _up = function (e) {
91 var input = that._inputField;
92 input.update();
93 };
94
95 // Move infobox
96 inputFieldElement.addEventListener("keyup", _up);
97 inputFieldElement.addEventListener("click", _up);
Nils Diewald5c5a7472015-04-02 22:13:38 +000098
99 // Set Analyzer for context
Nils Diewald0e6992a2015-04-14 20:13:52 +0000100 this._analyzer = analyzerClass.create(
Nils Diewald5c5a7472015-04-02 22:13:38 +0000101 param["context"] || KorAP.context
102 );
Nils Diewald19ccee92014-12-08 11:30:08 +0000103 return this;
104 },
105
Nils Diewald5c5a7472015-04-02 22:13:38 +0000106 inputField : function () {
107 return this._inputField;
108 },
Nils Diewald19ccee92014-12-08 11:30:08 +0000109
Nils Diewald5c5a7472015-04-02 22:13:38 +0000110 /**
Nils Diewald5c5a7472015-04-02 22:13:38 +0000111 * Return hint menu and probably init based on an action
112 */
113 menu : function (action) {
114
115 if (this._menu[action] === undefined) {
116
117 // No matching hint menu
118 if (KorAP.hintArray[action] === undefined)
119 return;
120
121 // Create matching hint menu
Nils Diewald0e6992a2015-04-14 20:13:52 +0000122 this._menu[action] = menuClass.create(
Nils Diewald5c5a7472015-04-02 22:13:38 +0000123 this, action, KorAP.hintArray[action]
Nils Diewald19ccee92014-12-08 11:30:08 +0000124 );
Nils Diewald5c5a7472015-04-02 22:13:38 +0000125 };
126
127 // Return matching hint menu
128 return this._menu[action];
129 },
130
131 /**
132 * Get the correct menu based on the context
133 */
134 contextMenu : function (ifContext) {
135 var context = this._inputField.context();
136 if (context === undefined || context.length == 0)
137 return ifContext ? undefined : this.menu("-");
138
139 context = this._analyzer.test(context);
140 if (context === undefined || context.length == 0)
141 return ifContext ? undefined : this.menu("-");
142
143 return this.menu(context);
144 },
145
146
147 /**
148 * Show the menu
149 */
150 show : function (ifContext) {
151
152 // Menu is already active
153 if (this.active)
154 return;
155
Nils Diewald5c5a7472015-04-02 22:13:38 +0000156 // Get the menu
157 var menu;
158 if (menu = this.contextMenu(ifContext)) {
Nils Diewald2488d052015-04-09 21:46:02 +0000159 var c = this._inputField.container();
160 c.classList.add('active');
161 c.appendChild(menu.element());
Nils Diewald5c5a7472015-04-02 22:13:38 +0000162 menu.show('');
163 menu.focus();
Nils Diewald5c5a7472015-04-02 22:13:38 +0000164 // Focus on input field
165 // this.inputField.element.focus();
Nils Diewald19ccee92014-12-08 11:30:08 +0000166 };
167 }
168 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000169});