summaryrefslogtreecommitdiffstats
path: root/data/js/lexer-input.js
diff options
context:
space:
mode:
authorJoakim Reinert <mail@jreinert.com>2015-04-18 23:55:22 +0200
committerFlorian Pritz <bluewind@xinu.at>2015-05-05 12:21:26 +0200
commitfe68477d8b60bc9303dd2bcd542915ac05e3fac6 (patch)
tree341ca9d71423c21033cbe5d333603bd3f2e0c3ca /data/js/lexer-input.js
parent9f1fb6e1c8fbe2c54b5d9a7f0755557acd7ea84a (diff)
Refactor lexer input setup
Diffstat (limited to 'data/js/lexer-input.js')
-rw-r--r--data/js/lexer-input.js51
1 files changed, 51 insertions, 0 deletions
diff --git a/data/js/lexer-input.js b/data/js/lexer-input.js
new file mode 100644
index 000000000..62b59650d
--- /dev/null
+++ b/data/js/lexer-input.js
@@ -0,0 +1,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;
+});
+})();