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

Refactor bhFilters with Filter Component (2653) #3107

Merged
merged 2 commits into from
Aug 31, 2018
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
5 changes: 5 additions & 0 deletions client/src/css/structure.css
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,11 @@ a[disabled] {
padding-left : 5px;
}

.filter-text-link {
color: #fff;
margin-left: 5px;
}

.strike {
text-decoration : line-through;
}
Expand Down
21 changes: 21 additions & 0 deletions client/src/js/components/bhFilter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
angular.module('bhima.components')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet 👍

.component('bhFilter', {
templateUrl : 'modules/templates/bhFilter.tmpl.html',
controller : bhFilterController,
bindings : {
filter : '<',
onRemove : '&',
}
,
});

function bhFilterController() {
const $ctrl = this;
const DEFAULT_FILTER_COMPARITOR = ':';

$ctrl.$onInit = function onInit() {
// fill in label details required by the template
$ctrl.filter = $ctrl.filter || {};
$ctrl.filter.comparitorLabel = $ctrl.filter._comparitor || DEFAULT_FILTER_COMPARITOR;
};
}
20 changes: 10 additions & 10 deletions client/src/js/components/bhFilters.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
angular.module('bhima.components')
.component('bhFilters', {
templateUrl : 'modules/templates/bhFilters.tmpl.html',
controller : bhFiltersController,
bindings : {
filters : '<',
onRemoveFilter : '&', // fires to remove the filter
},
});
.component('bhFilters', {
templateUrl : 'modules/templates/bhFilters.tmpl.html',
controller : bhFiltersController,
bindings : {
filters : '<',
onRemoveFilter : '&',
},
});

bhFiltersController.$inject = ['$filter'];
function bhFiltersController($filter) {
var $ctrl = this;
const $ctrl = this;

// formats the $viewValue according to any filters passed in
$ctrl.$onChanges = function onChanges(changes) {
if (!changes.filters) { return; }

var filters = changes.filters.currentValue;
const filters = changes.filters.currentValue;

if (!filters) { return; }

Expand Down
19 changes: 19 additions & 0 deletions client/src/modules/templates/bhFilter.tmpl.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<span data="filter">
<span class="label" ng-class="{ 'label-primary' : $ctrl.filter._isDefault, 'label-emphasize' : !$ctrl.filter._isDefault }">
<i class="fa fa-filter"></i>

<span translate>{{$ctrl.filter._label}}</span>
<span>{{$ctrl.filter.comparitorLabel}}</span>
<span>{{$ctrl.filter.displayValue}}</span>

<!-- optional close button, displayed if this is not a default filter -->
<a
href
ng-if="!$ctrl.filter._isDefault"
ng-click="$ctrl.onRemove({ filter: $ctrl.filter._key })"
class="filter-text-link">

<i class="fa fa-close"></i>
</a>
</span>
</span>
21 changes: 4 additions & 17 deletions client/src/modules/templates/bhFilters.tmpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,18 @@
<!--
TODO: these should be a custom CSS class. Label is just too small!
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a big deal, but this comment really applies to the component's HTML, not the outside template. It should probably be removed or placed there.

-->
<span class="label label-emphasize">
<i class="fa fa-filter"></i>
<span translate>{{filter._label}}</span> {{ filter._comparitor ? " " + filter._comparitor : ":" }}
{{ filter.displayValue }}
</span>
<bh-filter filter="filter"></bh-filter>
</li>
</ul>

<!-- Separator between filter types -->
<div ng-if="$ctrl.filters.defaultFilters.length && $ctrl.filters.customFilters.length" style="display : inline-block; width : 1px; height: 25px; vertical-align : top; background-color : #ccc;"></div>

<ul ng-if="$ctrl.filters.customFilters.length" class="list-inline filter-list" data-bh-filter-bar-custom-filters>

<li ng-repeat="filter in $ctrl.filters.customFilters">
<span class="label label-primary">
<i class="fa fa-filter"></i>
<span translate>{{filter._label}}</span>
{{ filter._comparitor ? " " + filter._comparitor : ":" }}
{{ filter.displayValue }}

<a ng-if="!filter.isDefault" href ng-click="$ctrl.onRemoveFilter({ filter : filter._key })"
style="color:#fff; margin-left:5px;">
<i class="fa fa-close"></i>
</a>

</span>
<!-- on-remove is passing an object containing 'locals' (variables) as part of the Angular -->
<!-- component callback structure - these methods are not directly called but are wrapper methods -->
<bh-filter filter="filter" on-remove="$ctrl.onRemoveFilter({ filter: filter})"></bh-filter>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very clean 👍

</li>
</ul>