This repository has been archived by the owner on Oct 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch the search view to react; refactor store & search (#215)
- Loading branch information
1 parent
cba315a
commit 7304167
Showing
11 changed files
with
152 additions
and
372 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
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 |
---|---|---|
@@ -1,36 +1,92 @@ | ||
var $ = require('jquery'); | ||
var Backbone = require('backbone'); | ||
Backbone.$ = $; | ||
var CONF = require('../config.js'); | ||
var React = require('react'); | ||
var Radio = require('backbone.radio'); | ||
var Snaplist = require('../collections/snaplist.js'); | ||
var Sections = require('../common/sections.js'); | ||
var StoreLayoutView = require('../views/store.js'); | ||
var CONF = require('../config.js'); | ||
|
||
function isDiffArray(arr1, arr2) { | ||
if (arr1.length != arr2.length) | ||
return true; | ||
|
||
for (var i = 0; i < arr1.length; ++i) { | ||
if (arr1[i] !== arr2[i]) | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
var updateInstalledStates = function(collection) { | ||
if (!collection) { | ||
return; | ||
} | ||
var installedBask = new Snaplist(); | ||
installedBask.fetch({ | ||
data: $.param({ | ||
'installed_only': true | ||
}), | ||
success: function(snaplist) { | ||
var installedById = {} | ||
snaplist.forEach(function(s) { | ||
installedById[s.id] = s | ||
}) | ||
collection.forEach(function(s) { | ||
if (installedById[s.id]) { | ||
// TODO make sure that active & installed state is preserved | ||
s.set('status', CONF.INSTALL_STATE.INSTALLED) | ||
s.set('icon', installedById[s.id].get('icon')) | ||
} | ||
}); | ||
}, | ||
error: function(snaplist) { | ||
console.log('error') | ||
} | ||
}); | ||
return collection | ||
}; | ||
|
||
module.exports = { | ||
updateInstalledStates: function(collection) { | ||
if (!collection) { | ||
return; | ||
fetchSnapListView: function(title, query, options) { | ||
var chan = Radio.channel('root'); | ||
var snaplist = new Snaplist(); | ||
var sections = []; | ||
|
||
if (localStorage) { | ||
sections = JSON.parse(localStorage.getItem('storeSections')) || []; | ||
} | ||
var installedBask = new Snaplist(); | ||
installedBask.fetch({ | ||
data: $.param({ | ||
'installed_only': true | ||
}), | ||
success: function(snaplist) { | ||
var installedById = {} | ||
snaplist.forEach(function(s) { | ||
installedById[s.id] = s | ||
}) | ||
collection.forEach(function(s) { | ||
if (installedById[s.id]) { | ||
// TODO make sure that active & installed state is preserved | ||
s.set('status', CONF.INSTALL_STATE.INSTALLED) | ||
s.set('icon', installedById[s.id].get('icon')) | ||
} | ||
}); | ||
}, | ||
error: function(snaplist) { | ||
console.log('error') | ||
|
||
var m = new Backbone.Model({ | ||
query: query, | ||
title: title, | ||
isGrid: true, | ||
isHomeActive: false, | ||
sections: sections.concat('private'), | ||
loading: true | ||
}); | ||
|
||
var element = React.createElement(StoreLayoutView, { | ||
model: m, | ||
collection: snaplist | ||
}); | ||
chan.command('set:content', {reactElement: element}); | ||
|
||
Sections.fetch().done(function(response) { | ||
if (isDiffArray(sections, response)) { | ||
if (localStorage) { | ||
localStorage.setItem('storeSections', JSON.stringify(response)); | ||
} | ||
m.set('sections', response.concat('private')); | ||
} | ||
}); | ||
return collection | ||
|
||
snaplist.fetch(options).always(function(response) { | ||
m.set('loading', false); | ||
}).done(function() { | ||
snaplist = updateInstalledStates(snaplist) | ||
}); | ||
}, | ||
updateInstalledStates: updateInstalledStates, | ||
}; |
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
|
||
var Backbone = require('backbone'); | ||
var React = require('react') | ||
var ReactBackbone = require('react.backbone'); | ||
|
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 |
---|---|---|
@@ -1,44 +1,12 @@ | ||
var $ = require('jquery'); | ||
var Backbone = require('backbone'); | ||
var Marionette = require('backbone.marionette'); | ||
var Radio = require('backbone.radio'); | ||
var SearchLayoutView = require('../views/search.js'); | ||
var Sections = require('../collections/sections.js'); | ||
var Bask = require('../collections/snaplist.js'); | ||
var SnaplistTools = require('../common/snaplists.js'); | ||
|
||
module.exports = { | ||
query: function(q) { | ||
var chan = Radio.channel('root'); | ||
var sections = new Sections(); | ||
var searchBask = new Bask(); | ||
|
||
var sp = sections.fetch(); | ||
|
||
// TODO impose the limit before the query | ||
// not at display time | ||
searchBask.fetch({ | ||
data: $.param({ | ||
'q': q | ||
}), | ||
success: function(snaplist) { | ||
snaplist = SnaplistTools.updateInstalledStates(snaplist) | ||
var view = new SearchLayoutView({ | ||
model: new Backbone.Model({ | ||
query: q, | ||
title: 'Search results for "' + q + '"', | ||
isGrid: true, | ||
isAlpha: true, | ||
canSort: false, | ||
canStyle: true, | ||
isHomeActive: false, | ||
sections: sections, | ||
}), | ||
sectionsPromise: sp, | ||
collection: snaplist, | ||
}); | ||
chan.command('set:content', {backboneView: view}); | ||
} | ||
}); | ||
SnaplistTools.fetchSnapListView( | ||
'Search results for "' + q + '"', | ||
q, | ||
{data: $.param({'q': q})} | ||
); | ||
} | ||
}; |
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 |
---|---|---|
@@ -1,82 +1,30 @@ | ||
var $ = require('jquery'); | ||
var Backbone = require('backbone'); | ||
Backbone.$ = $; | ||
var Marionette = require('backbone.marionette'); | ||
var React = require('react'); | ||
var Radio = require('backbone.radio'); | ||
var StoreLayoutView = require('../views/store.js'); | ||
var Snaplist = require('../collections/snaplist.js'); | ||
var Sections = require('../common/sections.js'); | ||
var SnaplistTools = require('../common/snaplists.js'); | ||
|
||
|
||
function isDiffArray(arr1, arr2) { | ||
if (arr1.length != arr2.length) | ||
return true; | ||
|
||
for (var i = 0; i < arr1.length; ++i) { | ||
if (arr1[i] !== arr2[i]) | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
var fetchSnapList = function(title, options) { | ||
var chan = Radio.channel('root'); | ||
var storeSnaplist = new Snaplist(); | ||
var sections = []; | ||
|
||
if (localStorage) { | ||
sections = JSON.parse(localStorage.getItem('storeSections')) || []; | ||
} | ||
|
||
var storeModel = new Backbone.Model({ | ||
query: '', | ||
title: title, | ||
isGrid: true, | ||
isAlpha: true, | ||
canSort: false, | ||
canStyle: true, | ||
isHomeActive: false, | ||
sections: sections.concat('private'), | ||
loading: true | ||
}); | ||
|
||
var element = React.createElement(StoreLayoutView, { | ||
model: storeModel, | ||
collection: storeSnaplist | ||
}); | ||
chan.command('set:content', {reactElement: element}); | ||
|
||
Sections.fetch().done(function(response) { | ||
if (isDiffArray(sections, response)) { | ||
if (localStorage) { | ||
localStorage.setItem('storeSections', JSON.stringify(response)); | ||
} | ||
storeModel.set('sections', response.concat('private')); | ||
} | ||
}); | ||
|
||
storeSnaplist.fetch(options).always(function(response) { | ||
storeModel.set('loading', false); | ||
}).done(function() { | ||
storeSnaplist = SnaplistTools.updateInstalledStates(storeSnaplist) | ||
}); | ||
} | ||
|
||
module.exports = { | ||
index: function() { | ||
fetchSnapList('Featured snaps', {data: $.param({'section': 'featured'})}) | ||
SnaplistTools.fetchSnapListView( | ||
'Featured snaps', | ||
'', | ||
{data: $.param({'section': 'featured'})} | ||
); | ||
}, | ||
section: function(s) { | ||
// Special case for private section which is not a section | ||
// per se but a specificity of a snap | ||
if (s === 'private') { | ||
fetchSnapList('Private snaps', {data: $.param({'private_snaps': true})}) | ||
SnaplistTools.fetchSnapListView( | ||
'Private snaps', | ||
'', | ||
{data: $.param({'private_snaps': true})} | ||
); | ||
} | ||
else { | ||
fetchSnapList(s, {data: $.param({'section': s})}) | ||
SnaplistTools.fetchSnapListView( | ||
s, | ||
'', | ||
{data: $.param({'section': s})} | ||
); | ||
} | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.