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

191 lines
5.7 KiB
JavaScript
Raw Normal View History

2015-08-18 21:44:36 +00:00
'use strict';
// Forms controller
2017-03-28 00:04:03 +00:00
angular.module('forms').controller('AdminFormController', ['$rootScope', '$window', '$scope', '$stateParams', '$state', 'Forms', 'CurrentForm', '$http', '$uibModal', 'myForm', '$filter', '$sce',
function($rootScope, $window, $scope, $stateParams, $state, Forms, CurrentForm, $http, $uibModal, myForm, $filter, $sce) {
2017-04-11 00:36:24 +00:00
$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;
2017-04-11 00:36:24 +00:00
$scope.onCopySuccess = function (e) {
2016-11-02 21:29:10 +00:00
$scope.copied = true;
};
2017-04-11 00:36:24 +00:00
$scope = $rootScope;
$scope.animationsEnabled = true;
$scope.myform = myForm;
$rootScope.saveInProgress = false;
2015-11-23 19:19:02 +00:00
2017-04-11 00:36:24 +00:00
CurrentForm.setForm($scope.myform);
2015-10-30 18:40:02 +00:00
$scope.formURL = '/#!/forms/' + $scope.myform._id;
2017-04-11 00:36:24 +00:00
if ($scope.myform.isLive) {
if ($window.subdomainsDisabled === true) {
2017-04-11 00:36:24 +00:00
$scope.actualFormURL = window.location.protocol + '//' + window.location.host + '/view' + $scope.formURL;
2017-03-28 00:04:03 +00:00
} else {
2017-04-11 00:36:24 +00:00
if (window.location.host.split('.').length < 3) {
$scope.actualFormURL = window.location.protocol + '//' + $scope.myform.admin.username + '.' + window.location.host + $scope.formURL;
} else {
$scope.actualFormURL = window.location.protocol + '//' + $scope.myform.admin.username + '.' + window.location.host.split('.').slice(1, 3).join('.') + $scope.formURL;
}
2017-03-28 00:04:03 +00:00
}
2017-04-11 00:36:24 +00:00
} else {
$scope.actualFormURL = window.location.protocol + '//' + window.location.host + $scope.formURL;
2016-11-08 22:44:00 +00:00
}
2017-04-11 00:36:24 +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)
2017-03-27 20:32:06 +00:00
.then(function(response){
2015-08-18 21:44:36 +00:00
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
2017-03-27 20:32:06 +00:00
}, function(error){
2015-08-18 21:44:36 +00:00
console.log('ERROR: Form could not be deleted.');
console.error(error);
});
}
};
// Update existing Form
2017-03-10 19:26:07 +00:00
$scope.update = $rootScope.update = function(updateImmediately, data, isDiffed, 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**
2017-03-10 19:26:07 +00:00
if(continueUpdate) {
var err = null;
if (!updateImmediately) {
$rootScope.saveInProgress = true;
}
if (isDiffed) {
$scope.updatePromise = $http.put('/forms/' + $scope.myform._id, {changes: data})
.then(function (response) {
if (refreshAfterUpdate) $rootScope.myform = $scope.myform = response.data;
// console.log(response.data);
}).catch(function (response) {
console.log('Error occured during form UPDATE.\n');
// console.log(response.data);
err = response.data;
}).finally(function () {
// console.log('finished updating');
if (!updateImmediately) {
$rootScope.saveInProgress = false;
}
if ((typeof cb) === 'function') {
return cb(err);
}
});
} else {
var dataToSend = data;
2017-03-27 20:32:06 +00:00
if(dataToSend.analytics && dataToSend.analytics.visitors){
delete dataToSend.analytics.visitors;
}
if(dataToSend.submissions){
delete dataToSend.submissions;
}
2017-03-10 19:26:07 +00:00
$scope.updatePromise = $http.put('/forms/' + $scope.myform._id, {form: dataToSend})
.then(function (response) {
if (refreshAfterUpdate) $rootScope.myform = $scope.myform = response.data;
}).catch(function (response) {
console.log('Error occured during form UPDATE.\n');
// console.log(response.data);
err = response.data;
}).finally(function () {
// console.log('finished updating');
if (!updateImmediately) {
$rootScope.saveInProgress = false;
}
if ((typeof cb) === 'function') {
return cb(err);
}
});
}
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
]);