Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new catalog updates needed to deal with empty meta_data #59

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
],
"coverageThreshold": {
"global": {
"statements": 43,
"statements": 44,
"branches": 31,
"functions": 48,
"lines": 43
Expand Down
188 changes: 188 additions & 0 deletions src/js/controllers/__snapshots__/ctrl-widget-catalog.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`widgetCatalogController defines expected scope vars 1`] = `
Array [
"$watch",
"$on",
"search",
"totalWidgets",
"count",
"noWidgetsInstalled",
"isShowingFilters",
"ready",
"activeFilters",
"showFilters",
"clearFilters",
"clearFiltersAndSearch",
"featuredWidgets",
"toggleFilter",
"widgets",
"filters",
"isFiltered",
"jestTest",
]
`;

exports[`widgetCatalogController implements url based filters and search on initial load 1`] = `
Array [
"$watch",
"$on",
"search",
"totalWidgets",
"count",
"noWidgetsInstalled",
"isShowingFilters",
"ready",
"activeFilters",
"showFilters",
"clearFilters",
"clearFiltersAndSearch",
"featuredWidgets",
"toggleFilter",
"widgets",
"filters",
"isFiltered",
"jestTest",
]
`;

exports[`widgetCatalogController implements url based filters and search on initial load 2`] = `
Object {
"SuPpOrTeD FoUr!!": Object {
"clean": "su_pp_or_te_d_fo_ur",
"isActive": false,
"text": "SuPpOrTeD FoUr!! Questions",
},
"feature1": Object {
"clean": "feature1",
"isActive": true,
"text": "feature1",
},
"feature2": Object {
"clean": "feature2",
"isActive": false,
"text": "feature2",
},
"feature3": Object {
"clean": "feature3",
"isActive": false,
"text": "feature3",
},
"supported1": Object {
"clean": "supported1",
"isActive": false,
"text": "supported1 Questions",
},
"supported2": Object {
"clean": "supported2",
"isActive": false,
"text": "supported2 Questions",
},
"supported_three": Object {
"clean": "supported_three",
"isActive": true,
"text": "supported_three Questions",
},
}
`;

exports[`widgetCatalogController initializes with no filters and search on initial load 1`] = `
Array [
"$watch",
"$on",
"search",
"totalWidgets",
"count",
"noWidgetsInstalled",
"isShowingFilters",
"ready",
"activeFilters",
"showFilters",
"clearFilters",
"clearFiltersAndSearch",
"featuredWidgets",
"toggleFilter",
"widgets",
"filters",
"isFiltered",
"jestTest",
]
`;

exports[`widgetCatalogController initializes with no filters and search on initial load 2`] = `
Object {
"SuPpOrTeD FoUr!!": Object {
"clean": "su_pp_or_te_d_fo_ur",
"isActive": false,
"text": "SuPpOrTeD FoUr!! Questions",
},
"feature1": Object {
"clean": "feature1",
"isActive": false,
"text": "feature1",
},
"feature2": Object {
"clean": "feature2",
"isActive": false,
"text": "feature2",
},
"feature3": Object {
"clean": "feature3",
"isActive": false,
"text": "feature3",
},
"supported1": Object {
"clean": "supported1",
"isActive": false,
"text": "supported1 Questions",
},
"supported2": Object {
"clean": "supported2",
"isActive": false,
"text": "supported2 Questions",
},
"supported_three": Object {
"clean": "supported_three",
"isActive": false,
"text": "supported_three Questions",
},
}
`;

exports[`widgetCatalogController properly generates clean filter names 1`] = `
Array [
"feature1",
"feature3",
"supported1",
"supported_three",
"su_pp_or_te_d_fo_ur",
"feature2",
"supported2",
]
`;

exports[`widgetCatalogController toggling on a filter updates scope 1`] = `
Array [
Object {
"dir": "mockDir1",
"icon": "widget.jpg",
"id": 1,
"in_catalog": "1",
"meta_data": Object {
"about": "information about the widget",
"demo": "1",
"excerpt": "more information about the widget",
"features": Array [
"feature1",
"feature3",
],
"supported_data": Array [
"supported1",
"supported_three",
"SuPpOrTeD FoUr!!",
],
},
"name": "widget1",
},
]
`;
59 changes: 39 additions & 20 deletions src/js/controllers/ctrl-widget-catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ if (window.location.href.match(/\/widgets($|\/\D|\?)/g)) {
}

app.controller('widgetCatalogCtrl', function(Please, $scope, $window, $location, widgetSrv) {
const filterList = {}
const filterList = {} // key is raw filter name, value is filter object
const mapCleanToFilter = {} // key is clean name, value is filter object
const displayedWidgets = []
let allWidgets = []

const resetFilters = () => {
for (i in filterList) delete filterList[i]
for (i in mapCleanToFilter) delete mapCleanToFilter[i]
}

const showFilters = () => {
$scope.isShowingFilters = true
}
Expand Down Expand Up @@ -68,12 +73,17 @@ app.controller('widgetCatalogCtrl', function(Please, $scope, $window, $location,

const _getFiltersFromWidgets = widgets => {
widgets.forEach(widget => {
widget.meta_data.features.forEach(feature => {
if (!filterList.hasOwnProperty(feature)) _registerFilter(feature)
})
widget.meta_data.supported_data.forEach(data => {
if (!filterList.hasOwnProperty(data)) _registerFilter(data, ' Questions')
})
if (widget.meta_data.hasOwnProperty('features')) {
widget.meta_data.features.forEach(feature => {
if (!filterList.hasOwnProperty(feature)) _registerFilter(feature)
})
}

if (widget.meta_data.hasOwnProperty('supported_data')) {
widget.meta_data.supported_data.forEach(data => {
if (!filterList.hasOwnProperty(data)) _registerFilter(data, ' Questions')
})
}
})
}

Expand All @@ -84,10 +94,15 @@ app.controller('widgetCatalogCtrl', function(Please, $scope, $window, $location,

// check for filter matches
for (let filterName in filterList) {
const isActive = filterList[filterName].isActive

if (isActive && !wFeatures.includes(filterName) && !wSupport.includes(filterName)) {
return false
// this filter is active
if (filterList[filterName].isActive) {
// widget has features/support and the feature isn't in either
if (
(!wFeatures || !wFeatures.includes(filterName)) &&
(!wSupport || !wSupport.includes(filterName))
) {
return false
}
}
}

Expand Down Expand Up @@ -125,18 +140,19 @@ app.controller('widgetCatalogCtrl', function(Please, $scope, $window, $location,

const _loadWidgets = () => {
// load list of widgets
widgetSrv.getWidgetsByType('all').then(widgets => {
if (!widgets || !widgets.length || !widgets.length > 0) {
$scope.noWidgetsInstalled = true
Please.$apply()
return
widgetSrv.getWidgetsByType('all').then(loaded => {
if (!loaded || !loaded.length || !loaded.length > 0) {
resetFilters()
loaded = []
}
$scope.noWidgetsInstalled = false
allWidgets = widgets
_getFiltersFromWidgets(widgets)
allWidgets = loaded

$scope.noWidgetsInstalled = allWidgets.length == 0

_getFiltersFromWidgets(allWidgets)

// memoize icon paths
widgets.forEach(widget => {
allWidgets.forEach(widget => {
widget.icon = Materia.Image.iconUrl(widget.dir, 275)
})

Expand All @@ -157,6 +173,7 @@ app.controller('widgetCatalogCtrl', function(Please, $scope, $window, $location,
}

const _getFiltersFromURL = () => {
$scope.isShowingFilters = false
for (let key in $location.search()) {
if (key == 'search') {
$scope.search = $location.search().search
Expand All @@ -169,13 +186,15 @@ app.controller('widgetCatalogCtrl', function(Please, $scope, $window, $location,

$scope.search = ''
$scope.totalWidgets = -1
$scope.count = -1
$scope.noWidgetsInstalled = false
$scope.isShowingFilters = false
$scope.ready = false
$scope.activeFilters = []
$scope.showFilters = showFilters
$scope.clearFilters = clearFilters
$scope.clearFiltersAndSearch = clearFiltersAndSearch
$scope.featuredWidgets = []
$scope.toggleFilter = toggleFilter
$scope.widgets = []
$scope.filters = filterList
Expand Down
Loading