tellform/public/modules/forms/admin/controllers/admin-form.client.controller.js

148 lines
4.8 KiB
JavaScript
Raw Normal View History

2015-08-18 21:44:36 +00:00
'use strict';
// Forms controller
angular.module('forms').controller('AdminFormController', ['$rootScope', '$scope', '$stateParams', '$state', 'Forms', 'CurrentForm', '$http', '$uibModal', 'myForm', '$filter', '$sce',
function($rootScope, $scope, $stateParams, $state, Forms, CurrentForm, $http, $uibModal, myForm, $filter, $sce) {
$scope.trustSrc = function(src) {
return $sce.trustAsResourceUrl(src);
};
2015-08-18 21:44:36 +00:00
//Set active tab to Create
$scope.activePill = 0;
2016-11-02 21:29:10 +00:00
$scope.copied = false;
$scope.onCopySuccess = function(e) {
$scope.copied = true;
};
2015-08-18 21:44:36 +00:00
$scope = $rootScope;
2015-11-06 21:37:18 +00:00
$scope.animationsEnabled = true;
2015-10-30 18:40:02 +00:00
$scope.myform = myForm;
2015-08-18 21:44:36 +00:00
$rootScope.saveInProgress = false;
2015-11-23 19:19:02 +00:00
2015-10-30 18:40:02 +00:00
CurrentForm.setForm($scope.myform);
$scope.formURL = "/#!/forms/" + $scope.myform._id;
2016-11-09 18:02:12 +00:00
if(window.location.host.split('.').length < 3){
$scope.actualFormURL = window.location.protocol + '//' + $scope.myform.admin.username + '.' + window.location.host + $scope.formURL;
2016-11-08 22:44:00 +00:00
} else {
2016-11-09 18:02:12 +00:00
$scope.actualFormURL = window.location.protocol + '//' + $scope.myform.admin.username + '.' + window.location.host.split('.').slice(1,3).join('.') + $scope.formURL;
2016-11-08 22:44:00 +00:00
}
var refreshFrame = $scope.refreshFrame = function(){
if(document.getElementById('iframe')) {
document.getElementById('iframe').contentWindow.location.reload();
}
};
$scope.tabData = [
2015-11-06 17:25:30 +00:00
{
2016-06-16 00:38:22 +00:00
heading: $filter('translate')('CONFIGURE_TAB'),
templateName: 'configure'
2015-11-06 17:25:30 +00:00
},
{
2016-06-16 00:38:22 +00:00
heading: $filter('translate')('ANALYZE_TAB'),
templateName: 'analyze'
2015-11-06 17:25:30 +00:00
}
];
2015-09-15 22:21:49 +00:00
2015-08-18 21:44:36 +00:00
$scope.setForm = function(form){
$scope.myform = form;
};
2015-08-18 21:44:36 +00:00
$rootScope.resetForm = function(){
$scope.myform = Forms.get({
formId: $stateParams.formId
});
};
2016-04-29 06:00:41 +00:00
/*
** DeleteModal Functions
2015-08-18 21:44:36 +00:00
*/
$scope.openDeleteModal = function(){
2015-11-06 21:37:18 +00:00
$scope.deleteModal = $uibModal.open({
2015-11-23 19:19:02 +00:00
animation: $scope.animationsEnabled,
templateUrl: 'formDeleteModal.html',
2015-11-23 19:19:02 +00:00
controller: 'AdminFormController',
resolve: {
myForm: function(){
return $scope.myform;
}
}
2015-11-06 21:37:18 +00:00
});
$scope.deleteModal.result.then(function (selectedItem) {
2015-11-23 19:19:02 +00:00
$scope.selected = selectedItem;
2015-11-06 21:37:18 +00:00
}, function () {
2015-11-23 19:19:02 +00:00
console.log('Modal dismissed at: ' + new Date());
2015-08-18 21:44:36 +00:00
});
};
2015-11-06 21:37:18 +00:00
2015-08-18 21:44:36 +00:00
$scope.cancelDeleteModal = function(){
2015-08-19 22:29:01 +00:00
if($scope.deleteModal){
$scope.deleteModal.dismiss('cancel');
2015-08-18 21:44:36 +00:00
}
};
// Remove existing Form
2015-08-19 22:29:01 +00:00
$scope.removeCurrentForm = function() {
if($scope.deleteModal && $scope.deleteModal.opened){
2015-08-18 21:44:36 +00:00
2015-08-19 22:29:01 +00:00
$scope.deleteModal.close();
2016-04-29 06:00:41 +00:00
2015-08-19 22:29:01 +00:00
var form_id = $scope.myform._id;
if(!form_id) throw new Error('Error - removeCurrentForm(): $scope.myform._id does not exist');
2016-04-29 06:00:41 +00:00
2015-08-19 22:29:01 +00:00
$http.delete('/forms/'+form_id)
2015-08-18 21:44:36 +00:00
.success(function(data, status, headers){
console.log('form deleted successfully');
2016-04-29 06:00:41 +00:00
$state.go('listForms', {}, {reload: true});
2015-08-18 21:44:36 +00:00
}).error(function(error){
console.log('ERROR: Form could not be deleted.');
console.error(error);
});
}
};
// Update existing Form
$scope.update = $rootScope.update = function(updateImmediately, diffChanges, refreshAfterUpdate, cb){
refreshFrame();
2015-08-19 22:29:01 +00:00
2015-08-18 21:44:36 +00:00
var continueUpdate = true;
2015-08-19 22:29:01 +00:00
if(!updateImmediately){
continueUpdate = !$rootScope.saveInProgress;
2015-08-18 21:44:36 +00:00
}
2016-04-29 06:00:41 +00:00
//Update form **if we are not currently updating** or if **shouldUpdateNow flag is set**
2015-08-18 21:44:36 +00:00
if(continueUpdate){
var err = null;
2015-08-19 22:29:01 +00:00
if(!updateImmediately){ $rootScope.saveInProgress = true; }
2015-08-18 21:44:36 +00:00
$scope.updatePromise = $http.put('/forms/'+$scope.myform._id, { changes: diffChanges })
2015-08-18 21:44:36 +00:00
.then(function(response){
if(refreshAfterUpdate) $rootScope.myform = $scope.myform = response.data;
2015-08-19 22:29:01 +00:00
// console.log(response.data);
2015-08-18 21:44:36 +00:00
}).catch(function(response){
2015-10-30 20:14:48 +00:00
console.log('Error occured during form UPDATE.\n');
2015-08-19 22:29:01 +00:00
// console.log(response.data);
2015-08-18 21:44:36 +00:00
err = response.data;
2016-04-29 06:00:41 +00:00
}).finally(function() {
2015-08-19 22:29:01 +00:00
// console.log('finished updating');
if(!updateImmediately){$rootScope.saveInProgress = false; }
if( (typeof cb) === 'function'){
2016-04-29 06:00:41 +00:00
return cb(err);
2015-08-19 22:29:01 +00:00
}
2015-08-18 21:44:36 +00:00
});
}
};
2015-10-30 18:40:02 +00:00
2015-08-18 21:44:36 +00:00
}
2016-04-29 06:00:41 +00:00
]);