summaryrefslogtreecommitdiffstats
path: root/data/js/lexer-input.js
blob: 62b59650d8180fc8954e5e82896ce37fe77e16d5 (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
(function () {
'use strict';
define(['util', 'underscore', 'jquery-ui'], function (Util, _) {
	var PrivateFunctions = {
		switchLexer: function (lexer, baseUrl) {
			var url = baseUrl + '/' + Util.fixedEncodeURIComponent(lexer);
			window.location = url;
		},
		lexerSelected: function (event, ui) {
			event.preventDefault();
			var baseUrl = $(event.target).data('base-url');
			this.switchLexer(ui.item.value, baseUrl);
		},
		setupAutocomplete: function (lexers) {
			var lexerSource = [];
			for (var key in lexers) {
				lexerSource.push({ label: lexers[key], value: key });
			}

			$('.lexer-form input').autocomplete({
				source: lexerSource,
				select: _.bind(PrivateFunctions.lexerSelected, PrivateFunctions)
			});
		},
		setupEvents: function () {
			$('.lexer-form').on('submit', _.bind(function (event) {
				event.preventDefault();
				var input = $(event.target).find('input');
				var lexer = input.val();
				var baseUrl = input.data('base-url');
				this.switchLexer(lexer, baseUrl);
			}, this));

			$('.lexer-toggle').on('click', function(event) {
				setTimeout(function() {
					var dropDown = $(event.target).siblings('.dropdown-menu');
					dropDown.find('input').focus();
				}, 0);
			});
		}
	};
	var LexerInput = {
		initialize: function (lexers) {
			PrivateFunctions.setupAutocomplete(lexers);
			PrivateFunctions.setupEvents();
		}
	};

	return LexerInput;
});
})();