Skip to content

Commit

Permalink
first version
Browse files Browse the repository at this point in the history
  • Loading branch information
darkdh committed Jul 26, 2016
1 parent 611f617 commit 593d8c6
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 19 deletions.
2 changes: 1 addition & 1 deletion docs/state.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ AppStore
'general.downloads.default-save-path': string, // default path for saving files
'general.autohide-menu': boolean, // true if the Windows menu should be autohidden
'general.disable-title-mode': boolean, // true if title mode should always be disabled
'search.default-search-engine': string, // path to the open search XML
'search.default-search-engine': string, // name of search engine, from js/data/searchProviders.js
'search.offer-search-suggestions': boolean, // true if suggestions should be offered from the default search engine when available.
'tabs.switch-to-new-tabs': boolean, // true if newly opened tabs should be focused immediately
'tabs.paint-tabs': boolean, // true if the page theme color and favicon color should be used for tabs
Expand Down
89 changes: 80 additions & 9 deletions js/about/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const messages = require('../constants/messages')
const settings = require('../constants/settings')
const aboutActions = require('./aboutActions')
const getSetting = require('../settings').getSetting
const SortableTable = require('../components/sortableTable')
const searchProviders = require('../data/searchProviders')

const adblock = appConfig.resourceNames.ADBLOCK
const cookieblock = appConfig.resourceNames.COOKIEBLOCK
Expand Down Expand Up @@ -161,19 +163,88 @@ class GeneralTab extends ImmutableComponent {
}
}

class SearchEntry extends React.Component {
constructor () {
super()
this.state = {
hover: false
}
}

onMouseOver () {
this.setState({hover: true})
}

onMouseOut () {
this.setState({hover: false})
}

onClick (name) {
this.props.onChangeSetting(settings.DEFAULT_SEARCH_ENGINE, name)
}

render () {
return <div onMouseOver={this.onMouseOver.bind(this)} onMouseOut={this.onMouseOut.bind(this)}
onClick={this.onClick.bind(this, this.props.name)}
style={this.state.hover ? {background: '#ffcc99', cursor: 'pointer'} : {}}>
<span style={this.props.iconStyle}>
</span>
<span style={this.props.textStyle}>{this.props.name}</span>
</div>
}
}

class SearchSelectEntry extends React.Component {
constructor (props) {
super(props)
this.state = {
select: props.name === getSetting(settings.DEFAULT_SEARCH_ENGINE)
}

ipc.on(messages.SETTINGS_UPDATED, (e, settings) => {
const settingsMap = Immutable.fromJS(settings || {})
if (this.props.name === settingsMap.get('search.default-search-engine')) {
this.setState({select: true})
} else {
this.setState({select: false})
}
})
}

render () {
return <div>
{this.state.select ? 'x' : ''}
</div>
}
}

