Fix: Error in ledger where show results were not updated as the $watch was only for hidden items and did not detect and filter the change in the list.

Fix: Error in ledger where if an entry was selected, if show button was triggered using the return key, it would open that entry instead of loading data
This commit is contained in:
Amritanshu 2014-06-12 01:30:21 +05:30
parent d99fc89e34
commit 09a27e099e
6 changed files with 66 additions and 20 deletions

View File

@ -26,6 +26,7 @@
</style>
<script src="/js/mousetrap.min.js"></script>
<script src="/js/mousetrap-brewman.js"></script>
<script src="/js/jquery-2.1.0.min.js"></script>
<script src="/js/jquery.scrolltoview.js"></script>
<script src="/js/lodash.min.js"></script>

View File

@ -0,0 +1,50 @@
Mousetrap = (function (Mousetrap) {
'use strict';
var self = Mousetrap,
_oldBind = self.bind,
_oldUnbind = self.unbind,
args;
self.stopCallback = function (e, element) {
// if the element has the class "mousetrap" then no need to stop
if ((' ' + element.className + ' ').indexOf(' mousetrap ') > -1) {
return false;
}
// stop for input, select, and textarea
return ['INPUT', 'SELECT', 'TEXTAREA', 'BUTTON'].indexOf(element.tagName) > -1 || element.isContentEditable;
};
self.bind = function () {
args = arguments;
// normal call
if (typeof args[0] === 'string' || args[0] instanceof Array) {
return _oldBind.apply(this, args);
}
// object passed in
for (var key in args[0]) {
if (args[0].hasOwnProperty(key)) {
_oldBind(key, args[0][key], args[1]);
}
}
};
self.unbind = function () {
args = arguments;
// normal call
if (typeof args[0] === 'string' || args[0] instanceof Array) {
return _oldUnbind.apply(this, args);
}
// object passed in
for (var key in args[0]) {
if (args[0].hasOwnProperty(key)) {
_oldUnbind(key, args[1]);
}
}
};
return Mousetrap;
})(Mousetrap);

View File

@ -54,7 +54,8 @@
<tr id="{{$index}}" ng-repeat="item in ledger" ng-class="{danger:!item.Posted && $index!=selected, warning:$index==selected}"
ng-click="setSelected($index)">
<td class="no-wrap">{{item.Date}}</td>
<td><a href="{{item.Url}}">{{item.Name}}</a></td>
<td ng-if="!item.Url">{{item.Name}}</td>
<td ng-if="item.Url"><a href="{{item.Url}}">{{item.Name}}</a></td>
<td class="no-wrap">{{item.Type}}</td>
<td>{{item.Narration}}</td>
<td class="text-right no-wrap">{{item.Debit | currency | clr}}</td>

View File

@ -125,21 +125,9 @@ overlordDirective.directive('chosen', ['$parse', function ($parse) {
overlordDirective.directive('keypress', [function () {
return function (scope, element, attrs) {
var keypress = scope.$eval(attrs.keypress || '{}');
for (var k in keypress) {
if (keypress.hasOwnProperty(k)) {
(function (k, val) {
Mousetrap.bind(k, val);
})(k, keypress[k]);
}
}
Mousetrap.bind(keypress);
element.on('$destroy', function () {
for (var k in keypress) {
if (keypress.hasOwnProperty(k)) {
(function (k) {
Mousetrap.unbind(k);
})(k);
}
}
Mousetrap.unbind(keypress);
});
};

View File

@ -35,12 +35,20 @@ var LedgerCtrl = ['$scope', '$routeParams', '$location', 'asDateFilter', 'ledger
$scope.selected = index;
};
// Replace with $watchGroup in AngularJS 1.3+
$scope.$watch('hidden', function () {
var filtered = $scope.doFilter($scope.hidden, $scope.info.Body);
$scope.ledger = filtered.Body;
$scope.footer = filtered.Footer;
}, true);
$scope.$watch('info', function () {
var filtered = $scope.doFilter($scope.hidden, $scope.info.Body);
$scope.ledger = filtered.Body;
$scope.footer = filtered.Footer;
}, true);
$scope.doFilter = _.memoize(function (hidden, input) {
var data = angular.copy(input),
debit = 0, credit = 0, running = 0;

View File

@ -2,14 +2,12 @@
var ReconcileCtrl = ['$scope', '$routeParams', '$location', 'asDateFilter', 'reconcile', 'Reconcile', 'Account', function ($scope, $routeParams, $location, asDate, reconcile, Reconcile, Account) {
$scope.info = reconcile;
$scope.hidden = [];
$scope.show = function () {
var id = $scope.info.Account.LedgerID,
startDate = asDate($scope.info.StartDate),
finishDate = asDate($scope.info.FinishDate);
if (id === $routeParams.id && startDate === $routeParams.StartDate && finishDate === $routeParams.FinishDate) {
Reconcile.get({id: id, StartDate: startDate, FinishDate: finishDate}, function (data) {
$scope.doFilter.cache = {};
$scope.info = data;
});
} else {
@ -49,7 +47,7 @@ var ReconcileCtrl = ['$scope', '$routeParams', '$location', 'asDateFilter', 'rec
};
$scope.shortcuts = {
'up': function () {
'up': function (e) {
if ($scope.selected > 0) {
$scope.$apply(function () {
$scope.selected = $scope.selected -= 1;
@ -58,7 +56,7 @@ var ReconcileCtrl = ['$scope', '$routeParams', '$location', 'asDateFilter', 'rec
e.preventDefault();
}
},
'down': function () {
'down': function (e) {
if ($scope.selected < $scope.info.Body.length - 1) {
$scope.$apply(function () {
$scope.selected = $scope.selected += 1;
@ -67,7 +65,7 @@ var ReconcileCtrl = ['$scope', '$routeParams', '$location', 'asDateFilter', 'rec
e.preventDefault();
}
},
'enter': function () {
'enter': function (e) {
var path = $scope.info.Body[$scope.selected].Url.replace(/^(?:\/\/|[^\/]+)*/, "");
$scope.$apply(function () {
$location.path(path).search('StartDate', null).search('FinishDate', null);