Skip to content

Commit

Permalink
WEB: Fixed array edit form
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasletsch committed Feb 5, 2016
1 parent 3a51879 commit d355f25
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 18 deletions.
5 changes: 3 additions & 2 deletions gui/inventory-web/app/src/authentication/LoginController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Login Controller
* @constructor
*/
function LoginController($log, $scope, $rootScope, $state, AuthenticationService, StructureService) {
function LoginController($log, $scope, $http, $state, AuthenticationService, StructureService) {

$scope.logout = function () {
$http.post('/logout', {}).success(function() {
Expand All @@ -12,10 +12,11 @@ function LoginController($log, $scope, $rootScope, $state, AuthenticationService
console.log("Logout failed");
AuthenticationService.logout();
});
$state.go("default");
}
}

export default [
'$log', '$scope', '$rootScope', '$state', 'AuthenticationService', 'StructureService',
'$log', '$scope', '$http', '$state', 'AuthenticationService', 'StructureService',
LoginController
];
42 changes: 29 additions & 13 deletions gui/inventory-web/app/src/entities/EntitiesEditController.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,53 @@ function EntitiesEditController(EntitiesRepository, $log, $rootScope, $scope, $s
$scope.entityName = $stateParams.entityName;
$scope.entityId = $stateParams.id;
$scope.resources = $rootScope.resources;

prepareEditForm();

if ($stateParams.id) {
EntitiesRepository.find($scope.entityName, $scope.entityId, entityLoaded);
} else {
$scope.schema = $scope.preparedSchema;
$scope.form = $scope.preparedForm;
$scope.model = {};
}

function entityLoaded(entity) {
$log.debug("Entity returned: " + JSON.stringify(entity));
$log.debug("Entity " + (typeof entity) + " returned: " + JSON.stringify(entity));
$scope.schema = $scope.preparedSchema;
$scope.form = $scope.preparedForm;
$scope.model = entity;
$log.debug("Entity array: " + Object.prototype.toString.call(entity.values));
}

function prepareEditForm() {
function prepareEditForm() {
$log.debug("Schema: " + JSON.stringify($rootScope.schemata[$scope.entityName]));
$scope.schema = $rootScope.schemata[$scope.entityName];
$scope.preparedSchema = $rootScope.schemata[$scope.entityName];
$scope.preparedForm = {};

if (FORM_FIELDS[$scope.entityName]) {
$scope.form = FORM_FIELDS[$scope.entityName];
$scope.preparedForm = FORM_FIELDS[$scope.entityName];
} else {
$scope.form = FORM_FIELDS['default'];
$scope.preparedForm = FORM_FIELDS['default'];
}
$translate("save").then(function (translation) {
$scope.form = $scope.form.concat([{type: "submit", title: translation}]);
$scope.preparedForm = $scope.preparedForm.concat([{type: "submit", title: translation}]);
});

angular.forEach($scope.schema.properties, function (property, key) {
angular.forEach($scope.preparedSchema.properties, function (property, key) {
translateProperty(property, key);
});

$scope.onSubmit = function (form) {
$scope.onSubmit = function (form, model) {
$log.debug("Submitting...");
// First we broadcast an event so all fields validate themselves
$scope.$broadcast('schemaFormValidate');

// Then we check if the form is valid
//if (form.$valid) {
$log.debug("Form: " + JSON.stringify(form));
$log.debug("Model: " + JSON.stringify($scope.model));
EntitiesRepository.save($scope.entityName, $scope.entityId, $scope.model, function success() {
$log.debug("Model: " + JSON.stringify(model, undefined, 2));
EntitiesRepository.save($scope.entityName, $scope.entityId, model, function success() {
$log.debug("Successful saved");
$state.go("entities.list", {entityName: $scope.entityName})
});
Expand All @@ -54,19 +61,28 @@ function EntitiesEditController(EntitiesRepository, $log, $rootScope, $scope, $s
//}
}
}

function translateProperty(property, key) {
$translate(property.title).then(function (translation) {
property.title = translation;
});
if(property.type == 'array') {
angular.forEach(property.items.properties, function(innerProperty, innerKey) {
if (property.type == 'array') {
$translate(property.items.title).then(function (translation) {
property.items.title = translation;
});
angular.forEach(property.items.properties, function (innerProperty, innerKey) {
translateProperty(innerProperty, innerKey);
})
}
}
}

var FORM_FIELDS = {defaults: ["*"], users: ["name"], unitOfMeasurements: ["code", "description"], valueLists: ["key", "values"]};
var FORM_FIELDS = {
defaults: ["*"],
users: ["name"],
unitOfMeasurements: ["code", "description"],
valueLists: ["key", {"key": "values", "add": "Neuer Wert", "items": ["values[].value"]}]
};

export default [
'EntitiesRepository', '$log', '$rootScope', '$scope', '$state', '$translate', '$stateParams', 'uiGridConstants',
Expand Down
2 changes: 2 additions & 0 deletions gui/inventory-web/app/src/entities/EntitiesRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ function EntitiesRepository($log, $rootScope, $http) {
}
$http.put(url, entity, {headers: {"If-Match": entity.version}}).then(function success(response) {
successCallback(response.data);
}, function error(response) {
$log.error("Could not save!");
});
} else {
var url = $rootScope.resources[entityName];
Expand Down
2 changes: 1 addition & 1 deletion gui/inventory-web/app/src/entities/view/entities.edit.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<form sf-schema="schema" sf-form="form" sf-model="model" ng-submit="onSubmit(form)"></form>
<form sf-schema="schema" sf-form="form" sf-model="model" ng-submit="onSubmit(form, model)" name="edit_form"></form>
2 changes: 2 additions & 0 deletions gui/inventory-web/app/src/translations_de.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"org.moserp.environment" : "Allgemeines",
"org.moserp.facility" : "Einrichtungen",
"org.moserp.inventory" : "Lager",
"org.moserp.product" : "Produkte",

"shipments" : "Lieferungen",
Expand Down
6 changes: 5 additions & 1 deletion gui/inventory-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
"traceur-runtime": "github:jmcriffey/[email protected]"
}
},
"scripts": {
"server": "jspm-server",
"start": "npm run server"
},
"devDependencies": {
"http-server": "^0.6.1",
"jasmine-core": "^2.3.4",
Expand All @@ -43,6 +47,6 @@
"karma-junit-reporter": "^0.2.2",
"protractor": "^2.1.0",
"shelljs": "^0.2.6",
"lite-server": "^1.3.4"
"jspm-server": "^1.0.1"
}
}
3 changes: 2 additions & 1 deletion gui/inventory-web/readme.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Basic Auth with AngularJS from https://github.com/cornflourblue/angular-authentication-example
To run locally:
npm start jspm-server

0 comments on commit d355f25

Please sign in to comment.