class SearchTab extends ImmutableComponent {
get searchProviders () {
let entries = searchProviders.providers
let array = []
const iconSize = 16
entries.forEach((entry) => {
let iconStyle = {
backgroundImage: `url(${entry.image})`,
minWidth: iconSize,
width: iconSize,
backgroundSize: iconSize,
height: iconSize,
display: 'inline-block'
}
let textStyle = {paddingLeft: '5px'}
array.push([<SearchSelectEntry name={entry.name} />,
<SearchEntry name={entry.name} iconStyle={iconStyle} textStyle={textStyle}
onChangeSetting={this.props.onChangeSetting} />, entry.shortcut])
})
return array
}
render () {
return <div>
<div className='sectionTitle' data-l10n-id='searchSettings' />
<SettingsList>
<SettingItem dataL10nId='defaultSearchEngine'>
<select value={getSetting(settings.DEFAULT_SEARCH_ENGINE, this.props.settings)}
onChange={changeSetting.bind(null, this.props.onChangeSetting, settings.DEFAULT_SEARCH_ENGINE)}>
<option value='content/search/google.xml'>Google</option>
<option value='content/search/duckduckgo.xml'>DuckDuckGo</option>
</select>
</SettingItem>
</SettingsList>
<SettingItem dataL10nId='defaultSearchEngine'>
<SortableTable headings={['Default', 'Search Engines', 'Engine Go Key (type first)']} rows={this.searchProviders} />
</SettingItem>
<div className='sectionTitle' data-l10n-id='locationBarSettings' />
<SettingsList>
<SettingCheckbox dataL10nId='showHistoryMatches' prefKey={settings.HISTORY_SUGGESTIONS} settings={this.props.settings} onChangeSetting={this.props.onChangeSetting} />
Expand Down
24 changes: 17 additions & 7 deletions js/components/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const remote = electron.remote
// Actions
const windowActions = require('../actions/windowActions')
const webviewActions = require('../actions/webviewActions')
const loadOpenSearch = require('../lib/openSearch').loadOpenSearch
const contextMenus = require('../contextMenus')
const getSetting = require('../settings').getSetting
const getOrigin = require('../state/siteUtil').getOrigin
Expand Down Expand Up @@ -48,6 +47,8 @@ const keyCodes = require('../constants/keyCodes')
// State handling
const FrameStateUtil = require('../state/frameStateUtil')

const searchProviders = require('../data/searchProviders')

// Util
const cx = require('../lib/classSet.js')
const eventUtil = require('../lib/eventUtil')
Expand Down Expand Up @@ -177,16 +178,25 @@ class Main extends ImmutableComponent {
ipc.on(messages.LEAVE_FULL_SCREEN, this.exitFullScreen.bind(this))
}

loadOpenSearch () {
loadSearchProviders () {
let entries = searchProviders.providers
let engine = getSetting(settings.DEFAULT_SEARCH_ENGINE)
if (this.lastLoadedOpenSearch === undefined || engine !== this.lastLoadedOpenSearch) {
loadOpenSearch(engine).then((searchDetail) => windowActions.setSearchDetail(searchDetail))
this.lastLoadedOpenSearch = engine
if (this.lastLoadedSearchProviders === undefined || engine !== this.lastLoadedSearchProviders) {
entries.forEach((entry) => {
if (entry.name === engine) {
windowActions.setSearchDetail(Immutable.fromJS({
searchURL: entry.search,
autocompleteURL: entry.autocomplete
}))
this.lastLoadedSearchProviders = engine
return false
}
})
}
}

componentDidUpdate (prevProps) {
this.loadOpenSearch()
this.loadSearchProviders()
const activeFrame = FrameStateUtil.getActiveFrame(this.props.windowState)
const activeFramePrev = FrameStateUtil.getActiveFrame(prevProps.windowState)
const activeFrameTitle = activeFrame && (activeFrame.get('title') || activeFrame.get('location')) || ''
Expand Down Expand Up @@ -343,7 +353,7 @@ class Main extends ImmutableComponent {
windowActions.setContextMenuDetail()
})

this.loadOpenSearch()
this.loadSearchProviders()

window.addEventListener('mousemove', (e) => {
self.checkForTitleMode(e.pageY)
Expand Down
13 changes: 13 additions & 0 deletions js/components/urlBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const settings = require('../constants/settings')
const contextMenus = require('../contextMenus')
const dndData = require('../dndData')
const appStoreRenderer = require('../stores/appStoreRenderer')
const searchProviders = require('../data/searchProviders')

const { isUrl, isIntermediateAboutPage } = require('../lib/appUrlUtil')

Expand Down Expand Up @@ -112,7 +113,19 @@ class UrlBar extends ImmutableComponent {
// load the selected suggestion
this.urlBarSuggestions.clickSelected(e)
} else {
let entries = searchProviders.providers
let searchUrl = this.searchDetail.get('searchURL').replace('{searchTerms}', encodeURIComponent(location))
if (!isLocationUrl) {
entries.forEach((entry) => {
const searchRE = new RegExp('^' + entry.shortcut + ' .*', 'g')
if (searchRE.test(location)) {
const replaceRE = new RegExp('^' + entry.shortcut + ' ', 'g')
location = location.replace(replaceRE, '')
searchUrl = entry.search.replace('{searchTerms}', encodeURIComponent(location))
return false
}
})
}
location = isLocationUrl ? location : searchUrl
// do search.
if (e.altKey) {
Expand Down
2 changes: 1 addition & 1 deletion js/constants/appConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ module.exports = {
'general.show-home-button': false,
'general.useragent.value': null, // Set at runtime
'general.autohide-menu': true,
'search.default-search-engine': 'content/search/google.xml',
'search.default-search-engine': 'Google',
'search.offer-search-suggestions': false, // false by default for privacy reasons
'tabs.switch-to-new-tabs': false,
'tabs.paint-tabs': true,
Expand Down
2 changes: 1 addition & 1 deletion js/contextMenus.js
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ function mainTemplateInit (nodeProps, frame) {
},
copyAddressMenuItem('copyImageAddress', nodeProps.srcURL)
)
if (getSetting(settings.DEFAULT_SEARCH_ENGINE) === 'content/search/google.xml' &&
if (getSetting(settings.DEFAULT_SEARCH_ENGINE) === 'Google' &&
nodeProps.srcURL && urlParse(nodeProps.srcURL).protocol !== 'data:') {
template.push(
{
Expand Down
Loading

0 comments on commit 593d8c6

Please sign in to comment.