forked from mozilla-b2g/gaia
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1119515 - Implement initial search suggestions. r=kgrandon
- Loading branch information
1 parent
d15388e
commit 1105180
Showing
12 changed files
with
235 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
(function() { | ||
|
||
'use strict'; | ||
/* globals Promise, Provider, Search */ | ||
/* globals SettingsListener */ | ||
/* globals LazyLoader */ | ||
|
||
var provider = null; | ||
var config = []; | ||
|
||
var suggestionTpl; | ||
var searchTpl; | ||
|
||
function providerUpdated(key) { | ||
if (!provider) { | ||
return; | ||
} | ||
|
||
config.forEach(item => { | ||
if (item.urlTemplate === provider) { | ||
searchTpl = item.urlTemplate; | ||
suggestionTpl = item.suggestionsUrlTemplate; | ||
var elem = document.getElementById('suggestion-provider'); | ||
navigator.mozL10n.setAttributes(elem, 'search-header', { | ||
provider: item.title.toUpperCase() | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
var SEARCH_PROVIDERS_KEY = 'search.providers'; | ||
var SEARCH_TEMPLATE_KEY = 'search.urlTemplate'; | ||
|
||
var req = navigator.mozSettings.createLock().get(SEARCH_PROVIDERS_KEY); | ||
req.onsuccess = function() { | ||
if (SEARCH_PROVIDERS_KEY in req.result) { | ||
config = req.result[SEARCH_PROVIDERS_KEY]; | ||
providerUpdated(); | ||
} | ||
}; | ||
|
||
SettingsListener.observe(SEARCH_TEMPLATE_KEY, false, value => { | ||
if (value) { | ||
provider = value; | ||
providerUpdated(); | ||
} | ||
}); | ||
|
||
function encodeTerms(str, search) { | ||
return str.replace('{searchTerms}', encodeURIComponent(search)); | ||
} | ||
|
||
function Suggestions() {} | ||
|
||
Suggestions.prototype = { | ||
|
||
__proto__: Provider.prototype, | ||
|
||
name: 'Suggestions', | ||
|
||
remote: true, | ||
|
||
init: function() { | ||
Provider.prototype.init.apply(this, arguments); | ||
}, | ||
|
||
click: function(e) { | ||
var suggestion = e.target.dataset.suggestion; | ||
var url = encodeTerms(searchTpl, suggestion); | ||
Search.navigate(url); | ||
}, | ||
|
||
search: function(input) { | ||
return new Promise((resolve, reject) => { | ||
var url = encodeTerms(suggestionTpl, input); | ||
LazyLoader.getJSON(url, true).then(result => { | ||
var results = result[1]; | ||
// We add an item to search the entered term as well | ||
results.unshift(result[0]); | ||
resolve(results); | ||
}); | ||
}); | ||
}, | ||
|
||
render: function(items) { | ||
var ul = document.createElement('ul'); | ||
ul.setAttribute('role', 'listbox'); | ||
|
||
items.forEach(function each(item) { | ||
var li = document.createElement('li'); | ||
li.dataset.suggestion = li.textContent = item; | ||
li.setAttribute('role', 'option'); | ||
ul.appendChild(li); | ||
}); | ||
|
||
this.clear(); | ||
|
||
if (ul.childNodes.length) { | ||
this.container.appendChild(ul); | ||
} | ||
} | ||
|
||
}; | ||
|
||
Search.provider(new Suggestions()); | ||
|
||
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,3 +37,5 @@ month-8=SEPTEMBER | |
month-9=OCTOBER | ||
month-10=NOVEMBER | ||
month-11=DECEMBER | ||
|
||
search-header={{ provider }} SEARCH |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,38 +43,36 @@ body, a { | |
display: flex; | ||
} | ||
|
||
section.suggestions { | ||
margin: 0 0.7rem; | ||
font-size: 1.4rem; | ||
font-style: italic; | ||
overflow: hidden; | ||
height: 3.5rem; | ||
#suggestions-wrapper { | ||
/* TODO: remove margin-top, fix for incorrectly sized grid */ | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
daleharvey
Author
Owner
|
||
margin-top: 2rem; | ||
} | ||
|
||
#suggestions { | ||
font-size: 1.6rem; | ||
overflow-x: hidden; | ||
margin-right: 1.6rem; | ||
} | ||
|
||
section.suggestions ul { | ||
#suggestions ul { | ||
list-style: none; | ||
color: #00AACC; | ||
white-space: nowrap; | ||
overflow-x: scroll; | ||
padding-left: 0; | ||
height: 5rem; | ||
} | ||
|
||
section.suggestions ul li { | ||
display: inline-block; | ||
margin-left: 1rem; | ||
padding: 0.3rem 0.7rem 0.9rem 0; | ||
#suggestions li { | ||
height: 6rem; | ||
line-height: 6rem; | ||
margin: 0; | ||
border-bottom: 0.1rem solid rgba(255, 255, 255, 0.05); | ||
background-image: url(/style/icons/search.png); | ||
background-repeat: no-repeat; | ||
background-position: 0.8rem center; | ||
text-indent: 4.5rem; | ||
} | ||
|
||
section.suggestions ul li+li:before { | ||
content: "\2022"; | ||
padding-right: 0.7rem; | ||
color: #666; | ||
} | ||
|
||
section.suggestions ul li:first-child { | ||
padding-left: 0.7rem; | ||
#suggestions li:active { | ||
background-color: rgba(0, 202, 242, 0.2); | ||
} | ||
|
||
section .result * { | ||
|
@@ -131,14 +129,6 @@ gaia-grid { | |
display: none; | ||
} | ||
|
||
#suggestions-wrapper { | ||
display: flex; | ||
} | ||
|
||
#suggestions { | ||
flex: 1; | ||
} | ||
|
||
#suggestions-notice-wrapper { | ||
background: rgba(45, 45, 45, 0.94); | ||
z-index: 100; | ||
|
@@ -194,3 +184,33 @@ gaia-grid { | |
#loading.loading { | ||
display: block; | ||
} | ||
|
||
h2 { | ||
text-align: center; | ||
} | ||
|
||
h2 span { | ||
position: relative; | ||
color: #00aac5; | ||
font-size: 1.4rem; | ||
font-weight: normal; | ||
font-style: italic; | ||
} | ||
|
||
h2 span:before, h2 span:after { | ||
border-top: 0.3rem solid #5f5f5f; | ||
content:""; | ||
This comment has been minimized.
Sorry, something went wrong. |
||
position: absolute; | ||
top: 0.9rem; | ||
right: 9em; | ||
bottom: 0; | ||
width: 80%; | ||
} | ||
|
||
h2 span:after { | ||
position: absolute; | ||
top: 0.9rem; | ||
left: 9em; | ||
right:0; | ||
bottom: 0; | ||
} | ||
This comment has been minimized.
Sorry, something went wrong. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
["foo", ["bar"]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
'use strict'; | ||
|
||
var Rocketbar = require('../../../system/test/marionette/lib/rocketbar.js'); | ||
var Server = require('../../../../shared/test/integration/server'); | ||
|
||
marionette('Search - Suggestions Test', function() { | ||
|
||
var client = marionette.client(require(__dirname + '/client_options.js')); | ||
var home, search, rocketbar, system, server; | ||
|
||
var providers; | ||
|
||
suiteSetup(function(done) { | ||
Server.create(__dirname + '/fixtures/', function(err, _server) { | ||
server = _server; | ||
done(); | ||
}); | ||
}); | ||
|
||
setup(function() { | ||
home = client.loader.getAppClass('verticalhome'); | ||
system = client.loader.getAppClass('system'); | ||
search = client.loader.getAppClass('search'); | ||
rocketbar = new Rocketbar(client); | ||
system.waitForStartup(); | ||
|
||
providers = [{ | ||
title: 'first', | ||
urlTemplate: server.url('sample.html'), | ||
suggestionsUrlTemplate: server.url('suggestions_one.json') | ||
}]; | ||
|
||
client.settings.set('search.providers', providers); | ||
client.settings.set('search.urlTemplate', providers[0].urlTemplate); | ||
}); | ||
|
||
test('Test suggestions', function() { | ||
|
||
home.waitForLaunch(); | ||
home.focusRocketBar(); | ||
|
||
rocketbar.enterText('sometext'); | ||
search.goToResults(); | ||
// Ensure we get 2 results (hardcoded in the provider results) | ||
client.waitFor(function() { | ||
return client.findElements(search.Selectors.suggestions).length === 2; | ||
}); | ||
|
||
// Ensure clicking on a result opens the browser correctly | ||
var first = client.helper.waitForElement('#suggestions li'); | ||
first.click(); | ||
|
||
client.switchToFrame(); | ||
rocketbar.switchToBrowserFrame(providers[0].urlTemplate); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Can you comment about what exactly is incorrectly sized?