MediaWiki:FandomizedChat/search.js

(function($, mw, mainRoom, FandomizedChat){   function ChatSearch(mainRoom){        this.id = 'ChatSearch';        this.searchLink = '/index.php?title=Special:Search&query=';        this.goLink = '/index.php?title=';        this.loaded = $.Deferred;        this.namespaceId = 0;        this.namespace = ;        this.limit = 15;        this.wgNamespaces = mw.config.get('wgFormattedNamespaces');        this.namespaces = this.getNamespaces;        this.param = {};        this.param.action = 'opensearch';        this.param.limit = this.limit;        this.searchText = ;        this.autocomplete = null;        this.i18n = null;        this.i18no = i18n;        this.wds = null;        this.loadi18n;        this.loadWDS;    }    ChatSearch.prototype.loadi18n = function{        mw.hook('dev.i18n').add($.proxy(function(i18no){            $.when(i18no.loadMessages('FandomizedChat')).done($.proxy(function(i18n){ this.i18n = i18n; }, this)); }, this)); };   ChatSearch.prototype.loadWDS = function{ mw.hook('dev.wds').add($.proxy(function(wds){ this.wds = wds; }, this)); };   ChatSearch.prototype.formatNamespace = function(string){ var extArr = string.split(':'), ext = extArr[0], name = extArr.slice(1).join(':'); if (this.namespaceExists(ext)){ this.namespace = ext; this.param.namespace = this.getNamespaceId(ext); this.param.search = name.trim; } else { this.namespace = ''; this.param.namespace = this.namespaceId; this.param.search = string.trim; }   };    ChatSearch.prototype.getNamespaces = function{ var keys = Object.keys(this.wgNamespaces), index = 0, obj = {}; while ((namespaceId = keys[index])){ var namespace = this.wgNamespaces[namespaceId]; obj[namespace] = namespaceId; index++; }       return obj; };   ChatSearch.prototype.updateSearch = function(event){ var value = $(event.target).val, Api = new mw.Api; this.formatNamespace(value); Api.get(this.param).done($.proxy(this.generate, this, value)); };   ChatSearch.prototype.generate = function(value, data){ var queries = data[1]; if (!queries.length) return; if (this.namespace !== ''){ queries = queries.map($.proxy(function(query){ return this.namespace + ':' + query; }, this)); }       this.autocomplete = this.createAutocomplete(value, queries); this.insertAutocomplete; };   ChatSearch.prototype.createAutocomplete = function(value, queries){ var $list = $(' ').addClass('ChatSearchAutocompleteList search-autocomplete-list'), $label = $(' ').addClass('ChatSearchAutocompleteLabel search-autocomplete-label'); $list.html(queries.map($.proxy(function(query){           var text = query.replace(value, function(match, string){ string = mw.html.escape(string); return ' ' + string + ' '; }),               url = this.goLink + encodeURIComponent(query);            return $(' ').addClass('ChatSearchAutocompleteItem search-autocomplete-item')                .attr('data-name', query).html( $('').attr('href', url).addClass('ChatSearchAutocompleteLink search-autocomplete-link') .on('click', function(event){                           event.preventDefault;                            var link = event.target.href;                            window.open(link, '_blank');                        }) );       }, this))); $label.html([           $(' ').addClass('ChatSearchAutocompleteLabelText search-autocomplete-label-text')                .text(this.i18n.msg('chat_search_autocomplete_label_text').plain),            $(' ').addClass('ChatSearchAutocompleteValue search-autocomplete-value')                .text(value)        ]).on('click', $.proxy(function{ var link = this.searchLink + encodeURIComponent(value); window.open(link, '_blank'); }, this)); return [$list, $label]; };   ChatSearch.prototype.insertAutocomplete = function{ if ($('.ChatSearchAutocomplete').length){ $('.ChatSearchAutocomplete').html(this.autocomplete); }   }; }(jQuery, mediaWiki, mainRoom, window.FandomizedChat));