(function (angular, Controllers) { Controllers = Controllers || {}; Controllers.e5Admin = Controllers.e5Admin || {}; Controllers.e5Admin.Base = Controllers.e5Admin.Base || {}; angular.extend(Controllers.e5Admin.Base, { _default: function ($scope, $state, $filter, noty, Form, CurrentUser, $e5aMessage, $e5aBootbox) { var $current = $state.$current; while ($current.name.length > 0) { if (angular.isDefined(Form.data[$current.name])) { break; } $current = $current.parent; } $scope.selectedItems = function () { return $filter('filter')($scope.$load($scope.Model), { '$$selected': true }); }; $scope.state = Form.data[$current.name]; $scope.showContent = function () { let returnValue = true; if (angular.isDefined($scope.state.show)) { returnValue = !!($scope.$eval($scope.state.show)); } return returnValue; }; $scope.user = CurrentUser; $scope.isArray = angular.isArray; $scope.remove = function (model) { $e5aBootbox.removeConfirm("Czy na pewno usunąć: " + model.id + "?").then(function () { model.$remove(function () { $scope.$clearCache(); $e5aMessage.success(); }, $e5aMessage.failure); }); }; $scope.removeList = function (selected) { let variety = $filter('variety')(selected.length, 'produkt', 'produkty', 'produktów'); $e5aBootbox.removeConfirm("Czy na pewno usunąć " + selected.length + ' ' + variety + "?").then(function () { let ids = []; for (let product of selected) { ids.push(product.id); } $scope.$load($scope.Model).$resolved = false; $scope.Model.removeList({ products: ids }, function () { $scope.$clearCache(); }, function () { $scope.$load($scope.Model).$resolved = true; noty.showError("Problem podczas usuwania produktów :("); }); }); }; $scope.cancel = function () { $scope.$clearCache(); $state.go('^.list'); }; $scope.submit = function (form) { $scope.model.$resolved = false; if (form.$invalid) { $scope.model.$resolved = true; noty.showError("Popraw formularz w znaczonych miejscach"); return; } var success = (data) => { if (form.backToList) { $scope.$clearCache(); $state.go('^.list'); } $e5aMessage.success(); $scope.model.$resolved = true; }; var failure = (error) => { $scope.model.$resolved = true; $e5aMessage.failure(error); console.log('Błąd zapisu danych', error); }; if (angular.isDefined($scope.model.id)) { $scope.model.$update() .then( success, failure ) } else { $scope.model.$create() .then( success, failure ); } }; }, List: function ($scope, $stateParams, $controller, StateService, Model, Form, $filter) { $controller(Controllers.e5Admin.Base._default, { '$scope': $scope, 'Form': Form }); $scope.Model = Model; $scope.allSelected = false; $scope.selectAll = function (event) { let newVal = null; if ($scope.allSelected) { newVal = false; } else { newVal = true; } $scope.allSelected = !$scope.allSelected; angular.forEach($scope.$load($scope.Model), function (value, key) { value.$$selected = newVal; }); }; $scope.rowClick = function (model) { if (!model.$$selected) { angular.forEach($scope.$load($scope.Model), function (value, key) { value.$$selected = false; }); model.$$selected = true; } else { return; } }; $scope.checkSelected = function (event) { let countAll = $scope.$load($scope.Model).length; let filtered = $filter('filter')($scope.$load($scope.Model), { "$$selected": true }); let selected = filtered.length; $scope.allSelected = countAll === selected ? true : false; }; let _default = {}; if (angular.isUndefined($scope.state) || angular.isUndefined($scope.state.types) || angular.isUndefined($scope.state.types.list)) { _default = { view: 'list' }; } else { _default = { view: ($scope.state.types.list ? 'list' : 'grid') }; } StateService.state($scope, _default); angular.forEach($stateParams, function (item, key) { $scope.search[key] = item; }); }, Edit: function ($scope, $controller, $stateParams, Model, Form) { $scope.model = Model.get($stateParams); $controller(Controllers.e5Admin.Base._default, { '$scope': $scope, 'Form': Form }); }, Add: function ($scope, $controller, $stateParams, Model, Form, RewriteService) { $scope.model = new Model; $scope.model.$resolved = true; angular.forEach($stateParams, function (item, key) { $scope.model[key] = (parseInt(item) ? parseInt(item) : item); }); $scope.$watch('model.name', function (value) { if (angular.isDefined($scope.model.id) && $scope.model.id > 0) { return; } if (angular.isUndefined(value)) { return; } $scope.model.rewrite = RewriteService.transform(value); }); $controller(Controllers.e5Admin.Base._default, { '$scope': $scope, 'Form': Form }); } }); })(angular, Controllers);