Share Gaia assets and adapters, keep private profiles and development documents local, and update both public READMEs.
14 lines
No EOL
5.9 KiB
JavaScript
14 lines
No EOL
5.9 KiB
JavaScript
;(function(){'use strict';var SEARCH_DELAY=500;var MAX_GRID_SIZE=4;window.Search={_port:null,providers:{},gridCount:0,searchResults:document.getElementById('search-results'),offlineMessage:document.getElementById('offline-message'),suggestionsWrapper:document.getElementById('suggestions-wrapper'),grid:document.getElementById('icons'),gridWrapper:document.getElementById('icons-wrapper'),suggestionsEnabled:false,suggestionNotice:document.getElementById('suggestions-notice-wrapper'),get settingsLink(){return document.getElementById('settings-link');},toShowNotice:null,NOTICE_KEY:'notice-shown',init:function(){this.dedupe=new SearchDedupe();this.metrics=new MetricsHelper();this.metrics.init();var self=this;navigator.mozApps.getSelf().onsuccess=function(){var app=this.result;app.connect('search-results').then(function onConnAccepted(ports){ports.forEach(function(port){self._port=port;});setConnectionHandler();},function onConnectionRejected(reason){console.log('Error connecting: '+reason+'\n');});};function setConnectionHandler(){navigator.mozSetMessageHandler('connection',function(connectionRequest){var keyword=connectionRequest.keyword;var port=connectionRequest.port;if(keyword==='search'){port.onmessage=self.dispatchMessage.bind(self);port.start();}});initializeProviders();}
|
|
function initializeProviders(){for(var i in self.providers){self.providers[i].init(self);}}
|
|
var enabledKey='search.suggestions.enabled';SettingsListener.observe(enabledKey,true,function(enabled){this.suggestionsEnabled=enabled;}.bind(this));this.initNotice();this.initConnectivityCheck();this.contextmenu=new Contextmenu();window.addEventListener('resize',this.resize);window.addEventListener('scroll',this.onScroll);navigator.mozSetMessageHandler('activity',this.handleActivityEvents.bind(this));},resize:function(){if(this.grid&&this.grid.render){this.grid.render({rerender:true,skipDivider:true});}},onScroll:function(){window.focus();},provider:function(provider){if(!(provider.name in this.providers)){this.providers[provider.name]=provider;}},dispatchMessage:function(msg){if(typeof this[msg.data.action]==='function'){this[msg.data.action](msg);}},change:function(msg){clearTimeout(this.changeTimeout);this.showSearchResults();var input=msg.data.input;var providers=this.providers;this.changeTimeout=setTimeout(()=>{this.clear();this.dedupe.reset();Object.keys(providers).forEach((providerKey)=>{var provider=providers[providerKey];var preventRemoteFetch=UrlHelper.isURL(input)||msg.data.isPrivateBrowser||!this.suggestionsEnabled;if(provider.remote&&preventRemoteFetch){return;}
|
|
if(provider.name==='Suggestions'){var toShow=input.length>2&&this.toShowNotice&&this.suggestionsEnabled&&this.suggestionNotice.hidden&&navigator.onLine;if(toShow){this.suggestionNotice.hidden=false;}}
|
|
provider.search(input,preventRemoteFetch).then((results)=>{this.collect(provider,results);},error=>{if(error)console.warn('Search provider '+provider.name+': '+error);});});},SEARCH_DELAY);},initNotice:function(){var confirm=document.getElementById('suggestions-notice-confirm');confirm.addEventListener('click',this.discardNotice.bind(this,true));var settingsLink=this.settingsLink;if(settingsLink){settingsLink.addEventListener('click',this.openSettings.bind(this));}
|
|
asyncStorage.getItem(this.NOTICE_KEY,function(value){if(this.toShowNotice===null){this.toShowNotice=!value;}}.bind(this));},openSettings:function(){this.discardNotice();new MozActivity({name:'configure',data:{target:'device',section:'search'}});},discardNotice:function(focus){this.suggestionNotice.hidden=true;this.toShowNotice=false;asyncStorage.setItem(this.NOTICE_KEY,true);if(focus){this._port.postMessage({'action':'focus'});}},expandSearch:function(query){this.clear();this.providers.WebResults.fullscreen(query);},collect:function(provider,results){if(provider.dedupes){results=this.dedupe.reduce(results,provider.dedupeStrategy);}
|
|
if(provider.isGridProvider&&(results.length+this.gridCount)>MAX_GRID_SIZE){var spaces=MAX_GRID_SIZE-this.gridCount;if(spaces<1){this.abort();return;}
|
|
results.splice(spaces,(results.length-spaces));}
|
|
if(provider.isGridProvider){this.gridCount+=results.length;}
|
|
this.gridWrapper.classList.toggle('hidden',!this.gridCount);provider.render(results);},handleActivityEvents:function(activity){var activityName=activity.source.name;if(activityName==='search'){this.submit({data:{input:activity.source.data.keyword}});}},submit:function(msg){this.discardNotice();var input=msg.data.input;if(UrlHelper.isNotURL(input)){this.metrics.report('websearch',SearchProvider('title'));var url=SearchProvider('searchUrl').replace('{searchTerms}',encodeURIComponent(input));this.navigate(url);return;}
|
|
var hasScheme=UrlHelper.hasScheme(input);if(!hasScheme){input='http://'+input;}
|
|
this.navigate(input);},clear:function(msg){this.abort();for(var i in this.providers){this.providers[i].clear();}
|
|
this.gridCount=0;this.suggestionNotice.hidden=true;},showSearchResults:function(){if(this.searchResults){this.searchResults.classList.remove('hidden');}},abort:function(){clearTimeout(this.changeTimeout);for(var i in this.providers){this.providers[i].abort();}},close:function(){this.abort();this._port.postMessage({'action':'hide'});},navigate:function(url){new MozActivity({name:'view',data:{type:'url',url:url}});},setInput:function(input){this._port.postMessage({'action':'input','input':input});this.expandSearch(input);},initConnectivityCheck:function(){var self=this;function onConnectivityChange(){if(navigator.onLine){self.searchResults.classList.remove('offline');}else{self.searchResults.classList.add('offline');}}
|
|
this.offlineMessage.addEventListener('click',function(){var activity=new window.MozActivity({name:'configure',data:{target:'device',section:'root',filterBy:'connectivity'}});activity.onsuccess=function(){};});window.addEventListener('offline',onConnectivityChange);window.addEventListener('online',onConnectivityChange);onConnectivityChange();}};window.addEventListener('load',Search.init.bind(Search));})(); |