Skip to content
Open
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
1 change: 0 additions & 1 deletion applications/default/extensions/form/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ form.form = function(forms, callback) {
name: 'submit',
title: 'Save',
type: 'submit',
url: '/rest/' + (typeSettings.mainTypeName || typeName),
classes: ['btn-primary'],
weight: 50
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"name": "submit",
"title": "Save",
"type": "submit",
"url": "/settings/edit-account-submit",
"url": "/settings/edit-account-submit/[:username|user]",
"weight": 15
}
]
Expand Down
2 changes: 2 additions & 0 deletions applications/default/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
<script src="/lib/angular-sanitize/angular-sanitize.min.js"></script>
<script src="/lib/angular-summernote/dist/angular-summernote.min.js"></script>
<script src="/lib/ng-file-upload/angular-file-upload.min.js"></script>
<script src="/lib/lodash/dist/lodash.js"></script>
<script src="/lib/restangular/dist/restangular.min.js"></script>

<script src="/js/app.js"></script>
<script src="/js/services.js"></script>
Expand Down
33 changes: 32 additions & 1 deletion applications/default/public/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,43 @@ angular.module('choko', [
'ngResource',
'ngSanitize',
'summernote',
'angularFileUpload'
'angularFileUpload',
'restangular'
])

.config(['$locationProvider', function($locationProvider) {
$locationProvider.html5Mode({
enabled: true,
requireBase: false
});
}])

.config(['RestangularProvider', function(RestangularProvider) {
RestangularProvider.setBaseUrl('/rest');

// @todo: We need improve the caching functionality.
// The items list not change after add an ítem, because
// he get the items list from the caché.
RestangularProvider.setDefaultHttpFields({
cache: false
});

// Add a response intereceptor
RestangularProvider.addResponseInterceptor(function(data, operation, what, url, response, deferred) {
var extractedData;
var temp = [];

if (operation === 'getList') {
Object.keys(data).forEach(function(name){
temp.push(data[name]);
});

extractedData = temp;
}
else {
extractedData = data;
}

return extractedData;
});
}]);
113 changes: 77 additions & 36 deletions applications/default/public/js/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ angular.module('choko')

}])

.controller('ViewController', ['$scope', '$location', '$http', 'Choko', 'Params', 'Token',
function ($scope, $location, $http, Choko, Params, Token) {
.controller('ViewController', ['$scope', '$location', '$http', 'Choko', 'Params', 'Token', 'Restangular',
function ($scope, $location, $http, Choko, Params, Token, Restangular) {

$scope.prepareDisplay = function(name, callback) {
Choko.get({type: 'display', key: name}, function(display) {
Expand All @@ -155,6 +155,13 @@ angular.module('choko')
});
};


// Prevente creation of service if no itemType set.
if ($scope.view.itemType) {
// Create a new Service for Itemtype.
var itemTypeREST = Restangular.service($scope.view.itemType);
}

// Parse parameters when needed.
if (typeof $scope.view.itemKey !== 'undefined') {
$scope.view.itemKey = Params.parse($scope.view.itemKey, $scope);
Expand All @@ -172,22 +179,26 @@ angular.module('choko')

// Handle 'list' type views.
if ($scope.view.type === 'list' && $scope.view.itemType) {
var query = {
type: $scope.view.itemType
};
var query = {};

if ($scope.view.query) {
angular.extend(query, $scope.view.query);
}

$scope.items = {};

if ($scope.view.template) {
Choko.get(query, function(response) {
$scope.items = response;
});
}

// Expose view list promise to scope
$scope.viewList = itemTypeREST.getList(query);
$scope.items = {};

$scope.viewList.then(function(response) {
$scope.items = response;
});

if (!$scope.view.template && $scope.view.listStyle) {
$scope.view.template = '/templates/' + $scope.view.listStyle + '.html';
}
Expand All @@ -203,13 +214,16 @@ angular.module('choko')

// Handle 'item' type views.
if ($scope.view.type === 'item' && $scope.view.itemType) {

// Expose variables to the scope
$scope.data = {};
$scope.view.title = '';
Choko.get({type: $scope.view.itemType, key: $scope.view.itemKey}, function(response) {
$scope.viewItem = itemTypeREST.one($scope.view.itemKey).get();

$scope.viewItem.then(function(response) {
$scope.data = response;
$scope.view.title = response.title;
},
function(response) {
}, function(response) {
// Error.
if ($scope.page) {
// If it's a page, show error, otherwise fail silently.
Expand All @@ -222,7 +236,11 @@ angular.module('choko')

// Handle 'form' type views.
if ($scope.view.type === 'form' && $scope.view.formName) {
var typeForm = 'post';
var itemREST = null;

$scope.data = {};

$scope.buildForm = function () {
Choko.get({type: 'form', key: $scope.view.formName}, function(response) {
$scope.form = response;
Expand All @@ -242,41 +260,64 @@ angular.module('choko')
};

if ($scope.view.itemType && $scope.view.itemKey) {
// Load item for editing.
Choko.get({type: $scope.view.itemType, key: $scope.view.itemKey}, function(response) {
$scope.data = response;
$scope.buildForm();
});

// Set type form to PUT.
typeForm = 'put';

itemTypeREST.one($scope.view.itemKey)
.get()
.then(function(response) {
$scope.data = response;
$scope.buildForm();
});
}
else {

// Verify if the form is the type PUT to build the form
if (typeForm != 'put') {
$scope.buildForm();
}

$scope.submit = function(url) {
// Add itemKey to the URL if any.
if ($scope.view.itemKey) {
url += '/' + $scope.view.itemKey;

// Replace tokens in url.
if (url) {
url = Token.replace(url, $scope);
}

$http.post(url, $scope.data)
.success(function(data, status, headers, config) {
$scope.data = data;
// Add params to data if any.
Object.keys($scope.view.params || {}).forEach(function(param) {
$scope.data[param] = $scope.data[param] || $scope.view.params[param];
});

delete $scope.errors;
if(!itemTypeREST) {
$scope.viewForm = Restangular.oneUrl('url', url).post('', $scope.data);
}
else {
if (url) {
$scope.viewForm = Restangular.oneUrl('url', url).post('', $scope.data);
} else {
$scope.viewForm = typeForm === 'post' ?
itemTypeREST.post($scope.data) :
$scope.data.put();
}
}

if ($scope.form.redirect) {
// Replace tokens in redirects. Make 'item' an alias to 'data'
// so item parser can be used in tokens.
$scope.item = $scope.data;
$scope.form.redirect = Token.replace($scope.form.redirect, $scope);
$scope.viewForm.then(function(response) {
$scope.data = response;
delete $scope.errors;

$location.path($scope.form.redirect);
}
})
.error(function(data, status, headers, config) {
$scope.status = status;
$scope.errors = data;
});
};
if ($scope.form.redirect) {
// Replace tokens in redirects. Make 'item' an alias to 'data'
// so item parser can be used in tokens.
$scope.item = $scope.data;
$scope.form.redirect = Token.replace($scope.form.redirect, $scope);

$location.path($scope.form.redirect);
}
}, function(response) {
$scope.status = response.status;
$scope.errors = response.data;
});
}
}
}]);
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"angular-sanitize": "1.3.x",
"summernote": "0.6.x",
"angular-summernote": "0.3.x",
"ng-file-upload": "1.6.x"
"ng-file-upload": "1.6.x",
"restangular": "1.4.x"
}
}