1 | "use strict";
|
2 | var __extends = (this && this.__extends) || (function () {
|
3 | var extendStatics = function (d, b) {
|
4 | extendStatics = Object.setPrototypeOf ||
|
5 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
6 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
7 | return extendStatics(d, b);
|
8 | }
|
9 | return function (d, b) {
|
10 | extendStatics(d, b);
|
11 | function __() { this.constructor = d; }
|
12 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
13 | };
|
14 | })();
|
15 | var __assign = (this && this.__assign) || function () {
|
16 | __assign = Object.assign || function(t) {
|
17 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
18 | s = arguments[i];
|
19 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
20 | t[p] = s[p];
|
21 | }
|
22 | return t;
|
23 | };
|
24 | return __assign.apply(this, arguments);
|
25 | };
|
26 | Object.defineProperty(exports, "__esModule", { value: true });
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | var algorithm_1 = require("@phosphor/algorithm");
|
35 | var coreutils_1 = require("@phosphor/coreutils");
|
36 | var commands_1 = require("@phosphor/commands");
|
37 | var domutils_1 = require("@phosphor/domutils");
|
38 | var virtualdom_1 = require("@phosphor/virtualdom");
|
39 | var widget_1 = require("./widget");
|
40 |
|
41 |
|
42 |
|
43 | var CommandPalette = (function (_super) {
|
44 | __extends(CommandPalette, _super);
|
45 | |
46 |
|
47 |
|
48 |
|
49 |
|
50 | function CommandPalette(options) {
|
51 | var _this = _super.call(this, { node: Private.createNode() }) || this;
|
52 | _this._activeIndex = -1;
|
53 | _this._items = [];
|
54 | _this._results = null;
|
55 | _this.addClass('p-CommandPalette');
|
56 | _this.setFlag(widget_1.Widget.Flag.DisallowLayout);
|
57 | _this.commands = options.commands;
|
58 | _this.renderer = options.renderer || CommandPalette.defaultRenderer;
|
59 | _this.commands.commandChanged.connect(_this._onGenericChange, _this);
|
60 | _this.commands.keyBindingChanged.connect(_this._onGenericChange, _this);
|
61 | return _this;
|
62 | }
|
63 | |
64 |
|
65 |
|
66 | CommandPalette.prototype.dispose = function () {
|
67 | this._items.length = 0;
|
68 | this._results = null;
|
69 | _super.prototype.dispose.call(this);
|
70 | };
|
71 | Object.defineProperty(CommandPalette.prototype, "searchNode", {
|
72 | |
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | get: function () {
|
79 | return this.node.getElementsByClassName('p-CommandPalette-search')[0];
|
80 | },
|
81 | enumerable: true,
|
82 | configurable: true
|
83 | });
|
84 | Object.defineProperty(CommandPalette.prototype, "inputNode", {
|
85 | |
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | get: function () {
|
92 | return this.node.getElementsByClassName('p-CommandPalette-input')[0];
|
93 | },
|
94 | enumerable: true,
|
95 | configurable: true
|
96 | });
|
97 | Object.defineProperty(CommandPalette.prototype, "contentNode", {
|
98 | |
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | get: function () {
|
107 | return this.node.getElementsByClassName('p-CommandPalette-content')[0];
|
108 | },
|
109 | enumerable: true,
|
110 | configurable: true
|
111 | });
|
112 | Object.defineProperty(CommandPalette.prototype, "items", {
|
113 | |
114 |
|
115 |
|
116 | get: function () {
|
117 | return this._items;
|
118 | },
|
119 | enumerable: true,
|
120 | configurable: true
|
121 | });
|
122 | |
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 | CommandPalette.prototype.addItem = function (options) {
|
130 |
|
131 | var item = Private.createItem(this.commands, options);
|
132 |
|
133 | this._items.push(item);
|
134 |
|
135 | this.refresh();
|
136 |
|
137 | return item;
|
138 | };
|
139 | |
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 | CommandPalette.prototype.removeItem = function (item) {
|
148 | this.removeItemAt(this._items.indexOf(item));
|
149 | };
|
150 | |
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 | CommandPalette.prototype.removeItemAt = function (index) {
|
159 |
|
160 | var item = algorithm_1.ArrayExt.removeAt(this._items, index);
|
161 |
|
162 | if (!item) {
|
163 | return;
|
164 | }
|
165 |
|
166 | this.refresh();
|
167 | };
|
168 | |
169 |
|
170 |
|
171 | CommandPalette.prototype.clearItems = function () {
|
172 |
|
173 | if (this._items.length === 0) {
|
174 | return;
|
175 | }
|
176 |
|
177 | this._items.length = 0;
|
178 |
|
179 | this.refresh();
|
180 | };
|
181 | |
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 | CommandPalette.prototype.refresh = function () {
|
195 | this._results = null;
|
196 | this.update();
|
197 | };
|
198 | |
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 | CommandPalette.prototype.handleEvent = function (event) {
|
209 | switch (event.type) {
|
210 | case 'click':
|
211 | this._evtClick(event);
|
212 | break;
|
213 | case 'keydown':
|
214 | this._evtKeyDown(event);
|
215 | break;
|
216 | case 'input':
|
217 | this.refresh();
|
218 | break;
|
219 | case 'focus':
|
220 | case 'blur':
|
221 | this._toggleFocused();
|
222 | break;
|
223 | }
|
224 | };
|
225 | |
226 |
|
227 |
|
228 | CommandPalette.prototype.onBeforeAttach = function (msg) {
|
229 | this.node.addEventListener('click', this);
|
230 | this.node.addEventListener('keydown', this);
|
231 | this.node.addEventListener('input', this);
|
232 | this.node.addEventListener('focus', this, true);
|
233 | this.node.addEventListener('blur', this, true);
|
234 | };
|
235 | |
236 |
|
237 |
|
238 | CommandPalette.prototype.onAfterDetach = function (msg) {
|
239 | this.node.removeEventListener('click', this);
|
240 | this.node.removeEventListener('keydown', this);
|
241 | this.node.removeEventListener('input', this);
|
242 | this.node.removeEventListener('focus', this, true);
|
243 | this.node.removeEventListener('blur', this, true);
|
244 | };
|
245 | |
246 |
|
247 |
|
248 | CommandPalette.prototype.onActivateRequest = function (msg) {
|
249 | if (this.isAttached) {
|
250 | var input = this.inputNode;
|
251 | input.focus();
|
252 | input.select();
|
253 | }
|
254 | };
|
255 | |
256 |
|
257 |
|
258 | CommandPalette.prototype.onUpdateRequest = function (msg) {
|
259 |
|
260 | var query = this.inputNode.value;
|
261 | var contentNode = this.contentNode;
|
262 |
|
263 | var results = this._results;
|
264 | if (!results) {
|
265 |
|
266 | results = this._results = Private.search(this._items, query);
|
267 |
|
268 | this._activeIndex = (query ? algorithm_1.ArrayExt.findFirstIndex(results, Private.canActivate) : -1);
|
269 | }
|
270 |
|
271 | if (!query && results.length === 0) {
|
272 | virtualdom_1.VirtualDOM.render(null, contentNode);
|
273 | return;
|
274 | }
|
275 |
|
276 | if (query && results.length === 0) {
|
277 | var content_1 = this.renderer.renderEmptyMessage({ query: query });
|
278 | virtualdom_1.VirtualDOM.render(content_1, contentNode);
|
279 | return;
|
280 | }
|
281 |
|
282 | var renderer = this.renderer;
|
283 | var activeIndex = this._activeIndex;
|
284 | var content = new Array(results.length);
|
285 | for (var i = 0, n = results.length; i < n; ++i) {
|
286 | var result = results[i];
|
287 | if (result.type === 'header') {
|
288 | var indices = result.indices;
|
289 | var category = result.category;
|
290 | content[i] = renderer.renderHeader({ category: category, indices: indices });
|
291 | }
|
292 | else {
|
293 | var item = result.item;
|
294 | var indices = result.indices;
|
295 | var active = i === activeIndex;
|
296 | content[i] = renderer.renderItem({ item: item, indices: indices, active: active });
|
297 | }
|
298 | }
|
299 |
|
300 | virtualdom_1.VirtualDOM.render(content, contentNode);
|
301 |
|
302 | if (activeIndex < 0 || activeIndex >= results.length) {
|
303 | contentNode.scrollTop = 0;
|
304 | }
|
305 | else {
|
306 | var element = contentNode.children[activeIndex];
|
307 | domutils_1.ElementExt.scrollIntoViewIfNeeded(contentNode, element);
|
308 | }
|
309 | };
|
310 | |
311 |
|
312 |
|
313 | CommandPalette.prototype._evtClick = function (event) {
|
314 |
|
315 | if (event.button !== 0) {
|
316 | return;
|
317 | }
|
318 |
|
319 | var index = algorithm_1.ArrayExt.findFirstIndex(this.contentNode.children, function (node) {
|
320 | return node.contains(event.target);
|
321 | });
|
322 |
|
323 | if (index === -1) {
|
324 | return;
|
325 | }
|
326 |
|
327 | event.preventDefault();
|
328 | event.stopPropagation();
|
329 |
|
330 | this._execute(index);
|
331 | };
|
332 | |
333 |
|
334 |
|
335 | CommandPalette.prototype._evtKeyDown = function (event) {
|
336 | if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) {
|
337 | return;
|
338 | }
|
339 | switch (event.keyCode) {
|
340 | case 13:
|
341 | event.preventDefault();
|
342 | event.stopPropagation();
|
343 | this._execute(this._activeIndex);
|
344 | break;
|
345 | case 38:
|
346 | event.preventDefault();
|
347 | event.stopPropagation();
|
348 | this._activatePreviousItem();
|
349 | break;
|
350 | case 40:
|
351 | event.preventDefault();
|
352 | event.stopPropagation();
|
353 | this._activateNextItem();
|
354 | break;
|
355 | }
|
356 | };
|
357 | |
358 |
|
359 |
|
360 | CommandPalette.prototype._activateNextItem = function () {
|
361 |
|
362 | if (!this._results || this._results.length === 0) {
|
363 | return;
|
364 | }
|
365 |
|
366 | var ai = this._activeIndex;
|
367 | var n = this._results.length;
|
368 | var start = ai < n - 1 ? ai + 1 : 0;
|
369 | var stop = start === 0 ? n - 1 : start - 1;
|
370 | this._activeIndex = algorithm_1.ArrayExt.findFirstIndex(this._results, Private.canActivate, start, stop);
|
371 |
|
372 | this.update();
|
373 | };
|
374 | |
375 |
|
376 |
|
377 | CommandPalette.prototype._activatePreviousItem = function () {
|
378 |
|
379 | if (!this._results || this._results.length === 0) {
|
380 | return;
|
381 | }
|
382 |
|
383 | var ai = this._activeIndex;
|
384 | var n = this._results.length;
|
385 | var start = ai <= 0 ? n - 1 : ai - 1;
|
386 | var stop = start === n - 1 ? 0 : start + 1;
|
387 | this._activeIndex = algorithm_1.ArrayExt.findLastIndex(this._results, Private.canActivate, start, stop);
|
388 |
|
389 | this.update();
|
390 | };
|
391 | |
392 |
|
393 |
|
394 | CommandPalette.prototype._execute = function (index) {
|
395 |
|
396 | if (!this._results) {
|
397 | return;
|
398 | }
|
399 |
|
400 | var part = this._results[index];
|
401 | if (!part) {
|
402 | return;
|
403 | }
|
404 |
|
405 | if (part.type === 'header') {
|
406 | var input = this.inputNode;
|
407 | input.value = part.category.toLowerCase() + " ";
|
408 | input.focus();
|
409 | this.refresh();
|
410 | return;
|
411 | }
|
412 |
|
413 | if (!part.item.isEnabled) {
|
414 | return;
|
415 | }
|
416 |
|
417 | this.commands.execute(part.item.command, part.item.args);
|
418 |
|
419 | this.inputNode.value = '';
|
420 |
|
421 | this.refresh();
|
422 | };
|
423 | |
424 |
|
425 |
|
426 | CommandPalette.prototype._toggleFocused = function () {
|
427 | var focused = document.activeElement === this.inputNode;
|
428 | this.toggleClass('p-mod-focused', focused);
|
429 | };
|
430 | |
431 |
|
432 |
|
433 | CommandPalette.prototype._onGenericChange = function () {
|
434 | this.refresh();
|
435 | };
|
436 | return CommandPalette;
|
437 | }(widget_1.Widget));
|
438 | exports.CommandPalette = CommandPalette;
|
439 |
|
440 |
|
441 |
|
442 | (function (CommandPalette) {
|
443 | |
444 |
|
445 |
|
446 | var Renderer = (function () {
|
447 | function Renderer() {
|
448 | }
|
449 | |
450 |
|
451 |
|
452 |
|
453 |
|
454 |
|
455 |
|
456 | Renderer.prototype.renderHeader = function (data) {
|
457 | var content = this.formatHeader(data);
|
458 | return virtualdom_1.h.li({ className: 'p-CommandPalette-header' }, content);
|
459 | };
|
460 | |
461 |
|
462 |
|
463 |
|
464 |
|
465 |
|
466 |
|
467 | Renderer.prototype.renderItem = function (data) {
|
468 | var className = this.createItemClass(data);
|
469 | var dataset = this.createItemDataset(data);
|
470 | return (virtualdom_1.h.li({ className: className, dataset: dataset }, this.renderItemIcon(data), this.renderItemContent(data), this.renderItemShortcut(data)));
|
471 | };
|
472 | |
473 |
|
474 |
|
475 |
|
476 |
|
477 |
|
478 |
|
479 | Renderer.prototype.renderEmptyMessage = function (data) {
|
480 | var content = this.formatEmptyMessage(data);
|
481 | return virtualdom_1.h.li({ className: 'p-CommandPalette-emptyMessage' }, content);
|
482 | };
|
483 | |
484 |
|
485 |
|
486 |
|
487 |
|
488 |
|
489 |
|
490 | Renderer.prototype.renderItemIcon = function (data) {
|
491 | var className = this.createIconClass(data);
|
492 | return virtualdom_1.h.div({ className: className }, data.item.iconLabel);
|
493 | };
|
494 | |
495 |
|
496 |
|
497 |
|
498 |
|
499 |
|
500 |
|
501 | Renderer.prototype.renderItemContent = function (data) {
|
502 | return (virtualdom_1.h.div({ className: 'p-CommandPalette-itemContent' }, this.renderItemLabel(data), this.renderItemCaption(data)));
|
503 | };
|
504 | |
505 |
|
506 |
|
507 |
|
508 |
|
509 |
|
510 |
|
511 | Renderer.prototype.renderItemLabel = function (data) {
|
512 | var content = this.formatItemLabel(data);
|
513 | return virtualdom_1.h.div({ className: 'p-CommandPalette-itemLabel' }, content);
|
514 | };
|
515 | |
516 |
|
517 |
|
518 |
|
519 |
|
520 |
|
521 |
|
522 | Renderer.prototype.renderItemCaption = function (data) {
|
523 | var content = this.formatItemCaption(data);
|
524 | return virtualdom_1.h.div({ className: 'p-CommandPalette-itemCaption' }, content);
|
525 | };
|
526 | |
527 |
|
528 |
|
529 |
|
530 |
|
531 |
|
532 |
|
533 | Renderer.prototype.renderItemShortcut = function (data) {
|
534 | var content = this.formatItemShortcut(data);
|
535 | return virtualdom_1.h.div({ className: 'p-CommandPalette-itemShortcut' }, content);
|
536 | };
|
537 | |
538 |
|
539 |
|
540 |
|
541 |
|
542 |
|
543 |
|
544 | Renderer.prototype.createItemClass = function (data) {
|
545 |
|
546 | var name = 'p-CommandPalette-item';
|
547 |
|
548 | if (!data.item.isEnabled) {
|
549 | name += ' p-mod-disabled';
|
550 | }
|
551 | if (data.item.isToggled) {
|
552 | name += ' p-mod-toggled';
|
553 | }
|
554 | if (data.active) {
|
555 | name += ' p-mod-active';
|
556 | }
|
557 |
|
558 | var extra = data.item.className;
|
559 | if (extra) {
|
560 | name += " " + extra;
|
561 | }
|
562 |
|
563 | return name;
|
564 | };
|
565 | |
566 |
|
567 |
|
568 |
|
569 |
|
570 |
|
571 |
|
572 | Renderer.prototype.createItemDataset = function (data) {
|
573 | return __assign({}, data.item.dataset, { command: data.item.command });
|
574 | };
|
575 | |
576 |
|
577 |
|
578 |
|
579 |
|
580 |
|
581 |
|
582 | Renderer.prototype.createIconClass = function (data) {
|
583 | var name = 'p-CommandPalette-itemIcon';
|
584 | var extra = data.item.iconClass;
|
585 | return extra ? name + " " + extra : name;
|
586 | };
|
587 | |
588 |
|
589 |
|
590 |
|
591 |
|
592 |
|
593 |
|
594 | Renderer.prototype.formatHeader = function (data) {
|
595 | if (!data.indices || data.indices.length === 0) {
|
596 | return data.category;
|
597 | }
|
598 | return algorithm_1.StringExt.highlight(data.category, data.indices, virtualdom_1.h.mark);
|
599 | };
|
600 | |
601 |
|
602 |
|
603 |
|
604 |
|
605 |
|
606 |
|
607 | Renderer.prototype.formatEmptyMessage = function (data) {
|
608 | return "No commands found that match '" + data.query + "'";
|
609 | };
|
610 | |
611 |
|
612 |
|
613 |
|
614 |
|
615 |
|
616 |
|
617 | Renderer.prototype.formatItemShortcut = function (data) {
|
618 | var kb = data.item.keyBinding;
|
619 | return kb ? kb.keys.map(commands_1.CommandRegistry.formatKeystroke).join(', ') : null;
|
620 | };
|
621 | |
622 |
|
623 |
|
624 |
|
625 |
|
626 |
|
627 |
|
628 | Renderer.prototype.formatItemLabel = function (data) {
|
629 | if (!data.indices || data.indices.length === 0) {
|
630 | return data.item.label;
|
631 | }
|
632 | return algorithm_1.StringExt.highlight(data.item.label, data.indices, virtualdom_1.h.mark);
|
633 | };
|
634 | |
635 |
|
636 |
|
637 |
|
638 |
|
639 |
|
640 |
|
641 | Renderer.prototype.formatItemCaption = function (data) {
|
642 | return data.item.caption;
|
643 | };
|
644 | return Renderer;
|
645 | }());
|
646 | CommandPalette.Renderer = Renderer;
|
647 | |
648 |
|
649 |
|
650 | CommandPalette.defaultRenderer = new Renderer();
|
651 | })(CommandPalette = exports.CommandPalette || (exports.CommandPalette = {}));
|
652 | exports.CommandPalette = CommandPalette;
|
653 |
|
654 |
|
655 |
|
656 | var Private;
|
657 | (function (Private) {
|
658 | |
659 |
|
660 |
|
661 | function createNode() {
|
662 | var node = document.createElement('div');
|
663 | var search = document.createElement('div');
|
664 | var wrapper = document.createElement('div');
|
665 | var input = document.createElement('input');
|
666 | var content = document.createElement('ul');
|
667 | search.className = 'p-CommandPalette-search';
|
668 | wrapper.className = 'p-CommandPalette-wrapper';
|
669 | input.className = 'p-CommandPalette-input';
|
670 | content.className = 'p-CommandPalette-content';
|
671 | input.spellcheck = false;
|
672 | wrapper.appendChild(input);
|
673 | search.appendChild(wrapper);
|
674 | node.appendChild(search);
|
675 | node.appendChild(content);
|
676 | return node;
|
677 | }
|
678 | Private.createNode = createNode;
|
679 | |
680 |
|
681 |
|
682 | function createItem(commands, options) {
|
683 | return new CommandItem(commands, options);
|
684 | }
|
685 | Private.createItem = createItem;
|
686 | |
687 |
|
688 |
|
689 | function search(items, query) {
|
690 |
|
691 | var scores = matchItems(items, query);
|
692 |
|
693 | scores.sort(scoreCmp);
|
694 |
|
695 | return createResults(scores);
|
696 | }
|
697 | Private.search = search;
|
698 | |
699 |
|
700 |
|
701 | function canActivate(result) {
|
702 | return result.type === 'item' && result.item.isEnabled;
|
703 | }
|
704 | Private.canActivate = canActivate;
|
705 | |
706 |
|
707 |
|
708 | function normalizeCategory(category) {
|
709 | return category.trim().replace(/\s+/g, ' ');
|
710 | }
|
711 | |
712 |
|
713 |
|
714 | function normalizeQuery(text) {
|
715 | return text.replace(/\s+/g, '').toLowerCase();
|
716 | }
|
717 | |
718 |
|
719 |
|
720 | function matchItems(items, query) {
|
721 |
|
722 | query = normalizeQuery(query);
|
723 |
|
724 | var scores = [];
|
725 |
|
726 | for (var i = 0, n = items.length; i < n; ++i) {
|
727 |
|
728 | var item = items[i];
|
729 | if (!item.isVisible) {
|
730 | continue;
|
731 | }
|
732 |
|
733 | if (!query) {
|
734 | scores.push({
|
735 | matchType: 3 ,
|
736 | categoryIndices: null,
|
737 | labelIndices: null,
|
738 | score: 0, item: item
|
739 | });
|
740 | continue;
|
741 | }
|
742 |
|
743 | var score = fuzzySearch(item, query);
|
744 |
|
745 | if (!score) {
|
746 | continue;
|
747 | }
|
748 |
|
749 |
|
750 | if (!item.isEnabled) {
|
751 | score.score += 1000;
|
752 | }
|
753 |
|
754 | scores.push(score);
|
755 | }
|
756 |
|
757 | return scores;
|
758 | }
|
759 | |
760 |
|
761 |
|
762 | function fuzzySearch(item, query) {
|
763 |
|
764 | var category = item.category.toLowerCase();
|
765 | var label = item.label.toLowerCase();
|
766 | var source = category + " " + label;
|
767 |
|
768 | var score = Infinity;
|
769 | var indices = null;
|
770 |
|
771 | var rgx = /\b\w/g;
|
772 |
|
773 | while (true) {
|
774 |
|
775 | var rgxMatch = rgx.exec(source);
|
776 |
|
777 | if (!rgxMatch) {
|
778 | break;
|
779 | }
|
780 |
|
781 | var match = algorithm_1.StringExt.matchSumOfDeltas(source, query, rgxMatch.index);
|
782 |
|
783 | if (!match) {
|
784 | break;
|
785 | }
|
786 |
|
787 | if (match && match.score <= score) {
|
788 | score = match.score;
|
789 | indices = match.indices;
|
790 | }
|
791 | }
|
792 |
|
793 | if (!indices || score === Infinity) {
|
794 | return null;
|
795 | }
|
796 |
|
797 | var pivot = category.length + 1;
|
798 |
|
799 | var j = algorithm_1.ArrayExt.lowerBound(indices, pivot, function (a, b) { return a - b; });
|
800 |
|
801 | var categoryIndices = indices.slice(0, j);
|
802 | var labelIndices = indices.slice(j);
|
803 |
|
804 | for (var i = 0, n = labelIndices.length; i < n; ++i) {
|
805 | labelIndices[i] -= pivot;
|
806 | }
|
807 |
|
808 | if (categoryIndices.length === 0) {
|
809 | return {
|
810 | matchType: 0 ,
|
811 | categoryIndices: null,
|
812 | labelIndices: labelIndices,
|
813 | score: score, item: item
|
814 | };
|
815 | }
|
816 |
|
817 | if (labelIndices.length === 0) {
|
818 | return {
|
819 | matchType: 1 ,
|
820 | categoryIndices: categoryIndices,
|
821 | labelIndices: null,
|
822 | score: score, item: item
|
823 | };
|
824 | }
|
825 |
|
826 | return {
|
827 | matchType: 2 ,
|
828 | categoryIndices: categoryIndices,
|
829 | labelIndices: labelIndices,
|
830 | score: score, item: item
|
831 | };
|
832 | }
|
833 | |
834 |
|
835 |
|
836 | function scoreCmp(a, b) {
|
837 |
|
838 | var m1 = a.matchType - b.matchType;
|
839 | if (m1 !== 0) {
|
840 | return m1;
|
841 | }
|
842 |
|
843 | var d1 = a.score - b.score;
|
844 | if (d1 !== 0) {
|
845 | return d1;
|
846 | }
|
847 |
|
848 | var i1 = 0;
|
849 | var i2 = 0;
|
850 | switch (a.matchType) {
|
851 | case 0 :
|
852 | i1 = a.labelIndices[0];
|
853 | i2 = b.labelIndices[0];
|
854 | break;
|
855 | case 1 :
|
856 | case 2 :
|
857 | i1 = a.categoryIndices[0];
|
858 | i2 = b.categoryIndices[0];
|
859 | break;
|
860 | }
|
861 |
|
862 | if (i1 !== i2) {
|
863 | return i1 - i2;
|
864 | }
|
865 |
|
866 | var d2 = a.item.category.localeCompare(b.item.category);
|
867 | if (d2 !== 0) {
|
868 | return d2;
|
869 | }
|
870 |
|
871 | var r1 = a.item.rank;
|
872 | var r2 = b.item.rank;
|
873 | if (r1 !== r2) {
|
874 | return r1 < r2 ? -1 : 1;
|
875 | }
|
876 |
|
877 | return a.item.label.localeCompare(b.item.label);
|
878 | }
|
879 | |
880 |
|
881 |
|
882 | function createResults(scores) {
|
883 |
|
884 | var visited = new Array(scores.length);
|
885 | algorithm_1.ArrayExt.fill(visited, false);
|
886 |
|
887 | var results = [];
|
888 |
|
889 | for (var i = 0, n = scores.length; i < n; ++i) {
|
890 |
|
891 | if (visited[i]) {
|
892 | continue;
|
893 | }
|
894 |
|
895 | var _a = scores[i], item = _a.item, categoryIndices = _a.categoryIndices;
|
896 |
|
897 | var category = item.category;
|
898 |
|
899 | results.push({ type: 'header', category: category, indices: categoryIndices });
|
900 |
|
901 | for (var j = i; j < n; ++j) {
|
902 |
|
903 | if (visited[j]) {
|
904 | continue;
|
905 | }
|
906 |
|
907 | var _b = scores[j], item_1 = _b.item, labelIndices = _b.labelIndices;
|
908 |
|
909 | if (item_1.category !== category) {
|
910 | continue;
|
911 | }
|
912 |
|
913 | results.push({ type: 'item', item: item_1, indices: labelIndices });
|
914 |
|
915 | visited[j] = true;
|
916 | }
|
917 | }
|
918 |
|
919 | return results;
|
920 | }
|
921 | |
922 |
|
923 |
|
924 | var CommandItem = (function () {
|
925 | |
926 |
|
927 |
|
928 | function CommandItem(commands, options) {
|
929 | this._commands = commands;
|
930 | this.category = normalizeCategory(options.category);
|
931 | this.command = options.command;
|
932 | this.args = options.args || coreutils_1.JSONExt.emptyObject;
|
933 | this.rank = options.rank !== undefined ? options.rank : Infinity;
|
934 | }
|
935 | Object.defineProperty(CommandItem.prototype, "label", {
|
936 | |
937 |
|
938 |
|
939 | get: function () {
|
940 | return this._commands.label(this.command, this.args);
|
941 | },
|
942 | enumerable: true,
|
943 | configurable: true
|
944 | });
|
945 | Object.defineProperty(CommandItem.prototype, "iconClass", {
|
946 | |
947 |
|
948 |
|
949 | get: function () {
|
950 | return this._commands.iconClass(this.command, this.args);
|
951 | },
|
952 | enumerable: true,
|
953 | configurable: true
|
954 | });
|
955 | Object.defineProperty(CommandItem.prototype, "iconLabel", {
|
956 | |
957 |
|
958 |
|
959 | get: function () {
|
960 | return this._commands.iconLabel(this.command, this.args);
|
961 | },
|
962 | enumerable: true,
|
963 | configurable: true
|
964 | });
|
965 | Object.defineProperty(CommandItem.prototype, "caption", {
|
966 | |
967 |
|
968 |
|
969 | get: function () {
|
970 | return this._commands.caption(this.command, this.args);
|
971 | },
|
972 | enumerable: true,
|
973 | configurable: true
|
974 | });
|
975 | Object.defineProperty(CommandItem.prototype, "className", {
|
976 | |
977 |
|
978 |
|
979 | get: function () {
|
980 | return this._commands.className(this.command, this.args);
|
981 | },
|
982 | enumerable: true,
|
983 | configurable: true
|
984 | });
|
985 | Object.defineProperty(CommandItem.prototype, "dataset", {
|
986 | |
987 |
|
988 |
|
989 | get: function () {
|
990 | return this._commands.dataset(this.command, this.args);
|
991 | },
|
992 | enumerable: true,
|
993 | configurable: true
|
994 | });
|
995 | Object.defineProperty(CommandItem.prototype, "isEnabled", {
|
996 | |
997 |
|
998 |
|
999 | get: function () {
|
1000 | return this._commands.isEnabled(this.command, this.args);
|
1001 | },
|
1002 | enumerable: true,
|
1003 | configurable: true
|
1004 | });
|
1005 | Object.defineProperty(CommandItem.prototype, "isToggled", {
|
1006 | |
1007 |
|
1008 |
|
1009 | get: function () {
|
1010 | return this._commands.isToggled(this.command, this.args);
|
1011 | },
|
1012 | enumerable: true,
|
1013 | configurable: true
|
1014 | });
|
1015 | Object.defineProperty(CommandItem.prototype, "isVisible", {
|
1016 | |
1017 |
|
1018 |
|
1019 | get: function () {
|
1020 | return this._commands.isVisible(this.command, this.args);
|
1021 | },
|
1022 | enumerable: true,
|
1023 | configurable: true
|
1024 | });
|
1025 | Object.defineProperty(CommandItem.prototype, "keyBinding", {
|
1026 | |
1027 |
|
1028 |
|
1029 | get: function () {
|
1030 | var _a = this, command = _a.command, args = _a.args;
|
1031 | return algorithm_1.ArrayExt.findLastValue(this._commands.keyBindings, function (kb) {
|
1032 | return kb.command === command && coreutils_1.JSONExt.deepEqual(kb.args, args);
|
1033 | }) || null;
|
1034 | },
|
1035 | enumerable: true,
|
1036 | configurable: true
|
1037 | });
|
1038 | return CommandItem;
|
1039 | }());
|
1040 | })(Private || (Private = {}));
|