Refactoring.

This commit is contained in:
Alexis Lahouze 2015-08-18 17:15:56 +02:00
parent cf657c1ad7
commit a23d653738
2 changed files with 146 additions and 209 deletions

View File

@ -26,23 +26,21 @@ accountantApp
.controller(
"SchedulerController", [
"$scope", "$rootScope", "$routeParams", "ScheduledOperations",
function($scope, $rootScope, $routeParams, ScheduledOperations) {
"$scope", "$rootScope", "$routeParams", "notificationService", "ScheduledOperations",
function($scope, $rootScope, $routeParams, notificationService, ScheduledOperations) {
// Operations store and selection
$scope.operations = [];
$scope.selectedOperation = null;
// Placeholder for saved value to cancel entry edition
$scope.savedOperation = null;
$scope.categories = [];
$scope.resetNewOperation = function() {
$scope.newOperation = new ScheduledOperations({});
$scope.addOperation = function() {
if(! $scope.inserted) {
$scope.inserted = new ScheduledOperations({});
$scope.inserted.account_id = $routeParams.accountId;
$scope.operations.splice(0, 0, $scope.inserted);
}
};
$scope.resetNewOperation();
$scope.loadOperations = function(accountId) {
// Clean up selected entry.
$scope.selectedOperation = null;
@ -50,95 +48,44 @@ accountantApp
$scope.operations = ScheduledOperations.query({
account: $routeParams.accountId
}, function(data) {
$scope.$emit("operationsLoadedEvent", {operations: data});
});
};
// Returns true if the entry is in editing state.
$scope.isEditing = function(operation) {
return operation.state === 'edit';
};
$scope.isDisplaying = function(operation) {
return !operation.state;
};
$scope.createOperation = function(operation) {
operation.account_id = $routeParams.accountId;
operation.$save(function(data) {
$scope.resetNewOperation();
$scope.$emit("operationCreatedEvent", data);
});
};
// Notify on success.
$scope.$on("operationCreatedEvent", function(e, operation) {
new PNotify({
type: "success",
title: "Save",
text: "Operation #" + operation.id + " created."
});
});
// Save operation.
$scope.saveOperation = function(operation) {
operation.$save(function(data) {
$scope.$emit("operationSavedEvent", operation);
});
};
$scope.saveOperation = function($data, $index) {
var operation;
// Notify on success.
$scope.$on("operationSavedEvent", function(e, operation) {
new PNotify({
type: "success",
title: "Save",
text: "Operation #" + operation.id + " saved."
if($data.$save) {
operation = $data;
} else {
operation = $scope.operations[$index];
operation = angular.merge(operation, $data);
}
var promise = operation.$save()
if(operation == $scope.inserted) {
promise = promise.then(function(data) {
$scope.inserted = false;
return data;
});
});
}
$scope.editOperation = function(operation) {
// Enter edit state.
operation.state='edit';
return promise.then(function(data) {
notificationService.success("Operation #" + data.id + " saved.");
return data;
});
};
// Reload operation from server to cancel edition.
$scope.cancelEditOperation = function(operation) {
operation.$get();
};
// Remove an operation.
$scope.removeOperation = function(operation, modalScope) {
operation.$delete(function(data) {
new PNotify({
type: "success",
title: "Delete",
text: "Operation #" + operation.id + " deleted."
});
// Send the "entry removed" event.
$scope.$emit("operationRemovedEvent", operation);
$scope.closeModal(modalScope);
});
};
// Close modal.
$scope.closeModal = function(modalScope) {
// Close the modal dialog
if(modalScope && modalScope.dismiss) {
modalScope.dismiss();
$scope.cancelEditOperation = function(operation, rowform) {
if(operation == $scope.inserted) {
$scope.entries.splice(0, 1);
} else {
rowform.$cancel();
}
};
// Load operations on account selection.
// FIXME Alexis Lahouze 2015-07-15 Deprecated.
$rootScope.$on("accountSelectedEvent", function(event, args){
$scope.loadOperations(args.account.id);
});
// Load operations on controller initialization.
$scope.loadOperations($routeParams.accountId);
}]);

View File

@ -16,139 +16,129 @@
-->
<!-- vim: set tw=80 ts=2 sw=2 sts=2: -->
<!-- Row with entry table -->
<div class="row">
<table class="table table-striped table-condensed table-hover">
<!-- Head of the table containing column headers and size -->
<thead>
<tr>
<th style="width: 120px">Date de d&eacute;but</th>
<th style="width: 120px">Date de fin</th>
<th style="width: 20px">Jour</th>
<th style="width: 20px">Fr&eacute;q.</th>
<th>Libell&eacute; de l'op&eacute;ration</th>
<th style="width: 50px">Montant</th>
<th style="width: 100px">Cat&eacute;gorie</th>
<th style="width: 60px">Actions</th>
</tr>
</thead>
<div>
<div class="row">
<div class="col-md-3 col-md-offset-1">
<button class="btn btn-success" ng-click="addOperation()">Ajouter</button>
</div>
</div>
<!-- Body of the table containing the entries -->
<tbody>
<tr class="form-inline">
<td>
<input type="text" class="form-control input-sm" ng-model="newOperation.start_date" data-date-format="yyyy-MM-dd" bs-datepicker/>
</td>
<div class="row">
<table class="table table-striped table-condensed table-hover">
<!-- Head of the table containing column headers and size -->
<thead>
<tr>
<th class="col-md-1">Date de d&eacute;but</th>
<th class="col-md-1">Date de fin</th>
<th class="col-md-1">Jour</th>
<th class="col-md-1">Fr&eacute;q.</th>
<th>Libell&eacute; de l'op&eacute;ration</th>
<th class="col-md-1">Montant</th>
<th class="col-md-2">Cat&eacute;gorie</th>
<th class="col-md-1">Actions</th>
</tr>
</thead>
<td>
<input type="text" class="form-control input-sm" ng-model="newOperation.stop_date" data-date-format="yyyy-MM-dd" bs-datepicker/>
</td>
<!-- Body of the table containing the entries -->
<tbody>
<tr id="{{ operation.id }}" class="form-inline"
ng-repeat="operation in operations">
<td class="col-md-1">
<span editable-text="operation.start_date"
e-style="width: 100%"
e-bs-datepicker e-data-date-format="yyyy-MM-dd"
e-name="start_date" e-form="rowform" e-required>
{{ operation.start_date | date: "yyyy-MM-dd" }}
</span>
</td>
<td>
<input type="text" class="form-control input-sm" ng-model="newOperation.day"/>
</td>
<td>
<span editable-text="operation.stop_date"
e-style="width: 100%"
e-bs-datepicker e-data-date-format="yyyy-MM-dd"
e-name="stop_date" e-form="rowform" e-required>
{{ operation.stop_date | date: "yyyy-MM-dd" }}
</span>
</td>
<td>
<input type="text" class="form-control input-sm" ng-model="newOperation.frequency"/>
</td>
<td>
<span editable-number="operation.day"
e-style="width: 100%"
e-name="day" e-form="rowform" e-required>
{{ operation.day }}
</span>
</td>
<td>
<input type="text" class="form-control input-sm" ng-model="newOperation.label"/>
</td>
<td>
<span editable-number="operation.frequency"
e-style="width: 100%"
e-name="frequency" e-form="rowform" e-required>
{{ operation.frequency }}
</span>
</td>
<td>
<input type="text" class="form-control input-sm" ng-model="newOperation.value"/>
</td>
<td>
<span editable-text="operation.label"
e-style="width: 100%"
e-placeholder="Libellé de l'opération"
e-name="label" e-form="rowform" e-required>
{{ operation.label }}
</span>
</td>
<td>
<input type="text" class="form-control input-sm" ng-model="newOperation.category" bs-typeahead="categories"/>
</td>
<td>
<span editable-number="operation.value"
e-style="width: 100%"
e-name="value" e-form="rowform" e-required>
{{ operation.value }}
</span>
</td>
<td>
<div class="btn-group">
<button class="btn btn-xs btn-success" ng-click="createOperation(newOperation)" title="Save">
<span class="fa fa-plus"></span>
</button>
<td>
<span editable-text="operation.category"
e-style="width: 100%"
e-name="category" e-form="rowform">
{{ operation.category }}
</span>
</td>
<button class="btn btn-xs btn-default" ng-click="resetNewOperation()" title="Cancel">
<span class="fa fa-times"></span>
</button>
</div>
</td>
</tr>
<td>
<form editable-form name="rowform"
onbeforesave="saveOperation($data, $index)"
shown="operation == inserted">
<div class="btn-group">
<!-- Save current operation -->
<button type="submit" class="btn btn-xs btn-success"
ng-if="rowform.$visible" title="Save">
<span class="fa fa-floppy-o"></span>
</button>
<tr id="operation_{{operation.id}}" class="form-inline"
ng-repeat-start="operation in operations"
ng-if="isDisplaying(operation)">
<td>{{operation.start_date}}</td>
<!-- Edit operation. -->
<button type="button" class="btn btn-xs btn-default"
ng-if="!rowform.$visible"
ng-click="rowform.$show()" title="edit">
<span class="fa fa-pencil-square-o"></span>
</button>
<td>{{operation.stop_date}}</td>
<!-- Cancel edit. -->
<button type="button" class="btn btn-xs btn-default"
ng-if="rowform.$visible"
ng-click="cancelEditOperation(operations, rowform)"
title="Cancel">
<span class="fa fa-times"></span>
</button>
<td>{{operation.day}}</td>
<td>{{operation.frequency}}</td>
<td>{{operation.label}}</td>
<td>{{operation.value}}</td>
<td>{{operation.category}}</td>
<td>
<div class="btn-group">
<button class="btn btn-xs btn-default" ng-click="editOperation(operation)" title="edit">
<span class="fa fa-pencil-square-o"></span>
</button>
<button class="btn btn-xs btn-default" bs-modal="static/templates/operation_remove.html" title="remove">
<span class="fa fa-trash"></span>
</button>
</div>
</td>
</tr>
<tr id="operation_{{operation.id}}" class="form-inline"
ng-repeat-end
ng-if="isEditing(operation)">
<td>
<input type="text" class="form-control input-sm" ng-model="operation.start_date" data-date-format="yyyy-MM-dd" bs-datepicker/>
</td>
<td>
<input type="text" class="form-control input-sm" ng-model="operation.stop_date" data-date-format="yyyy-MM-dd" bs-datepicker/>
</td>
<td>
<input type="text" class="form-control input-sm" ng-model="operation.day"/>
</td>
<td>
<input type="text" class="form-control input-sm" ng-model="operation.frequency"/>
</td>
<td>
<input type="text" class="form-control input-sm" ng-model="operation.label"/>
</td>
<td>
<input type="text" class="form-control input-sm" ng-model="operation.value"/>
</td>
<td>
<input type="text" class="form-control input-sm" ng-model="operation.category" bs-typeahead="categories"/>
</td>
<td>
<div class="btn-group">
<button class="btn btn-xs btn-success" ng-click="saveOperation(operation)" title="Save">
<span class="fa fa-floppy-o"></span>
</button>
<button class="btn btn-xs btn-default" ng-click="cancelEditOperation(operation)" title="Cancel">
<span class="fa fa-times"></span>
</button>
</div>
</td>
</tr>
</tbody>
</table>
<!-- Remove operation. -->
<button type="button" class="btn btn-xs btn-default"
bs-modal="static/templates/operation_remove.html"
title="remove">
<span class="fa fa-trash"></span>
</button>
</div>
</form>
</td>
</tr>
</tbody>
</table>
</div>
</div>